1 18 19 package alt.jiapi.util; 20 21 import java.lang.reflect.Method ; 22 23 import alt.jiapi.InstrumentationContext; 24 25 48 public class Bootstrapper { 49 public Bootstrapper(String className, String methodName, 50 Class []parameterTypes, Object []args, 51 ClassLoader classLoader) { 52 bootstrap(className, methodName, parameterTypes, args, classLoader); 53 } 54 55 66 public void bootstrap(String className, String methodName, 67 Class []parameterTypes, Object []args, 68 ClassLoader classLoader) { 69 try { 70 Class clazz = classLoader.loadClass(className); 71 Method method = clazz.getMethod(methodName, parameterTypes); 72 Object obj = clazz.newInstance(); 73 method.invoke(obj, new Object [] { args }); 74 } catch (Exception e) { 75 e.printStackTrace(); 76 } 77 } 78 79 89 public static void launch(String className, Object []args, 90 InstrumentationContext ctx, 91 ClassLoader classLoader) { 92 93 Class []types = new Class [] { String [].class }; 94 launch(className, "main", types, args, ctx, classLoader); 95 } 96 97 108 public static void launch(String className, String methodName, 109 Class []parameterTypes, Object []args, 110 InstrumentationContext ctx, 111 ClassLoader classLoader) { 112 try { 113 Method setContext = classLoader.getClass().getMethod("setContext", 115 new Class [] { ctx.getClass() }); 116 setContext.invoke(classLoader, new Object [] { ctx }); 117 } 118 catch (Exception e) { 119 e.printStackTrace(); 120 System.err.println("\n" + e.getClass() + ": " + e.getMessage()); 121 System.err.println("\nYou are propably trying to launch an application without properly bootstrapping it. A recommended way is to use utility alt.jiapi.util.Bootstrapper. At your shell prompt say:\njava alt.jiapi.util.Bootstrapper org.your.App [params]\nThat will launch your application so that the classes needed for instrumentation are loaded to a same namespace. There's also a script which can be used for bootstrapping:\njiapi.sh org.your.App [params]\n\n"); 122 return; 123 } 124 125 try { 126 Class clazz = classLoader.loadClass(className); 128 Method method = clazz.getMethod(methodName, parameterTypes); 129 130 133 if (args == null) { 134 args = new String [0]; 135 } 136 137 method.invoke(null, new Object [] { args }); 139 } catch (Exception e) { 140 e.printStackTrace(); 141 } 142 } 143 144 147 public static void main(String args[]) { 148 String [] _args = new String [args.length - 1]; 150 System.arraycopy(args, 1, _args, 0, _args.length); 151 152 ClassLoader classLoader = InstrumentingClassLoader.createClassLoader(); 153 Class []types = new Class [] { _args.getClass() }; 154 new Bootstrapper(args[0], "main", types, _args, classLoader); 155 } 156 } 157 | Popular Tags |