use of org.hotswap.agent.javassist.CtMethod in project HotswapAgent by HotswapProjects.
the class JavaProxyBytecodeTransformer method getInitCall.
@Override
protected String getInitCall(CtClass cc, String initFieldName) throws Exception {
// clinit method already contains the setting of our static clinitFieldName to true
CtMethod method = cc.getClassInitializer().toMethod(initFieldName, cc);
method.setModifiers(Modifier.PRIVATE | Modifier.STATIC);
cc.addMethod(method);
return method.getName() + "();";
}
use of org.hotswap.agent.javassist.CtMethod in project HotswapAgent by HotswapProjects.
the class CglibProxyBytecodeTransformer method getInitCall.
@Override
protected String getInitCall(CtClass cc, String initFieldName) throws Exception {
CtMethod[] methods = cc.getDeclaredMethods();
StringBuilder strB = new StringBuilder();
for (CtMethod ctMethod : methods) {
if (ctMethod.getName().startsWith("CGLIB$STATICHOOK")) {
ctMethod.insertAfter(initFieldName + "=true;");
strB.insert(0, ctMethod.getName() + "();");
break;
}
}
if (strB.length() == 0)
throw new RuntimeException("Could not find CGLIB$STATICHOOK method");
return strB.toString() + "CGLIB$BIND_CALLBACKS(this);";
}
use of org.hotswap.agent.javassist.CtMethod in project HotswapAgent by HotswapProjects.
the class Javac method compileBody.
/**
* Compiles a method (or constructor) body.
*
* @param src a single statement or a block.
* If null, this method produces a body returning zero or null.
*/
public Bytecode compileBody(CtBehavior method, String src) throws CompileError {
try {
int mod = method.getModifiers();
recordParams(method.getParameterTypes(), Modifier.isStatic(mod));
CtClass rtype;
if (method instanceof CtMethod) {
gen.setThisMethod((CtMethod) method);
rtype = ((CtMethod) method).getReturnType();
} else
rtype = CtClass.voidType;
recordReturnType(rtype, false);
boolean isVoid = rtype == CtClass.voidType;
if (src == null)
makeDefaultBody(bytecode, rtype);
else {
Parser p = new Parser(new Lex(src));
SymbolTable stb = new SymbolTable(stable);
Stmnt s = p.parseStatement(stb);
if (p.hasMore())
throw new CompileError("the method/constructor body must be surrounded by {}");
boolean callSuper = false;
if (method instanceof CtConstructor)
callSuper = !((CtConstructor) method).isClassInitializer();
gen.atMethodBody(s, callSuper, isVoid);
}
return bytecode;
} catch (NotFoundException e) {
throw new CompileError(e.toString());
}
}
use of org.hotswap.agent.javassist.CtMethod in project HotswapAgent by HotswapProjects.
the class BeansDeployerTransformer method transform.
/**
* Basic CdiArchive transformation.
*
* @param clazz
* @param classPool
* @throws NotFoundException
* @throws CannotCompileException
*/
@OnClassLoadEvent(classNameRegexp = "org.apache.webbeans.config.BeansDeployer")
public static void transform(CtClass clazz, ClassPool classPool) throws NotFoundException, CannotCompileException {
StringBuilder src = new StringBuilder(" if (deployed) {");
src.append("ClassLoader curCl = Thread.currentThread().getContextClassLoader();");
src.append(PluginManagerInvoker.buildInitializePlugin(OwbPlugin.class, "curCl"));
src.append(PluginManagerInvoker.buildCallPluginMethod("curCl", OwbPlugin.class, "init"));
src.append(PluginManagerInvoker.buildCallPluginMethod("curCl", OwbPlugin.class, "registerBeansXmls", "$1.getBeanXmls()", "java.util.Set"));
src.append("}");
CtMethod startApplication = clazz.getDeclaredMethod("deploy");
startApplication.insertAfter(src.toString());
LOGGER.debug("Class '{}' patched with OwbPlugin registration.", clazz.getName());
}
use of org.hotswap.agent.javassist.CtMethod in project HotswapAgent by HotswapProjects.
the class JettyPlugin method patchWebXmlConfiguration.
/**
* Plugin initialization step needs to be fine tuned. It can be intialized only AFTER the classloader
* already knows about hotswap-agent.properties file (i.e. after webapp basic path is added to the classloader),
* but BEFORE first servlet is initialized.
*
* WebXmlConfiguration seems to be good place which should work in most setups. The plugin is intialized before
* web.xml file is processed - basic paths should be known, but nothing is processed yet.
*
* Application classloader is processed during plugin initialization. It means that other plugins triggered
* on plugin init should fire as well - for jetty is important core watchResources plugin, which will handle
* extraClassPath and watchResources configuration properties (jetty fortunately depends only on basic
* URLClassLoader behaviour which is handled by that plugin).
*/
@OnClassLoadEvent(classNameRegexp = "org.eclipse.jetty.webapp.WebXmlConfiguration")
public static void patchWebXmlConfiguration(CtClass ctClass) throws NotFoundException, CannotCompileException, ClassNotFoundException {
try {
// after application context initialized, but before processing started
CtMethod doStart = ctClass.getDeclaredMethod("configure");
// init the plugin
String src = PluginManagerInvoker.buildInitializePlugin(JettyPlugin.class, "context.getClassLoader()");
src += PluginManagerInvoker.buildCallPluginMethod("context.getClassLoader()", JettyPlugin.class, "init", "context", "java.lang.Object");
doStart.insertBefore(src);
} catch (NotFoundException e) {
LOGGER.warning("org.eclipse.jetty.webapp.WebAppContext does not contain startContext method. Jetty plugin will be disabled.\n" + "*** This is Ok, Jetty plugin handles only special properties ***");
return;
}
}
Aggregations