Search in sources :

Example 36 with CtClass

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();
    }
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) ClassPool(org.hotswap.agent.javassist.ClassPool)

Example 37 with CtClass

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();
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) CtField(org.hotswap.agent.javassist.CtField) ArrayList(java.util.ArrayList) CtMethod(org.hotswap.agent.javassist.CtMethod) CtConstructor(org.hotswap.agent.javassist.CtConstructor)

Example 38 with CtClass

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 + "); " + "}");
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) Set(java.util.Set) CtField(org.hotswap.agent.javassist.CtField) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 39 with CtClass

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 + "); " + "}");
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) Set(java.util.Set) CtField(org.hotswap.agent.javassist.CtField) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 40 with CtClass

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);
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) CtMethod(org.hotswap.agent.javassist.CtMethod)

Aggregations

CtClass (org.hotswap.agent.javassist.CtClass)69 NotFoundException (org.hotswap.agent.javassist.NotFoundException)20 CtMethod (org.hotswap.agent.javassist.CtMethod)18 ClassPool (org.hotswap.agent.javassist.ClassPool)14 OnClassLoadEvent (org.hotswap.agent.annotation.OnClassLoadEvent)13 CannotCompileException (org.hotswap.agent.javassist.CannotCompileException)8 CtConstructor (org.hotswap.agent.javassist.CtConstructor)8 CtField (org.hotswap.agent.javassist.CtField)8 HashMap (java.util.HashMap)6 LoaderClassPath (org.hotswap.agent.javassist.LoaderClassPath)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 Test (org.junit.Test)5 Iterator (java.util.Iterator)4 ExprEditor (org.hotswap.agent.javassist.expr.ExprEditor)4 Method (java.lang.reflect.Method)3 IdentityHashMap (java.util.IdentityHashMap)3 BadBytecode (org.hotswap.agent.javassist.bytecode.BadBytecode)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2