1 package com.sun.org.apache.bcel.internal.util; 2 3 56 57 import java.lang.reflect.*; 58 59 75 public class JavaWrapper { 76 private java.lang.ClassLoader loader; 77 78 private static java.lang.ClassLoader getClassLoader() { 79 String s = System.getProperty("bcel.classloader"); 80 81 if((s == null) || "".equals(s)) 82 s = "com.sun.org.apache.bcel.internal.util.ClassLoader"; 83 84 try { 85 return (java.lang.ClassLoader )Class.forName(s).newInstance(); 86 } catch(Exception e) { 87 throw new RuntimeException (e.toString()); 88 } 89 } 90 91 public JavaWrapper(java.lang.ClassLoader loader) { 92 this.loader = loader; 93 } 94 95 public JavaWrapper() { 96 this(getClassLoader()); 97 } 98 99 104 public void runMain(String class_name, String [] argv) throws ClassNotFoundException 105 { 106 Class cl = loader.loadClass(class_name); 107 Method method = null; 108 109 try { 110 method = cl.getMethod("_main", new Class [] { argv.getClass() }); 111 112 114 int m = method.getModifiers(); 115 Class r = method.getReturnType(); 116 117 if(!(Modifier.isPublic(m) && Modifier.isStatic(m)) || 118 Modifier.isAbstract(m) || (r != Void.TYPE)) 119 throw new NoSuchMethodException (); 120 } catch(NoSuchMethodException no) { 121 System.out.println("In class " + class_name + 122 ": public static void _main(String[] argv) is not defined"); 123 return; 124 } 125 126 try { 127 method.invoke(null, new Object [] { argv }); 128 } catch(Exception ex) { 129 ex.printStackTrace(); 130 } 131 } 132 133 136 public static void _main(String [] argv) throws Exception { 137 139 if(argv.length == 0) { 140 System.out.println("Missing class name."); 141 return; 142 } 143 144 String class_name = argv[0]; 145 String [] new_argv = new String [argv.length - 1]; 146 System.arraycopy(argv, 1, new_argv, 0, new_argv.length); 147 148 JavaWrapper wrapper = new JavaWrapper(); 149 wrapper.runMain(class_name, new_argv); 150 } 151 } 152 153 | Popular Tags |