Top Description Inners Fields Constructors Methods
javassist.bytecode.analysis

public Class Analyzer

extends Object
implements Opcode
Class Inheritance
All Implemented Interfaces
javassist.bytecode.Opcode
Imports
javassist.ClassPool, .CtClass, .CtMethod, .NotFoundException, javassist.bytecode.AccessFlag, .BadBytecode, .CodeAttribute, .CodeIterator, .ConstPool, .Descriptor, .ExceptionTable, .MethodInfo, .Opcode

A data-flow analyzer that determines the type state of the stack and local variable table at every reachable instruction in a method. During analysis, bytecode verification is performed in a similar manner to that described in the JVM specification.

Example:

// Method to analyze
public Object doSomething(int x) {
    Number n;
    if (x < 5) {
       n = new Double(0);
    } else {
       n = new Long(0);
    }

    return n;
}

// Which compiles to:
// 0:   iload_1
// 1:   iconst_5
// 2:   if_icmpge   17
// 5:   new #18; //class java/lang/Double
// 8:   dup
// 9:   dconst_0
// 10:  invokespecial   #44; //Method java/lang/Double."<init>":(D)V
// 13:  astore_2
// 14:  goto    26
// 17:  new #16; //class java/lang/Long
// 20:  dup
// 21:  lconst_1
// 22:  invokespecial   #47; //Method java/lang/Long."<init>":(J)V
// 25:  astore_2
// 26:  aload_2
// 27:  areturn

public void analyzeIt(CtClass clazz, MethodInfo method) {
    Analyzer analyzer = new Analyzer();
    Frame[] frames = analyzer.analyze(clazz, method);
    frames[0].getLocal(0).getCtClass(); // returns clazz;
    frames[0].getLocal(1).getCtClass(); // returns java.lang.String
    frames[1].peek(); // returns Type.INTEGER
    frames[27].peek().getCtClass(); // returns java.lang.Number
}
Author
Jason T. Greene
See Also
FramePrinter

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private CtClass
private Analyzer.ExceptionInfo[]
private Frame[]
private final SubroutineScanner
private Subroutine[]

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Frame[]

Returns:

an array, indexed by instruction position, of the starting frame state, or null if this method doesn't have code
analyze
(CtClass
the declaring class of the method
clazz
,
MethodInfo
the method to analyze
method
)

Performs data-flow analysis on a method and returns an array, indexed by instruction position, containing the starting frame state of all reachable instructions.

public Frame[]

Returns:

an array, indexed by instruction position, of the starting frame state, or null if this method doesn't have code
analyze
(CtMethod
the method to analyze
method
)

Performs data-flow analysis on a method and returns an array, indexed by instruction position, containing the starting frame state of all reachable instructions.

private void
analyzeNextEntry(MethodInfo method, CodeIterator iter, IntQueue queue, Executor executor)

private Analyzer.ExceptionInfo[]
private Frame
firstFrame(MethodInfo method, int maxLocals, int maxStack)

private int
getNext(CodeIterator iter, int of, int restore)

private int
lookAhead(CodeIterator iter, int pos)

private void
merge(IntQueue queue, Frame frame, int target)

private void
mergeExceptionHandlers(IntQueue queue, MethodInfo method, int pos, Frame frame)

private void
mergeJsr(IntQueue queue, Frame frame, Subroutine sub, int pos, int next)

private void
mergeLookupSwitch(IntQueue queue, int pos, CodeIterator iter, Frame frame)

private void
mergeRet(IntQueue queue, CodeIterator iter, int pos, Frame frame, Subroutine subroutine)

private void
mergeTableSwitch(IntQueue queue, int pos, CodeIterator iter, Frame frame)

private Type
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

clazzback to summary
private CtClass clazz
exceptionsback to summary
private Analyzer.ExceptionInfo[] exceptions
framesback to summary
private Frame[] frames
scannerback to summary
private final SubroutineScanner scanner
subroutinesback to summary
private Subroutine[] subroutines

Constructor Detail

Analyzerback to summary
public Analyzer()

Method Detail

analyzeback to summary
public Frame[] analyze(CtClass clazz, MethodInfo method) throws BadBytecode

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.

Parameters
clazz:CtClass

the declaring class of the method

method:MethodInfo

the method to analyze

Returns:Frame[]

an array, indexed by instruction position, of the starting frame state, or null if this method doesn't have code

Exceptions
BadBytecode:
if the bytecode does not comply with the JVM specification
analyzeback to summary
public Frame[] analyze(CtMethod method) throws BadBytecode

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.

Parameters
method:CtMethod

the method to analyze

Returns:Frame[]

an array, indexed by instruction position, of the starting frame state, or null if this method doesn't have code

Exceptions
BadBytecode:
if the bytecode does not comply with the JVM specification
analyzeNextEntryback to summary
private void analyzeNextEntry(MethodInfo method, CodeIterator iter, IntQueue queue, Executor executor) throws BadBytecode
buildExceptionInfoback to summary
private Analyzer.ExceptionInfo[] buildExceptionInfo(MethodInfo method)
firstFrameback to summary
private Frame firstFrame(MethodInfo method, int maxLocals, int maxStack)
getNextback to summary
private int getNext(CodeIterator iter, int of, int restore) throws BadBytecode
lookAheadback to summary
private int lookAhead(CodeIterator iter, int pos) throws BadBytecode
mergeback to summary
private void merge(IntQueue queue, Frame frame, int target)
mergeExceptionHandlersback to summary
private void mergeExceptionHandlers(IntQueue queue, MethodInfo method, int pos, Frame frame)
mergeJsrback to summary
private void mergeJsr(IntQueue queue, Frame frame, Subroutine sub, int pos, int next) throws BadBytecode
mergeLookupSwitchback to summary
private void mergeLookupSwitch(IntQueue queue, int pos, CodeIterator iter, Frame frame) throws BadBytecode
mergeRetback to summary
private void mergeRet(IntQueue queue, CodeIterator iter, int pos, Frame frame, Subroutine subroutine) throws BadBytecode
mergeTableSwitchback to summary
private void mergeTableSwitch(IntQueue queue, int pos, CodeIterator iter, Frame frame) throws BadBytecode
zeroExtendback to summary
private Type zeroExtend(Type type)
javassist.bytecode.analysis back to summary

private Class Analyzer.ExceptionInfo

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private int
private int
private int
private Type

Constructor Summary

AccessConstructor and Description
private
ExceptionInfo(int start, int end, int handler, Type type)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

endback to summary
private int end
handlerback to summary
private int handler
startback to summary
private int start
typeback to summary
private Type type

Constructor Detail

ExceptionInfoback to summary
private ExceptionInfo(int start, int end, int handler, Type type)