Search in sources :

Example 21 with CtMethod

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

the class ELResolverPlugin method patchJBossReflectionUtil.

@OnClassLoadEvent(classNameRegexp = "org.jboss.el.util.ReflectionUtil")
public static void patchJBossReflectionUtil(CtClass ctClass) throws NotFoundException, CannotCompileException {
    CtField ctField = new CtField(CtClass.booleanType, "$$ha$haInitialized", ctClass);
    ctField.setModifiers(org.hotswap.agent.javassist.Modifier.PRIVATE | org.hotswap.agent.javassist.Modifier.STATIC);
    ctClass.addField(ctField, CtField.Initializer.constant(false));
    String buildInitializePlugin = PluginManagerInvoker.buildInitializePlugin(ELResolverPlugin.class, "base.getClass().getClassLoader()");
    String registerJBossReflectionUtil = PluginManagerInvoker.buildCallPluginMethod("base.getClass().getClassLoader()", ELResolverPlugin.class, "registerJBossReflectionUtil");
    CtMethod mFindMethod = ctClass.getDeclaredMethod("findMethod");
    mFindMethod.insertAfter("if(!$$ha$haInitialized) {" + "$$ha$haInitialized=true;" + buildInitializePlugin + registerJBossReflectionUtil + "}");
    LOGGER.debug("org.jboss.el.util.ReflectionUtil enhanced with resource bundles registration.");
}
Also used : CtField(org.hotswap.agent.javassist.CtField) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 22 with CtMethod

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

the class ELResolverPlugin method checkJuelEL.

private static boolean checkJuelEL(CtClass ctClass) {
    try {
        // JUEL, (JSF BeanELResolver[s])
        // check if we have purgeBeanClasses method
        CtMethod purgeMeth = ctClass.getDeclaredMethod("purgeBeanClasses");
        ctClass.addMethod(CtNewMethod.make("public void " + PURGE_CLASS_CACHE_METHOD_NAME + "(java.lang.ClassLoader classLoader) {" + "purgeBeanClasses(classLoader);" + "}", ctClass));
        return true;
    } catch (NotFoundException | CannotCompileException e) {
    // purgeBeanClasses method not found -do nothing
    }
    return false;
}
Also used : NotFoundException(org.hotswap.agent.javassist.NotFoundException) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException) CtMethod(org.hotswap.agent.javassist.CtMethod)

Example 23 with CtMethod

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

the class ProxyUtil method makeProxy.

/**
 * Make proxy.
 *
 * @param proxy
 *            the proxy
 * @param proxied
 *            the proxied
 * @param classPool
 *            the class pool
 * @param classLoader
 *            the class loader
 * @throws Exception
 *             the exception
 */
public static void makeProxy(CtClass proxy, CtClass proxied, ClassPool classPool, ClassLoader classLoader) throws Exception {
    proxy.setSuperclass(proxied);
    for (CtMethod m : proxied.getMethods()) {
        int mod = m.getModifiers();
        if (// 
        !Modifier.isFinal(mod) && // 
        !Modifier.isStatic(mod) && // 
        isVisible(mod, proxied.getPackageName(), m) && (!m.getDeclaringClass().getName().equals("java.lang.Object"))) {
            String meth = toProxy(m);
            CtMethod newMethod = CtNewMethod.make(meth, proxy);
            proxy.addMethod(newMethod);
        }
    }
    for (CtClass i : proxied.getInterfaces()) {
        proxy.addInterface(i);
    }
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) CtMethod(org.hotswap.agent.javassist.CtMethod)

Example 24 with CtMethod

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

the class ProxyUtil method addMethod.

/**
 * Adds the method.
 *
 * @param classLoader
 *            the class loader
 * @param classPool
 *            the class pool
 * @param clazz
 *            the clazz
 * @param returns
 *            the returns
 * @param method
 *            the method
 * @param args
 *            the args
 * @throws CannotCompileException
 *             the cannot compile exception
 */
public static void addMethod(ClassLoader classLoader, ClassPool classPool, CtClass clazz, String returns, String method, String[] args) throws CannotCompileException {
    try {
        CtMethod oldMethod = clazz.getDeclaredMethod(method, getParamTypes(classPool, args));
        if (oldMethod != null) {
            oldMethod.setName('_' + method);
        }
    } catch (NotFoundException e) {
    }
    StringBuilder body = new StringBuilder();
    // 
    body.append(" public ").append(returns).append(' ').append(method).append('(').append(getMethodArgs(args)).append(')').append("{\n");
    body.append("    ");
    if (!"void".equals(returns)) {
        body.append("return ");
    }
    // 
    body.append(ReInitializableHelper.class.getName()).append('.').append(method).append('(').append(getCallArgs(args)).append(')').append(";\n");
    body.append('}');
    CtMethod newMethod = CtNewMethod.make(body.toString(), clazz);
    clazz.addMethod(newMethod);
}
Also used : NotFoundException(org.hotswap.agent.javassist.NotFoundException) CtMethod(org.hotswap.agent.javassist.CtMethod) ReInitializableHelper(org.hotswap.agent.plugin.hibernate3.session.proxy.ReInitializableHelper)

Example 25 with CtMethod

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

the class Javac method compileMethod.

private CtBehavior compileMethod(Parser p, MethodDecl md) throws CompileError {
    int mod = MemberResolver.getModifiers(md.getModifiers());
    CtClass[] plist = gen.makeParamList(md);
    CtClass[] tlist = gen.makeThrowsList(md);
    recordParams(plist, Modifier.isStatic(mod));
    md = p.parseMethod2(stable, md);
    try {
        if (md.isConstructor()) {
            CtConstructor cons = new CtConstructor(plist, gen.getThisClass());
            cons.setModifiers(mod);
            md.accept(gen);
            cons.getMethodInfo().setCodeAttribute(bytecode.toCodeAttribute());
            cons.setExceptionTypes(tlist);
            return cons;
        } else {
            Declarator r = md.getReturn();
            CtClass rtype = gen.resolver.lookupClass(r);
            recordReturnType(rtype, false);
            CtMethod method = new CtMethod(rtype, r.getVariable().get(), plist, gen.getThisClass());
            method.setModifiers(mod);
            gen.setThisMethod(method);
            md.accept(gen);
            if (md.getBody() != null)
                method.getMethodInfo().setCodeAttribute(bytecode.toCodeAttribute());
            else
                method.setModifiers(mod | Modifier.ABSTRACT);
            method.setExceptionTypes(tlist);
            return method;
        }
    } 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)

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