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

org.zeroturnaround.javarebel.integration.weblogic.CompilerForJDTCBP Maven / Gradle / Ivy

package org.zeroturnaround.javarebel.integration.weblogic;

import org.zeroturnaround.bundled.javassist.CannotCompileException;
import org.zeroturnaround.bundled.javassist.ClassPool;
import org.zeroturnaround.bundled.javassist.CtClass;
import org.zeroturnaround.bundled.javassist.CtMethod;
import org.zeroturnaround.bundled.javassist.expr.ExprEditor;
import org.zeroturnaround.bundled.javassist.expr.FieldAccess;
import org.zeroturnaround.javarebel.integration.support.JavassistClassBytecodeProcessor;

/**
 * Transforms weblogic.ejb.container.ejbc.CompilerForJDT
 * to log Java compiler results.
 * 
 * @author Rein Raudjärv
 */
public class CompilerForJDTCBP extends JavassistClassBytecodeProcessor {
  
  private static final boolean ENABLED = System.getProperty("rebel.log.weblogic.jdt") != null;

  public void process(ClassPool cp, ClassLoader cl, CtClass ctClass) throws Exception {
    if (ENABLED)
      doProcess(cp, ctClass);
  }

  private void doProcess(ClassPool cp, CtClass ctClass) throws CannotCompileException {
    cp.importPackage("org.zeroturnaround.javarebel");
    
    CtMethod[] methods = ctClass.getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
      CtMethod ctMethod = methods[i];
      if ("compile".equals(ctMethod.getName()))
        ctMethod.instrument(new ExprEditor() {
          public void edit(FieldAccess f) throws CannotCompileException {
            if ("result".equals(f.getFieldName()) && f.isWriter()) {
              f.replace(
                  "{" +
                  "  LoggerFactory.getInstance().log(\": \" + $1);" +
                  "  $proceed($1);" +
                  "}");
            }
          }
        });
    }
  }
  
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy