All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zeroturnaround.javarebel.integration.equinox.EquinoxDefaultClassLoaderClassBytecodeProcessor Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package org.zeroturnaround.javarebel.integration.equinox;

import org.zeroturnaround.bundled.javassist.ClassPool;
import org.zeroturnaround.bundled.javassist.CtClass;
import org.zeroturnaround.bundled.javassist.CtConstructor;
import org.zeroturnaround.bundled.javassist.CtNewMethod;
import org.zeroturnaround.javarebel.ClassResourceSource;
import org.zeroturnaround.javarebel.integration.support.JavassistClassBytecodeProcessor;

public class EquinoxDefaultClassLoaderClassBytecodeProcessor extends
		JavassistClassBytecodeProcessor {

	public void process(ClassPool cp, ClassLoader cl, CtClass ctClass) throws Exception {
		CtConstructor[] constructors = ctClass.getConstructors();
		for (int i = 0; i < constructors.length; i++) {
			CtConstructor constructor = constructors[i];
			constructor.insertAfter("org.zeroturnaround.javarebel.IntegrationFactory.getInstance().registerClassLoader(" +
					"(ClassLoader) $0, " +
					"(org.zeroturnaround.javarebel.ClassResourceSource) $0);");
		}
				
		ctClass.addInterface(cp.get(ClassResourceSource.class.getName()));

        ctClass.addMethod(CtNewMethod.make(
                "public org.zeroturnaround.javarebel.ClassResource getLocalResource(String name) {" + 
                "    org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry be = " + 
                "      $0.getClasspathManager()" + 
                "        .findLocalEntry(name);" +
                
                "    if (be == null)" + 
                "      return null;" + 
                
                "    java.net.URL url = be.getLocalURL();" + 
                
                "    if (url == null)" + 
                "      return null;" + 
                
                "    org.zeroturnaround.javarebel.LoggerFactory.getInstance()" +
                "       .log(\"Found URL '\" + url + \"' for resource '\" + name + \"'.\");" + 

                "    if (url.getProtocol().equals(\"file\")) {" + 
                "      return new org.zeroturnaround.javarebel.support.FileClassResource(url);" + 
                "    }" + 
                "    else if (url.getProtocol().equals(\"jar\") || url.getProtocol().equals(\"zip\") || url.getProtocol().equals(\"code-source\")) {" + 
                "      return new org.zeroturnaround.javarebel.support.JarClassResource(url);" + 
                "    }" + 
                "    else {" + 
                "      return new org.zeroturnaround.javarebel.support.FallbackClassResource(url);" + 
                "    }" + 
                "  }"
                , ctClass));
        
        ctClass.addMethod(CtNewMethod.make(
                "public org.zeroturnaround.javarebel.ClassResource[] getLocalResources(String name) {" + 
                "    java.util.Enumeration en = " + 
                "      $0.getClasspathManager()" + 
                "        .findLocalEntries(name);" +
                
                "    if (en == null)" + 
                "      return null;" + 
                
                "    java.util.List resources = new java.util.ArrayList();" +
                "    while (en.hasMoreElements()) {" +
                "      java.net.URL url = ((org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry) en.nextElement()).getLocalURL();" + 
                
                "      org.zeroturnaround.javarebel.LoggerFactory.getInstance()" +
                "         .log(\"Found URL '\" + url + \"' for resource '\" + name + \"'.\");" + 
                
                "      org.zeroturnaround.javarebel.ClassResource resource;" +
                "      if (url.getProtocol().equals(\"file\")) {" + 
                "        resource = new org.zeroturnaround.javarebel.support.FileClassResource(url);" + 
                "      }" + 
                "      else if (url.getProtocol().equals(\"jar\") || url.getProtocol().equals(\"zip\") || url.getProtocol().equals(\"code-source\")) {" + 
                "        resource = new org.zeroturnaround.javarebel.support.JarClassResource(url);" + 
                "      }" + 
                "      else {" + 
                "        resource = new org.zeroturnaround.javarebel.support.FallbackClassResource(url);" + 
                "      }" + 
                
                "      resources.add(resource);" +
                "      " +
                "    }" +
                
                "    return (org.zeroturnaround.javarebel.ClassResource[]) resources.toArray(new org.zeroturnaround.javarebel.ClassResource[resources.size()]);" +
                "  }"
                , ctClass));

        ctClass.addMethod(CtNewMethod.make(
				"public org.zeroturnaround.javarebel.ClassResource getClassResource(String className) {" + 
				"    org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry be = " + 
				"      $0.getClasspathManager()" + 
				"        .findLocalEntry(className.replace('.', '/') + \".class\");" +
				
				"    if (be == null)" + 
				"      return null;" + 
				
				"    java.net.URL url = be.getLocalURL();" + 
				
				"    if (url == null)" + 
				"      return null;" + 
				
				"    org.zeroturnaround.javarebel.LoggerFactory.getInstance()" +
				"       .log(\"Found URL '\" + url + \"' for class '\" + className + \"'.\");" + 

				"    if (url.getProtocol().equals(\"file\")) {" + 
				"      return new org.zeroturnaround.javarebel.support.FileClassResource(url);" + 
				"    }" + 
				"    else if (url.getProtocol().equals(\"jar\") || url.getProtocol().equals(\"zip\") || url.getProtocol().equals(\"code-source\")) {" + 
				"      return new org.zeroturnaround.javarebel.support.JarClassResource(url);" + 
				"    }" + 
				"    else {" + 
				"      return new org.zeroturnaround.javarebel.support.FallbackClassResource(url);" + 
				"    }" + 
				"  }"
				, ctClass));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy