Search in sources :

Example 1 with CtConstructor

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

the class BeanDeploymentArchiveTransformer method transformGlassFishBda.

/**
 * GlassFish BeanDeploymentArchiveImpl transformation.
 *
 * @param clazz
 * @param classPool
 * @throws NotFoundException
 * @throws CannotCompileException
 */
@OnClassLoadEvent(classNameRegexp = "org.glassfish.weld.BeanDeploymentArchiveImpl")
public static void transformGlassFishBda(CtClass clazz, ClassPool classPool) throws NotFoundException, CannotCompileException {
    StringBuilder src = new StringBuilder("{");
    src.append(PluginManagerInvoker.buildInitializePlugin(WeldPlugin.class, "this.moduleClassLoaderForBDA"));
    src.append(PluginManagerInvoker.buildCallPluginMethod("this.moduleClassLoaderForBDA", WeldPlugin.class, "initInGlassFish"));
    src.append("    Class agC = Class.forName(\"org.hotswap.agent.plugin.weld.command.BeanClassRefreshAgent\", true, this.moduleClassLoaderForBDA);");
    src.append("    java.lang.reflect.Method agM  = agC.getDeclaredMethod(\"registerArchive\", new Class[] {java.lang.ClassLoader.class, org.jboss.weld.bootstrap.spi.BeanDeploymentArchive.class, java.lang.String.class});");
    src.append("    agM.invoke(null, new Object[] { this.moduleClassLoaderForBDA, this, null});");
    src.append("}");
    for (CtConstructor constructor : clazz.getDeclaredConstructors()) {
        constructor.insertAfter(src.toString());
    }
    LOGGER.debug("Class 'org.jboss.as.weld.deployment.BeanDeploymentArchiveImpl' patched with BDA registration.");
}
Also used : WeldPlugin(org.hotswap.agent.plugin.weld.WeldPlugin) CtConstructor(org.hotswap.agent.javassist.CtConstructor) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 2 with CtConstructor

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

the class BeanDeploymentArchiveTransformer method transformJbossBda.

/**
 * JbossAS (Wildfly) BeanDeploymentArchiveImpl transformation.
 *
 * @param clazz
 * @param classPool
 * @throws NotFoundException
 * @throws CannotCompileException
 */
@OnClassLoadEvent(classNameRegexp = "org.jboss.as.weld.deployment.BeanDeploymentArchiveImpl")
public static void transformJbossBda(CtClass clazz, ClassPool classPool) throws NotFoundException, CannotCompileException {
    StringBuilder src = new StringBuilder("{");
    src.append("if (beansXml!=null && beanArchiveType!=null && (\"EXPLICIT\".equals(beanArchiveType.toString()) || \"IMPLICIT\".equals(beanArchiveType.toString()))){");
    src.append(PluginManagerInvoker.buildInitializePlugin(WeldPlugin.class, "module.getClassLoader()"));
    src.append(PluginManagerInvoker.buildCallPluginMethod("module.getClassLoader()", WeldPlugin.class, "initInJBossAS"));
    src.append("    Class agC = Class.forName(\"org.hotswap.agent.plugin.weld.command.BeanClassRefreshAgent\", true, module.getClassLoader());");
    src.append("    java.lang.reflect.Method agM  = agC.getDeclaredMethod(\"registerArchive\", new Class[] {java.lang.ClassLoader.class, org.jboss.weld.bootstrap.spi.BeanDeploymentArchive.class, java.lang.String.class});");
    src.append("    agM.invoke(null, new Object[] { module.getClassLoader(),this, beanArchiveType.toString()});");
    src.append("}}");
    for (CtConstructor constructor : clazz.getDeclaredConstructors()) {
        constructor.insertAfter(src.toString());
    }
    LOGGER.debug("Class 'org.jboss.as.weld.deployment.BeanDeploymentArchiveImpl' patched with BDA registration.");
}
Also used : WeldPlugin(org.hotswap.agent.plugin.weld.WeldPlugin) CtConstructor(org.hotswap.agent.javassist.CtConstructor) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 3 with CtConstructor

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

the class ProxyFactoryTransformer method patchProxyFactory.

/**
 * Patch ProxyFactory class.
 *   - add factory registration into constructor
 *   - changes call classLoader.loadClass(...) in getProxyClass() to ProxyClassLoadingDelegate.loadClass(classLoader, ...)
 *   - changes call ClassFileUtils.toClass() in createProxyClass() to ProxyClassLoadingDelegate.loadClass(...)
 *
 * @param ctClass the ProxyFactory class
 * @param classPool the class pool
 * @throws NotFoundException the not found exception
 * @throws CannotCompileException the cannot compile exception
 */
@OnClassLoadEvent(classNameRegexp = "org.jboss.weld.bean.proxy.ProxyFactory")
public static void patchProxyFactory(CtClass ctClass, ClassPool classPool) throws NotFoundException, CannotCompileException {
    CtClass[] constructorParams = new CtClass[] { classPool.get("java.lang.String"), classPool.get("java.lang.Class"), classPool.get("java.util.Set"), // classPool.get("java.lang.String"),
    classPool.get("javax.enterprise.inject.spi.Bean"), classPool.get("boolean") };
    CtConstructor declaredConstructor = ctClass.getDeclaredConstructor(constructorParams);
    // TODO : we should find constructor without this() call and put registration only into this one
    declaredConstructor.insertAfter("if (" + PluginManager.class.getName() + ".getInstance().isPluginInitialized(\"" + WeldPlugin.class.getName() + "\", this.classLoader)) {" + PluginManagerInvoker.buildCallPluginMethod("this.classLoader", WeldPlugin.class, "registerProxyFactory", "this", "java.lang.Object", "bean", "java.lang.Object", "this.classLoader", "java.lang.ClassLoader", "proxiedBeanType", "java.lang.Class") + "}");
    CtMethod getProxyClassMethod = ctClass.getDeclaredMethod("getProxyClass");
    getProxyClassMethod.instrument(new ExprEditor() {

        public void edit(MethodCall m) throws CannotCompileException {
            if (m.getClassName().equals(ClassLoader.class.getName()) && m.getMethodName().equals("loadClass"))
                m.replace("{ $_ = org.hotswap.agent.plugin.weld.command.ProxyClassLoadingDelegate.loadClass(this.classLoader,$1); }");
        }
    });
    CtMethod createProxyClassMethod = ctClass.getDeclaredMethod("createProxyClass");
    createProxyClassMethod.instrument(new ExprEditor() {

        public void edit(MethodCall m) throws CannotCompileException {
            if (m.getClassName().equals("org.jboss.weld.util.bytecode.ClassFileUtils") && m.getMethodName().equals("toClass"))
                m.replace("{ $_ = org.hotswap.agent.plugin.weld.command.ProxyClassLoadingDelegate.toClass($$); }");
        }
    });
}
Also used : PluginManager(org.hotswap.agent.config.PluginManager) CtClass(org.hotswap.agent.javassist.CtClass) WeldPlugin(org.hotswap.agent.plugin.weld.WeldPlugin) 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) CtConstructor(org.hotswap.agent.javassist.CtConstructor) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 4 with CtConstructor

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

the class RepositoryTransformer method patchRepositoryComponent.

/**
 * Register DeltaspikePlugin and add reinitialization method to RepositoryComponent
 *
 * @param ctClass
 * @throws CannotCompileException the cannot compile exception
 */
@OnClassLoadEvent(classNameRegexp = "org.apache.deltaspike.data.impl.meta.RepositoryComponent")
public static void patchRepositoryComponent(CtClass ctClass) throws CannotCompileException {
    StringBuilder src = new StringBuilder("{");
    src.append(PluginManagerInvoker.buildInitializePlugin(DeltaSpikePlugin.class));
    src.append(PluginManagerInvoker.buildCallPluginMethod(DeltaSpikePlugin.class, "registerRepoComponent", "this", "java.lang.Object", "this.repoClass", "java.lang.Class"));
    src.append("}");
    for (CtConstructor constructor : ctClass.getDeclaredConstructors()) {
        constructor.insertAfter(src.toString());
    }
    ctClass.addMethod(CtNewMethod.make("public void " + REINITIALIZE_METHOD + "() {" + "   this.methods.clear(); " + "   initialize();" + "}", ctClass));
    LOGGER.debug("org.apache.deltaspike.data.impl.meta.RepositoryComponent - registration hook and reinitialization method added.");
}
Also used : DeltaSpikePlugin(org.hotswap.agent.plugin.deltaspike.DeltaSpikePlugin) CtConstructor(org.hotswap.agent.javassist.CtConstructor) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 5 with CtConstructor

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

the class MojarraPlugin method facesApplicationAssociateInitialized.

@OnClassLoadEvent(classNameRegexp = "com.sun.faces.application.ApplicationResourceBundle")
public static void facesApplicationAssociateInitialized(CtClass ctClass) throws NotFoundException, CannotCompileException {
    String registerResourceBundle = PluginManagerInvoker.buildCallPluginMethod(MojarraPlugin.class, "registerApplicationResourceBundle", "baseName", "java.lang.String", "resources", "java.lang.Object");
    String buildInitializePlugin = PluginManagerInvoker.buildInitializePlugin(MojarraPlugin.class);
    for (CtConstructor constructor : ctClass.getDeclaredConstructors()) {
        constructor.insertAfter(buildInitializePlugin);
        constructor.insertAfter(registerResourceBundle);
    }
    LOGGER.debug("com.sun.faces.application.ApplicationAssociate enhanced with resource bundles registration.");
}
Also used : CtConstructor(org.hotswap.agent.javassist.CtConstructor) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Aggregations

CtConstructor (org.hotswap.agent.javassist.CtConstructor)21 OnClassLoadEvent (org.hotswap.agent.annotation.OnClassLoadEvent)16 CtClass (org.hotswap.agent.javassist.CtClass)8 CtMethod (org.hotswap.agent.javassist.CtMethod)6 NotFoundException (org.hotswap.agent.javassist.NotFoundException)5 WeldPlugin (org.hotswap.agent.plugin.weld.WeldPlugin)4 CtField (org.hotswap.agent.javassist.CtField)3 CannotCompileException (org.hotswap.agent.javassist.CannotCompileException)2 ExprEditor (org.hotswap.agent.javassist.expr.ExprEditor)2 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Command (org.hotswap.agent.command.Command)1 PluginManager (org.hotswap.agent.config.PluginManager)1 CtBehavior (org.hotswap.agent.javassist.CtBehavior)1 MethodInfo (org.hotswap.agent.javassist.bytecode.MethodInfo)1 FieldAccess (org.hotswap.agent.javassist.expr.FieldAccess)1 MethodCall (org.hotswap.agent.javassist.expr.MethodCall)1 DeltaSpikePlugin (org.hotswap.agent.plugin.deltaspike.DeltaSpikePlugin)1