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

org.zeroturnaround.javarebel.integration.oc4j.PolicyClassLoaderCBP Maven / Gradle / Ivy

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

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 PolicyClassLoaderCBP extends JavassistClassBytecodeProcessor {
  public void process(ClassPool cp, ClassLoader cl, CtClass ctClass) throws Exception {
    cp.importPackage("java.util");
    cp.importPackage("java.net");
    cp.importPackage("oracle.classloader");    
    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.getDeclaredMethod("findLocalClass").insertBefore(
        "{   synchronized ($0) {" + 
        "      Class result =" + 
        "        findLoadedClass($2);" + 
        "      if (result != null)" + 
        "        return result;" +
        "      try {" + 
        "        result = " + 
        "          org.zeroturnaround.javarebel.IntegrationFactory.getInstance()" + 
        "            .findReloadableClass($0, $2);" + 
        "        if (result != null)" + 
        "          return result;" +
        "      } catch (ClassNotFoundException e) {" +
        "      }" + 
        "    }}");
    
    ctClass.getDeclaredMethod("findLocalResource").insertBefore(
        "{" +
        "  Integration integration = IntegrationFactory.getInstance();" +
        "  if (integration.isResourceReplaced($0, $2))" +
        "    return integration.findResource($0, $2);" + 
        "}");
    
    ctClass.getDeclaredMethod("addAllLocalResources").insertBefore(
        "{" + 
        "  Integration integration = IntegrationFactory.getInstance();" +
        "  if (integration.isResourceReplaced($0, $2)) {" +
        "    URL[] result = ResourceUtil.toURLs(integration.findResources($0, $2));" +
        "    for (int i = 0; i < result.length; i++) {" +
        "      $3.add(result[i]);" +
        "    }" +
        "    return;" +
        "  }" +
        "}");
    
    // ClassResourceSource methods
    
    ctClass.addMethod(CtNewMethod.make(
        "public Resource getClassResource(String className) {" +
        "  final String resourcePath = className.replace('.', '/') + \".class\";" +
        "  URL url = CodeSourceSearchPolicy.DEFAULT.findResourceURL(resourcePath, $0.codeSources);" +
        "  if (url == null) return null;" +
        "  return ResourceUtil.asResource(url);" +
        "}", ctClass));
    
    ctClass.addMethod(CtNewMethod.make(
        "public Resource getLocalResource(String resourcePath) {" +
        "  URL url = CodeSourceSearchPolicy.DEFAULT.findResourceURL(resourcePath, $0.codeSources);" +
        "  if (url == null) return null;" +
        "  return ResourceUtil.asResource(url);" +
        "}", ctClass));
    
    ctClass.addMethod(CtNewMethod.make(
        "public Resource[] getLocalResources(String resourcePath) {" +
        "  List resources = new ArrayList();" +
        "  CodeSourceSearchPolicy.DEFAULT.findAllResourceURLs(resourcePath, this.codeSources, resources);" +
        "  Resource[] result = new Resource[resources.size()];" +
        "  for (int i = 0; i < result.length; i++) {" + 
        "    result[i] = ResourceUtil.asResource((URL) resources.get(i));" +
        "  }" + 
        "  return result;" +
        "}", ctClass));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy