Search in sources :

Example 56 with CtClass

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

the class Type method findCommonSuperClass.

private static CtClass findCommonSuperClass(CtClass one, CtClass two) throws NotFoundException {
    CtClass deep = one;
    CtClass shallow = two;
    CtClass backupShallow = shallow;
    CtClass backupDeep = deep;
    // Phase 1 - Find the deepest hierarchy, set deep and shallow correctly
    for (; ; ) {
        // In case we get lucky, and find a match early
        if (eq(deep, shallow) && deep.getSuperclass() != null)
            return deep;
        CtClass deepSuper = deep.getSuperclass();
        CtClass shallowSuper = shallow.getSuperclass();
        if (shallowSuper == null) {
            // right, now reset shallow
            shallow = backupShallow;
            break;
        }
        if (deepSuper == null) {
            // wrong, swap them, since deep is now useless, its our tmp before we swap it
            deep = backupDeep;
            backupDeep = backupShallow;
            backupShallow = deep;
            deep = shallow;
            shallow = backupShallow;
            break;
        }
        deep = deepSuper;
        shallow = shallowSuper;
    }
    // Phase 2 - Move deepBackup up by (deep end - deep)
    for (; ; ) {
        deep = deep.getSuperclass();
        if (deep == null)
            break;
        backupDeep = backupDeep.getSuperclass();
    }
    deep = backupDeep;
    // The common super class is easy to find now
    while (!eq(deep, shallow)) {
        deep = deep.getSuperclass();
        shallow = shallow.getSuperclass();
    }
    return deep;
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass)

Example 57 with CtClass

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

the class Type method getComponent.

/**
 * Returns the array component if this type is an array. If the type
 * is not an array null is returned.
 *
 * @return the array component if an array, otherwise null
 */
public Type getComponent() {
    if (this.clazz == null || !this.clazz.isArray())
        return null;
    CtClass component;
    try {
        component = this.clazz.getComponentType();
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }
    Type type = (Type) prims.get(component);
    return (type != null) ? type : new Type(component);
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) NotFoundException(org.hotswap.agent.javassist.NotFoundException)

Example 58 with CtClass

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

the class Type method mergeClasses.

private Type mergeClasses(Type type) throws NotFoundException {
    CtClass superClass = findCommonSuperClass(this.clazz, type.clazz);
    // If its Object, then try and find a common interface(s)
    if (superClass.getSuperclass() == null) {
        Map interfaces = findCommonInterfaces(type);
        if (interfaces.size() == 1)
            return new Type((CtClass) interfaces.values().iterator().next());
        if (interfaces.size() > 1)
            return new MultiType(interfaces);
        // Only Object is in common
        return new Type(superClass);
    }
    // Check for a common interface that is not on the found supertype
    Map commonDeclared = findExclusiveDeclaredInterfaces(type, superClass);
    if (commonDeclared.size() > 0) {
        return new MultiType(commonDeclared, new Type(superClass));
    }
    return new Type(superClass);
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) IdentityHashMap(java.util.IdentityHashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 59 with CtClass

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

the class AnnotationImpl method getDefault.

private Object getDefault(String name, Method method) throws ClassNotFoundException, RuntimeException {
    String classname = annotation.getTypeName();
    if (pool != null) {
        try {
            CtClass cc = pool.get(classname);
            ClassFile cf = cc.getClassFile2();
            MethodInfo minfo = cf.getMethod(name);
            if (minfo != null) {
                AnnotationDefaultAttribute ainfo = (AnnotationDefaultAttribute) minfo.getAttribute(AnnotationDefaultAttribute.tag);
                if (ainfo != null) {
                    MemberValue mv = ainfo.getDefaultValue();
                    return mv.getValue(classLoader, pool, method);
                }
            }
        } catch (NotFoundException e) {
            throw new RuntimeException("cannot find a class file: " + classname);
        }
    }
    throw new RuntimeException("no default value: " + classname + "." + name + "()");
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) ClassFile(org.hotswap.agent.javassist.bytecode.ClassFile) AnnotationDefaultAttribute(org.hotswap.agent.javassist.bytecode.AnnotationDefaultAttribute) NotFoundException(org.hotswap.agent.javassist.NotFoundException) MethodInfo(org.hotswap.agent.javassist.bytecode.MethodInfo)

Example 60 with CtClass

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

the class Javac method compileBody.

/**
 * Compiles a method (or constructor) body.
 *
 * @param src	a single statement or a block.
 *              If null, this method produces a body returning zero or null.
 */
public Bytecode compileBody(CtBehavior method, String src) throws CompileError {
    try {
        int mod = method.getModifiers();
        recordParams(method.getParameterTypes(), Modifier.isStatic(mod));
        CtClass rtype;
        if (method instanceof CtMethod) {
            gen.setThisMethod((CtMethod) method);
            rtype = ((CtMethod) method).getReturnType();
        } else
            rtype = CtClass.voidType;
        recordReturnType(rtype, false);
        boolean isVoid = rtype == CtClass.voidType;
        if (src == null)
            makeDefaultBody(bytecode, rtype);
        else {
            Parser p = new Parser(new Lex(src));
            SymbolTable stb = new SymbolTable(stable);
            Stmnt s = p.parseStatement(stb);
            if (p.hasMore())
                throw new CompileError("the method/constructor body must be surrounded by {}");
            boolean callSuper = false;
            if (method instanceof CtConstructor)
                callSuper = !((CtConstructor) method).isClassInitializer();
            gen.atMethodBody(s, callSuper, isVoid);
        }
        return bytecode;
    } catch (NotFoundException e) {
        throw new CompileError(e.toString());
    }
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) NotFoundException(org.hotswap.agent.javassist.NotFoundException) CtMethod(org.hotswap.agent.javassist.CtMethod) CtConstructor(org.hotswap.agent.javassist.CtConstructor)

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