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

org.zeroturnaround.javarebel.integration.tomcat.WebappClassLoaderClassBytecodeProcessor Maven / Gradle / Ivy

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

import org.zeroturnaround.bundled.javassist.ClassPool;
import org.zeroturnaround.bundled.javassist.CtClass;
import org.zeroturnaround.bundled.javassist.CtConstructor;
import org.zeroturnaround.bundled.javassist.NotFoundException;
import org.zeroturnaround.javarebel.integration.generic.FindClassClassBytecodeProcessor;

/**
 * Transforms org.apache.catalina.loader.WebappClassLoader.
 * 
 * @see FindClassClassBytecodeProcessor
 * 
 * @author Rein Raudjärv
 */
public class WebappClassLoaderClassBytecodeProcessor extends FindClassClassBytecodeProcessor {

  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("$0.started = true;");
    }
    
    super.process(cp, cl, ctClass);
    
    for (int i = 0; i < constructors.length; i++) {
      CtConstructor constructor = constructors[i];
      constructor.insertAfter("$0.started = false;");
    }
    
    ctClass.getDeclaredMethod("findLoadedResource").setBody(
        "{ " +
        "  java.net.URL url = findResource($1);" +
        "  try {" +
        "    return url != null ? url.openStream() : null;" +
        "  } catch (java.io.IOException e) {" +
        "    return null;" +
        "  }" +
    "}");
    
    cp.importPackage("org.zeroturnaround.javarebel");
    
    try {
      ctClass.getDeclaredMethod("stop").insertBefore(
          "{" +
          "  IntegrationFactory.getInstance().unregisterClassLoader($0);" +
          "}");
    } catch (NotFoundException e) {
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy