Search in sources :

Example 21 with CtClass

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

the class Executor method paramTypesFromDesc.

private Type[] paramTypesFromDesc(String desc) throws BadBytecode {
    CtClass[] classes = null;
    try {
        classes = Descriptor.getParameterTypes(desc, classPool);
    } catch (NotFoundException e) {
        throw new BadBytecode("Could not find class in descriptor [pos = " + lastPos + "]: " + e.getMessage());
    }
    if (classes == null)
        throw new BadBytecode("Could not obtain parameters for descriptor [pos = " + lastPos + "]: " + desc);
    Type[] types = new Type[classes.length];
    for (int i = 0; i < types.length; i++) types[i] = Type.get(classes[i]);
    return types;
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) NotFoundException(org.hotswap.agent.javassist.NotFoundException) BadBytecode(org.hotswap.agent.javassist.bytecode.BadBytecode)

Example 22 with CtClass

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

the class Javac method compileMethod.

private CtBehavior compileMethod(Parser p, MethodDecl md) throws CompileError {
    int mod = MemberResolver.getModifiers(md.getModifiers());
    CtClass[] plist = gen.makeParamList(md);
    CtClass[] tlist = gen.makeThrowsList(md);
    recordParams(plist, Modifier.isStatic(mod));
    md = p.parseMethod2(stable, md);
    try {
        if (md.isConstructor()) {
            CtConstructor cons = new CtConstructor(plist, gen.getThisClass());
            cons.setModifiers(mod);
            md.accept(gen);
            cons.getMethodInfo().setCodeAttribute(bytecode.toCodeAttribute());
            cons.setExceptionTypes(tlist);
            return cons;
        } else {
            Declarator r = md.getReturn();
            CtClass rtype = gen.resolver.lookupClass(r);
            recordReturnType(rtype, false);
            CtMethod method = new CtMethod(rtype, r.getVariable().get(), plist, gen.getThisClass());
            method.setModifiers(mod);
            gen.setThisMethod(method);
            md.accept(gen);
            if (md.getBody() != null)
                method.getMethodInfo().setCodeAttribute(bytecode.toCodeAttribute());
            else
                method.setModifiers(mod | Modifier.ABSTRACT);
            method.setExceptionTypes(tlist);
            return method;
        }
    } 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)

Example 23 with CtClass

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

the class Bytecode method setMaxLocals.

/**
 * Sets <code>max_locals</code>.
 *
 * <p>This computes the number of local variables
 * used to pass method parameters and sets <code>max_locals</code>
 * to that number plus <code>locals</code>.
 *
 * @param isStatic          true if <code>params</code> must be
 *                          interpreted as parameters to a static method.
 * @param params            parameter types.
 * @param locals            the number of local variables excluding
 *                          ones used to pass parameters.
 */
public void setMaxLocals(boolean isStatic, CtClass[] params, int locals) {
    if (!isStatic)
        ++locals;
    if (params != null) {
        CtClass doubleType = CtClass.doubleType;
        CtClass longType = CtClass.longType;
        int n = params.length;
        for (int i = 0; i < n; ++i) {
            CtClass type = params[i];
            if (type == doubleType || type == longType)
                locals += 2;
            else
                ++locals;
        }
    }
    maxLocals = locals;
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass)

Example 24 with CtClass

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

the class TypeData method commonSuperClass.

/**
 * Finds the most specific common super class of the given classes.
 * This method is a copy from javassist.bytecode.analysis.Type.
 */
public static CtClass commonSuperClass(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 25 with CtClass

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

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