use of org.hotswap.agent.javassist.CtClass in project HotswapAgent by HotswapProjects.
the class Compiler method processClasses.
private static void processClasses(CompiledClass[] entries, int n) throws Exception {
Reflection implementor = new Reflection();
ClassPool pool = ClassPool.getDefault();
implementor.start(pool);
for (int i = 0; i < n; ++i) {
CtClass c = pool.get(entries[i].classname);
if (entries[i].metaobject != null || entries[i].classobject != null) {
String metaobj, classobj;
if (entries[i].metaobject == null)
metaobj = "javassist.tools.reflect.Metaobject";
else
metaobj = entries[i].metaobject;
if (entries[i].classobject == null)
classobj = "javassist.tools.reflect.ClassMetaobject";
else
classobj = entries[i].classobject;
if (!implementor.makeReflective(c, pool.get(metaobj), pool.get(classobj)))
System.err.println("Warning: " + c.getName() + " is reflective. It was not changed.");
System.err.println(c.getName() + ": " + metaobj + ", " + classobj);
} else
System.err.println(c.getName() + ": not reflective");
}
for (int i = 0; i < n; ++i) {
implementor.onLoad(pool, entries[i].classname);
pool.get(entries[i].classname).writeFile();
}
}
use of org.hotswap.agent.javassist.CtClass in project HotswapAgent by HotswapProjects.
the class CtClassSignature method getValue.
@Override
public String getValue() throws Exception {
List<String> strings = new ArrayList<>();
if (hasElement(ClassSignatureElement.METHOD)) {
boolean usePrivateMethod = hasElement(ClassSignatureElement.METHOD_PRIVATE);
boolean useStaticMethod = hasElement(ClassSignatureElement.METHOD_STATIC);
for (CtMethod method : ctClass.getDeclaredMethods()) {
if (!usePrivateMethod && Modifier.isPrivate(method.getModifiers()))
continue;
if (!useStaticMethod && Modifier.isStatic(method.getModifiers()))
continue;
strings.add(getMethodString(method));
}
}
if (hasElement(ClassSignatureElement.CONSTRUCTOR)) {
boolean usePrivateConstructor = hasElement(ClassSignatureElement.CONSTRUCTOR_PRIVATE);
for (CtConstructor method : ctClass.getDeclaredConstructors()) {
if (!usePrivateConstructor && Modifier.isPrivate(method.getModifiers()))
continue;
strings.add(getConstructorString(method));
}
}
if (hasElement(ClassSignatureElement.CLASS_ANNOTATION)) {
strings.add(annotationToString(ctClass.getAvailableAnnotations()));
}
if (hasElement(ClassSignatureElement.INTERFACES)) {
for (CtClass iClass : ctClass.getInterfaces()) {
strings.add(iClass.getName());
}
}
if (hasElement(ClassSignatureElement.SUPER_CLASS)) {
if (ctClass.getSuperclass() != null && !ctClass.getSuperclass().getName().equals(Object.class.getName()))
strings.add(ctClass.getSuperclass().getName());
}
if (hasElement(ClassSignatureElement.FIELD)) {
boolean useStaticField = hasElement(ClassSignatureElement.FIELD_STATIC);
boolean useFieldAnnotation = hasElement(ClassSignatureElement.FIELD_ANNOTATION);
for (CtField field : ctClass.getDeclaredFields()) {
if (!useStaticField && Modifier.isStatic(field.getModifiers()))
continue;
String fieldSignature = field.getType().getName() + " " + field.getName();
if (useFieldAnnotation) {
fieldSignature += annotationToString(field.getAvailableAnnotations());
}
strings.add(fieldSignature + ";");
}
}
Collections.sort(strings);
StringBuilder strBuilder = new StringBuilder();
for (String methodString : strings) {
strBuilder.append(methodString);
}
return strBuilder.toString();
}
use of org.hotswap.agent.javassist.CtClass 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.javassist.CtClass 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.javassist.CtClass in project HotswapAgent by HotswapProjects.
the class EnhancerProxyCreater method buildProxyCreaterClass.
/**
* Builds a class that has a single public static method create(Object beanFactry, Object bean, Class[] classes,
* Object[] params). The method of the created class returns a Cglib Enhancer created proxy of the parameter bean.
* The proxy has single callback, whish is a subclass of DetachableBeanHolder. Classname prefix for created proxies
* will be HOTSWAPAGENT_
*
* @param cglibPackage
* Cglib Package name
* @param callback
* Callback class used for Enhancer
* @param namingPolicy
* NamingPolicy class used for Enhancer
* @param cp
* @return Class that creates proxies via method "public static Object create(Object beanFactry, Object bean,
* Class[] classes, Object[] params)"
* @throws CannotCompileException
*/
private Class<?> buildProxyCreaterClass(String cglibPackage, Class<?> callback, Class<?> namingPolicy, ClassPool cp) throws CannotCompileException {
CtClass ct = cp.makeClass("HotswapAgentSpringBeanProxy" + getClassSuffix(cglibPackage));
String proxy = cglibPackage + "proxy.";
String core = cglibPackage + "core.";
String rawBody = "public static Object create(Object beanFactry, Object bean, Class[] classes, Object[] params) {" + //
"{2} handler = new {2}(bean, beanFactry, classes, params);" + //
" {0}Enhancer e = new {0}Enhancer();" + //
" e.setUseCache(true);" + //
" Class[] proxyInterfaces = new Class[bean.getClass().getInterfaces().length];" + //
" Class[] classInterfaces = bean.getClass().getInterfaces();" + //
" for (int i = 0; i < classInterfaces.length; i++) {" + //
" proxyInterfaces[i] = classInterfaces[i];" + //
" }" + //
" e.setInterfaces(proxyInterfaces);" + //
" e.setSuperclass(bean.getClass().getSuperclass());" + //
" e.setCallback(handler);" + //
" e.setCallbackType({2}.class);" + //
" e.setNamingPolicy(new {3}());" + //
" return e.create();" + " }";
String body = rawBody.replaceAll("\\{0\\}", proxy).replaceAll("\\{1\\}", core).replaceAll("\\{2\\}", callback.getName()).replaceAll("\\{3\\}", namingPolicy.getName());
CtMethod m = CtNewMethod.make(body, ct);
ct.addMethod(m);
return ct.toClass(loader, pd);
}
Aggregations