Search in sources :

Example 6 with LoaderClassPath

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

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

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

the class ComponentScanWithWildcardsTest method copyClassFile.

public static File copyClassFile(Class<?> clazz, String newName) throws Exception {
    String directoryName = clazz.getClassLoader().getResource("").getPath();
    ClassPool classPool = new ClassPool();
    classPool.appendClassPath(new LoaderClassPath(clazz.getClassLoader()));
    CtClass ctClass = classPool.getAndRename(clazz.getName(), newName);
    ctClass.writeFile(directoryName);
    File file = new File(directoryName + File.separatorChar + newName.replace('.', File.separatorChar) + ".class");
    assertTrue(file.exists());
    return file;
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) ClassPool(org.hotswap.agent.javassist.ClassPool) LoaderClassPath(org.hotswap.agent.javassist.LoaderClassPath) File(java.io.File)

Example 9 with LoaderClassPath

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

the class AnonymousClassInfoTest method getAnonymousCtClassInfo.

public AnonymousClassInfo getAnonymousCtClassInfo() throws NotFoundException, ClassNotFoundException, IOException, CannotCompileException {
    Class clazz = getClass().getClassLoader().loadClass(AnonymousTestClass1.class.getName() + "$1");
    ClassPool classPool = new ClassPool();
    classPool.appendClassPath(new LoaderClassPath(getClass().getClassLoader()));
    return new AnonymousClassInfo(classPool.get(clazz.getName()));
}
Also used : ClassPool(org.hotswap.agent.javassist.ClassPool) LoaderClassPath(org.hotswap.agent.javassist.LoaderClassPath) AnonymousClassInfo(org.hotswap.agent.plugin.jvm.AnonymousClassInfo)

Example 10 with LoaderClassPath

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

the class AnonymousClassInfosTest method getClassPoolInfos.

private AnonymousClassInfos getClassPoolInfos(Class clazz) throws ClassNotFoundException {
    ClassPool classPool = new ClassPool();
    classPool.insertClassPath(new LoaderClassPath(getClass().getClassLoader()));
    return new AnonymousClassInfos(classPool, clazz.getName());
}
Also used : ClassPool(org.hotswap.agent.javassist.ClassPool) AnonymousClassInfos(org.hotswap.agent.plugin.jvm.AnonymousClassInfos) LoaderClassPath(org.hotswap.agent.javassist.LoaderClassPath)

Aggregations

ClassPool (org.hotswap.agent.javassist.ClassPool)12 LoaderClassPath (org.hotswap.agent.javassist.LoaderClassPath)12 CtClass (org.hotswap.agent.javassist.CtClass)6 ByteArrayInputStream (java.io.ByteArrayInputStream)3 CannotCompileException (org.hotswap.agent.javassist.CannotCompileException)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ProtectionDomain (java.security.ProtectionDomain)1 ArrayList (java.util.ArrayList)1 LoadEvent (org.hotswap.agent.annotation.LoadEvent)1 OnClassLoadEvent (org.hotswap.agent.annotation.OnClassLoadEvent)1 AnonymousClassInfo (org.hotswap.agent.plugin.jvm.AnonymousClassInfo)1 AnonymousClassInfos (org.hotswap.agent.plugin.jvm.AnonymousClassInfos)1 AppClassLoaderExecutor (org.hotswap.agent.util.AppClassLoaderExecutor)1