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

org.zeroturnaround.javarebel.integration.eclipse301.Eclipse301DefaultClassLoaderClassBytecodeProcessor Maven / Gradle / Ivy

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

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

public class Eclipse301DefaultClassLoaderClassBytecodeProcessor extends
		JavassistClassBytecodeProcessor {

	public void process(ClassPool cp, ClassLoader cl, CtClass ctClass) throws Exception {
		ctClass.addInterface(cp.get(ClassResourceSource.class.getName()));

		CtConstructor[] constructors = ctClass.getConstructors();
		for (int i = 0; i < constructors.length; i++) {
			CtConstructor constructor = constructors[i];
			constructor.insertAfter("org.zeroturnaround.javarebel.IntegrationFactory.getInstance().registerClassLoader("
					+ "$0, "
					+ "(org.zeroturnaround.javarebel.ClassResourceSource)$0);");
		}

		CtMethod m = ctClass.getMethod("findClassImpl", "(Ljava/lang/String;Lorg/eclipse/osgi/framework/internal/defaultadaptor/DefaultClassLoader$ClasspathEntry;)Ljava/lang/Class;");

		m.insertBefore("{   synchronized ($0) {"
				+ "      Class result = "
				+ "        org.zeroturnaround.javarebel.IntegrationFactory.getInstance()"
				+ "          .findReloadableClass($0, $1);\n"
				+ "      if (result != null)" + "        return result;\n"
				+ "    }}\n");

        ctClass.addMethod(CtNewMethod.make("public org.zeroturnaround.javarebel.ClassResource getLocalResource(String name) {"
                + "     org.eclipse.osgi.framework.adaptor.core.BundleEntry entry = null;"
                + "     for (int i = 0; i < classpathEntries.length; i++) {\r\n"
                + "         if (classpathEntries[i] != null) {\r\n"
                + "             entry = classpathEntries[i].getBundleFile().getEntry(name);"
                + "             if (entry != null) {\r\n"
                + "                 break;\r\n"
                + "             }\r\n"
                + "         }\r\n"
                + "     }"
                + "     if (entry == null) {"
                + "     if (fragClasspaths != null) {\r\n"
                + "         int size = fragClasspaths.size();\r\n"
                + "         for (int i = 0; i < size; i++) {\r\n"
                + "             org.eclipse.osgi.framework.internal.defaultadaptor.DefaultClassLoader.FragmentClasspath fragCP = (org.eclipse.osgi.framework.internal.defaultadaptor.DefaultClassLoader.FragmentClasspath) fragClasspaths.elementAt(i);\r\n"
                + "             for (int j = 0; j < fragCP.classpathEntries.length; j++) {\r\n"
                + "                 entry = fragCP.classpathEntries[j].getBundleFile().getEntry(name);\r\n"
                + "                 if (entry != null) {\r\n"
                + "                     break;\r\n"
                + "                 }\r\n"
                + "             }\r\n"
                + "         }\r\n"
                + "     }"
                + "}"
                + "     if (entry == null) return null;\n"
                +

                "    java.net.URL url = entry.getLocalURL();"
                +

                "    if (url == null)"
                + "      return null;"
                +

                "    org.zeroturnaround.javarebel.LoggerFactory.getInstance()"
                + "       .log(\"Found URL '\" + url + \"' for class '\" + $1 + \"'.\");"
                +

                "    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.List entries = new java.util.ArrayList();"
                
                + "     for (int i = 0; i < classpathEntries.length; i++) {\r\n"
                + "         if (classpathEntries[i] != null) {\r\n"
                + "             org.eclipse.osgi.framework.adaptor.core.BundleEntry entry = classpathEntries[i].getBundleFile().getEntry(name);"
                + "             if (entry != null) {\r\n"
                + "                 entries.add(entry);\r\n"
                + "             }\r\n"
                + "         }\r\n"
                + "     }"
                
                + "     if (fragClasspaths != null) {\r\n"
                + "         int size = fragClasspaths.size();\r\n"
                + "         for (int i = 0; i < size; i++) {\r\n"
                + "             org.eclipse.osgi.framework.internal.defaultadaptor.DefaultClassLoader.FragmentClasspath fragCP = (org.eclipse.osgi.framework.internal.defaultadaptor.DefaultClassLoader.FragmentClasspath) fragClasspaths.elementAt(i);\r\n"
                + "             for (int j = 0; j < fragCP.classpathEntries.length; j++) {\r\n"
                + "                 org.eclipse.osgi.framework.adaptor.core.BundleEntry entry = fragCP.classpathEntries[j].getBundleFile().getEntry(name);\r\n"
                + "                 if (entry != null) {\r\n"
                + "                     entries.add(entry);\r\n"
                + "                 }\r\n"
                + "             }\r\n"
                + "         }\r\n"
                + "     }"
                
                + "     if (entries.isEmpty()) return null;\n"
                +

                "    java.util.List resources = new java.util.ArrayList();" +
                "    java.util.Iterator it = entries.iterator();" +
                "    while (it.hasNext()) {" +
                "      java.net.URL url = ((org.eclipse.osgi.framework.adaptor.core.BundleEntry) it.next()).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) {"
				+ "		String filename = $1.replace('.', '/').concat(\".class\");\n"
				+ "org.eclipse.osgi.framework.adaptor.core.BundleEntry entry = null;"
				+ "		for (int i = 0; i < classpathEntries.length; i++) {\r\n"
				+ "			if (classpathEntries[i] != null) {\r\n"
				+ "				entry = classpathEntries[i].getBundleFile().getEntry(filename);"
				+ "				if (entry != null) {\r\n"
				+ "					break;\r\n"
				+ "				}\r\n"
				+ "			}\r\n"
				+ "		}"
				+ "		if (entry == null) {"
				+ "		if (fragClasspaths != null) {\r\n"
				+ "			int size = fragClasspaths.size();\r\n"
				+ "			for (int i = 0; i < size; i++) {\r\n"
				+ "				org.eclipse.osgi.framework.internal.defaultadaptor.DefaultClassLoader.FragmentClasspath fragCP = (org.eclipse.osgi.framework.internal.defaultadaptor.DefaultClassLoader.FragmentClasspath) fragClasspaths.elementAt(i);\r\n"
				+ "				for (int j = 0; j < fragCP.classpathEntries.length; j++) {\r\n"
				+ "					entry = fragCP.classpathEntries[j].getBundleFile().getEntry(filename);\r\n"
				+ "					if (entry != null) {\r\n"
				+ "						break;\r\n"
				+ "					}\r\n"
				+ "				}\r\n"
				+ "			}\r\n"
				+ "		}"
				+ "}"
				+ "		if (entry == null) return null;\n"
				+

				"    java.net.URL url = entry.getLocalURL();"
				+

				"    if (url == null)"
				+ "      return null;"
				+

				"    org.zeroturnaround.javarebel.LoggerFactory.getInstance()"
				+ "       .log(\"Found URL '\" + url + \"' for class '\" + $1 + \"'.\");"
				+

				"    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