Package org.apache.bcel.classfile
Class Utility
java.lang.Object
org.apache.bcel.classfile.Utility
Utility functions that do not really belong to any class in particular.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
accessToString
(int access_flags) Convert bit field of flags into string such as `static final'.static String
accessToString
(int access_flags, boolean for_class) Convert bit field of flags into string such as `static final'.static String
classOrInterface
(int access_flags) static int
clearBit
(int flag, int i) static String
codeToString
(byte[] code, ConstantPool constant_pool, int index, int length) static String
codeToString
(byte[] code, ConstantPool constant_pool, 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 String
codeToString
(ByteSequence bytes, ConstantPool constant_pool) static String
codeToString
(ByteSequence bytes, ConstantPool constant_pool, boolean verbose) Disassemble a stream of byte codes and return the string representation.static String
compactClassName
(String str) Shorten long class names, java/lang/String becomes String.static String
compactClassName
(String str, boolean chopit) Shorten long class names, java/lang/String becomes java.lang.String, e.g..static String
compactClassName
(String str, String prefix, boolean chopit) Shorten long class name str, i.e., chop off the prefix, if the class name starts with this string and the flag chopit is true.static String
convertString
(String label) Escape all occurences of newline chars '\n', quotes \", etc.static byte[]
Decode a string back to a byte array.static String
encode
(byte[] bytes, boolean compress) Encode byte array it into Java identifier string, i.e., a string that only contains the following characters: (a, ...static String
Fillup char with up to length characters with char `fill' and justify it left or right.static String
format
(int i, int length, boolean left_justify, char fill) Return a string for an integer justified left or right and filled up with `fill' characters if necessary.static String
getSignature
(String type) Parse Java type such as "char", or "java.lang.String[]" and return the signature in byte code format, e.g.static boolean
isJavaIdentifierPart
(char ch) static boolean
isSet
(int flag, int i) 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 String
methodSignatureReturnType
(String signature) Converts return type portion of method signature to string with all class names compacted.static String
methodSignatureReturnType
(String signature, boolean chopit) Converts return type portion of method signature to string.static String
methodSignatureToString
(String signature, String name, String access) Converts method signature to string with all class names compacted.static String
methodSignatureToString
(String signature, String name, String access, boolean chopit) Converts method signature to string.static String
methodSignatureToString
(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 String
methodTypeToSignature
(String ret, String[] argv) Converts string containing the method return and argument types to a byte code method signature.static void
printArray
(PrintStream out, Object[] obj) static void
printArray
(PrintWriter out, Object[] obj) static String
printArray
(Object[] obj) static String
printArray
(Object[] obj, boolean braces) static String
printArray
(Object[] obj, boolean braces, boolean quote) static String
Replace all occurrences of old in str with new.static short
searchOpcode
(String name) Map opcode names to opcode numbers.static int
setBit
(int flag, int i) static String
signatureToString
(String signature) Converts a signature to a string with all class names compacted.static String
signatureToString
(String signature, boolean chopit) Converts a signature to a string.static String
toHexString
(byte[] bytes) Convert bytes into hexadecimal stringstatic byte
typeOfMethodSignature
(String signature) Return type of method signature as a byte value as defined in Constantsstatic byte
typeOfSignature
(String signature) Return type of signature as a byte value as defined in Constantsstatic String
typeSignatureToString
(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
public Utility()
-
-
Method Details
-
accessToString
Convert bit field of flags into string such as `static final'.- Parameters:
access_flags
- 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:
access_flags
- Access flagsfor_class
- access flags are for class qualifiers ?- Returns:
- String representation of flags
-
classOrInterface
- Parameters:
access_flags
- the class flags- Returns:
- "class" or "interface", depending on the ACC_INTERFACE flag
-
codeToString
public static String codeToString(byte[] code, ConstantPool constant_pool, 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 arrayconstant_pool
- Array of constantsindex
- offset in `code' array (number of opcodes, not bytes!)length
- number of opcodes to decompile, -1 for allverbose
- be verbose, e.g. print constant pool index- Returns:
- String representation of byte codes
-
codeToString
-
codeToString
public static String codeToString(ByteSequence bytes, ConstantPool constant_pool, boolean verbose) throws IOException Disassemble a stream of byte codes and return the string representation.- Parameters:
bytes
- stream of bytesconstant_pool
- Array of constantsverbose
- be verbose, e.g. print constant pool index- Returns:
- String representation of byte code
- Throws:
IOException
- if a failure from reading from the bytes argument occurs
-
codeToString
public static String codeToString(ByteSequence bytes, ConstantPool constant_pool) throws IOException - Throws:
IOException
-
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, e.g.. If chopit is true the prefix java.lang is also removed.- Parameters:
str
- The long class namechopit
- flag that determines whether chopping is executed or not- Returns:
- Compacted class name
-
compactClassName
Shorten long class name str, i.e., 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 nameprefix
- The prefix the get rid offchopit
- flag that determines whether chopping is executed or not- Returns:
- Compacted class name
-
setBit
public static int setBit(int flag, int i) - Returns:
- `flag' with bit `i' set to 1
-
clearBit
public static int clearBit(int flag, int i) - Returns:
- `flag' with bit `i' set to 0
-
isSet
public static boolean isSet(int flag, int i) - Returns:
- true, if bit `i' in `flag' is set
-
methodTypeToSignature
Converts string containing the method return and argument types to a byte code method signature.- Parameters:
ret
- Return type of methodargv
- Types of method arguments- Returns:
- Byte code representation of method signature
- Throws:
ClassFormatException
- if the signature is for Void
-
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
-
methodSignatureArgumentTypes
public static String[] methodSignatureArgumentTypes(String signature, boolean chopit) throws ClassFormatException Converts argument list portion of method signature to string.- Parameters:
signature
- Method signaturechopit
- flag that determines whether chopping is executed or not- Returns:
- String Array of argument types
- Throws:
ClassFormatException
-
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
-
methodSignatureReturnType
public static String methodSignatureReturnType(String signature, boolean chopit) throws ClassFormatException Converts return type portion of method signature to string.- Parameters:
signature
- Method signaturechopit
- flag that determines whether chopping is executed or not- Returns:
- String representation of method return type
- Throws:
ClassFormatException
-
methodSignatureToString
Converts method signature to string with all class names compacted.- Parameters:
signature
- to convertname
- of methodaccess
- 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 convertname
- of methodaccess
- flags of methodchopit
- 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 signaturename
- Method nameaccess
- Method access rightschopit
- flag that determines whether chopping is executed or notvars
- the LocalVariableTable for the method- Returns:
- Java type declaration
- Throws:
ClassFormatException
-
replace
Replace all occurrences of old in str with new.- Parameters:
str
- String to permuteold
- String to be replacednew_
- Replacement string- Returns:
- new String object
-
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 convertchopit
- flag that determines whether chopping is executed or not- Returns:
- String containg human readable signature
-
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 signaturechopit
- flag that determines whether chopping is executed or not- Returns:
- string containing human readable type signature
- Throws:
ClassFormatException
- Since:
- 6.4.0
-
getSignature
Parse Java type such as "char", or "java.lang.String[]" and return the signature in byte code format, e.g. "C" or "[Ljava/lang/String;" respectively.- Parameters:
type
- Java type- Returns:
- byte code signature
-
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
- 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
- if signature isn't a known type- See Also:
-
searchOpcode
Map opcode names to opcode numbers. E.g., return Constants.ALOAD for "aload" -
toHexString
Convert bytes into hexadecimal string- Parameters:
bytes
- an array of bytes to convert to hexadecimal- Returns:
- bytes as hexadecimal string, e.g. 00 fa 12 ...
-
format
Return a string for an integer justified left or right and filled up with `fill' characters if necessary.- Parameters:
i
- integer to formatlength
- length of desired stringleft_justify
- format left or rightfill
- fill character- Returns:
- formatted int
-
fillup
Fillup char with up to length characters with char `fill' and justify it left or right.- Parameters:
str
- string to formatlength
- length of desired stringleft_justify
- format left or rightfill
- fill character- Returns:
- formatted string
-
printArray
-
printArray
-
printArray
-
printArray
-
printArray
-
isJavaIdentifierPart
public static boolean isJavaIdentifierPart(char ch) - 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, _)
-
encode
Encode byte array it into Java identifier string, i.e., 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 convertcompress
- use gzip to minimize string- Throws:
IOException
- if there's a gzip exception
-
decode
Decode a string back to a byte array.- Parameters:
s
- the string to convertuncompress
- use gzip to uncompress the stream of bytes- Throws:
IOException
- if there's a gzip exception
-
convertString
Escape all occurences of newline chars '\n', quotes \", etc.
-