Search in sources :

Example 36 with CtMethod

use of org.hotswap.agent.javassist.CtMethod in project HotswapAgent by HotswapProjects.

the class JavaProxyBytecodeTransformer method getInitCall.

@Override
protected String getInitCall(CtClass cc, String initFieldName) throws Exception {
    // clinit method already contains the setting of our static clinitFieldName to true
    CtMethod method = cc.getClassInitializer().toMethod(initFieldName, cc);
    method.setModifiers(Modifier.PRIVATE | Modifier.STATIC);
    cc.addMethod(method);
    return method.getName() + "();";
}
Also used : CtMethod(org.hotswap.agent.javassist.CtMethod)

Example 37 with CtMethod

use of org.hotswap.agent.javassist.CtMethod in project HotswapAgent by HotswapProjects.

the class CglibProxyBytecodeTransformer method getInitCall.

@Override
protected String getInitCall(CtClass cc, String initFieldName) throws Exception {
    CtMethod[] methods = cc.getDeclaredMethods();
    StringBuilder strB = new StringBuilder();
    for (CtMethod ctMethod : methods) {
        if (ctMethod.getName().startsWith("CGLIB$STATICHOOK")) {
            ctMethod.insertAfter(initFieldName + "=true;");
            strB.insert(0, ctMethod.getName() + "();");
            break;
        }
    }
    if (strB.length() == 0)
        throw new RuntimeException("Could not find CGLIB$STATICHOOK method");
    return strB.toString() + "CGLIB$BIND_CALLBACKS(this);";
}
Also used : CtMethod(org.hotswap.agent.javassist.CtMethod)

Example 38 with CtMethod

use of org.hotswap.agent.javassist.CtMethod in project HotswapAgent by HotswapProjects.

the class Javac method compileBody.

/**
 * Compiles a method (or constructor) body.
 *
 * @param src	a single statement or a block.
 *              If null, this method produces a body returning zero or null.
 */
public Bytecode compileBody(CtBehavior method, String src) throws CompileError {
    try {
        int mod = method.getModifiers();
        recordParams(method.getParameterTypes(), Modifier.isStatic(mod));
        CtClass rtype;
        if (method instanceof CtMethod) {
            gen.setThisMethod((CtMethod) method);
            rtype = ((CtMethod) method).getReturnType();
        } else
            rtype = CtClass.voidType;
        recordReturnType(rtype, false);
        boolean isVoid = rtype == CtClass.voidType;
        if (src == null)
            makeDefaultBody(bytecode, rtype);
        else {
            Parser p = new Parser(new Lex(src));
            SymbolTable stb = new SymbolTable(stable);
            Stmnt s = p.parseStatement(stb);
            if (p.hasMore())
                throw new CompileError("the method/constructor body must be surrounded by {}");
            boolean callSuper = false;
            if (method instanceof CtConstructor)
                callSuper = !((CtConstructor) method).isClassInitializer();
            gen.atMethodBody(s, callSuper, isVoid);
        }
        return bytecode;
    } catch (NotFoundException e) {
        throw new CompileError(e.toString());
    }
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) NotFoundException(org.hotswap.agent.javassist.NotFoundException) CtMethod(org.hotswap.agent.javassist.CtMethod) CtConstructor(org.hotswap.agent.javassist.CtConstructor)

Example 39 with CtMethod

use of org.hotswap.agent.javassist.CtMethod in project HotswapAgent by HotswapProjects.

the class BeansDeployerTransformer method transform.

/**
 * Basic CdiArchive transformation.
 *
 * @param clazz
 * @param classPool
 * @throws NotFoundException
 * @throws CannotCompileException
 */
@OnClassLoadEvent(classNameRegexp = "org.apache.webbeans.config.BeansDeployer")
public static void transform(CtClass clazz, ClassPool classPool) throws NotFoundException, CannotCompileException {
    StringBuilder src = new StringBuilder(" if (deployed) {");
    src.append("ClassLoader curCl = Thread.currentThread().getContextClassLoader();");
    src.append(PluginManagerInvoker.buildInitializePlugin(OwbPlugin.class, "curCl"));
    src.append(PluginManagerInvoker.buildCallPluginMethod("curCl", OwbPlugin.class, "init"));
    src.append(PluginManagerInvoker.buildCallPluginMethod("curCl", OwbPlugin.class, "registerBeansXmls", "$1.getBeanXmls()", "java.util.Set"));
    src.append("}");
    CtMethod startApplication = clazz.getDeclaredMethod("deploy");
    startApplication.insertAfter(src.toString());
    LOGGER.debug("Class '{}' patched with OwbPlugin registration.", clazz.getName());
}
Also used : OwbPlugin(org.hotswap.agent.plugin.owb.OwbPlugin) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 40 with CtMethod

use of org.hotswap.agent.javassist.CtMethod in project HotswapAgent by HotswapProjects.

the class JettyPlugin method patchWebXmlConfiguration.

/**
 * Plugin initialization step needs to be fine tuned. It can be intialized only AFTER the classloader
 * already knows about hotswap-agent.properties file (i.e. after webapp basic path is added to the classloader),
 * but BEFORE first servlet is initialized.
 *
 * WebXmlConfiguration seems to be good place which should work in most setups. The plugin is intialized before
 * web.xml file is processed - basic paths should be known, but nothing is processed yet.
 *
 * Application classloader is processed during plugin initialization. It means that other plugins triggered
 * on plugin init should fire as well - for jetty is important core watchResources plugin, which will handle
 * extraClassPath and watchResources configuration properties (jetty fortunately depends only on basic
 * URLClassLoader behaviour which is handled by that plugin).
 */
@OnClassLoadEvent(classNameRegexp = "org.eclipse.jetty.webapp.WebXmlConfiguration")
public static void patchWebXmlConfiguration(CtClass ctClass) throws NotFoundException, CannotCompileException, ClassNotFoundException {
    try {
        // after application context initialized, but before processing started
        CtMethod doStart = ctClass.getDeclaredMethod("configure");
        // init the plugin
        String src = PluginManagerInvoker.buildInitializePlugin(JettyPlugin.class, "context.getClassLoader()");
        src += PluginManagerInvoker.buildCallPluginMethod("context.getClassLoader()", JettyPlugin.class, "init", "context", "java.lang.Object");
        doStart.insertBefore(src);
    } catch (NotFoundException e) {
        LOGGER.warning("org.eclipse.jetty.webapp.WebAppContext does not contain startContext method. Jetty plugin will be disabled.\n" + "*** This is Ok, Jetty plugin handles only special properties ***");
        return;
    }
}
Also used : NotFoundException(org.hotswap.agent.javassist.NotFoundException) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Aggregations

CtMethod (org.hotswap.agent.javassist.CtMethod)48 OnClassLoadEvent (org.hotswap.agent.annotation.OnClassLoadEvent)29 CtClass (org.hotswap.agent.javassist.CtClass)18 NotFoundException (org.hotswap.agent.javassist.NotFoundException)15 CtField (org.hotswap.agent.javassist.CtField)13 CannotCompileException (org.hotswap.agent.javassist.CannotCompileException)11 CtConstructor (org.hotswap.agent.javassist.CtConstructor)6 ExprEditor (org.hotswap.agent.javassist.expr.ExprEditor)6 MethodCall (org.hotswap.agent.javassist.expr.MethodCall)4 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 Set (java.util.Set)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Command (org.hotswap.agent.command.Command)1 PluginManager (org.hotswap.agent.config.PluginManager)1 FieldAccess (org.hotswap.agent.javassist.expr.FieldAccess)1