Search in sources :

Example 6 with CannotCompileException

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

the class DeltaSpikeProxyTransformer method patchAsmProxyClassGenerator.

/**
 * Delegates loadClass to org.hotswap.agent.plugin.deltaspike.proxy.ProxyClassLoadingDelegate::loadClass
 *
 * @param ctClass
 * @throws NotFoundException the not found exception
 * @throws CannotCompileException the cannot compile exception
 */
@OnClassLoadEvent(classNameRegexp = "org.apache.deltaspike.proxy.impl.AsmProxyClassGenerator")
public static void patchAsmProxyClassGenerator(CtClass ctClass) throws NotFoundException, CannotCompileException {
    CtMethod generateProxyClassMethod = ctClass.getDeclaredMethod("generateProxyClass");
    generateProxyClassMethod.instrument(new ExprEditor() {

        public void edit(MethodCall m) throws CannotCompileException {
            if (m.getClassName().equals("org.apache.deltaspike.proxy.impl.AsmProxyClassGenerator") && m.getMethodName().equals("loadClass"))
                m.replace("{ $_ = org.hotswap.agent.plugin.deltaspike.proxy.ProxyClassLoadingDelegate.loadClass($$); }");
        }
    });
}
Also used : ExprEditor(org.hotswap.agent.javassist.expr.ExprEditor) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException) MethodCall(org.hotswap.agent.javassist.expr.MethodCall) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 7 with CannotCompileException

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

the class ViewConfigTransformer method patchViewConfigExtension.

/**
 * Register DeltaspikePlugin and add reinitialization method to RepositoryComponent
 *
 * @param ctClass
 * @param classPool the class pool
 * @throws CannotCompileException the cannot compile exception
 * @throws NotFoundException the not found exception
 */
@OnClassLoadEvent(classNameRegexp = "org.apache.deltaspike.jsf.impl.config.view.ViewConfigExtension")
public static void patchViewConfigExtension(CtClass ctClass, ClassPool classPool) throws CannotCompileException, NotFoundException {
    CtMethod init = ctClass.getDeclaredMethod("init");
    init.insertAfter("{" + "if (this.isActivated) {" + PluginManagerInvoker.buildInitializePlugin(DeltaSpikePlugin.class) + "}" + "}");
    LOGGER.debug("org.apache.deltaspike.jsf.impl.config.view.ViewConfigExtension enhanced with plugin initialization.");
    CtClass viewConfigResProxyClass = classPool.get("org.hotswap.agent.plugin.deltaspike.jsf.ViewConfigResolverProxy");
    CtField viewConfigResProxyField = new CtField(viewConfigResProxyClass, VIEW_CONFIG_RESOLVER_PROXY_FIELD, ctClass);
    ctClass.addField(viewConfigResProxyField);
    CtMethod generateProxyClassMethod = ctClass.getDeclaredMethod("transformMetaDataTree");
    generateProxyClassMethod.instrument(new ExprEditor() {

        public void edit(NewExpr e) throws CannotCompileException {
            if (e.getClassName().equals("org.apache.deltaspike.jsf.impl.config.view.DefaultViewConfigResolver"))
                e.replace("{ " + "java.lang.Object _resolver = new org.apache.deltaspike.jsf.impl.config.view.DefaultViewConfigResolver($$); " + "if (this." + VIEW_CONFIG_RESOLVER_PROXY_FIELD + "==null) {" + "this." + VIEW_CONFIG_RESOLVER_PROXY_FIELD + "=new org.hotswap.agent.plugin.deltaspike.jsf.ViewConfigResolverProxy();" + "}" + "this." + VIEW_CONFIG_RESOLVER_PROXY_FIELD + ".setViewConfigResolver(_resolver);" + "java.util.List _list = org.hotswap.agent.plugin.deltaspike.jsf.ViewConfigResolverUtils.findViewConfigRootClasses(this.rootViewConfigNode);" + PluginManagerInvoker.buildCallPluginMethod(DeltaSpikePlugin.class, "registerViewConfigRootClasses", "this", "java.lang.Object", "_list", "java.util.List") + "   $_ = this." + VIEW_CONFIG_RESOLVER_PROXY_FIELD + ";" + "}");
        }
    });
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) CtField(org.hotswap.agent.javassist.CtField) ExprEditor(org.hotswap.agent.javassist.expr.ExprEditor) NewExpr(org.hotswap.agent.javassist.expr.NewExpr) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 8 with CannotCompileException

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

the class ELResolverPlugin method checkApacheEL.

private static boolean checkApacheEL(CtClass ctClass) {
    try {
        // ApacheEL has field cache
        CtField field = ctClass.getField("cache");
        // Apache BeanELResolver (has cache property)
        ctClass.addField(new CtField(CtClass.booleanType, "$$ha$purgeRequested", ctClass), CtField.Initializer.constant(false));
        ctClass.addMethod(CtNewMethod.make("public void " + PURGE_CLASS_CACHE_METHOD_NAME + "(java.lang.ClassLoader classLoader) {" + "$$ha$purgeRequested=true;" + "}", ctClass));
        CtMethod mGetBeanProperty = ctClass.getDeclaredMethod("property");
        mGetBeanProperty.insertBefore("if($$ha$purgeRequested) {" + "$$ha$purgeRequested=false;" + "this.cache = new javax.el.BeanELResolver.ConcurrentCache(CACHE_SIZE); " + "}");
        return true;
    } catch (NotFoundException e1) {
    } catch (CannotCompileException e2) {
    }
    return false;
}
Also used : CtField(org.hotswap.agent.javassist.CtField) NotFoundException(org.hotswap.agent.javassist.NotFoundException) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException) CtMethod(org.hotswap.agent.javassist.CtMethod)

Example 9 with CannotCompileException

use of org.hotswap.agent.javassist.CannotCompileException 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 10 with CannotCompileException

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

the class ClassFile method setSuperclass.

/**
 * Sets the super class.
 *
 * <p>
 * The new super class should inherit from the old super class.
 * This method modifies constructors so that they call constructors declared
 * in the new super class.
 */
public void setSuperclass(String superclass) throws CannotCompileException {
    if (superclass == null)
        superclass = "java.lang.Object";
    try {
        this.superClass = constPool.addClassInfo(superclass);
        ArrayList list = methods;
        int n = list.size();
        for (int i = 0; i < n; ++i) {
            MethodInfo minfo = (MethodInfo) list.get(i);
            minfo.setSuperclass(superclass);
        }
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
    cachedSuperclass = superclass;
}
Also used : ArrayList(java.util.ArrayList) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException)

Aggregations

CannotCompileException (org.hotswap.agent.javassist.CannotCompileException)23 CtMethod (org.hotswap.agent.javassist.CtMethod)11 NotFoundException (org.hotswap.agent.javassist.NotFoundException)9 OnClassLoadEvent (org.hotswap.agent.annotation.OnClassLoadEvent)8 CtClass (org.hotswap.agent.javassist.CtClass)7 ExprEditor (org.hotswap.agent.javassist.expr.ExprEditor)6 CtField (org.hotswap.agent.javassist.CtField)4 MethodCall (org.hotswap.agent.javassist.expr.MethodCall)4 IOException (java.io.IOException)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)2 ClassPool (org.hotswap.agent.javassist.ClassPool)2 CtConstructor (org.hotswap.agent.javassist.CtConstructor)2 LoaderClassPath (org.hotswap.agent.javassist.LoaderClassPath)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ProtectionDomain (java.security.ProtectionDomain)1 LoadEvent (org.hotswap.agent.annotation.LoadEvent)1