Search in sources :

Example 16 with CtMethod

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

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

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

Example 19 with CtMethod

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

the class HaCdiCommons method transformGet2.

/**
 * Transform  2 arguments get method :
 *    <code>public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext);</code>
 *
 * @param classPool the class pool
 * @param ctClass the ct class
 * @throws CannotCompileException the cannot compile exception
 * @throws NotFoundException the not found exception
 */
public static void transformGet2(ClassPool classPool, CtClass ctClass) throws CannotCompileException, NotFoundException {
    CtMethod methGet2 = ctClass.getDeclaredMethod("get", new CtClass[] { classPool.get("javax.enterprise.context.spi.Contextual"), classPool.get("javax.enterprise.context.spi.CreationalContext") });
    methGet2.insertAfter(getRegistrationCode());
}
Also used : CtMethod(org.hotswap.agent.javassist.CtMethod)

Example 20 with CtMethod

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

the class ELResolverPlugin method checkApacheEL.

private static boolean checkApacheEL(CtClass ctClass) {
    try {
        // ApacheEL has field cache
        CtField field = ctClass.getField("cache");
        // Apache BeanELResolver (has cache property)
        ctClass.addField(new CtField(CtClass.booleanType, "$$ha$purgeRequested", ctClass), CtField.Initializer.constant(false));
        ctClass.addMethod(CtNewMethod.make("public void " + PURGE_CLASS_CACHE_METHOD_NAME + "(java.lang.ClassLoader classLoader) {" + "$$ha$purgeRequested=true;" + "}", ctClass));
        CtMethod mGetBeanProperty = ctClass.getDeclaredMethod("property");
        mGetBeanProperty.insertBefore("if($$ha$purgeRequested) {" + "$$ha$purgeRequested=false;" + "this.cache = new javax.el.BeanELResolver.ConcurrentCache(CACHE_SIZE); " + "}");
        return true;
    } catch (NotFoundException e1) {
    } catch (CannotCompileException e2) {
    }
    return false;
}
Also used : CtField(org.hotswap.agent.javassist.CtField) NotFoundException(org.hotswap.agent.javassist.NotFoundException) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException) CtMethod(org.hotswap.agent.javassist.CtMethod)

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