Search in sources :

Example 1 with CodeAttribute

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

the class SubroutineScanner method scan.

public Subroutine[] scan(MethodInfo method) throws BadBytecode {
    CodeAttribute code = method.getCodeAttribute();
    CodeIterator iter = code.iterator();
    subroutines = new Subroutine[code.getCodeLength()];
    subTable.clear();
    done.clear();
    scan(0, iter, null);
    ExceptionTable exceptions = code.getExceptionTable();
    for (int i = 0; i < exceptions.size(); i++) {
        int handler = exceptions.handlerPc(i);
        // If an exception is thrown in subroutine, the handler
        // is part of the same subroutine.
        scan(handler, iter, subroutines[exceptions.startPc(i)]);
    }
    return subroutines;
}
Also used : CodeAttribute(org.hotswap.agent.javassist.bytecode.CodeAttribute) CodeIterator(org.hotswap.agent.javassist.bytecode.CodeIterator) ExceptionTable(org.hotswap.agent.javassist.bytecode.ExceptionTable)

Example 2 with CodeAttribute

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

the class FieldInitLink method modifyConstructors.

private void modifyConstructors(ClassFile cf) throws CannotCompileException, NotFoundException {
    if (fieldInitializers == null)
        return;
    ConstPool cp = cf.getConstPool();
    List list = cf.getMethods();
    int n = list.size();
    for (int i = 0; i < n; ++i) {
        MethodInfo minfo = (MethodInfo) list.get(i);
        if (minfo.isConstructor()) {
            CodeAttribute codeAttr = minfo.getCodeAttribute();
            if (codeAttr != null)
                try {
                    Bytecode init = new Bytecode(cp, 0, codeAttr.getMaxLocals());
                    CtClass[] params = Descriptor.getParameterTypes(minfo.getDescriptor(), classPool);
                    int stacksize = makeFieldInitializer(init, params);
                    insertAuxInitializer(codeAttr, init, stacksize);
                    minfo.rebuildStackMapIf6(classPool, cf);
                } catch (BadBytecode e) {
                    throw new CannotCompileException(e);
                }
        }
    }
}
Also used : ConstPool(org.hotswap.agent.javassist.bytecode.ConstPool) CodeAttribute(org.hotswap.agent.javassist.bytecode.CodeAttribute) ArrayList(java.util.ArrayList) List(java.util.List) MethodInfo(org.hotswap.agent.javassist.bytecode.MethodInfo) BadBytecode(org.hotswap.agent.javassist.bytecode.BadBytecode) Bytecode(org.hotswap.agent.javassist.bytecode.Bytecode) BadBytecode(org.hotswap.agent.javassist.bytecode.BadBytecode)

Example 3 with CodeAttribute

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

the class FieldInitLink method modifyClassConstructor.

private void modifyClassConstructor(ClassFile cf, Bytecode code, int stacksize, int localsize) throws CannotCompileException {
    MethodInfo m = cf.getStaticInitializer();
    if (m == null) {
        code.add(Bytecode.RETURN);
        code.setMaxStack(stacksize);
        code.setMaxLocals(localsize);
        m = new MethodInfo(cf.getConstPool(), "<clinit>", "()V");
        m.setAccessFlags(AccessFlag.STATIC);
        m.setCodeAttribute(code.toCodeAttribute());
        cf.addMethod(m);
        CtMember.Cache cache = hasMemberCache();
        if (cache != null)
            cache.addConstructor(new CtConstructor(m, this));
    } else {
        CodeAttribute codeAttr = m.getCodeAttribute();
        if (codeAttr == null)
            throw new CannotCompileException("empty <clinit>");
        try {
            CodeIterator it = codeAttr.iterator();
            int pos = it.insertEx(code.get());
            it.insert(code.getExceptionTable(), pos);
            int maxstack = codeAttr.getMaxStack();
            if (maxstack < stacksize)
                codeAttr.setMaxStack(stacksize);
            int maxlocals = codeAttr.getMaxLocals();
            if (maxlocals < localsize)
                codeAttr.setMaxLocals(localsize);
        } catch (BadBytecode e) {
            throw new CannotCompileException(e);
        }
    }
    try {
        m.rebuildStackMapIf6(classPool, cf);
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
Also used : CodeAttribute(org.hotswap.agent.javassist.bytecode.CodeAttribute) CodeIterator(org.hotswap.agent.javassist.bytecode.CodeIterator) MethodInfo(org.hotswap.agent.javassist.bytecode.MethodInfo) BadBytecode(org.hotswap.agent.javassist.bytecode.BadBytecode)

Example 4 with CodeAttribute

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

the class Expr method runEditor.

protected void runEditor(ExprEditor ed, CodeIterator oldIterator) throws CannotCompileException {
    CodeAttribute codeAttr = oldIterator.get();
    int orgLocals = codeAttr.getMaxLocals();
    int orgStack = codeAttr.getMaxStack();
    int newLocals = locals();
    codeAttr.setMaxStack(stack());
    codeAttr.setMaxLocals(newLocals);
    ExprEditor.LoopContext context = new ExprEditor.LoopContext(newLocals);
    int size = oldIterator.getCodeLength();
    int endPos = oldIterator.lookAhead();
    oldIterator.move(currentPos);
    if (ed.doit(thisClass, thisMethod, context, oldIterator, endPos))
        edited = true;
    oldIterator.move(endPos + oldIterator.getCodeLength() - size);
    codeAttr.setMaxLocals(orgLocals);
    codeAttr.setMaxStack(orgStack);
    maxLocals = context.maxLocals;
    maxStack += context.maxStack;
}
Also used : CodeAttribute(org.hotswap.agent.javassist.bytecode.CodeAttribute)

Example 5 with CodeAttribute

use of org.hotswap.agent.javassist.bytecode.CodeAttribute 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

CodeAttribute (org.hotswap.agent.javassist.bytecode.CodeAttribute)7 CodeIterator (org.hotswap.agent.javassist.bytecode.CodeIterator)4 BadBytecode (org.hotswap.agent.javassist.bytecode.BadBytecode)3 ConstPool (org.hotswap.agent.javassist.bytecode.ConstPool)3 MethodInfo (org.hotswap.agent.javassist.bytecode.MethodInfo)3 ExceptionTable (org.hotswap.agent.javassist.bytecode.ExceptionTable)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ClassPool (org.hotswap.agent.javassist.ClassPool)1 CtClass (org.hotswap.agent.javassist.CtClass)1 NotFoundException (org.hotswap.agent.javassist.NotFoundException)1 Bytecode (org.hotswap.agent.javassist.bytecode.Bytecode)1 ExceptionsAttribute (org.hotswap.agent.javassist.bytecode.ExceptionsAttribute)1