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