public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { try { ClassReader classReader = new ClassReader("com/asm/Before_Demo1"); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); ClassAdapter ca = new AopClassAdapter(cw); classReader.accept(ca, ClassReader.SKIP_DEBUG); byte[] byteArray = cw.toByteArray(); MyClassLoader myClassLoader = new MyClassLoader(); Class<?> clazz = myClassLoader.myDefineClass(byteArray, "com.asm.Before_Demo1"); Method method = clazz.getMethod("sayHello"); method.invoke(clazz.newInstance()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public void addJar(String n) { System.out.println(" add jar :" + n); try { loader.addURL(new URL("file://" + n)); } catch (MalformedURLException e) { e.printStackTrace(); } }
public void addWildcardPathRecursively(String aa) throws MalformedURLException { if (aa.contains("examples")) return; File dir = new File(aa.replace("**", "")); if (dir.exists()) { extendLibraryPath(dir.getAbsolutePath()); String[] ll = dir.list( new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".jar"); } }); if (ll != null) for (String l : ll) {; // System.out.println(" l = " + l); String fp = new File(dir.getAbsolutePath() + "/" + l).getAbsolutePath(); URL url = new URL("file://" + fp + (fp.endsWith(".jar") ? "" : "/")); loader.addURL(url); extendedClassPaths.add(fp); } File[] f = dir.listFiles( new FileFilter() { public boolean accept(File pathname) { return pathname.isDirectory(); } }); if (f != null) { for (File ff : f) { addWildcardPathRecursively(ff.getAbsolutePath()); } } } else { System.err.println( " warning: wildcard path <" + aa + "> is not a directory or does not exist "); } }
public void launch() { System.out.println( "## trampoline <" + this.getClass() + ":" + this.getClass().getClassLoader() + ">"); trampoline = this; String exceptions = SystemProperties.getProperty("trampolineExceptions", null); ignored = new String[] { "apple.", "java.", "javax.", "sun.", "com.apple", "apple.", "field.namespace", "field.math", "field.launch.", "org.objectweb", "com.sun", "org.xml", "org.w3c", "$Prox", "org.eclipse", "main", "field.util.BetterWeak", "field.misc.ANSIColorUtils", "ch.rand", "org.python", "org.apache.batik", "org.antlr", "field.util.TaskQueue", "com.lowagie", "net.sf.cglib.proxy", "com.seaglasslookandfeel", "org.pushingpixels", "net.sourceforge.napkinlaf.", "com.kenai.jaffl" }; allowed = new String[] { "phobos", "com.sun.script.", "com.sun.scenario", "com.sun.stylesheet", "com.sun.opengl", "com.sun.gluegen", "javax.media.opengl", "javax.media.nativewindow", "javax.jmdns" }; if (exceptions != null) { ArrayList a = new ArrayList(Arrays.asList(ignored)); a.addAll(Arrays.asList(exceptions.split(":"))); ignored = (String[]) a.toArray(ignored); } loader = new MyClassLoader( ((URLClassLoader) this.getClass().getClassLoader()).getURLs(), (this.getClass().getClassLoader())); System.setSecurityManager(new PermissiveSecurityManager()); String extendedJars = SystemProperties.getProperty("extendedJars", null); if (extendedJars != null) { String[] ex = extendedJars.split(":"); for (String e : ex) {; // System.out.println(" adding jar <" + e + // ">"); addJar(e); } } Vector v = (Vector) ReflectionTools.illegalGetObject(this.getClass().getClassLoader(), "classes"); if (debug) ; // System.out.println(" already loaded all of <" + v + // ">"); if (!System.getProperty("asserts", "none").equals("none")) loader.setDefaultAssertionStatus(true); Thread.currentThread().setContextClassLoader(loader); String extensionsDir = SystemProperties.getProperty("extensions.dir", "../../extensions/"); Trampoline2.trampoline.addExtensionsDirectory(new File(extensionsDir)); String extensionsDir2 = System.getProperty("user.home") + "/Library/Application Support/Field/extensions"; if (!new File(extensionsDir2).exists()) new File(extensionsDir2).mkdirs(); if (new File(extensionsDir2).exists()) Trampoline2.trampoline.addExtensionsDirectory(new File(extensionsDir2)); try {; // System.out.println(Arrays.asList(loader.getURLs())); final Class c = (loader.loadClass(classToLaunch)); ; // System.out.println(" c = " + c + " " + // c.getClassLoader() + " " + loader); try { Method main = c.getDeclaredMethod("main", new Class[] {new String[0].getClass()}); try { main.invoke(null, new Object[] {null}); return; } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return; } catch (SecurityException e) { e.printStackTrace(); return; } catch (NoSuchMethodException e) { } Launcher.getLauncher().mainThread = Thread.currentThread(); printInfo(); Launcher.mainInstance = (iLaunchable) c.newInstance(); printInfo(); Launcher.mainInstance.launch(); printInfo(); printInfo(); } catch (Throwable e) { e.printStackTrace(); if (SystemProperties.getIntProperty("exitOnException", 0) == 1 || e instanceof Error || e.getCause() instanceof Error) System.exit(1); } if (SystemProperties.getIntProperty("nosave", 0) == 1) System.setSecurityManager(new NoWriteSecurityManager()); else if (SystemProperties.getIntProperty("collectResources", 0) == 1) System.setSecurityManager(new CollectResourcesSecurityManager()); else System.setSecurityManager(new NoopSecurityManager()); }
public void addWildcardPath(String aa) throws MalformedURLException { File dir = new File(aa.replace("**", "")); if (dir.exists()) { loader.addURL(new URL("file://" + dir.getAbsolutePath() + "/")); extendedClassPaths.add(dir.getAbsolutePath()); extendLibraryPath(dir.getAbsolutePath()); String[] ll = dir.list( new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".jar"); } }); if (ll != null) for (String l : ll) { String fp = new File(dir.getAbsolutePath() + "/" + l).getAbsolutePath(); URL url = new URL("file://" + fp + (fp.endsWith(".jar") ? "" : "/")); loader.addURL(url); extendedClassPaths.add(fp); } File[] f = dir.listFiles( new FileFilter() { public boolean accept(File pathname) { return pathname.isDirectory(); } }); if (f != null) { for (File ff : f) { addExtensionsDirectory(ff); } } System.out.println(" checking <" + dir + "> for natives "); File[] natives = dir.listFiles( new FileFilter() { public boolean accept(File file) { return file.getPath().endsWith(".dylib") || file.getPath().endsWith(".jnilib"); } }); System.out.println(" found <" + natives.length + ">"); for (File n : natives) { try { // System.out.println(" preemptive load of <" + n + ">"); // System.load(n.getAbsolutePath()); } catch (Throwable t) { t.printStackTrace(); } } } else { System.err.println( " warning: wildcard path <" + aa + "> is not a directory or does not exist "); } }
public void addExtensionsDirectory(File path) { if (path.getName().endsWith("**")) path = new File(path.getAbsolutePath().substring(0, path.getAbsolutePath().length() - 2)); if (path.exists()) { // ;//System.out.println(" adding extenions dir <" + // path + // ">"); try { System.out.println(" adding to loader <" + "file://" + path.getAbsolutePath() + "/" + ">"); loader.addURL(new URL("file://" + path.getCanonicalPath() + "/")); // URL[] uu = loader.getURLs(); // for(URL uuu : uu) // ;//System.out.println(" "+uuu); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } extendedClassPaths.add(path.getAbsolutePath()); String[] jars = path.list( new FilenameFilter() { public boolean accept(File dir, String name) { return (name.endsWith(".jar")); } }); if (jars != null) for (String j : jars) { try { loader.addURL(new URL("file://" + path.getCanonicalPath() + "/" + j)); extendedClassPaths.add(path.getCanonicalPath() + "/" + j); JarFile m = new JarFile(new File(path.getCanonicalFile() + "/" + j)); Manifest manifest = m.getManifest(); if (manifest != null) { String a = (String) manifest.getMainAttributes().get(new Attributes.Name("Field-PluginClass")); ; // System.out.println(" jar <" // + path + // "> declares plugin <" // + a + ">"); if (a != null) { plugins.add(a); } injectManifestProperties(manifest); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } System.out.println(" checking <" + path + "> for natives "); File[] natives = path.listFiles( new FileFilter() { public boolean accept(File file) { return file.getPath().endsWith(".dylib") || file.getPath().endsWith(".jnilib"); } }); System.out.println(" found <" + natives.length + ">"); for (File n : natives) { try { // System.load(n.getAbsolutePath()); } catch (Throwable t) { t.printStackTrace(); } } File[] dirs = path.listFiles( new FileFilter() { public boolean accept(File file) { return file.isDirectory() && !file.getName().endsWith("_"); } }); for (File j : dirs) { // ;//System.out.println(" adding next dir <" + // j + // ">"); addExtensionsDirectory(j); // try { // loader.addURL(new URL("file://" + // j.getAbsolutePath())); // extendedClassPaths.add(j.getAbsolutePath()); // } catch (MalformedURLException e) { // e.printStackTrace(); // } } File[] rawManifests = path.listFiles( new FileFilter() { public boolean accept(File file) { return (file.getAbsolutePath().endsWith(".mf") && !prohibitExtension( file.getName().substring(0, file.getName().length() - 3))) || ((file.getAbsolutePath().endsWith(".mf_")) && alsoAcceptExtension( file.getName().substring(0, file.getName().length() - 4))); } }); for (File j : rawManifests) { // ;//System.out.println(" adding raw manifest <" // + // j + ">"); try { Manifest m = new Manifest(new BufferedInputStream(new FileInputStream(j))); String aa = (String) m.getMainAttributes().get(new Attributes.Name("Field-RedirectionPath")); ; // System.out.println(aa + " " + j); if (aa != null && aa.endsWith("**")) { addWildcardPath(aa); } else if (aa != null) { for (String a : aa.split(":")) { a = a.trim(); String fp = (new File(a).isAbsolute() ? new File(a).getAbsolutePath() : new File(j.getParent(), a).getAbsolutePath()); if (!extendedClassPaths.contains(fp)) { if (!new File(fp).exists()) { System.err.println( " warning, path <" + new File(fp).getAbsolutePath() + ">added to classpath through Field-RedirectionPath inside extension " + j + " doesn't exist"); } else { URL url = new URL("file://" + fp + (fp.endsWith(".jar") ? "" : "/")); ; // System.out.println(" adding url to main classloader <" // + url // + // "> <" // + new // File(url.getPath()).exists() // + // ">"); loader.addURL(url); extendedClassPaths.add(fp); } } } } else { } String b = (String) m.getMainAttributes().get(new Attributes.Name("Field-PluginClass")); if (b != null) { plugins.add(b); } injectManifestProperties(m); } catch (FileNotFoundException e) { // TODO // Auto-generated // catch // block e.printStackTrace(); } catch (IOException e) { // TODO // Auto-generated // catch // block e.printStackTrace(); } } } }
public Class<?> loadClass(String classname, byte[] b) { Class<?> c = loader._defineClass(classname, b, 0, b.length); return c; }
public static List<JarClassModel> getJarMethod(String jarFile,String dependfile) throws Exception { String NORMAL_METHOD= "waitequalsnotifynotifyAlltoStringhashCodegetClass"; List<JarClassModel> jarClassModels = new ArrayList<>(); // List<String[]> a = new ArrayList<String[]>(); /* MyClassLoader myclassLoader = new MyClassLoader(urls,null); */ try { //通过jarFile 和JarEntry得到所有的类 JarFile jar = new JarFile(jarFile);// Enumeration<JarEntry> e = jar.entries(); while (e.hasMoreElements()) { JarEntry entry = (JarEntry) e.nextElement(); //entry.getMethod() if (entry.getName().indexOf("META-INF") < 0) { String sName = entry.getName(); String substr[] = sName.split("/"); String pName = ""; for (int i = 0; i < substr.length - 1; i++) { if (i > 0) pName = pName + "." + substr[i]; else pName = substr[i]; } //System.out.println(pName); if (sName.indexOf(".class") < 0) { sName = sName.substring(0, sName.length() - 1); } else { //通过URLClassLoader.loadClass方法得到具体某个类 URL[] urls=new URL[]{}; MyClassLoader myClassLoader=new MyClassLoader(urls,null); if (dependfile != null) { List<String> dependjarlist = getDependJarFiles(dependfile); for (String string : dependjarlist) { URL url=new URL("file:"+string); myClassLoader.addJar(url); } } URL url1=new URL("file:"+jarFile); myClassLoader.addJar(url1); String ppName = sName.replace("/", ".").replace(".class", ""); Class<?> myClass = null; try { myClass = myClassLoader.loadClass(ppName); } catch (ClassNotFoundException e2) { // TODO: handle exception System.out.println("class not found exception"); e2.printStackTrace(); } /* //通过getMethods得到类中包含的方法 System.out.println("------------------------------------------------"); System.out.println("sName:"+sName); System.out.println("pName:"+pName); System.out.println("ppName:"+ppName); */ Method m[] = myClass.getMethods(); if (m.length>0) { JarClassModel jarClassModel = new JarClassModel(pName,ppName); for(int i=0; i<m.length; i++){ String sm = m[i].getName(); if (NORMAL_METHOD.indexOf(sm) <0) { JarMethodModel jarMethodModel = new JarMethodModel(sm); // System.out.println("sm:"+sm); Type[] parameter = m[i].getGenericParameterTypes(); List<String> paraList = new ArrayList<String>(); for (int j = 0; j < parameter.length; j++) { if (parameter[j].toString().contains(" ")) { // System.out.println(parameter[j].toString().split(" ")[0]); // System.out.println(parameter[j].toString().split(" ")[1]); paraList.add(parameter[j].toString().split(" ")[1]); }else { // System.out.println("para:"+parameter[j].toString()); paraList.add(parameter[j].toString()); } } jarMethodModel.setParameters(paraList); jarClassModel.addmethod(jarMethodModel); } } jarClassModels.add(jarClassModel); } // System.out.println("------------------------------------------------"); } } } return jarClassModels; } catch (IOException e1) { // TODO Auto-generated catch block //e1.printStackTrace(); } catch (NullPointerException e2){ //e2.printStackTrace(); } catch (Exception e) { // TODO: handle exception //e.printStackTrace(); } return jarClassModels; }