Search in sources :

Example 31 with CtClass

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

the class ClassLoaderDefineClassPatcher method patch.

@Override
public void patch(final ClassLoader classLoaderFrom, final String pluginPath, final ClassLoader classLoaderTo, final ProtectionDomain protectionDomain) {
    List<byte[]> cache = getPluginCache(classLoaderFrom, pluginPath);
    if (cache != null) {
        final ClassPool cp = new ClassPool();
        cp.appendClassPath(new LoaderClassPath(getClass().getClassLoader()));
        for (byte[] pluginBytes : cache) {
            CtClass pluginClass = null;
            try {
                // force to load class in classLoaderFrom (it may not yet be loaded) and if the classLoaderTo
                // is parent of classLoaderFrom, after definition in classLoaderTo will classLoaderFrom return
                // class from parent classloader instead own definition (hence change of behaviour).
                InputStream is = new ByteArrayInputStream(pluginBytes);
                pluginClass = cp.makeClass(is);
                try {
                    classLoaderFrom.loadClass(pluginClass.getName());
                } catch (NoClassDefFoundError e) {
                    LOGGER.trace("Skipping class loading {} in classloader {} - " + "class has probably unresolvable dependency.", pluginClass.getName(), classLoaderTo);
                }
                // and load the class in classLoaderTo as well. NOw the class is defined in BOTH classloaders.
                pluginClass.toClass(classLoaderTo, protectionDomain);
            } catch (CannotCompileException e) {
                LOGGER.trace("Skipping class definition {} in app classloader {} - " + "class is probably already defined.", pluginClass.getName(), classLoaderTo);
            } catch (NoClassDefFoundError e) {
                LOGGER.trace("Skipping class definition {} in app classloader {} - " + "class has probably unresolvable dependency.", pluginClass.getName(), classLoaderTo);
            } catch (Throwable e) {
                LOGGER.trace("Skipping class definition app classloader {} - " + "unknown error.", e, classLoaderTo);
            }
        }
    }
    LOGGER.debug("Classloader {} patched with plugin classes from agent classloader {}.", classLoaderTo, classLoaderFrom);
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ClassPool(org.hotswap.agent.javassist.ClassPool) LoaderClassPath(org.hotswap.agent.javassist.LoaderClassPath) CannotCompileException(org.hotswap.agent.javassist.CannotCompileException)

Example 32 with CtClass

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

the class Expr method mayThrow.

/**
 * Returns the list of exceptions that the expression may throw. This list
 * includes both the exceptions that the try-catch statements including the
 * expression can catch and the exceptions that the throws declaration
 * allows the method to throw.
 */
public CtClass[] mayThrow() {
    ClassPool pool = thisClass.getClassPool();
    ConstPool cp = thisMethod.getConstPool();
    LinkedList list = new LinkedList();
    try {
        CodeAttribute ca = thisMethod.getCodeAttribute();
        ExceptionTable et = ca.getExceptionTable();
        int pos = currentPos;
        int n = et.size();
        for (int i = 0; i < n; ++i) if (et.startPc(i) <= pos && pos < et.endPc(i)) {
            int t = et.catchType(i);
            if (t > 0)
                try {
                    addClass(list, pool.get(cp.getClassInfo(t)));
                } catch (NotFoundException e) {
                }
        }
    } catch (NullPointerException e) {
    }
    ExceptionsAttribute ea = thisMethod.getExceptionsAttribute();
    if (ea != null) {
        String[] exceptions = ea.getExceptions();
        if (exceptions != null) {
            int n = exceptions.length;
            for (int i = 0; i < n; ++i) try {
                addClass(list, pool.get(exceptions[i]));
            } catch (NotFoundException e) {
            }
        }
    }
    return (CtClass[]) list.toArray(new CtClass[list.size()]);
}
Also used : ConstPool(org.hotswap.agent.javassist.bytecode.ConstPool) CtClass(org.hotswap.agent.javassist.CtClass) ExceptionsAttribute(org.hotswap.agent.javassist.bytecode.ExceptionsAttribute) CodeAttribute(org.hotswap.agent.javassist.bytecode.CodeAttribute) ClassPool(org.hotswap.agent.javassist.ClassPool) NotFoundException(org.hotswap.agent.javassist.NotFoundException) ExceptionTable(org.hotswap.agent.javassist.bytecode.ExceptionTable) LinkedList(java.util.LinkedList)

Example 33 with CtClass

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

the class Expr method storeStack0.

private static void storeStack0(int i, int n, CtClass[] params, int regno, Bytecode bytecode) {
    if (i >= n)
        return;
    else {
        CtClass c = params[i];
        int size;
        if (c instanceof CtPrimitiveType)
            size = ((CtPrimitiveType) c).getDataSize();
        else
            size = 1;
        storeStack0(i + 1, n, params, regno + size, bytecode);
        bytecode.addStore(regno, c);
    }
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) CtPrimitiveType(org.hotswap.agent.javassist.CtPrimitiveType)

Example 34 with CtClass

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

the class TransformCall method matchClass.

private boolean matchClass(String name, ClassPool pool) {
    if (classname.equals(name))
        return true;
    try {
        CtClass clazz = pool.get(name);
        CtClass declClazz = pool.get(classname);
        if (clazz.subtypeOf(declClazz))
            try {
                CtMethod m = clazz.getMethod(methodname, methodDescriptor);
                return m.getDeclaringClass().getName().equals(classname);
            } catch (NotFoundException e) {
                // maybe the original method has been removed.
                return true;
            }
    } catch (NotFoundException e) {
        return false;
    }
    return false;
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) NotFoundException(org.hotswap.agent.javassist.NotFoundException) CtMethod(org.hotswap.agent.javassist.CtMethod)

Example 35 with CtClass

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

the class framedump method main.

/**
 * Main method.
 *
 * @param args <code>args[0]</code> is the class file name.
 */
public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("Usage: java javassist.tools.framedump <fully-qualified class name>");
        return;
    }
    ClassPool pool = ClassPool.getDefault();
    CtClass clazz = pool.get(args[0]);
    System.out.println("Frame Dump of " + clazz.getName() + ":");
    FramePrinter.print(clazz, System.out);
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) ClassPool(org.hotswap.agent.javassist.ClassPool)

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