Search in sources :

Example 1 with CtField

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

the class CdiContextsTransformer method transformOwbContexts.

@OnClassLoadEvent(classNameRegexp = "(org.apache.webbeans.context.AbstractContext)|" + "(org.apache.myfaces.flow.cdi.FlowScopedContextImpl)|" + "(org.apache.myfaces.cdi.view.ViewScopeContextImpl)")
public static void transformOwbContexts(CtClass clazz, ClassPool classPool, ClassLoader cl) throws NotFoundException, CannotCompileException {
    CtClass superClass = clazz.getSuperclass();
    while (superClass != null) {
        if ("org.apache.webbeans.context.AbstractContext".equals(superClass.getName())) {
            return;
        }
        superClass = superClass.getSuperclass();
    }
    LOGGER.debug("Adding interface {} to {}.", OwbHotswapContext.class.getName(), clazz.getName());
    clazz.addInterface(classPool.get(OwbHotswapContext.class.getName()));
    CtField toReloadFld = CtField.make("public transient java.util.Set $$ha$toReloadOwb = null;", clazz);
    clazz.addField(toReloadFld);
    CtField reloadingFld = CtField.make("public transient boolean $$ha$reloadingOwb = false;", clazz);
    clazz.addField(reloadingFld);
    CtMethod addBeanToReload = CtMethod.make("public void $$ha$addBeanToReloadOwb(javax.enterprise.context.spi.Contextual bean){" + "if ($$ha$toReloadOwb == null)" + "$$ha$toReloadOwb = new java.util.HashSet();" + "$$ha$toReloadOwb.add(bean);" + "}", clazz);
    clazz.addMethod(addBeanToReload);
    CtMethod getBeansToReload = CtMethod.make("public java.util.Set $$ha$getBeansToReloadOwb(){return $$ha$toReloadOwb;}", clazz);
    clazz.addMethod(getBeansToReload);
    CtMethod reload = CtMethod.make("public void $$ha$reloadOwb() {" + ContextualReloadHelper.class.getName() + ".reload(this);}", clazz);
    clazz.addMethod(reload);
    CtMethod isActive = clazz.getDeclaredMethod("isActive");
    isActive.insertAfter("if($_ && !$$ha$reloadingOwb ) { " + "$$ha$reloadingOwb = true;" + "$$ha$reloadOwb();" + "$$ha$reloadingOwb = false;" + "}" + "return $_;");
    // addDestroyMethod(clazz, classPool);
    LOGGER.debug("Class '{}' patched with hot-swapping support", clazz.getName());
}
Also used : OwbHotswapContext(org.hotswap.agent.plugin.owb.beans.OwbHotswapContext) ContextualReloadHelper(org.hotswap.agent.plugin.owb.beans.ContextualReloadHelper) CtClass(org.hotswap.agent.javassist.CtClass) CtField(org.hotswap.agent.javassist.CtField) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 2 with CtField

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

the class AbstractProxyBytecodeTransformer method addStaticInitStateField.

/**
 * Adds a static boolean field to the class indicating the state of initialization
 *
 * @param cc
 *            CtClass to be modified
 * @param clinitFieldName
 *            field name in CtClass
 * @throws Exception
 */
protected void addStaticInitStateField(CtClass cc, String clinitFieldName) throws Exception {
    CtField f = new CtField(CtClass.booleanType, clinitFieldName, cc);
    f.setModifiers(Modifier.PRIVATE | Modifier.STATIC);
    // init value "true" will be inside clinit, so the field wont actually be initialized on redefinition
    cc.addField(f, "true");
}
Also used : CtField(org.hotswap.agent.javassist.CtField)

Example 3 with CtField

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

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

the class ModuleClassLoaderTransformer method patchModuleClassLoader.

/**
 * @param ctClass the ct class
 * @throws NotFoundException the not found exception
 * @throws CannotCompileException the cannot compile exception
 */
@OnClassLoadEvent(classNameRegexp = "org.jboss.modules.ModuleClassLoader")
public static void patchModuleClassLoader(ClassPool classPool, CtClass ctClass) throws CannotCompileException {
    try {
        CtField pathField = ctClass.getDeclaredField("paths");
        CtClass pathsType = pathField.getType();
        String pathsGetter = "";
        CtClass ctHaClassLoader = classPool.get(HotswapAgentClassLoaderExt.class.getName());
        ctClass.addInterface(ctHaClassLoader);
        if ("java.util.concurrent.atomic.AtomicReference".equals(pathsType.getName())) {
            // version>=1.5
            pathsGetter = ".get()";
        }
        CtClass objectClass = classPool.get(Object.class.getName());
        CtField ctField = new CtField(objectClass, "$$ha$prepend", ctClass);
        ctClass.addField(ctField);
        ctClass.addMethod(CtNewMethod.make("private void $$ha$setupPrepend() {" + "       Class clPaths = Class.forName(\"org.jboss.modules.Paths\", true, this.getClass().getClassLoader());" + "       java.lang.reflect.Method spM  = clPaths.getDeclaredMethod(\"$$ha$setPrepend\", new Class[] {java.lang.Object.class});" + "       spM.invoke(this.paths" + pathsGetter + ", new java.lang.Object[] { $$ha$prepend });" + "}", ctClass));
        // Implementation of HotswapAgentClassLoaderExt.setExtraClassPath(...)
        ctClass.addMethod(CtNewMethod.make("public void setExtraClassPath(java.net.URL[] extraClassPath) {" + "   try {" + "       java.util.List resLoaderList = new java.util.ArrayList();" + "       for (int i=0; i<extraClassPath.length; i++) {" + "           try {" + "               java.net.URL url = extraClassPath[i];" + "               java.io.File root = new java.io.File(url.getPath());" + "               org.jboss.modules.ResourceLoader resourceLoader = org.jboss.modules.ResourceLoaders.createFileResourceLoader(url.getPath(), root);" + "               resLoaderList.add(resourceLoader);" + "           } catch (java.lang.Exception e) {" + "           " + ModuleClassLoaderTransformer.class.getName() + ".logSetExtraClassPathException(e);" + "           }" + "       }" + "       this.$$ha$prepend = resLoaderList;" + "       $$ha$setupPrepend();" + "   } catch (java.lang.Exception e) {" + "       " + ModuleClassLoaderTransformer.class.getName() + ".logSetExtraClassPathException(e);" + "   }" + "}", ctClass));
        CtMethod methRecalculate = ctClass.getDeclaredMethod("recalculate");
        methRecalculate.setName("_recalculate");
        ctClass.addMethod(CtNewMethod.make("boolean recalculate() {" + "   boolean ret = _recalculate();" + "   $$ha$setupPrepend();" + "   return ret;" + "}", ctClass));
        CtClass ctResLoadClass = classPool.get("org.jboss.modules.ResourceLoaderSpec[]");
        CtMethod methResourceLoaders = ctClass.getDeclaredMethod("setResourceLoaders", new CtClass[] { ctResLoadClass });
        methResourceLoaders.setBody("{" + "   boolean ret = setResourceLoaders((org.jboss.modules.Paths)this.paths" + pathsGetter + ", $1);" + "   $$ha$setupPrepend();" + "   return ret;" + "}");
    } catch (NotFoundException e) {
        LOGGER.warning("Unable to find field \"paths\" in org.jboss.modules.ModuleClassLoader.", e);
    }
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) CtField(org.hotswap.agent.javassist.CtField) NotFoundException(org.hotswap.agent.javassist.NotFoundException) HotswapAgentClassLoaderExt(org.hotswap.agent.util.classloader.HotswapAgentClassLoaderExt) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 5 with CtField

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

the class ModuleClassLoaderTransformer method patchModulesPaths.

/**
 * @param classPool the class pool
 * @param ctClass the ct class
 * @throws NotFoundException the not found exception
 * @throws CannotCompileException the cannot compile exception
 */
@OnClassLoadEvent(classNameRegexp = "org.jboss.modules.Paths")
public static void patchModulesPaths(ClassPool classPool, CtClass ctClass) throws NotFoundException, CannotCompileException {
    CtClass objectClass = classPool.get(Object.class.getName());
    CtField ctField = new CtField(objectClass, "$$ha$prepend", ctClass);
    ctClass.addField(ctField);
    try {
        CtMethod methGetAllPaths = ctClass.getDeclaredMethod("getAllPaths");
        methGetAllPaths.setBody("{" + "   if (this.$$ha$prepend != null) {" + "       java.util.Map result = new org.hotswap.agent.plugin.jbossmodules.PrependingMap(this.allPaths, this.$$ha$prepend);" + "       return result;" + "   }" + "   return this.allPaths;" + "}");
        ctClass.addMethod(CtNewMethod.make("public void $$ha$setPrepend(java.lang.Object prepend) {" + "   this.$$ha$prepend = prepend; " + "}", ctClass));
    } catch (NotFoundException e) {
        LOGGER.warning("Unable to find method \"getAllPaths()\" in org.jboss.modules.Paths.", e);
    }
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) CtField(org.hotswap.agent.javassist.CtField) NotFoundException(org.hotswap.agent.javassist.NotFoundException) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Aggregations

CtField (org.hotswap.agent.javassist.CtField)19 CtMethod (org.hotswap.agent.javassist.CtMethod)12 OnClassLoadEvent (org.hotswap.agent.annotation.OnClassLoadEvent)9 CtClass (org.hotswap.agent.javassist.CtClass)7 NotFoundException (org.hotswap.agent.javassist.NotFoundException)6 CannotCompileException (org.hotswap.agent.javassist.CannotCompileException)3 Set (java.util.Set)2 CtConstructor (org.hotswap.agent.javassist.CtConstructor)2 ArrayList (java.util.ArrayList)1 ExprEditor (org.hotswap.agent.javassist.expr.ExprEditor)1 NewExpr (org.hotswap.agent.javassist.expr.NewExpr)1 SessionFactoryProxy (org.hotswap.agent.plugin.hibernate3.session.proxy.SessionFactoryProxy)1 ContextualReloadHelper (org.hotswap.agent.plugin.owb.beans.ContextualReloadHelper)1 OwbHotswapContext (org.hotswap.agent.plugin.owb.beans.OwbHotswapContext)1 ContextualReloadHelper (org.hotswap.agent.plugin.weld.beans.ContextualReloadHelper)1 WeldHotswapContext (org.hotswap.agent.plugin.weld.beans.WeldHotswapContext)1 HotswapAgentClassLoaderExt (org.hotswap.agent.util.classloader.HotswapAgentClassLoaderExt)1