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

org.zeroturnaround.javarebel.integration.netbeans.NetbeansJarClassLoaderCBP Maven / Gradle / Ivy

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

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.bundled.javassist.NotFoundException;
import org.zeroturnaround.javarebel.ClassBytecodeProcessor;
import org.zeroturnaround.javarebel.integration.support.JavassistClassBytecodeProcessor;

public class NetbeansJarClassLoaderCBP 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.generic");
    cp.importPackage("org.zeroturnaround.javarebel.integration.util");

    ctClass.addInterface(cp.get(ClassBytecodeProcessor.class.getName()));

    CtConstructor[] constructors = ctClass.getConstructors();
    for (int i = 0; i < constructors.length; i++) {
      CtConstructor constructor = constructors[i];
      constructor.insertAfter(
          "IntegrationFactory.getInstance().registerClassLoader($0, new FindResourceClassResourceSource($0));" + 
          "addCoveredPackages(Arrays.asList(new String[]{\"com.zeroturnaround.javarebel\"}));"
      );
    }

    // netbeans binary patches some classes, so we try to preserve it
    String patchCode = null;
    CtClass patchClass = null;
    try {
      patchClass = cp.get("org.netbeans.PatchByteCode");

      // netbeans 6.5
      patchClass.getMethod("patch", "([BLjava/lang/String;)[B");
      patchCode = "return org.netbeans.PatchByteCode.patch($3, $1);";
    } catch (NotFoundException e) {
    }
    if (patchClass != null && patchCode == null) {
      try {
        // netbeans 6.7
        patchClass.getMethod("patch", "([B)[B");
        ctClass.getDeclaredField("patchingBytecode");
        patchCode = 
          "if (patchingBytecode == null) { " +
          "  patchingBytecode = Boolean.valueOf(findResource(\"META-INF/.bytecodePatched\") != null); " +
          "} " +
          "if (Boolean.TRUE.equals(patchingBytecode)) { " +
          "  return org.netbeans.PatchByteCode.patch($3);" +
          "} " +
          "return bytecode;";
      } catch (NotFoundException e) {
      }
    }
    // skip patching
    if (patchCode == null) {
      patchCode = "return $3;";
    }

    ctClass.addMethod(
        CtNewMethod.make(
            "public byte[] process(ClassLoader cl, String classname, byte[] bytecode) { " +
            patchCode +
            "}", ctClass)
    );


    CtMethod m = ctClass.getMethod("doLoadClass", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Class;");
    m.insertBefore(
        "{" + 
        "try {" +
        "	IntegrationFactory.getInstance().addIntegrationProcessor($0, $2, $0);" +
        "	Class c = IntegrationFactory.getInstance().findReloadableClass($0, $2);" +
        "	if (c != null) return c;" + 
        "} catch (ClassNotFoundException e) {}" +
        "}"
    );

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy