Search in sources :

Example 6 with ClassPool

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

the class HotSwapperJpda method swapClasses.

/**
 * Swap class definition from another class file.
 * <p/>
 * This is mainly useful for unit testing - declare multiple version of a class and then
 * hotswap definition and do the tests.
 *
 * @param original original class currently in use
 * @param swap     fully qualified class name of class to swap
 * @throws Exception swap exception
 */
public void swapClasses(Class original, String swap) throws Exception {
    // need to recreate classpool on each swap to avoid stale class definition
    ClassPool classPool = new ClassPool();
    classPool.appendClassPath(new LoaderClassPath(original.getClassLoader()));
    CtClass ctClass = classPool.getAndRename(swap, original.getName());
    reload(original.getName(), ctClass.toBytecode());
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) ClassPool(org.hotswap.agent.javassist.ClassPool) LoaderClassPath(org.hotswap.agent.javassist.LoaderClassPath)

Example 7 with ClassPool

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

the class HotSwapper method swapClasses.

/**
 * Swap class definition from another class file.
 * <p/>
 * This is mainly useful for unit testing - declare multiple version of a class and then
 * hotswap definition and do the tests.
 *
 * @param original original class currently in use
 * @param swap     fully qualified class name of class to swap
 * @throws Exception swap exception
 */
public static void swapClasses(Class original, String swap) throws Exception {
    // need to recreate classpool on each swap to avoid stale class definition
    ClassPool classPool = new ClassPool();
    classPool.appendClassPath(new LoaderClassPath(original.getClassLoader()));
    CtClass ctClass = classPool.getAndRename(swap, original.getName());
    reload(original, ctClass.toBytecode());
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) ClassPool(org.hotswap.agent.javassist.ClassPool) LoaderClassPath(org.hotswap.agent.javassist.LoaderClassPath)

Example 8 with ClassPool

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

the class HotSwapper method newClass.

public static Class newClass(String className, String directory, ClassLoader cl) {
    try {
        ClassPool classPool = new ClassPool();
        classPool.appendClassPath(new LoaderClassPath(cl));
        CtClass makeClass = classPool.makeClass(className);
        makeClass.writeFile(directory);
        return makeClass.toClass();
    } catch (Throwable ex) {
        Logger.getLogger(HotSwapper.class.getName()).log(Level.SEVERE, null, ex);
        throw new RuntimeException(ex);
    }
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) ClassPool(org.hotswap.agent.javassist.ClassPool) LoaderClassPath(org.hotswap.agent.javassist.LoaderClassPath)

Example 9 with ClassPool

use of org.hotswap.agent.javassist.ClassPool 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 10 with ClassPool

use of org.hotswap.agent.javassist.ClassPool 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)

Aggregations

ClassPool (org.hotswap.agent.javassist.ClassPool)23 CtClass (org.hotswap.agent.javassist.CtClass)14 LoaderClassPath (org.hotswap.agent.javassist.LoaderClassPath)12 NotFoundException (org.hotswap.agent.javassist.NotFoundException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 CannotCompileException (org.hotswap.agent.javassist.CannotCompileException)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ConstPool (org.hotswap.agent.javassist.bytecode.ConstPool)2 ExceptionTable (org.hotswap.agent.javassist.bytecode.ExceptionTable)2 Test (org.junit.Test)2 File (java.io.File)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ProtectionDomain (java.security.ProtectionDomain)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Init (org.hotswap.agent.annotation.Init)1 LoadEvent (org.hotswap.agent.annotation.LoadEvent)1