Search in sources :

Example 1 with OnClassLoadEvent

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

the class OwbPlugin method classReload.

/**
 * Called on class redefinition. Class may be bean class
 *
 * @param classLoader the class loader in which class is redefined (Archive class loader)
 * @param ctClass the ct class
 * @param original the original
 */
@OnClassLoadEvent(classNameRegexp = ".*", events = LoadEvent.REDEFINE)
public void classReload(ClassLoader classLoader, CtClass ctClass, Class<?> original) {
    if (classLoader != appClassLoader) {
        LOGGER.debug("Attempt to redefine class '{}' in unsupported classLoader{}.", original.getName(), classLoader);
        return;
    }
    if (original == null || isSyntheticCdiClass(ctClass.getName()) || isInnerNonPublicStaticClass(ctClass)) {
        if (original != null) {
            LOGGER.trace("Skipping synthetic or inner class {}.", original.getName());
        }
        return;
    }
    try {
        String classUrl = ctClass.getURL().toExternalForm();
        Iterator<Entry<URL, URL>> iterator = registeredArchives.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<URL, URL> entry = iterator.next();
            if (classUrl.startsWith(entry.getKey().toExternalForm())) {
                LOGGER.debug("Class '{}' redefined in classLoader {}.", original.getName(), classLoader);
                String oldSignForProxyCheck = OwbClassSignatureHelper.getSignatureForProxyClass(original);
                String oldSignByStrategy = OwbClassSignatureHelper.getSignatureByStrategy(beanReloadStrategy, original);
                scheduler.scheduleCommand(new BeanClassRefreshCommand(appClassLoader, original.getName(), oldSignForProxyCheck, oldSignByStrategy, entry.getValue(), beanReloadStrategy), WAIT_ON_REDEFINE);
                break;
            }
        }
    } catch (Exception e) {
        LOGGER.error("classReload() exception {}.", e, e.getMessage());
    }
}
Also used : Entry(java.util.Map.Entry) BeanClassRefreshCommand(org.hotswap.agent.plugin.owb.command.BeanClassRefreshCommand) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) NotFoundException(org.hotswap.agent.javassist.NotFoundException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 2 with OnClassLoadEvent

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

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

the class ProxyFactoryTransformer method patchProxyFactory.

/**
 * Patch AbstractProxyFactory 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.apache.webbeans.proxy.AbstractProxyFactory")
public static void patchProxyFactory(CtClass ctClass, ClassPool classPool) throws NotFoundException, CannotCompileException {
    CtMethod getProxyClassMethod = ctClass.getDeclaredMethod("getUnusedProxyClassName");
    getProxyClassMethod.instrument(new ExprEditor() {

        public void edit(MethodCall m) throws CannotCompileException {
            if (m.getClassName().equals(Class.class.getName()) && m.getMethodName().equals("forName"))
                m.replace("{ $_ = org.hotswap.agent.plugin.owb.command.ProxyClassLoadingDelegate.forName($$); }");
        }
    });
    CtMethod createProxyClassMethod = ctClass.getDeclaredMethod("createProxyClass", new CtClass[] { classPool.get(ClassLoader.class.getName()), classPool.get(String.class.getName()), classPool.get(Class.class.getName()), classPool.get(Method.class.getName() + "[]"), classPool.get(Method.class.getName() + "[]"), classPool.get(Constructor.class.getName()) });
    createProxyClassMethod.instrument(new ExprEditor() {

        public void edit(MethodCall m) throws CannotCompileException {
            if (m.getMethodName().equals("defineAndLoadClass"))
                m.replace("{ $_ = org.hotswap.agent.plugin.owb.command.ProxyClassLoadingDelegate.defineAndLoadClass(this, $$); }");
        }
    });
}
Also used : ExprEditor(org.hotswap.agent.javassist.expr.ExprEditor) CtClass(org.hotswap.agent.javassist.CtClass) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException) CtMethod(org.hotswap.agent.javassist.CtMethod) Method(java.lang.reflect.Method) MethodCall(org.hotswap.agent.javassist.expr.MethodCall) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 4 with OnClassLoadEvent

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

the class ResteasyRegistryPlugin method patchFilterDispatcher.

/**
 * @param ctClass
 * @param classPool
 */
@OnClassLoadEvent(classNameRegexp = "org.jboss.resteasy.plugins.server.servlet.FilterDispatcher")
public static void patchFilterDispatcher(CtClass ctClass, ClassPool classPool) {
    try {
        CtMethod init = ctClass.getDeclaredMethod("init");
        init.insertAfter(// 
        "" + // 
        "java.lang.ClassLoader $$cl = Thread.currentThread().getContextClassLoader();" + // 
        "java.lang.Object $$servletContext = servletConfig.getServletContext();" + // 
        PluginManagerInvoker.buildInitializePlugin(ResteasyRegistryPlugin.class, "$$cl") + PluginManagerInvoker.buildCallPluginMethod("$$cl", ResteasyRegistryPlugin.class, "registerContext", "$$servletContext", // 
        "java.lang.Object") + PluginManagerInvoker.buildCallPluginMethod("$$cl", ResteasyRegistryPlugin.class, "registerServletContainerDispatcher", "servletContainerDispatcher", // 
        "java.lang.Object"));
    } catch (NotFoundException | CannotCompileException e) {
        LOGGER.error("Error patching FilterDispatcher", e);
    }
}
Also used : NotFoundException(org.hotswap.agent.javassist.NotFoundException) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 5 with OnClassLoadEvent

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

the class ResteasyRegistryPlugin method patchServletDispatcher.

@OnClassLoadEvent(classNameRegexp = "org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher")
public static void patchServletDispatcher(CtClass ctClass, ClassPool classPool) {
    try {
        CtMethod init = ctClass.getDeclaredMethod("init");
        init.insertAfter(// 
        "" + // 
        "java.lang.Object $$servletContext = servletConfig.getServletContext();" + // 
        "java.lang.ClassLoader $$cl = Thread.currentThread().getContextClassLoader();" + // 
        PluginManagerInvoker.buildInitializePlugin(ResteasyRegistryPlugin.class, "$$cl") + PluginManagerInvoker.buildCallPluginMethod("$$cl", ResteasyRegistryPlugin.class, "registerContext", "$$servletContext", // 
        "java.lang.Object") + PluginManagerInvoker.buildCallPluginMethod("$$cl", ResteasyRegistryPlugin.class, "registerServletContainerDispatcher", "servletContainerDispatcher", // 
        "java.lang.Object"));
    } catch (NotFoundException | CannotCompileException e) {
        LOGGER.error("Error patching HttpServletDispatcher", e);
    }
}
Also used : NotFoundException(org.hotswap.agent.javassist.NotFoundException) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException) 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