Search in sources :

Example 11 with CtMethod

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

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

the class Jersey2Plugin 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 = "org.glassfish.jersey.servlet.ServletContainer")
public static void jerseyServletCallInitialized(CtClass ctClass, ClassPool classPool) throws NotFoundException, CannotCompileException {
    CtMethod init = ctClass.getDeclaredMethod("init", new CtClass[] { classPool.get("org.glassfish.jersey.servlet.WebConfig") });
    init.insertBefore(PluginManagerInvoker.buildInitializePlugin(Jersey2Plugin.class));
    LOGGER.info("org.glassfish.jersey.servlet.ServletContainer enhanced with plugin initialization.");
    String registerThis = PluginManagerInvoker.buildCallPluginMethod(Jersey2Plugin.class, "registerJerseyContainer", "this", "java.lang.Object", "getConfiguration()", "java.lang.Object");
    init.insertAfter(registerThis);
    // Workaround a Jersey issue where ServletContainer cannot be reloaded since it is in an immutable state
    CtMethod reload = ctClass.getDeclaredMethod("reload", new CtClass[] { classPool.get("org.glassfish.jersey.server.ResourceConfig") });
    reload.insertBefore("$1 = new org.glassfish.jersey.server.ResourceConfig($1);");
}
Also used : CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 13 with CtMethod

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

the class Jersey2Plugin method fixAnnoationAcceptingListener.

/**
 *  Fix a scanning issue with jersey pre-2.4 versions.  https://java.net/jira/browse/JERSEY-1936
 */
@OnClassLoadEvent(classNameRegexp = "org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener")
public static void fixAnnoationAcceptingListener(CtClass ctClass) throws NotFoundException, CannotCompileException {
    CtMethod process = ctClass.getDeclaredMethod("process");
    process.insertAfter("try { $2.close(); } catch (Exception e) {}");
}
Also used : CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 14 with CtMethod

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

the class Jersey2Plugin method fixSingleHk2LocatorManager.

/**
 * Fix CDI CDI_MULTIPLE_LOCATORS_INTO_SIMPLE_APP exception on class redefinition
 */
@OnClassLoadEvent(classNameRegexp = "org.glassfish.jersey.ext.cdi1x.internal.SingleHk2LocatorManager")
public static void fixSingleHk2LocatorManager(CtClass ctClass) throws NotFoundException, CannotCompileException {
    CtMethod process = ctClass.getDeclaredMethod("registerLocator");
    process.insertBefore("if (this.locator != null) return;");
    LOGGER.debug("SingleHk2LocatorManager : patched()");
}
Also used : CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 15 with CtMethod

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

the class JettyPlugin method patchWebXmlConfiguration6x.

// same as above for older jetty versions
@OnClassLoadEvent(classNameRegexp = "org.mortbay.jetty.webapp.WebXmlConfiguration")
public static void patchWebXmlConfiguration6x(CtClass ctClass) throws NotFoundException, CannotCompileException, ClassNotFoundException {
    try {
        // after application context initialized, but before processing started
        CtMethod doStart = ctClass.getDeclaredMethod("configureWebApp");
        // init the plugin
        String src = PluginManagerInvoker.buildInitializePlugin(JettyPlugin.class, "getWebAppContext().getClassLoader()");
        src += PluginManagerInvoker.buildCallPluginMethod("getWebAppContext().getClassLoader()", JettyPlugin.class, "init", "getWebAppContext()", "java.lang.Object");
        doStart.insertBefore(src);
    } catch (NotFoundException e) {
        LOGGER.warning("org.mortbay.jetty.webapp.WebXmlConfiguration does not contain startContext method. Jetty plugin will be disabled.\n" + "*** This is Ok, Jetty plugin handles only special properties ***");
        return;
    }
}
Also used : NotFoundException(org.hotswap.agent.javassist.NotFoundException) 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