Package com.ibm.wala.shrikeBT.shrikeCT
Class ClassInstrumenter
- java.lang.Object
-
- com.ibm.wala.shrikeBT.shrikeCT.ClassInstrumenter
-
public final class ClassInstrumenter extends Object
This class provides a convenient way to instrument every method in a class. It assumes you are using ShrikeCT to read and write classes. It's stateful; initially every method is set to the original code read from the class, but you can then go in and modify the methods.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ClassInstrumenter.MethodExaminer
Implement this interface to instrument every method of a class using visitMethods() below.
-
Constructor Summary
Constructors Constructor Description ClassInstrumenter(String inputName, byte[] bytes, ClassHierarchyProvider cha, boolean reuseStackMaps)
Create a class instrumenter from raw bytes.ClassInstrumenter(String inputName, ClassReader cr, ClassHierarchyProvider cha, boolean reuseStackMaps)
Create a class instrumenter from a preinitialized class reader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MethodData
createEmptyMethodData(String name, String sig, int access)
void
deleteMethod(int i)
Indicate that the method should be deleted from the class.ClassWriter
emitClass()
Create a class which is a copy of the original class but with the new method code.ClassWriter
emitClass(ClassWriter w)
void
enableFakeLineNumbers(int offset)
Calling this means that methods without line numbers get fake line numbers added: each bytecode instruction is treated as at line 'offset' + the offset of the instruction.String
getInputName()
CodeReader
getMethodCode(int i)
Get the original code resource for the method.ClassReader
getReader()
boolean
isChanged()
Check whether any methods in the class have actually been changed.void
replaceMethod(int i, MethodData md)
Replace the code for method i with new code.void
resetMethod(int i)
Reset method i back to the code from the original class, and "undelete" it if it was marked for deletion.MethodData
visitMethod(int i)
Get the current state of method i.void
visitMethods(ClassInstrumenter.MethodExaminer me)
Do something to every method in the class.
-
-
-
Constructor Detail
-
ClassInstrumenter
public ClassInstrumenter(String inputName, byte[] bytes, ClassHierarchyProvider cha, boolean reuseStackMaps) throws InvalidClassFileException
Create a class instrumenter from raw bytes.- Throws:
InvalidClassFileException
-
ClassInstrumenter
public ClassInstrumenter(String inputName, ClassReader cr, ClassHierarchyProvider cha, boolean reuseStackMaps) throws InvalidClassFileException
Create a class instrumenter from a preinitialized class reader.- Throws:
IllegalArgumentException
- if cr is nullInvalidClassFileException
-
-
Method Detail
-
getInputName
public String getInputName()
- Returns:
- name of resource from which this class was read
-
enableFakeLineNumbers
public void enableFakeLineNumbers(int offset)
Calling this means that methods without line numbers get fake line numbers added: each bytecode instruction is treated as at line 'offset' + the offset of the instruction.
-
getReader
public ClassReader getReader()
- Returns:
- the reader for the class
-
deleteMethod
public void deleteMethod(int i)
Indicate that the method should be deleted from the class.- Parameters:
i
- the index of the method to delete
-
createEmptyMethodData
public MethodData createEmptyMethodData(String name, String sig, int access)
-
visitMethods
public void visitMethods(ClassInstrumenter.MethodExaminer me) throws InvalidClassFileException
Do something to every method in the class. This will visit all methods, including those already marked for deletion.- Parameters:
me
- the visitor to apply to each method- Throws:
InvalidClassFileException
-
visitMethod
public MethodData visitMethod(int i) throws InvalidClassFileException
Get the current state of method i. This can be edited using a MethodEditor.- Parameters:
i
- the index of the method to inspect- Throws:
InvalidClassFileException
-
getMethodCode
public CodeReader getMethodCode(int i) throws InvalidClassFileException
Get the original code resource for the method.- Parameters:
i
- the index of the method to inspect- Throws:
InvalidClassFileException
-
resetMethod
public void resetMethod(int i)
Reset method i back to the code from the original class, and "undelete" it if it was marked for deletion.- Parameters:
i
- the index of the method to reset
-
replaceMethod
public void replaceMethod(int i, MethodData md)
Replace the code for method i with new code. This also "undeletes" the method if it was marked for deletion.- Parameters:
i
- the index of the method to replace- Throws:
IllegalArgumentException
- if md is null
-
isChanged
public boolean isChanged()
Check whether any methods in the class have actually been changed.
-
emitClass
public ClassWriter emitClass() throws InvalidClassFileException
Create a class which is a copy of the original class but with the new method code. We return the ClassWriter used, so more methods and fields (and other changes) can still be added. We fix up any debug information to be consistent with the changes to the code.- Throws:
InvalidClassFileException
-
emitClass
public ClassWriter emitClass(ClassWriter w) throws InvalidClassFileException
- Throws:
InvalidClassFileException
-
-