use of org.hotswap.agent.annotation.OnClassLoadEvent in project HotswapAgent by HotswapProjects.
the class ClassInitPlugin method patch.
@OnClassLoadEvent(classNameRegexp = ".*", events = LoadEvent.REDEFINE)
public static void patch(final CtClass ctClass, final ClassLoader classLoader, final Class<?> originalClass) throws IOException, CannotCompileException, NotFoundException {
if (isSyntheticClass(originalClass)) {
return;
}
final String className = ctClass.getName();
try {
CtMethod origMethod = ctClass.getDeclaredMethod(HOTSWAP_AGENT_CLINIT_METHOD);
ctClass.removeMethod(origMethod);
} catch (org.hotswap.agent.javassist.NotFoundException ex) {
// swallow
}
CtConstructor clinit = ctClass.getClassInitializer();
if (clinit != null) {
LOGGER.debug("Adding __ha_clinit to class: {}", className);
CtConstructor haClinit = new CtConstructor(clinit, ctClass, null);
haClinit.getMethodInfo().setName(HOTSWAP_AGENT_CLINIT_METHOD);
haClinit.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
ctClass.addConstructor(haClinit);
final boolean[] reinitializeStatics = new boolean[] { false };
haClinit.instrument(new ExprEditor() {
public void edit(FieldAccess f) throws CannotCompileException {
try {
if (f.isStatic() && f.isWriter()) {
Field originalField = null;
try {
originalField = originalClass.getDeclaredField(f.getFieldName());
} catch (NoSuchFieldException e) {
LOGGER.debug("New field will be initialized {}", f.getFieldName());
reinitializeStatics[0] = true;
}
if (originalField != null) {
// ENUM$VALUES is last in enumeration
if (originalClass.isEnum() && "ENUM$VALUES".equals(f.getFieldName())) {
if (reinitializeStatics[0]) {
LOGGER.debug("New field will be initialized {}", f.getFieldName());
} else {
reinitializeStatics[0] = checkOldEnumValues(ctClass, originalClass);
}
} else {
LOGGER.debug("Skipping old field {}", f.getFieldName());
f.replace("{}");
}
}
}
} catch (Exception e) {
LOGGER.error("Patching __ha_clinit method failed.", e);
}
}
});
if (reinitializeStatics[0]) {
PluginManager.getInstance().getScheduler().scheduleCommand(new Command() {
@Override
public void executeCommand() {
try {
Class<?> clazz = classLoader.loadClass(className);
Method m = clazz.getDeclaredMethod(HOTSWAP_AGENT_CLINIT_METHOD, new Class[] {});
if (m != null) {
m.invoke(null, new Object[] {});
}
} catch (Exception e) {
LOGGER.error("Error initializing redefined class {}", e, className);
} finally {
reloadFlag = false;
}
}
}, // Hack : init should be done after dependant class redefinition. Since the class can
150);
// be proxied by syntetic proxy, the class init must be scheduled after proxy redefinition.
// Currently proxy redefinition (in ProxyPlugin) is scheduled with 100ms delay, therefore
// the class init must be scheduled after it.
} else {
reloadFlag = false;
}
}
}
use of org.hotswap.agent.annotation.OnClassLoadEvent in project HotswapAgent by HotswapProjects.
the class ResteasyPlugin method patchFilterDispatcher.
@OnClassLoadEvent(classNameRegexp = "org.jboss.resteasy.plugins.server.servlet.FilterDispatcher")
public static void patchFilterDispatcher(CtClass ctClass, ClassPool classPool) throws NotFoundException, CannotCompileException {
CtClass fltCfgClass = classPool.get("javax.servlet.FilterConfig");
CtField configField = new CtField(fltCfgClass, FIELD_NAME, ctClass);
ctClass.addField(configField);
CtClass setClass = classPool.get(java.util.Set.class.getName());
CtField paramsField = new CtField(setClass, PARAMETER_FIELD_NAME, ctClass);
ctClass.addField(paramsField);
CtMethod methInit = ctClass.getDeclaredMethod("init");
methInit.insertBefore("{" + " if(this." + PARAMETER_FIELD_NAME + " == null) {" + PluginManagerInvoker.buildInitializePlugin(ResteasyPlugin.class) + PluginManagerInvoker.buildCallPluginMethod(ResteasyPlugin.class, "registerDispatcher", "this", "java.lang.Object") + " }" + " this." + FIELD_NAME + " = $1;" + " this." + PARAMETER_FIELD_NAME + " = " + ResteasyContextParams.class.getName() + ".init($1.getServletContext(), this." + PARAMETER_FIELD_NAME + "); " + "}");
}
use of org.hotswap.agent.annotation.OnClassLoadEvent in project HotswapAgent by HotswapProjects.
the class ResteasyPlugin method patchServletDispatcher.
@OnClassLoadEvent(classNameRegexp = "org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher")
public static void patchServletDispatcher(CtClass ctClass, ClassPool classPool) throws NotFoundException, CannotCompileException {
CtClass fltCfgClass = classPool.get("javax.servlet.ServletConfig");
CtField configField = new CtField(fltCfgClass, FIELD_NAME, ctClass);
ctClass.addField(configField);
CtClass setClass = classPool.get(java.util.Set.class.getName());
CtField paramsField = new CtField(setClass, PARAMETER_FIELD_NAME, ctClass);
ctClass.addField(paramsField);
CtMethod methInit = ctClass.getDeclaredMethod("init");
methInit.insertBefore("{" + " if(this." + PARAMETER_FIELD_NAME + " == null) {" + PluginManagerInvoker.buildInitializePlugin(ResteasyPlugin.class) + PluginManagerInvoker.buildCallPluginMethod(ResteasyPlugin.class, "registerDispatcher", "this", "java.lang.Object") + " }" + " this." + FIELD_NAME + " = $1;" + " this." + PARAMETER_FIELD_NAME + " = " + ResteasyContextParams.class.getName() + ".init($1.getServletContext(), this." + PARAMETER_FIELD_NAME + "); " + "}");
}
use of org.hotswap.agent.annotation.OnClassLoadEvent in project HotswapAgent by HotswapProjects.
the class SeamPlugin method seamServletCallInitialized.
@OnClassLoadEvent(classNameRegexp = "org.jboss.seam.init.Initialization")
public static void seamServletCallInitialized(CtClass ctClass) throws NotFoundException, CannotCompileException {
CtMethod init = ctClass.getDeclaredMethod("init");
init.insertBefore("{" + PluginManagerInvoker.buildInitializePlugin(SeamPlugin.class) + "}");
LOGGER.debug("org.jboss.seam.init.Initialization enhanced with plugin initialization.");
}
use of org.hotswap.agent.annotation.OnClassLoadEvent in project HotswapAgent by HotswapProjects.
the class WebappLoaderTransformer method patchStandardRoot.
/**
* Resource lookup for Tomcat 8x.
*
* Before the resource is handled by Tomcat, try to get extraResource handled by the plugin.
*/
@OnClassLoadEvent(classNameRegexp = "org.apache.catalina.webresources.StandardRoot")
public static void patchStandardRoot(ClassPool classPool, CtClass ctClass) throws NotFoundException, CannotCompileException, ClassNotFoundException {
CtClass ctFileResource = classPool.get("org.apache.catalina.webresources.FileResource");
CtConstructor ctFileResourceConstructor = ctFileResource.getConstructors()[0];
CtClass[] constructorTypes = ctFileResourceConstructor.getParameterTypes();
String constrParams;
if (constructorTypes.length == 4)
constrParams = "this, path, file, false";
else if (constructorTypes.length == 5)
constrParams = "this, path, file, false, null";
else {
LOGGER.warning("org.apache.catalina.webresources.FileResource unknown constructor. Tomcat plugin will not work properly.");
return;
}
try {
ctClass.getDeclaredMethod("getResourceInternal", new CtClass[] { classPool.get(String.class.getName()), CtPrimitiveType.booleanType }).insertBefore("java.io.File file = " + TomcatPlugin.class.getName() + ".getExtraResourceFile(this, path);" + "if (file != null) return new org.apache.catalina.webresources.FileResource(" + constrParams + ");");
} catch (NotFoundException e) {
LOGGER.warning("org.apache.catalina.webresources.StandardRoot does not contain getResourceInternal method. Tomcat plugin will not work properly.");
return;
}
// if getResources() should contain extraClasspath, expand the original returned array and prepend extraClasspath result
try {
ctClass.getDeclaredMethod("getResources", new CtClass[] { classPool.get(String.class.getName()), CtPrimitiveType.booleanType }).insertAfter("java.io.File file = " + TomcatPlugin.class.getName() + ".getExtraResourceFile(this, path);" + "if (file != null) {" + "org.apache.catalina.WebResource[] ret = new org.apache.catalina.WebResource[$_.length + 1];" + "ret[0] = new org.apache.catalina.webresources.FileResource(" + constrParams + ");" + "java.lang.System.arraycopy($_, 0, ret, 1, $_.length);" + "return ret;" + "} else {return $_;}");
} catch (NotFoundException e) {
LOGGER.warning("org.apache.catalina.webresources.StandardRoot does not contain getResourceInternal method. Tomcat plugin will not work properly.");
return;
}
}
Aggregations