Search in sources :

Example 46 with CtMethod

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

the class ELResolverPlugin method patchJBossEl.

/*
     * JBossEL has weak reference cache. Values are stored in ThreadGroupContext cache, that must be flushed from appropriate thread.
     * Therefore we must create request for cleanup cache in PURGE_CLASS_CACHE_METHOD and own cleanup is executed indirectly when
     * application calls getBeanProperty(...).
     */
private static void patchJBossEl(CtClass ctClass) {
    try {
        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));
        try {
            CtMethod mGetBeanProperty = ctClass.getDeclaredMethod("getBeanProperty");
            mGetBeanProperty.insertBefore("if($$ha$purgeRequested) {" + "$$ha$purgeRequested=false;" + "java.lang.reflect.Method meth = javax.el.BeanELResolver.SoftConcurrentHashMap.class.getDeclaredMethod(\"$$ha$createNewInstance\", null);" + "properties = (javax.el.BeanELResolver.SoftConcurrentHashMap) meth.invoke(properties, null);" + "}");
        } catch (NotFoundException e) {
            LOGGER.debug("FIXME : checkJBoss_3_0_EL() 'getBeanProperty(...)' not found in javax.el.BeanELResolver.");
        }
    } catch (CannotCompileException e) {
        LOGGER.error("patchJBossEl() exception {}", e.getMessage());
    }
}
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 47 with CtMethod

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

the class AbstractClassBeanTransformer method transformAbstractClassBean.

/**
 * @param ctClass
 * @param classPool
 * @throws NotFoundException
 * @throws CannotCompileException
 */
@OnClassLoadEvent(classNameRegexp = "org.jboss.weld.bean.AbstractClassBean")
public static void transformAbstractClassBean(CtClass ctClass, ClassPool classPool) throws NotFoundException, CannotCompileException {
    CtMethod method = ctClass.getDeclaredMethod("cleanupAfterBoot");
    method.setBody("{ }");
    LOGGER.debug("AbstractClassBean.cleanupAfterBoot patched");
}
Also used : CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 48 with CtMethod

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

the class CdiContextsTransformer method transformReloadingWeldContexts.

/**
 * Add context reloading functionality to base contexts classes.
 *
 * @param ctClass the class
 * @param classPool the class pool
 * @throws NotFoundException the not found exception
 * @throws CannotCompileException the cannot compile exception
 */
@OnClassLoadEvent(classNameRegexp = "(org.jboss.weld.context.AbstractManagedContext)|" + "(org.jboss.weld.context.AbstractSharedContext)|" + "(org.jboss.weld.context.unbound.DependentContextImpl)|" + "(org.jboss.weld.util.ForwardingContext)|" + "(org.apache.myfaces.flow.cdi.FlowScopedContextImpl)|" + "(org.apache.myfaces.cdi.view.ViewScopeContextImpl)")
public static void transformReloadingWeldContexts(CtClass ctClass, ClassPool classPool) throws NotFoundException, CannotCompileException {
    LOGGER.debug("Adding interface {} to {}.", WeldHotswapContext.class.getName(), ctClass.getName());
    ctClass.addInterface(classPool.get(WeldHotswapContext.class.getName()));
    CtField toReloadFld = CtField.make("public transient java.util.Set $$ha$toReloadWeld = null;", ctClass);
    ctClass.addField(toReloadFld);
    CtField reloadingFld = CtField.make("public transient boolean $$ha$reloadingWeld = false;", ctClass);
    ctClass.addField(reloadingFld);
    CtMethod addBeanToReload = CtMethod.make("public void $$ha$addBeanToReloadWeld(javax.enterprise.context.spi.Contextual bean) {" + "if ($$ha$toReloadWeld == null)" + "$$ha$toReloadWeld = new java.util.HashSet();" + "$$ha$toReloadWeld.add(bean);" + "}", ctClass);
    ctClass.addMethod(addBeanToReload);
    CtMethod getBeansToReload = CtMethod.make("public java.util.Set $$ha$getBeansToReloadWeld(){return $$ha$toReloadWeld;}", ctClass);
    ctClass.addMethod(getBeansToReload);
    CtMethod reload = CtMethod.make("public void $$ha$reloadWeld() {" + ContextualReloadHelper.class.getName() + ".reload(this);}", ctClass);
    ctClass.addMethod(reload);
    CtMethod isActive = ctClass.getDeclaredMethod("isActive");
    isActive.insertAfter("{" + "if($_ && !$$ha$reloadingWeld ) { " + "$$ha$reloadingWeld = true;" + "$$ha$reloadWeld();" + "$$ha$reloadingWeld = false;" + "}" + "return $_;" + "}");
    LOGGER.debug("Class '{}' patched with hot-swapping support", ctClass.getName());
}
Also used : ContextualReloadHelper(org.hotswap.agent.plugin.weld.beans.ContextualReloadHelper) CtField(org.hotswap.agent.javassist.CtField) WeldHotswapContext(org.hotswap.agent.plugin.weld.beans.WeldHotswapContext) 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