Search in sources :

Example 21 with OnClassLoadEvent

use of org.hotswap.agent.annotation.OnClassLoadEvent in project HotswapAgent by HotswapProjects.

the class MyFacesPlugin method facesApplicationAssociateInitialized.

@OnClassLoadEvent(classNameRegexp = "org.apache.myfaces.config.RuntimeConfig")
public static void facesApplicationAssociateInitialized(CtClass ctClass) throws NotFoundException, CannotCompileException {
    String buildInitializePlugin = PluginManagerInvoker.buildInitializePlugin(MyFacesPlugin.class);
    for (CtConstructor constructor : ctClass.getDeclaredConstructors()) {
        constructor.insertAfter(buildInitializePlugin);
    }
    LOGGER.debug("org.apache.myfaces.config.RuntimeConfig with plugin initialization.");
}
Also used : CtConstructor(org.hotswap.agent.javassist.CtConstructor) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 22 with OnClassLoadEvent

use of org.hotswap.agent.annotation.OnClassLoadEvent in project HotswapAgent by HotswapProjects.

the class OsgiEquinoxPlugin method patchEquinox.

@OnClassLoadEvent(classNameRegexp = "org.eclipse.osgi.launch.Equinox")
public static void patchEquinox(CtClass ctClass) throws CannotCompileException {
    String initializePlugin = PluginManagerInvoker.buildInitializePlugin(OsgiEquinoxPlugin.class);
    String initializeThis = PluginManagerInvoker.buildCallPluginMethod(OsgiEquinoxPlugin.class, "initOsgiEquinox");
    for (CtConstructor constructor : ctClass.getDeclaredConstructors()) {
        constructor.insertAfter(initializePlugin);
        constructor.insertAfter(initializeThis);
    }
}
Also used : CtConstructor(org.hotswap.agent.javassist.CtConstructor) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 23 with OnClassLoadEvent

use of org.hotswap.agent.annotation.OnClassLoadEvent 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 24 with OnClassLoadEvent

use of org.hotswap.agent.annotation.OnClassLoadEvent 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)

Example 25 with OnClassLoadEvent

use of org.hotswap.agent.annotation.OnClassLoadEvent in project HotswapAgent by HotswapProjects.

the class Jersey1Plugin method jerseyServletCallInitialized.

/**
 *  Initialize the plugin when Jersey's ServletContainer.init(WebConfig config) is called.  This is called from both init() for a servlet
 *  and init(Config) for a filter.
 *
 *  Also, add the ServletContainer to a list of registeredJerseyContainers so that we can call reload on it later when classes change
 */
@OnClassLoadEvent(classNameRegexp = "com.sun.jersey.spi.container.servlet.ServletContainer")
public static void jerseyServletCallInitialized(CtClass ctClass, ClassPool classPool) throws NotFoundException, CannotCompileException {
    CtMethod init = ctClass.getDeclaredMethod("init", new CtClass[] { classPool.get("com.sun.jersey.spi.container.servlet.WebConfig") });
    init.insertBefore(PluginManagerInvoker.buildInitializePlugin(Jersey1Plugin.class));
    LOGGER.info("com.sun.jersey.spi.container.servlet.WebConfig enhanced with plugin initialization.");
    String registerThis = PluginManagerInvoker.buildCallPluginMethod(Jersey1Plugin.class, "registerJerseyContainer", "this", "java.lang.Object", "this.webComponent.getResourceConfig()", "java.lang.Object");
    init.insertAfter(registerThis);
}
Also used : CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Aggregations

OnClassLoadEvent (org.hotswap.agent.annotation.OnClassLoadEvent)50 CtMethod (org.hotswap.agent.javassist.CtMethod)29 CtConstructor (org.hotswap.agent.javassist.CtConstructor)16 CtClass (org.hotswap.agent.javassist.CtClass)13 CtField (org.hotswap.agent.javassist.CtField)10 NotFoundException (org.hotswap.agent.javassist.NotFoundException)10 CannotCompileException (org.hotswap.agent.javassist.CannotCompileException)9 ExprEditor (org.hotswap.agent.javassist.expr.ExprEditor)5 IOException (java.io.IOException)4 WeldPlugin (org.hotswap.agent.plugin.weld.WeldPlugin)4 Method (java.lang.reflect.Method)3 MalformedURLException (java.net.MalformedURLException)3 URISyntaxException (java.net.URISyntaxException)3 MethodCall (org.hotswap.agent.javassist.expr.MethodCall)3 URL (java.net.URL)2 Set (java.util.Set)2 DeltaSpikePlugin (org.hotswap.agent.plugin.deltaspike.DeltaSpikePlugin)2 ClassFileTransformer (java.lang.instrument.ClassFileTransformer)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1