Class Utility
java.lang.Object
org.apache.bcel.classfile.Utility
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringaccessToString(int accessFlags) Convert bit field of flags into string such as 'static final'.static StringaccessToString(int accessFlags, boolean forClass) Convert bit field of flags into string such as 'static final'.static StringclassOrInterface(int accessFlags) Gets the class or interface type name.static intclearBit(int flag, int i) Clears a bit in a flag.static StringcodeToString(byte[] code, ConstantPool constantPool, int index, int length) Disassembles byte code.static StringcodeToString(byte[] code, ConstantPool constantPool, int index, int length, boolean verbose) Disassemble a byte array of JVM byte codes starting from code line 'index' and return the disassembled string representation.static StringcodeToString(ByteSequence bytes, ConstantPool constantPool) Disassembles byte code.static StringcodeToString(ByteSequence bytes, ConstantPool constantPool, boolean verbose) Disassemble a stream of byte codes and return the string representation.static StringcompactClassName(String str) Shorten long class names, java/lang/String becomes String.static StringcompactClassName(String str, boolean chopit) Shorten long class names, java/lang/String becomes java.lang.String, for example.static StringcompactClassName(String str, String prefix, boolean chopit) Shorten long class name str, that is, chop off the prefix, if the class name starts with this string and the flag chopit is true.static StringconvertString(String label) Escape all occurrences of newline chars '\n', quotes \", etc.static byte[]Decode a string back to a byte array.static Stringencode(byte[] bytes, boolean compress) Encode byte array it into Java identifier string, that is, a string that only contains the following characters: (a, ... z, A, ...static StringFillup char with up to length characters with char 'fill' and justify it left or right.static Stringformat(int i, int length, boolean leftJustify, char fill) Return a string for an integer justified left or right and filled up with 'fill' characters if necessary.static StringgetSignature(String type) Parse Java type such as "char", or "java.lang.String[]" and return the signature in byte code format, for example "C" or "[Ljava/lang/String;" respectively.static booleanisJavaIdentifierPart(char ch) Tests if a character is part of a Java identifier.static booleanisSet(int flag, int i) Tests if a bit is set.static String[]methodSignatureArgumentTypes(String signature) Converts argument list portion of method signature to string with all class names compacted.static String[]methodSignatureArgumentTypes(String signature, boolean chopit) Converts argument list portion of method signature to string.static StringmethodSignatureReturnType(String signature) Converts return type portion of method signature to string with all class names compacted.static StringmethodSignatureReturnType(String signature, boolean chopit) Converts return type portion of method signature to string.static StringmethodSignatureToString(String signature, String name, String access) Converts method signature to string with all class names compacted.static StringmethodSignatureToString(String signature, String name, String access, boolean chopit) Converts method signature to string.static StringmethodSignatureToString(String signature, String name, String access, boolean chopit, LocalVariableTable vars) This method converts a method signature string into a Java type declaration like 'void main(String[])' and throws a 'ClassFormatException' when the parsed type is invalid.static StringmethodTypeToSignature(String ret, String[] argv) Converts string containing the method return and argument types to a byte code method signature.static StringpackageToPath(String name) Converts '.'static StringpathToPackage(String str) Converts a path to a package name.static voidprintArray(PrintStream out, Object[] obj) Prints an array to a stream.static voidprintArray(PrintWriter out, Object[] obj) Prints an array to a writer.static StringprintArray(Object[] obj) Prints an array to a string.static StringprintArray(Object[] obj, boolean braces) Prints an array to a string.static StringprintArray(Object[] obj, boolean braces, boolean quote) Prints an array to a string.static StringReplace all occurrences of old in str with new.static shortsearchOpcode(String name) Map opcode names to opcode numbers.static intsetBit(int flag, int i) Sets a bit in a flag.static StringsignatureToString(String signature) Converts a signature to a string with all class names compacted.static StringsignatureToString(String signature, boolean chopit) Converts a signature to a string.static StringtoHexString(byte[] bytes) Convert bytes into hexadecimal stringstatic bytetypeOfMethodSignature(String signature) Return type of method signature as a byte value as defined in Constantsstatic bytetypeOfSignature(String signature) Return type of signature as a byte value as defined in Constantsstatic StringtypeSignatureToString(String signature, boolean chopit) This method converts a type signature string into a Java type declaration such as 'String[]' and throws a 'ClassFormatException' when the parsed type is invalid.
-
Constructor Details
-
Utility
-
-
Method Details
-
accessToString
Convert bit field of flags into string such as 'static final'.- Parameters:
accessFlags- Access flags.- Returns:
- String representation of flags.
-
accessToString
Convert bit field of flags into string such as 'static final'.Special case: Classes compiled with new compilers and with the 'ACC_SUPER' flag would be said to be "synchronized". This is because SUN used the same value for the flags 'ACC_SUPER' and 'ACC_SYNCHRONIZED'.
- Parameters:
accessFlags- Access flags.forClass- access flags are for class qualifiers ?.- Returns:
- String representation of flags.
-
classOrInterface
Gets the class or interface type name.- Parameters:
accessFlags- The class flags.- Returns:
- "class" or "interface", depending on the ACC_INTERFACE flag.
-
clearBit
Clears a bit in a flag.- Parameters:
flag- The flag value.i- The bit position.- Returns:
- 'flag' with bit 'i' set to 0.
-
codeToString
Disassembles byte code.- Parameters:
code- byte code array.constantPool- The constant pool.index- offset in code array.length- number of opcodes to decompile.- Returns:
- disassembled string representation.
-
codeToString
public static String codeToString(byte[] code, ConstantPool constantPool, int index, int length, boolean verbose) Disassemble a byte array of JVM byte codes starting from code line 'index' and return the disassembled string representation. Decode only 'num' opcodes (including their operands), use -1 if you want to decompile everything.- Parameters:
code- byte code array.constantPool- Array of constants.index- offset in 'code' array (number of opcodes, not bytes!).length- number of opcodes to decompile, -1 for all.verbose- be verbose, for example print constant pool index.- Returns:
- String representation of byte codes.
-
codeToString
Disassembles byte code.- Parameters:
bytes- stream of bytes.constantPool- The constant pool.- Returns:
- disassembled string representation.
- Throws:
IOException- Thrown if a failure from reading from the bytes argument occurs.
-
codeToString
public static String codeToString(ByteSequence bytes, ConstantPool constantPool, boolean verbose) throws IOException Disassemble a stream of byte codes and return the string representation.- Parameters:
bytes- stream of bytes.constantPool- Array of constants.verbose- be verbose, for example print constant pool index.- Returns:
- String representation of byte code.
- Throws:
IOException- Thrown if a failure from reading from the bytes argument occurs
-
compactClassName
Shorten long class names, java/lang/String becomes String.- Parameters:
str- The long class name.- Returns:
- Compacted class name.
-
compactClassName
Shorten long class names, java/lang/String becomes java.lang.String, for example. If chopit is true the prefix java.lang is also removed.- Parameters:
str- The long class name.chopit- flag that determines whether chopping is executed or not.- Returns:
- Compacted class name.
-
compactClassName
Shorten long class name str, that is, chop off the prefix, if the class name starts with this string and the flag chopit is true. Slashes / are converted to dots ..- Parameters:
str- The long class name.prefix- The prefix the get rid off.chopit- flag that determines whether chopping is executed or not.- Returns:
- Compacted class name.
-
convertString
Escape all occurrences of newline chars '\n', quotes \", etc.- Parameters:
label- The string to convert.- Returns:
- The converted string.
-
decode
Decode a string back to a byte array.- Parameters:
s- The string to convert.uncompress- use gzip to uncompress the stream of bytes.- Returns:
- The decoded byte array.
- Throws:
IOException- Thrown if there's a gzip exception or the decompressed data exceedsMAX_DECODED_LENGTH.
-
encode
Encode byte array it into Java identifier string, that is, a string that only contains the following characters: (a, ... z, A, ... Z, 0, ... 9, _, $). The encoding algorithm itself is not too clever: if the current byte's ASCII value already is a valid Java identifier part, leave it as it is. Otherwise it writes the escape character($) followed by:- the ASCII value as a hexadecimal string, if the value is not in the range 200..247
- a Java identifier char not used in a lowercase hexadecimal string, if the value is in the range 200..247
This operation inflates the original byte array by roughly 40-50%
- Parameters:
bytes- The byte array to convert.compress- use gzip to minimize string.- Returns:
- The encoded string.
- Throws:
IOException- Thrown if there's a gzip exception.
-
fillup
Fillup char with up to length characters with char 'fill' and justify it left or right.- Parameters:
str- string to format.length- length of desired string.leftJustify- format left or right.fill- fill character.- Returns:
- formatted string.
-
format
Return a string for an integer justified left or right and filled up with 'fill' characters if necessary.- Parameters:
i- integer to format.length- length of desired string.leftJustify- format left or right.fill- fill character.- Returns:
- formatted int.
-
getSignature
Parse Java type such as "char", or "java.lang.String[]" and return the signature in byte code format, for example "C" or "[Ljava/lang/String;" respectively.- Parameters:
type- Java type.- Returns:
- byte code signature.
-
isJavaIdentifierPart
Tests if a character is part of a Java identifier.- Parameters:
ch- The character to test if it's part of an identifier.- Returns:
- true, if character is one of (a, ... z, A, ... Z, 0, ... 9, _).
-
isSet
Tests if a bit is set.- Parameters:
flag- The flag value.i- The bit position.- Returns:
- true, if bit 'i' in 'flag' is set.
-
methodSignatureArgumentTypes
Converts argument list portion of method signature to string with all class names compacted.- Parameters:
signature- Method signature.- Returns:
- String Array of argument types.
- Throws:
ClassFormatException- Thrown if a class is malformed or cannot be interpreted as a class file
-
methodSignatureArgumentTypes
public static String[] methodSignatureArgumentTypes(String signature, boolean chopit) throws ClassFormatException Converts argument list portion of method signature to string.- Parameters:
signature- Method signature.chopit- flag that determines whether chopping is executed or not.- Returns:
- String Array of argument types.
- Throws:
ClassFormatException- Thrown if a class is malformed or cannot be interpreted as a class file
-
methodSignatureReturnType
Converts return type portion of method signature to string with all class names compacted.- Parameters:
signature- Method signature.- Returns:
- String representation of method return type.
- Throws:
ClassFormatException- Thrown if a class is malformed or cannot be interpreted as a class file
-
methodSignatureReturnType
public static String methodSignatureReturnType(String signature, boolean chopit) throws ClassFormatException Converts return type portion of method signature to string.- Parameters:
signature- Method signature.chopit- flag that determines whether chopping is executed or not.- Returns:
- String representation of method return type.
- Throws:
ClassFormatException- Thrown if a class is malformed or cannot be interpreted as a class file
-
methodSignatureToString
Converts method signature to string with all class names compacted.- Parameters:
signature- to convert.name- of method.access- flags of method.- Returns:
- Human readable signature.
-
methodSignatureToString
public static String methodSignatureToString(String signature, String name, String access, boolean chopit) Converts method signature to string.- Parameters:
signature- to convert.name- of method.access- flags of method.chopit- flag that determines whether chopping is executed or not.- Returns:
- Human readable signature.
-
methodSignatureToString
public static String methodSignatureToString(String signature, String name, String access, boolean chopit, LocalVariableTable vars) throws ClassFormatException This method converts a method signature string into a Java type declaration like 'void main(String[])' and throws a 'ClassFormatException' when the parsed type is invalid.- Parameters:
signature- Method signature.name- Method name.access- Method access rights.chopit- flag that determines whether chopping is executed or not.vars- The LocalVariableTable for the method.- Returns:
- Java type declaration.
- Throws:
ClassFormatException- Thrown if a class is malformed or cannot be interpreted as a class file
-
methodTypeToSignature
Converts string containing the method return and argument types to a byte code method signature.- Parameters:
ret- Return type of method.argv- Types of method arguments.- Returns:
- Byte code representation of method signature.
- Throws:
ClassFormatException- Thrown if the signature is for Void
-
packageToPath
Converts '.'s to '/'s.- Parameters:
name- Source.- Returns:
- converted value.
- Since:
- 6.7.0
-
pathToPackage
Converts a path to a package name.- Parameters:
str- The source path.- Returns:
- A package name.
- Since:
- 6.6.0
-
printArray
Prints an array to a string.- Parameters:
obj- The array to print.- Returns:
- The string representation.
-
printArray
Prints an array to a string.- Parameters:
obj- The array to print.braces- whether to include braces.- Returns:
- The string representation.
-
printArray
Prints an array to a string.- Parameters:
obj- The array to print.braces- whether to include braces.quote- whether to quote elements.- Returns:
- The string representation.
-
printArray
Prints an array to a stream.- Parameters:
out- The output stream.obj- The array to print.
-
printArray
Prints an array to a writer.- Parameters:
out- The output writer.obj- The array to print.
-
replace
-
searchOpcode
Map opcode names to opcode numbers. E.g., return Constants.ALOAD for "aload".- Parameters:
name- The opcode name.- Returns:
- The value.
-
setBit
Sets a bit in a flag.- Parameters:
flag- The flag value.i- The bit position.- Returns:
- 'flag' with bit 'i' set to 1.
-
signatureToString
Converts a signature to a string with all class names compacted. Class, Method and Type signatures are supported. Enum and Interface signatures are not supported.- Parameters:
signature- signature to convert.- Returns:
- String containg human readable signature.
-
signatureToString
Converts a signature to a string. Class, Method and Type signatures are supported. Enum and Interface signatures are not supported.- Parameters:
signature- signature to convert.chopit- flag that determines whether chopping is executed or not.- Returns:
- String containg human readable signature.
-
toHexString
Convert bytes into hexadecimal string- Parameters:
bytes- An array of bytes to convert to hexadecimal.- Returns:
- bytes as hexadecimal string, for example 00 fa 12 ...
-
typeOfMethodSignature
Return type of method signature as a byte value as defined in Constants- Parameters:
signature- in format described above.- Returns:
- type of method signature.
- Throws:
ClassFormatException- Thrown if signature is not a method signature- See Also:
-
typeOfSignature
Return type of signature as a byte value as defined in Constants- Parameters:
signature- in format described above.- Returns:
- type of signature.
- Throws:
ClassFormatException- Thrown if signature isn't a known type- See Also:
-
typeSignatureToString
public static String typeSignatureToString(String signature, boolean chopit) throws ClassFormatException This method converts a type signature string into a Java type declaration such as 'String[]' and throws a 'ClassFormatException' when the parsed type is invalid.- Parameters:
signature- type signature.chopit- flag that determines whether chopping is executed or not.- Returns:
- string containing human readable type signature.
- Throws:
ClassFormatException- Thrown if a class is malformed or cannot be interpreted as a class file- Since:
- 6.4.0
-