Package org.spongepowered.asm.util
Class Bytecode
java.lang.Object
org.spongepowered.asm.util.Bytecode
Utility methods for working with bytecode via ASM
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classInformation bundle returned fromfindDelegateInit(org.objectweb.asm.tree.MethodNode, java.lang.String, java.lang.String)static enumOrdinal member visibility level. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int[]All constant opcodesstatic final int[]Double constant opcodesstatic final int[]Float constant opcodesstatic final int[]Integer constant opcodesstatic final int[]Long constant opcodes -
Method Summary
Modifier and TypeMethodDescriptionstatic StringchangeDescriptorReturnType(String desc, String returnType) Changes the return type of a method descriptor to the specified symbolstatic Map<org.objectweb.asm.tree.LabelNode, org.objectweb.asm.tree.LabelNode> cloneLabels(org.objectweb.asm.tree.InsnList source) Clones all of the labels in the source instruction list and returns the clones in a map of old label -> new label.static voidcompareBridgeMethods(org.objectweb.asm.tree.MethodNode a, org.objectweb.asm.tree.MethodNode b) Compares two synthetic bridge methods and throws an exception if they are not compatible.static booleancompareFlags(org.objectweb.asm.tree.FieldNode f1, org.objectweb.asm.tree.FieldNode f2, int flag) Check whether the status of the specified flag matches on both of the supplied arguments.static booleancompareFlags(org.objectweb.asm.tree.MethodNode m1, org.objectweb.asm.tree.MethodNode m2, int flag) Check whether the status of the specified flag matches on both of the supplied arguments.static StringdescribeNode(org.objectweb.asm.tree.AbstractInsnNode node) Gets a description of the supplied node for debugging purposesstatic StringdescribeNode(org.objectweb.asm.tree.AbstractInsnNode node, boolean listFormat) Gets a description of the supplied node for debugging purposesstatic voiddumpClass(byte[] bytes) Dumps the output of CheckClassAdapter.verify to System.outstatic voiddumpClass(org.objectweb.asm.tree.ClassNode classNode) Dumps the output of CheckClassAdapter.verify to System.outstatic Bytecode.DelegateInitialiserfindDelegateInit(org.objectweb.asm.tree.MethodNode ctor, String superName, String ownerName) Find the call to super() or this() in a constructor.static org.objectweb.asm.tree.AbstractInsnNodefindInsn(org.objectweb.asm.tree.MethodNode method, int opcode) Find the first insn node with a matching opcode in the specified methodstatic org.objectweb.asm.tree.MethodNodefindMethod(org.objectweb.asm.tree.ClassNode classNode, String name, String desc) Finds a method given the method descriptorstatic StringgenerateDescriptor(Object returnType, Object... args) Generate a bytecode descriptor from the supplied tokens.static StringgenerateDescriptor(org.objectweb.asm.Type returnType, org.objectweb.asm.Type... args) Generate a bytecode descriptor from the supplied types.static intgetArgsSize(org.objectweb.asm.Type[] args) Get the size of the specified args array in local variable terms (eg.static intgetArgsSize(org.objectweb.asm.Type[] args, int startIndex, int endIndex) Get the size of the specified args array in local variable terms (eg.static StringgetBoxingType(org.objectweb.asm.Type type) Get the boxing type name for the specified type, if it is a primitive.static ObjectgetConstant(org.objectweb.asm.tree.AbstractInsnNode insn) If the supplied instruction is a constant, returns the constant value from the instructionstatic org.objectweb.asm.TypegetConstantType(org.objectweb.asm.tree.AbstractInsnNode insn) Returns theTypeof a particular constant instruction's payloadstatic StringgetDescriptor(org.objectweb.asm.Type... args) Generate a method descriptor without return type for the supplied args arraystatic StringgetDescriptor(org.objectweb.asm.Type returnType, org.objectweb.asm.Type... args) Generate a method descriptor with the specified typesstatic intgetFirstNonArgLocalIndex(org.objectweb.asm.tree.MethodNode method) Get the first variable index in the supplied method which is not an argument or "this" reference, this corresponds to the size of the arguments passed in to the method plus an extra spot for "this" if the method is non-staticstatic intgetFirstNonArgLocalIndex(org.objectweb.asm.Type[] args, boolean includeThis) Get the first non-arg variable index based on the supplied arg array and whether to include the "this" reference, this corresponds to the size of the arguments passed in to the method plus an extra spot for "this" is specifiedstatic intgetMaxLineNumber(org.objectweb.asm.tree.ClassNode classNode, int min, int pad) Compute the largest line number found in the specified classstatic StringgetOpcodeName(int opcode) Finds a constant name match for the supplied node's opcodestatic StringgetOpcodeName(org.objectweb.asm.tree.AbstractInsnNode node) Finds a constant name match for the supplied node's opcodestatic StringgetSimpleName(String desc) Returns the simple name from an object type descriptor (in L...; format)static StringgetSimpleName(org.objectweb.asm.Type type) Returns the simple name of a type representing a classstatic org.objectweb.asm.Type[]Get an array of Types from an array of classes.static StringgetUnboxingMethod(org.objectweb.asm.Type type) Get the unboxing method name for the specified primitive type's corresponding reference type.static Bytecode.VisibilitygetVisibility(org.objectweb.asm.tree.FieldNode field) Returns the ordinal visibility of the supplied argument where a higher value equals higher "visibility":Bytecode.Visibility.PRIVATEBytecode.Visibility.PROTECTEDBytecode.Visibility.PACKAGEBytecode.Visibility.PUBLICstatic Bytecode.VisibilitygetVisibility(org.objectweb.asm.tree.MethodNode method) Returns the ordinal visibility of the supplied argument where a higher value equals higher "visibility":Bytecode.Visibility.PRIVATEBytecode.Visibility.PROTECTEDBytecode.Visibility.PACKAGEBytecode.Visibility.PUBLICstatic booleanhasFlag(org.objectweb.asm.tree.ClassNode classNode, int flag) Check whether the specified flag is set on the specified classstatic booleanhasFlag(org.objectweb.asm.tree.FieldNode field, int flag) Check whether the specified flag is set on the specified fieldstatic booleanhasFlag(org.objectweb.asm.tree.MethodNode method, int flag) Check whether the specified flag is set on the specified methodstatic booleanisConstant(org.objectweb.asm.tree.AbstractInsnNode insn) Gets whether the supplied instruction is a constant instruction (eg.static booleanisStatic(org.objectweb.asm.tree.FieldNode field) Returns true if the supplied field node is staticstatic booleanisStatic(org.objectweb.asm.tree.MethodNode method) Returns true if the supplied method node is staticstatic booleanisVirtual(org.objectweb.asm.tree.MethodNode method) Checks whether the supplied method is virtual.static voidloadArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int pos) Injects appropriate LOAD opcodes into the supplied InsnList appropriate for each entry in the args array starting at posstatic voidloadArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int start, int end) Injects appropriate LOAD opcodes into the supplied InsnList appropriate for each entry in the args array starting at start (inclusive) and ending at end (inclusive)static voidloadArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int start, int end, org.objectweb.asm.Type[] casts) Injects appropriate LOAD opcodes into the supplied InsnList appropriate for each entry in the args array starting at start (inclusive) and ending at end (inclusive)static voidmerge(org.objectweb.asm.tree.ClassNode source, org.objectweb.asm.tree.ClassNode dest) Perform a naïve merge of ClassNode members onto a target ClassNodestatic booleanmethodHasLineNumbers(org.objectweb.asm.tree.MethodNode method) Returns true if the supplied method contains any line number informationstatic intparseOpcodeName(String opcodeName) Finds a matching constant in theOpcodesinterface for the specified opcode name.static voidprintMethod(org.objectweb.asm.tree.MethodNode method) Prints a representation of a method's instructions to stderrstatic voidprintMethodWithOpcodeIndices(org.objectweb.asm.tree.MethodNode method) Prints a representation of a method's instructions to stderrstatic voidprintNode(org.objectweb.asm.tree.AbstractInsnNode node) Prints a representation of the specified insn node to stderrstatic voidreplace(org.objectweb.asm.tree.ClassNode source, org.objectweb.asm.tree.ClassNode dest) Replace all values in a target ClassNode with values from the Sourcestatic voidsetVisibility(org.objectweb.asm.tree.ClassNode classNode, int access) Set the visibility of the specified class, leaving other access flags unchangedstatic voidsetVisibility(org.objectweb.asm.tree.ClassNode classNode, Bytecode.Visibility visibility) Set the visibility of the specified class, leaving other access flags unchangedstatic voidsetVisibility(org.objectweb.asm.tree.FieldNode field, int access) Set the visibility of the specified member, leaving other access flags unchangedstatic voidsetVisibility(org.objectweb.asm.tree.FieldNode field, Bytecode.Visibility visibility) Set the visibility of the specified member, leaving other access flags unchangedstatic voidsetVisibility(org.objectweb.asm.tree.MethodNode method, int access) Set the visibility of the specified member, leaving other access flags unchangedstatic voidsetVisibility(org.objectweb.asm.tree.MethodNode method, Bytecode.Visibility visibility) Set the visibility of the specified member, leaving other access flags unchangedstatic voidtextify(org.objectweb.asm.tree.ClassNode classNode, OutputStream out) Runs textifier on the specified class node and dumps the output to the specified output streamstatic voidtextify(org.objectweb.asm.tree.MethodNode methodNode, OutputStream out) Runs textifier on the specified method node and dumps the output to the specified output stream
-
Field Details
-
CONSTANTS_INT
public static final int[] CONSTANTS_INTInteger constant opcodes -
CONSTANTS_FLOAT
public static final int[] CONSTANTS_FLOATFloat constant opcodes -
CONSTANTS_DOUBLE
public static final int[] CONSTANTS_DOUBLEDouble constant opcodes -
CONSTANTS_LONG
public static final int[] CONSTANTS_LONGLong constant opcodes -
CONSTANTS_ALL
public static final int[] CONSTANTS_ALLAll constant opcodes
-
-
Method Details
-
findMethod
public static org.objectweb.asm.tree.MethodNode findMethod(org.objectweb.asm.tree.ClassNode classNode, String name, String desc) Finds a method given the method descriptor- Parameters:
classNode- the class to scanname- the method namedesc- the method descriptor- Returns:
- discovered method node or null
-
findInsn
public static org.objectweb.asm.tree.AbstractInsnNode findInsn(org.objectweb.asm.tree.MethodNode method, int opcode) Find the first insn node with a matching opcode in the specified method- Parameters:
method- method to searchopcode- opcode to search for- Returns:
- found node or null if not found
-
findDelegateInit
public static Bytecode.DelegateInitialiser findDelegateInit(org.objectweb.asm.tree.MethodNode ctor, String superName, String ownerName) Find the call to super() or this() in a constructor. This attempts to locate the first call to <init> which isn't an inline call to another object ctor being passed into the super invocation.- Parameters:
ctor- ctor to scansuperName- name of superclassownerName- name of owning class- Returns:
- Call to super(), this() or DelegateInitialiser.NONE if not found
-
textify
Runs textifier on the specified class node and dumps the output to the specified output stream- Parameters:
classNode- class to textifyout- output stream
-
textify
Runs textifier on the specified method node and dumps the output to the specified output stream- Parameters:
methodNode- method to textifyout- output stream
-
dumpClass
public static void dumpClass(org.objectweb.asm.tree.ClassNode classNode) Dumps the output of CheckClassAdapter.verify to System.out- Parameters:
classNode- the ClassNode to verify
-
dumpClass
public static void dumpClass(byte[] bytes) Dumps the output of CheckClassAdapter.verify to System.out- Parameters:
bytes- the bytecode of the class to check
-
printMethodWithOpcodeIndices
public static void printMethodWithOpcodeIndices(org.objectweb.asm.tree.MethodNode method) Prints a representation of a method's instructions to stderr- Parameters:
method- Method to print
-
printMethod
public static void printMethod(org.objectweb.asm.tree.MethodNode method) Prints a representation of a method's instructions to stderr- Parameters:
method- Method to print
-
printNode
public static void printNode(org.objectweb.asm.tree.AbstractInsnNode node) Prints a representation of the specified insn node to stderr- Parameters:
node- Node to print
-
describeNode
Gets a description of the supplied node for debugging purposes- Parameters:
node- node to describe- Returns:
- human-readable description of node
-
describeNode
Gets a description of the supplied node for debugging purposes- Parameters:
node- node to describelistFormat- format the returned string so that returned nodes line up (node names aligned to 14 chars)- Returns:
- human-readable description of node
-
getOpcodeName
Finds a constant name match for the supplied node's opcode- Parameters:
node- Node to query for opcode- Returns:
- opcode name
-
getOpcodeName
Finds a constant name match for the supplied node's opcode- Parameters:
opcode- Opcode to look up- Returns:
- opcode name
-
parseOpcodeName
Finds a matching constant in theOpcodesinterface for the specified opcode name. Supported formats are raw numeric values, bare constant names (eg. ACONST_NULL) or qualified names (eg. Opcodes.ACONST_NULL). Returns the value if found or -1 if not matched. Note that no validation is performed on numeric opcode values.- Parameters:
opcodeName- Opcode string to match- Returns:
- matched opcode value or -1 if not matched.
-
methodHasLineNumbers
public static boolean methodHasLineNumbers(org.objectweb.asm.tree.MethodNode method) Returns true if the supplied method contains any line number information- Parameters:
method- Method to scan- Returns:
- true if a line number node is located
-
isStatic
public static boolean isStatic(org.objectweb.asm.tree.MethodNode method) Returns true if the supplied method node is static- Parameters:
method- method node- Returns:
- true if the method has the
Opcodes.ACC_STATICflag
-
isStatic
public static boolean isStatic(org.objectweb.asm.tree.FieldNode field) Returns true if the supplied field node is static- Parameters:
field- field node- Returns:
- true if the field has the
Opcodes.ACC_STATICflag
-
getFirstNonArgLocalIndex
public static int getFirstNonArgLocalIndex(org.objectweb.asm.tree.MethodNode method) Get the first variable index in the supplied method which is not an argument or "this" reference, this corresponds to the size of the arguments passed in to the method plus an extra spot for "this" if the method is non-static- Parameters:
method- MethodNode to inspect- Returns:
- first available local index which is NOT used by a method argument or "this"
-
getFirstNonArgLocalIndex
public static int getFirstNonArgLocalIndex(org.objectweb.asm.Type[] args, boolean includeThis) Get the first non-arg variable index based on the supplied arg array and whether to include the "this" reference, this corresponds to the size of the arguments passed in to the method plus an extra spot for "this" is specified- Parameters:
args- Method argumentsincludeThis- Whether to include a slot for "this" (generally true for all non-static methods)- Returns:
- first available local index which is NOT used by a method argument or "this"
-
getArgsSize
public static int getArgsSize(org.objectweb.asm.Type[] args) Get the size of the specified args array in local variable terms (eg. doubles and longs take two spaces)- Parameters:
args- Method argument types as array- Returns:
- size of the specified arguments array in terms of stack slots
-
getArgsSize
public static int getArgsSize(org.objectweb.asm.Type[] args, int startIndex, int endIndex) Get the size of the specified args array in local variable terms (eg. doubles and longs take two spaces) using startIndex (inclusive) and endIndex (exclusive) to determine which arguments to process.- Parameters:
args- Method argument types as arraystartIndex- Start index in the array, not related to arg sizeendIndex- End index (exclusive) in the array, not related to size- Returns:
- size of the specified arguments array in terms of stack slots
-
loadArgs
public static void loadArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int pos) Injects appropriate LOAD opcodes into the supplied InsnList appropriate for each entry in the args array starting at pos- Parameters:
args- Argument typesinsns- Instruction List to inject intopos- Start position
-
loadArgs
public static void loadArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int start, int end) Injects appropriate LOAD opcodes into the supplied InsnList appropriate for each entry in the args array starting at start (inclusive) and ending at end (inclusive)- Parameters:
args- Argument typesinsns- Instruction List to inject intostart- Start positionend- End position
-
loadArgs
public static void loadArgs(org.objectweb.asm.Type[] args, org.objectweb.asm.tree.InsnList insns, int start, int end, org.objectweb.asm.Type[] casts) Injects appropriate LOAD opcodes into the supplied InsnList appropriate for each entry in the args array starting at start (inclusive) and ending at end (inclusive)- Parameters:
args- Argument typesinsns- Instruction List to inject intostart- Start positionend- End positioncasts- Type casts array
-
getTypes
Get an array of Types from an array of classes.- Parameters:
classes- Array of classes to convert- Returns:
- Array of types
-
cloneLabels
public static Map<org.objectweb.asm.tree.LabelNode,org.objectweb.asm.tree.LabelNode> cloneLabels(org.objectweb.asm.tree.InsnList source) Clones all of the labels in the source instruction list and returns the clones in a map of old label -> new label. This is used to facilitate the use ofAbstractInsnNode.clone(java.util.Map<org.objectweb.asm.tree.LabelNode, org.objectweb.asm.tree.LabelNode>).- Parameters:
source- instruction list- Returns:
- map of existing labels to their cloned counterparts
-
generateDescriptor
public static String generateDescriptor(org.objectweb.asm.Type returnType, org.objectweb.asm.Type... args) Generate a bytecode descriptor from the supplied types.- Parameters:
returnType- the method return type, can be null for voidargs- argument types
-
generateDescriptor
Generate a bytecode descriptor from the supplied tokens. Each token can be aType, aClassor otherwise is converted in-place by callingtoString.- Parameters:
returnType- object representing the method return type, can be null for voidargs- objects representing argument types
-
getDescriptor
Generate a method descriptor without return type for the supplied args array- Parameters:
args- argument types- Returns:
- method descriptor without return type
-
getDescriptor
public static String getDescriptor(org.objectweb.asm.Type returnType, org.objectweb.asm.Type... args) Generate a method descriptor with the specified types- Parameters:
returnType- return typeargs- argument types- Returns:
- generated method descriptor
-
changeDescriptorReturnType
Changes the return type of a method descriptor to the specified symbol- Parameters:
desc- descriptor to modifyreturnType- new return type- Returns:
- modified descriptor;
-
getSimpleName
Returns the simple name of a type representing a class- Parameters:
type- type- Returns:
- annotation's simple name
-
getSimpleName
Returns the simple name from an object type descriptor (in L...; format)- Parameters:
desc- type descriptor- Returns:
- "simple" name
-
isConstant
public static boolean isConstant(org.objectweb.asm.tree.AbstractInsnNode insn) Gets whether the supplied instruction is a constant instruction (eg. ICONST_1)- Parameters:
insn- instruction to check- Returns:
- true if the supplied instruction is a constant
-
getConstant
If the supplied instruction is a constant, returns the constant value from the instruction- Parameters:
insn- constant instruction to process- Returns:
- the constant value or null if the value cannot be parsed (null constant is returned as Type.VOID_TYPE)
-
getConstantType
public static org.objectweb.asm.Type getConstantType(org.objectweb.asm.tree.AbstractInsnNode insn) Returns theTypeof a particular constant instruction's payload- Parameters:
insn- constant instruction- Returns:
- type of constant or null if it cannot be parsed ( null constant is returned as Type.VOID_TYPE)
-
hasFlag
public static boolean hasFlag(org.objectweb.asm.tree.ClassNode classNode, int flag) Check whether the specified flag is set on the specified class- Parameters:
classNode- class nodeflag- flag to check- Returns:
- True if the specified flag is set in this method's access flags
-
hasFlag
public static boolean hasFlag(org.objectweb.asm.tree.MethodNode method, int flag) Check whether the specified flag is set on the specified method- Parameters:
method- method nodeflag- flag to check- Returns:
- True if the specified flag is set in this method's access flags
-
hasFlag
public static boolean hasFlag(org.objectweb.asm.tree.FieldNode field, int flag) Check whether the specified flag is set on the specified field- Parameters:
field- field nodeflag- flag to check- Returns:
- True if the specified flag is set in this field's access flags
-
compareFlags
public static boolean compareFlags(org.objectweb.asm.tree.MethodNode m1, org.objectweb.asm.tree.MethodNode m2, int flag) Check whether the status of the specified flag matches on both of the supplied arguments.- Parameters:
m1- First methodm2- Second methodflag- flag to compare- Returns:
- True if the flag is set to the same value on both members
-
compareFlags
public static boolean compareFlags(org.objectweb.asm.tree.FieldNode f1, org.objectweb.asm.tree.FieldNode f2, int flag) Check whether the status of the specified flag matches on both of the supplied arguments.- Parameters:
f1- First fieldf2- Second fieldflag- flag to compare- Returns:
- True if the flag is set to the same value on both members
-
isVirtual
public static boolean isVirtual(org.objectweb.asm.tree.MethodNode method) Checks whether the supplied method is virtual. Specifically this method returns true if the method is non-static and has an access level greater than private.- Parameters:
method- Method to test- Returns:
- true if virtual
-
getVisibility
Returns the ordinal visibility of the supplied argument where a higher value equals higher "visibility":- Parameters:
method- method to get visibility for- Returns:
- visibility level
-
getVisibility
Returns the ordinal visibility of the supplied argument where a higher value equals higher "visibility":- Parameters:
field- field to get visibility for- Returns:
- visibility level
-
setVisibility
public static void setVisibility(org.objectweb.asm.tree.ClassNode classNode, Bytecode.Visibility visibility) Set the visibility of the specified class, leaving other access flags unchanged- Parameters:
classNode- class to changevisibility- new visibility
-
setVisibility
public static void setVisibility(org.objectweb.asm.tree.MethodNode method, Bytecode.Visibility visibility) Set the visibility of the specified member, leaving other access flags unchanged- Parameters:
method- method to changevisibility- new visibility
-
setVisibility
public static void setVisibility(org.objectweb.asm.tree.FieldNode field, Bytecode.Visibility visibility) Set the visibility of the specified member, leaving other access flags unchanged- Parameters:
field- field to changevisibility- new visibility
-
setVisibility
public static void setVisibility(org.objectweb.asm.tree.ClassNode classNode, int access) Set the visibility of the specified class, leaving other access flags unchanged- Parameters:
classNode- class to changeaccess- new visibility
-
setVisibility
public static void setVisibility(org.objectweb.asm.tree.MethodNode method, int access) Set the visibility of the specified member, leaving other access flags unchanged- Parameters:
method- method to changeaccess- new visibility
-
setVisibility
public static void setVisibility(org.objectweb.asm.tree.FieldNode field, int access) Set the visibility of the specified member, leaving other access flags unchanged- Parameters:
field- field to changeaccess- new visibility
-
getMaxLineNumber
public static int getMaxLineNumber(org.objectweb.asm.tree.ClassNode classNode, int min, int pad) Compute the largest line number found in the specified class- Parameters:
classNode- Class to inspectmin- minimum value to returnpad- amount to pad at the end of files- Returns:
- computed max
-
getBoxingType
Get the boxing type name for the specified type, if it is a primitive. For non-primitive types, null is returned- Parameters:
type- type to box- Returns:
- boxing type or null
-
getUnboxingMethod
Get the unboxing method name for the specified primitive type's corresponding reference type. For example, if the type passed in is int, then the return value will be intValue. Returns null for non-primitive types.- Parameters:
type- primitive type to get unboxing method for- Returns:
- unboxing method name or null
-
compareBridgeMethods
public static void compareBridgeMethods(org.objectweb.asm.tree.MethodNode a, org.objectweb.asm.tree.MethodNode b) Compares two synthetic bridge methods and throws an exception if they are not compatible.- Parameters:
a- Incumbent methodb- Incoming method
-
merge
public static void merge(org.objectweb.asm.tree.ClassNode source, org.objectweb.asm.tree.ClassNode dest) Perform a naïve merge of ClassNode members onto a target ClassNode- Parameters:
source- Source ClassNode to merge fromdest- Destination ClassNode to merge to
-
replace
public static void replace(org.objectweb.asm.tree.ClassNode source, org.objectweb.asm.tree.ClassNode dest) Replace all values in a target ClassNode with values from the Source- Parameters:
source- Source ClassNode to merge fromdest- Destination ClassNode to merge to
-