1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.util;
20
21 import java.lang.reflect.Method;
22 import java.lang.reflect.Modifier;
23
24 import org.apache.commons.lang3.StringUtils;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class JavaWrapper {
47
48 private static java.lang.ClassLoader getClassLoader() {
49 final String s = System.getProperty("bcel.classloader");
50 if (StringUtils.isEmpty(s)) {
51 throw new IllegalStateException("The property 'bcel.classloader' must be defined");
52 }
53 try {
54 return (java.lang.ClassLoader) Class.forName(s).getConstructor().newInstance();
55 } catch (final Exception e) {
56 throw new IllegalStateException(e.toString(), e);
57 }
58 }
59
60
61
62
63 public static void main(final String[] argv) throws Exception {
64
65
66
67 if (argv.length == 0) {
68 System.out.println("Missing class name.");
69 return;
70 }
71 final String className = argv[0];
72 final String[] newArgv = new String[argv.length - 1];
73 System.arraycopy(argv, 1, newArgv, 0, newArgv.length);
74 new JavaWrapper().runMain(className, newArgv);
75 }
76
77 private final java.lang.ClassLoader loader;
78
79 public JavaWrapper() {
80 this(getClassLoader());
81 }
82
83 public JavaWrapper(final java.lang.ClassLoader loader) {
84 this.loader = loader;
85 }
86
87
88
89
90
91
92
93
94 public void runMain(final String className, final String[] argv) throws ClassNotFoundException {
95 final Class<?> cl = loader.loadClass(className);
96 Method method = null;
97 try {
98 method = cl.getMethod("main", argv.getClass());
99
100
101
102 final int m = method.getModifiers();
103 final Class<?> r = method.getReturnType();
104 if (!(Modifier.isPublic(m) && Modifier.isStatic(m)) || Modifier.isAbstract(m) || r != Void.TYPE) {
105 throw new NoSuchMethodException();
106 }
107 } catch (final NoSuchMethodException no) {
108 System.out.println("In class " + className + ": public static void main(String[] argv) is not defined");
109 return;
110 }
111 try {
112 method.invoke(null, (Object[]) argv);
113 } catch (final Exception ex) {
114 ex.printStackTrace();
115 }
116 }
117 }