View Javadoc

1   /**
2    * Copyright 2007 Google Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.tonicsystems.jarjar;
18  
19  import java.io.File;
20  import java.lang.reflect.InvocationTargetException;
21  import java.lang.reflect.Method;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  class MainUtil
26  {
27      public static void runMain(Object main, String[] args, String defCommand) throws Exception {
28          if (args.length > 0) {
29              String command = args[0];
30              Method[] methods = main.getClass().getMethods();
31              for (int i = 0; i < methods.length; i++) {
32                  Method method = methods[i];
33                  if (method.getName().equals(command)) {
34                      String[] remaining = new String[args.length - 1];
35                      System.arraycopy(args, 1, remaining, 0, remaining.length);
36                      try {
37                          method.invoke(main, bindParameters(method, remaining));
38                      } catch (InvocationTargetException e) {
39                          Throwable cause = e.getCause();
40                          if (cause instanceof IllegalArgumentException) {
41                              System.err.println("Syntax error: " + cause.getMessage());
42                          } else if (cause instanceof Exception) {
43                              throw (Exception) cause;
44                          } else {
45                              throw e;
46                          }
47                      }
48                      return;
49                  }
50              }
51          }
52          if (defCommand != null)
53              runMain(main, new String[]{ defCommand }, null);
54      }
55  
56      private static Object[] bindParameters(Method method, String[] args) {
57          List<Object> parameters = new ArrayList<Object>();
58          Class[] parameterTypes = method.getParameterTypes();
59          for (int i = 0, len = parameterTypes.length; i < len; i++) {
60              Class type = parameterTypes[i];
61              int remaining = Math.max(0, args.length - i);
62              if (type.equals(String[].class)) {
63                  String[] rest = new String[remaining];
64                  System.arraycopy(args, 1, rest, 0, remaining);
65                  parameters.add(rest);
66              } else if (remaining > 0) {
67                  parameters.add(convertParameter(args[i], parameterTypes[i]));
68              } else {
69                  parameters.add(null);
70              }
71          }
72          return parameters.toArray();
73      }
74  
75      private static Object convertParameter(String arg, Class type) {
76          if (type.equals(String.class)) {
77              return arg;
78          } else if (type.equals(Integer.class)) {
79              return Integer.valueOf(arg, 10);
80          } else if (type.equals(File.class)) {
81              return new File(arg);
82          } else {
83              throw new UnsupportedOperationException("Unknown type " + type);
84          }
85      }
86  }