Search in sources :

Example 6 with OnClassLoadEvent

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

the class ProxyPlugin method transformCglibProxy.

// @OnClassLoadEvent(classNameRegexp = "com/sun/proxy/\\$Proxy.*", events = LoadEvent.REDEFINE, skipSynthetic = false)
// public static byte[] transformJavaProxy(final Class<?> classBeingRedefined, final byte[] classfileBuffer,
// final ClassPool cp, final CtClass cc) throws IllegalClassFormatException, IOException, RuntimeException {
// try {
// return JavassistProxyTransformer.transform(classBeingRedefined, classfileBuffer, cc, cp);
// } catch (Exception e) {
// LOGGER.error("Error redifining Cglib proxy {}", e, classBeingRedefined.getName());
// }
// return classfileBuffer;
// }
// alternative method of redefining Java proxies, uses a new classlaoder instance
// @OnClassLoadEvent(classNameRegexp = "com/sun/proxy/\\$Proxy.*", events = LoadEvent.REDEFINE, skipSynthetic =
// false)
// public static byte[] transformJavaProxy(final Class<?> classBeingRedefined, final byte[] classfileBuffer,
// final ClassLoader loader) throws IllegalClassFormatException, IOException, RuntimeException {
// try {
// return NewClassLoaderJavaProxyTransformer.transform(classBeingRedefined, classfileBuffer, loader);
// } catch (Exception e) {
// LOGGER.error("Error redifining Cglib proxy {}", e, classBeingRedefined.getName());
// }
// return classfileBuffer;
// }
// 
// // alternative method of redefining Java proxies, uses a 2 step process. Crashed with jvm8
// @OnClassLoadEvent(classNameRegexp = "com/sun/proxy/\\$Proxy.*", events = LoadEvent.REDEFINE, skipSynthetic =
// false)
// public static byte[] transformJavaProxy(final Class<?> classBeingRedefined, final byte[] classfileBuffer,
// final ClassPool cp) {
// try {
// return JavaProxyTransformer.transform(classBeingRedefined, cp, classfileBuffer);
// } catch (Exception e) {
// LOGGER.error("Error redifining Cglib proxy {}", e, classBeingRedefined.getName());
// }
// return classfileBuffer;
// }
@OnClassLoadEvent(classNameRegexp = ".*", events = LoadEvent.REDEFINE, skipSynthetic = false)
public static byte[] transformCglibProxy(final Class<?> classBeingRedefined, final byte[] classfileBuffer, final ClassLoader loader, final ClassPool cp) throws Exception {
    GeneratorParams generatorParams = GeneratorParametersTransformer.getGeneratorParams(loader, classBeingRedefined.getName());
    if (!ClassLoaderHelper.isClassLoderStarted(loader)) {
        return classfileBuffer;
    }
    if (generatorParams == null) {
        return classfileBuffer;
    }
    // flush standard java beans caches
    loader.loadClass("java.beans.Introspector").getMethod("flushCaches").invoke(null);
    if (generatorParams.getParam().getClass().getName().endsWith(".Enhancer")) {
        try {
            return CglibEnhancerProxyTransformer.transform(classBeingRedefined, cp, classfileBuffer, loader, generatorParams);
        } catch (Exception e) {
            LOGGER.error("Error redifining Cglib Enhancer proxy {}", e, classBeingRedefined.getName());
        }
    }
    // Multistep transformation crashed jvm in java8 u05
    if (!isJava8OrNewer)
        try {
            return CglibProxyTransformer.transform(classBeingRedefined, cp, classfileBuffer, generatorParams);
        } catch (Exception e) {
            LOGGER.error("Error redifining Cglib proxy {}", e, classBeingRedefined.getName());
        }
    return classfileBuffer;
}
Also used : GeneratorParams(org.hotswap.agent.plugin.proxy.hscglib.GeneratorParams) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 7 with OnClassLoadEvent

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

the class HotswapWebObjectsPlugin method webObjectsIsStarting.

@OnClassLoadEvent(classNameRegexp = "com.webobjects.appserver.WOApplication")
public static void webObjectsIsStarting(CtClass ctClass) throws NotFoundException, CannotCompileException {
    CtMethod init = ctClass.getDeclaredMethod("run");
    init.insertBefore(PluginManagerInvoker.buildInitializePlugin(HotswapWebObjectsPlugin.class));
    LOGGER.debug("WOApplication.run() enhanced with plugin initialization.");
}
Also used : CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 8 with OnClassLoadEvent

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

the class WeldPlugin method classReload.

/**
 * If bda archive is defined for given class than new BeanClassRefreshCommand is created
 *
 * @param classLoader
 * @param ctClass
 * @param original
 */
@OnClassLoadEvent(classNameRegexp = ".*", events = LoadEvent.REDEFINE)
public void classReload(ClassLoader classLoader, CtClass ctClass, Class<?> original) {
    if (original != null && !isSyntheticCdiClass(ctClass.getName()) && !isInnerNonPublicStaticClass(ctClass)) {
        try {
            String archivePath = getArchivePath(classLoader, ctClass, original.getName());
            LOGGER.debug("Class '{}' redefined for archive {} ", original.getName(), archivePath);
            if (isBdaRegistered(classLoader, archivePath)) {
                String oldSignatureForProxyCheck = WeldClassSignatureHelper.getSignatureForProxyClass(original);
                String oldSignatureByStrategy = WeldClassSignatureHelper.getSignatureByStrategy(beanReloadStrategy, original);
                scheduler.scheduleCommand(new BeanClassRefreshCommand(classLoader, archivePath, registeredProxiedBeans, original.getName(), oldSignatureForProxyCheck, oldSignatureByStrategy, beanReloadStrategy), WAIT_ON_REDEFINE);
            }
        } catch (Exception e) {
            LOGGER.error("classReload() exception {}.", e, e.getMessage());
        }
    }
}
Also used : BeanClassRefreshCommand(org.hotswap.agent.plugin.weld.command.BeanClassRefreshCommand) 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 9 with OnClassLoadEvent

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

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

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