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

org.zeroturnaround.javarebel.integration.classworlds.ClassRealmCBP Maven / Gradle / Ivy

The newest version!
package org.zeroturnaround.javarebel.integration.classworlds;

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 ClassRealmCBP extends JavassistClassBytecodeProcessor {

	public void process(ClassPool cp, ClassLoader cl, CtClass ctClass) throws Exception {
        cp.importPackage("java.util");
        cp.importPackage("java.net");
        cp.importPackage("org.zeroturnaround.javarebel");
        cp.importPackage("org.zeroturnaround.javarebel.support");
        cp.importPackage("org.zeroturnaround.javarebel.integration.util");

        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("IntegrationFactory.getInstance().registerClassLoader($0, (ClassResourceSource) $0);");
        }

        ctClass.addMethod(CtNewMethod.make("public Resource getClassResource(String className) {"
                + " return ResourceUtil.getClassResource(this, className);}", ctClass));

        ctClass.addMethod(CtNewMethod.make("public Resource getLocalResource(String resourcePath) {"
                + " URL url = super.findResource(resourcePath);"
                + " if (url == null) return null;\n"
                + " return ResourceUtil.asResource(url);}", ctClass));

        ctClass.addMethod(CtNewMethod.make("public Resource[] getLocalResources(String resourcePath) {"
        		+ " Enumeration en = super.findResources(resourcePath);"
        		+ " if (en == null || !en.hasMoreElements()) return null;"
        		+ " URL[] urls = ResourceUtil.toURLs(en);"
                + " Resource[] result = new Resource[urls.length];" 
                + " for (int i = 0; i < urls.length; i++) {"
                + "     result[i] = ResourceUtil.asResource(urls[i]);"  
                + " }" 
                + " return result;}", ctClass));

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy