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);
}
}
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);
}
}
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);
}
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());
}
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;
}
Aggregations