Search in sources :

Example 11 with CtClass

use of org.hotswap.agent.javassist.CtClass 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);
    }
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) CtField(org.hotswap.agent.javassist.CtField) NotFoundException(org.hotswap.agent.javassist.NotFoundException) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 12 with CtClass

use of org.hotswap.agent.javassist.CtClass in project HotswapAgent by HotswapProjects.

the class SignatureTest method testOne.

@Test
public void testOne() throws Exception {
    CtClass makeClass = ClassPool.getDefault().get(OneMethod.class.getName());
    String expected = ClassSignatureComparerHelper.getJavaClassSignature(OneMethod.class, SIGNATURE_ELEMENTS);
    String actual = ClassSignatureComparerHelper.getCtClassSignature(makeClass, SIGNATURE_ELEMENTS);
    assertEquals("Signatures not equal", expected, actual);
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) Test(org.junit.Test)

Example 13 with CtClass

use of org.hotswap.agent.javassist.CtClass in project HotswapAgent by HotswapProjects.

the class SignatureTest method testAbstractClassSignature.

@Test
public void testAbstractClassSignature() throws Exception {
    CtClass makeClass = ClassPool.getDefault().get(B.class.getName());
    String expected = ClassSignatureComparerHelper.getJavaClassSignature(B.class, SIGNATURE_ELEMENTS);
    String actual = ClassSignatureComparerHelper.getCtClassSignature(makeClass, SIGNATURE_ELEMENTS);
    assertEquals("Signatures not equal", expected, actual);
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) Test(org.junit.Test)

Example 14 with CtClass

use of org.hotswap.agent.javassist.CtClass in project HotswapAgent by HotswapProjects.

the class GlassFishPlugin method transformFelix.

@OnClassLoadEvent(classNameRegexp = "org.apache.felix.framework.Felix")
public static void transformFelix(ClassPool classPool, CtClass ctClass) throws NotFoundException, CannotCompileException {
    CtClass[] constructorParams = new CtClass[] { classPool.get("java.util.Map") };
    CtConstructor declaredConstructor = ctClass.getDeclaredConstructor(constructorParams);
    declaredConstructor.insertBefore("{" + "if ($1 == null) { " + "$1 = new java.util.HashMap();" + "}" + "String __bootDeleg = (String) $1.get(\"" + FRAMEWORK_BOOTDELEGATION + "\");" + "if (__bootDeleg == null) {" + "__bootDeleg = \"\";" + "}" + "if (__bootDeleg.indexOf(\"org.hotswap.agent\") == -1) {" + "__bootDeleg = __bootDeleg.trim();" + "if (!__bootDeleg.isEmpty()) {" + "__bootDeleg = __bootDeleg + \", \";" + "}" + "__bootDeleg = __bootDeleg + \"" + BOOTDELEGATION_PACKAGES + "\";" + "$1.put(\"" + FRAMEWORK_BOOTDELEGATION + "\", __bootDeleg);" + "}" + "}");
    // declaredConstructor.insertAfter(PluginManagerInvoker.buildInitializePlugin(GlassFishPlugin.class));
    LOGGER.debug("Class 'org.apache.felix.framework.Felix' patched in classLoader {}.");
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) CtConstructor(org.hotswap.agent.javassist.CtConstructor) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 15 with CtClass

use of org.hotswap.agent.javassist.CtClass in project HotswapAgent by HotswapProjects.

the class ProxyUtil method makeProxy.

/**
 * Make proxy.
 *
 * @param proxy
 *            the proxy
 * @param proxied
 *            the proxied
 * @param classPool
 *            the class pool
 * @param classLoader
 *            the class loader
 * @throws Exception
 *             the exception
 */
public static void makeProxy(CtClass proxy, CtClass proxied, ClassPool classPool, ClassLoader classLoader) throws Exception {
    proxy.setSuperclass(proxied);
    for (CtMethod m : proxied.getMethods()) {
        int mod = m.getModifiers();
        if (// 
        !Modifier.isFinal(mod) && // 
        !Modifier.isStatic(mod) && // 
        isVisible(mod, proxied.getPackageName(), m) && (!m.getDeclaringClass().getName().equals("java.lang.Object"))) {
            String meth = toProxy(m);
            CtMethod newMethod = CtNewMethod.make(meth, proxy);
            proxy.addMethod(newMethod);
        }
    }
    for (CtClass i : proxied.getInterfaces()) {
        proxy.addInterface(i);
    }
}
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