Search in sources :

Example 6 with CodeAttribute

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

the class Analyzer method analyze.

/**
 * Performs data-flow analysis on a method and returns an array, indexed by
 * instruction position, containing the starting frame state of all reachable
 * instructions. Non-reachable code, and illegal code offsets are represented
 * as a null in the frame state array. This can be used to detect dead code.
 *
 * If the method does not contain code (it is either native or abstract), null
 * is returned.
 *
 * @param clazz the declaring class of the method
 * @param method the method to analyze
 * @return an array, indexed by instruction position, of the starting frame state,
 *         or null if this method doesn't have code
 * @throws BadBytecode if the bytecode does not comply with the JVM specification
 */
public Frame[] analyze(CtClass clazz, MethodInfo method) throws BadBytecode {
    this.clazz = clazz;
    CodeAttribute codeAttribute = method.getCodeAttribute();
    // Native or Abstract
    if (codeAttribute == null)
        return null;
    int maxLocals = codeAttribute.getMaxLocals();
    int maxStack = codeAttribute.getMaxStack();
    int codeLength = codeAttribute.getCodeLength();
    CodeIterator iter = codeAttribute.iterator();
    IntQueue queue = new IntQueue();
    exceptions = buildExceptionInfo(method);
    subroutines = scanner.scan(method);
    Executor executor = new Executor(clazz.getClassPool(), method.getConstPool());
    frames = new Frame[codeLength];
    frames[iter.lookAhead()] = firstFrame(method, maxLocals, maxStack);
    queue.add(iter.next());
    while (!queue.isEmpty()) {
        analyzeNextEntry(method, iter, queue, executor);
    }
    return frames;
}
Also used : CodeAttribute(org.hotswap.agent.javassist.bytecode.CodeAttribute) CodeIterator(org.hotswap.agent.javassist.bytecode.CodeIterator)

Example 7 with CodeAttribute

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

the class FramePrinter method print.

/**
 * Prints the instructions and the frame states of the given method.
 */
public void print(CtMethod method) {
    stream.println("\n" + getMethodString(method));
    MethodInfo info = method.getMethodInfo2();
    ConstPool pool = info.getConstPool();
    CodeAttribute code = info.getCodeAttribute();
    if (code == null)
        return;
    Frame[] frames;
    try {
        frames = (new Analyzer()).analyze(method.getDeclaringClass(), info);
    } catch (BadBytecode e) {
        throw new RuntimeException(e);
    }
    int spacing = String.valueOf(code.getCodeLength()).length();
    CodeIterator iterator = code.iterator();
    while (iterator.hasNext()) {
        int pos;
        try {
            pos = iterator.next();
        } catch (BadBytecode e) {
            throw new RuntimeException(e);
        }
        stream.println(pos + ": " + InstructionPrinter.instructionString(iterator, pos, pool));
        addSpacing(spacing + 3);
        Frame frame = frames[pos];
        if (frame == null) {
            stream.println("--DEAD CODE--");
            continue;
        }
        printStack(frame);
        addSpacing(spacing + 3);
        printLocals(frame);
    }
}
Also used : ConstPool(org.hotswap.agent.javassist.bytecode.ConstPool) 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)

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