Search in sources :

Example 46 with CtClass

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

the class ScopedClassPool method getCached.

/**
 * Get the cached class
 *
 * @param classname
 *            the class name
 * @return the class
 */
protected CtClass getCached(String classname) {
    CtClass clazz = getCachedLocally(classname);
    if (clazz == null) {
        boolean isLocal = false;
        ClassLoader dcl = getClassLoader0();
        if (dcl != null) {
            final int lastIndex = classname.lastIndexOf('$');
            String classResourceName = null;
            if (lastIndex < 0) {
                classResourceName = classname.replaceAll("[\\.]", "/") + ".class";
            } else {
                classResourceName = classname.substring(0, lastIndex).replaceAll("[\\.]", "/") + classname.substring(lastIndex) + ".class";
            }
            isLocal = dcl.getResource(classResourceName) != null;
        }
        if (!isLocal) {
            Map registeredCLs = repository.getRegisteredCLs();
            synchronized (registeredCLs) {
                Iterator it = registeredCLs.values().iterator();
                while (it.hasNext()) {
                    ScopedClassPool pool = (ScopedClassPool) it.next();
                    if (pool.isUnloadedClassLoader()) {
                        repository.unregisterClassLoader(pool.getClassLoader());
                        continue;
                    }
                    clazz = pool.getCachedLocally(classname);
                    if (clazz != null) {
                        return clazz;
                    }
                }
            }
        }
    }
    // *NOTE* NEED TO TEST WHEN SUPERCLASS IS IN ANOTHER UCL!!!!!!
    return clazz;
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) Iterator(java.util.Iterator) Map(java.util.Map)

Example 47 with CtClass

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

the class ScopedClassPool method getLocally.

/**
 * Get any local copy of the class
 *
 * @param classname
 *            the class name
 * @return the class
 * @throws NotFoundException
 *             when the class is not found
 */
public synchronized CtClass getLocally(String classname) throws NotFoundException {
    softcache.remove(classname);
    CtClass clazz = (CtClass) classes.get(classname);
    if (clazz == null) {
        clazz = createCtClass(classname, true);
        if (clazz == null)
            throw new NotFoundException(classname);
        super.cacheCtClass(classname, clazz, false);
    }
    return clazz;
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) NotFoundException(org.hotswap.agent.javassist.NotFoundException)

Example 48 with CtClass

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

the class CtClassSignature method getParams.

private String getParams(CtClass[] ctClasses) {
    StringBuilder strBuilder = new StringBuilder("(");
    boolean first = true;
    for (CtClass ctClass : ctClasses) {
        if (!first)
            strBuilder.append(",");
        else
            first = false;
        strBuilder.append(getName(ctClass));
    }
    strBuilder.append(")");
    return strBuilder.toString();
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass)

Example 49 with CtClass

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

the class SignatureTest method testInterfaceSignature.

@Test
public void testInterfaceSignature() throws Exception {
    CtClass makeClass = ClassPool.getDefault().get(TestSignatures.class.getName());
    String expected = ClassSignatureComparerHelper.getJavaClassSignature(TestSignatures.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 50 with CtClass

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

the class AnnotationHelperTest method testHasAnnotationJavassist.

@Test
public void testHasAnnotationJavassist() throws Exception {
    ClassPool ctPool = ClassPool.getDefault();
    CtClass ctClass = ctPool.getCtClass(AnonymousClassPatchPlugin.class.getName());
    assertTrue(AnnotationHelper.hasAnnotation(ctClass, "org.hotswap.agent.annotation.Plugin"));
    assertFalse(AnnotationHelper.hasAnnotation(ctClass, "xxxx"));
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) ClassPool(org.hotswap.agent.javassist.ClassPool) AnonymousClassPatchPlugin(org.hotswap.agent.plugin.jvm.AnonymousClassPatchPlugin) Test(org.junit.Test)

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