Search in sources :

Example 1 with CannotCompileException

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

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

use of org.hotswap.agent.javassist.CannotCompileException 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)

Example 4 with CannotCompileException

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

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

the class DeltaSpikeProxyTransformer method instrumentTryToLoadClassForName.

private static void instrumentTryToLoadClassForName(CtClass ctClass, String methodName) throws CannotCompileException {
    try {
        CtMethod getProxyClassMethod = ctClass.getDeclaredMethod(methodName);
        getProxyClassMethod.instrument(new ExprEditor() {

            public void edit(MethodCall m) throws CannotCompileException {
                if (m.getClassName().equals("org.apache.deltaspike.core.util.ClassUtils") && m.getMethodName().equals("tryToLoadClassForName"))
                    m.replace("{ $_ = org.hotswap.agent.plugin.deltaspike.proxy.ProxyClassLoadingDelegate.tryToLoadClassForName($$); }");
            }
        });
    } catch (NotFoundException e) {
        LOGGER.debug("Method '{}' not found in '{}'.", methodName, ctClass.getName());
    }
}
Also used : ExprEditor(org.hotswap.agent.javassist.expr.ExprEditor) NotFoundException(org.hotswap.agent.javassist.NotFoundException) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException) MethodCall(org.hotswap.agent.javassist.expr.MethodCall) CtMethod(org.hotswap.agent.javassist.CtMethod)

Aggregations

CannotCompileException (org.hotswap.agent.javassist.CannotCompileException)23 CtMethod (org.hotswap.agent.javassist.CtMethod)11 NotFoundException (org.hotswap.agent.javassist.NotFoundException)9 OnClassLoadEvent (org.hotswap.agent.annotation.OnClassLoadEvent)8 CtClass (org.hotswap.agent.javassist.CtClass)7 ExprEditor (org.hotswap.agent.javassist.expr.ExprEditor)6 CtField (org.hotswap.agent.javassist.CtField)4 MethodCall (org.hotswap.agent.javassist.expr.MethodCall)4 IOException (java.io.IOException)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)2 ClassPool (org.hotswap.agent.javassist.ClassPool)2 CtConstructor (org.hotswap.agent.javassist.CtConstructor)2 LoaderClassPath (org.hotswap.agent.javassist.LoaderClassPath)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ProtectionDomain (java.security.ProtectionDomain)1 LoadEvent (org.hotswap.agent.annotation.LoadEvent)1