Package org.apache.bcel.classfile
Class Utility
- java.lang.Object
-
- org.apache.bcel.classfile.Utility
-
public abstract class Utility extends java.lang.Object
Utility functions that do not really belong to any class in particular.- Version:
- $Id: Utility.java 438661 2006-08-30 21:57:25Z dbrosius $
- Author:
- M. Dahm
-
-
Constructor Summary
Constructors Constructor Description Utility()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
accessToString(int access_flags)
Convert bit field of flags into string such as `static final'.static java.lang.String
accessToString(int access_flags, boolean for_class)
Convert bit field of flags into string such as `static final'.static java.lang.String
classOrInterface(int access_flags)
static int
clearBit(int flag, int i)
static java.lang.String
codeToString(byte[] code, ConstantPool constant_pool, int index, int length)
static java.lang.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 java.lang.String
codeToString(ByteSequence bytes, ConstantPool constant_pool)
static java.lang.String
codeToString(ByteSequence bytes, ConstantPool constant_pool, boolean verbose)
Disassemble a stream of byte codes and return the string representation.static java.lang.String
compactClassName(java.lang.String str)
Shorten long class names, java/lang/String becomes String.static java.lang.String
compactClassName(java.lang.String str, boolean chopit)
Shorten long class names, java/lang/String becomes java.lang.String, e.g..static java.lang.String
compactClassName(java.lang.String str, java.lang.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 java.lang.String
convertString(java.lang.String label)
Escape all occurences of newline chars '\n', quotes \", etc.static byte[]
decode(java.lang.String s, boolean uncompress)
Decode a string back to a byte array.static java.lang.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 java.lang.String
fillup(java.lang.String str, int length, boolean left_justify, char fill)
Fillup char with up to length characters with char `fill' and justify it left or right.static java.lang.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 Attribute[]
getAnnotationAttributes(ConstantPoolGen cp, java.util.List vec)
Converts a list of AnnotationGen objects into a set of attributes that can be attached to the class file.static Attribute[]
getParameterAnnotationAttributes(ConstantPoolGen cp, java.util.List[] vec)
Annotations against a class are stored in one of four attribute kinds: - RuntimeVisibleParameterAnnotations - RuntimeInvisibleParameterAnnotationsstatic java.lang.String
getSignature(java.lang.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 java.lang.String[]
methodSignatureArgumentTypes(java.lang.String signature)
static java.lang.String[]
methodSignatureArgumentTypes(java.lang.String signature, boolean chopit)
static java.lang.String
methodSignatureReturnType(java.lang.String signature)
static java.lang.String
methodSignatureReturnType(java.lang.String signature, boolean chopit)
static java.lang.String
methodSignatureToString(java.lang.String signature, java.lang.String name, java.lang.String access)
Converts method signature to string with all class names compacted.static java.lang.String
methodSignatureToString(java.lang.String signature, java.lang.String name, java.lang.String access, boolean chopit)
static java.lang.String
methodSignatureToString(java.lang.String signature, java.lang.String name, java.lang.String access, boolean chopit, LocalVariableTable vars)
A returntype signature represents the return value from a method.static java.lang.String
methodTypeToSignature(java.lang.String ret, java.lang.String[] argv)
Converts string containing the method return and argument types to a byte code method signature.static void
printArray(java.io.PrintStream out, java.lang.Object[] obj)
static void
printArray(java.io.PrintWriter out, java.lang.Object[] obj)
static java.lang.String
printArray(java.lang.Object[] obj)
static java.lang.String
printArray(java.lang.Object[] obj, boolean braces)
static java.lang.String
printArray(java.lang.Object[] obj, boolean braces, boolean quote)
static java.lang.String
replace(java.lang.String str, java.lang.String old, java.lang.String new_)
Replace all occurences of old in str with new.static short
searchOpcode(java.lang.String name)
Map opcode names to opcode numbers.static int
setBit(int flag, int i)
static java.lang.String
signatureToString(java.lang.String signature)
Converts signature to string with all class names compacted.static java.lang.String
signatureToString(java.lang.String signature, boolean chopit)
The field signature represents the value of an argument to a function or the value of a variable.static java.lang.String
toHexString(byte[] bytes)
Convert bytes into hexidecimal stringstatic byte
typeOfMethodSignature(java.lang.String signature)
Return type of method signature as a byte value as defined in Constantsstatic byte
typeOfSignature(java.lang.String signature)
Return type of signature as a byte value as defined in Constants
-
-
-
Method Detail
-
accessToString
public static final java.lang.String accessToString(int access_flags)
Convert bit field of flags into string such as `static final'.- Parameters:
access_flags
- Access flags- Returns:
- String representation of flags
-
accessToString
public static final java.lang.String accessToString(int access_flags, boolean for_class)
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
public static final java.lang.String classOrInterface(int access_flags)
- Returns:
- "class" or "interface", depending on the ACC_INTERFACE flag
-
codeToString
public static final java.lang.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
public static final java.lang.String codeToString(byte[] code, ConstantPool constant_pool, int index, int length)
-
codeToString
public static final java.lang.String codeToString(ByteSequence bytes, ConstantPool constant_pool, boolean verbose) throws java.io.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:
java.io.IOException
-
codeToString
public static final java.lang.String codeToString(ByteSequence bytes, ConstantPool constant_pool) throws java.io.IOException
- Throws:
java.io.IOException
-
compactClassName
public static final java.lang.String compactClassName(java.lang.String str)
Shorten long class names, java/lang/String becomes String.- Parameters:
str
- The long class name- Returns:
- Compacted class name
-
compactClassName
public static final java.lang.String compactClassName(java.lang.String str, java.lang.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. 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
-
compactClassName
public static final java.lang.String compactClassName(java.lang.String str, boolean chopit)
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
-
setBit
public static final int setBit(int flag, int i)
- Returns:
- `flag' with bit `i' set to 1
-
clearBit
public static final int clearBit(int flag, int i)
- Returns:
- `flag' with bit `i' set to 0
-
isSet
public static final boolean isSet(int flag, int i)
- Returns:
- true, if bit `i' in `flag' is set
-
methodTypeToSignature
public static final java.lang.String methodTypeToSignature(java.lang.String ret, java.lang.String[] argv) throws ClassFormatException
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
-
methodSignatureArgumentTypes
public static final java.lang.String[] methodSignatureArgumentTypes(java.lang.String signature) throws ClassFormatException
- Parameters:
signature
- Method signature- Returns:
- Array of argument types
- Throws:
ClassFormatException
-
methodSignatureArgumentTypes
public static final java.lang.String[] methodSignatureArgumentTypes(java.lang.String signature, boolean chopit) throws ClassFormatException
- Parameters:
signature
- Method signaturechopit
- Shorten class names ?- Returns:
- Array of argument types
- Throws:
ClassFormatException
-
methodSignatureReturnType
public static final java.lang.String methodSignatureReturnType(java.lang.String signature) throws ClassFormatException
- Parameters:
signature
- Method signature- Returns:
- return type of method
- Throws:
ClassFormatException
-
methodSignatureReturnType
public static final java.lang.String methodSignatureReturnType(java.lang.String signature, boolean chopit) throws ClassFormatException
- Parameters:
signature
- Method signaturechopit
- Shorten class names ?- Returns:
- return type of method
- Throws:
ClassFormatException
-
methodSignatureToString
public static final java.lang.String methodSignatureToString(java.lang.String signature, java.lang.String name, java.lang.String access)
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 final java.lang.String methodSignatureToString(java.lang.String signature, java.lang.String name, java.lang.String access, boolean chopit)
-
methodSignatureToString
public static final java.lang.String methodSignatureToString(java.lang.String signature, java.lang.String name, java.lang.String access, boolean chopit, LocalVariableTable vars) throws ClassFormatException
A returntype signature represents the return value from a method. It is a series of bytes in the following grammar:::= | V The character V indicates that the method returns no value. Otherwise, the signature indicates the type of the return value. An argument signature represents an argument passed to a method: ::= A method signature represents the arguments that the method expects, and the value that it returns. ::= ( ) ::= * This method converts such a 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 rights- Returns:
- Java type declaration
- Throws:
ClassFormatException
-
replace
public static final java.lang.String replace(java.lang.String str, java.lang.String old, java.lang.String new_)
Replace all occurences of old in str with new.- Parameters:
str
- String to permuteold
- String to be replacednew_
- Replacement string- Returns:
- new String object
-
signatureToString
public static final java.lang.String signatureToString(java.lang.String signature)
Converts signature to string with all class names compacted.- Parameters:
signature
- to convert- Returns:
- Human readable signature
-
signatureToString
public static final java.lang.String signatureToString(java.lang.String signature, boolean chopit)
The field signature represents the value of an argument to a function or the value of a variable. It is a series of bytes generated by the following grammar:::= ::= | | ::= B|C|D|F|I|J|S|Z ::= L ; ::= [ The meaning of the base types is as follows: B byte signed byte C char character D double double precision IEEE float F float single precision IEEE float I int integer J long long integer L ; ... an object of the given class S short signed short Z boolean true or false [ ... array - Parameters:
signature
- Class signaturechopit
- Flag that determines whether chopping is executed or not- Returns:
- Java type declaration
- Throws:
ClassFormatException
-
getSignature
public static java.lang.String getSignature(java.lang.String type)
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
public static final byte typeOfMethodSignature(java.lang.String signature) throws ClassFormatException
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
- See Also:
Constants
-
typeOfSignature
public static final byte typeOfSignature(java.lang.String signature) throws ClassFormatException
Return type of signature as a byte value as defined in Constants- Parameters:
signature
- in format described above- Returns:
- type of signature
- Throws:
ClassFormatException
- See Also:
Constants
-
searchOpcode
public static short searchOpcode(java.lang.String name)
Map opcode names to opcode numbers. E.g., return Constants.ALOAD for "aload"
-
toHexString
public static final java.lang.String toHexString(byte[] bytes)
Convert bytes into hexidecimal string- Returns:
- bytes as hexidecimal string, e.g. 00 FA 12 ...
-
format
public static final java.lang.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.- Parameters:
i
- integer to formatlength
- length of desired stringleft_justify
- format left or rightfill
- fill character- Returns:
- formatted int
-
fillup
public static final java.lang.String fillup(java.lang.String str, int length, boolean left_justify, char fill)
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
public static final void printArray(java.io.PrintStream out, java.lang.Object[] obj)
-
printArray
public static final void printArray(java.io.PrintWriter out, java.lang.Object[] obj)
-
printArray
public static final java.lang.String printArray(java.lang.Object[] obj)
-
printArray
public static final java.lang.String printArray(java.lang.Object[] obj, boolean braces)
-
printArray
public static final java.lang.String printArray(java.lang.Object[] obj, boolean braces, boolean quote)
-
isJavaIdentifierPart
public static boolean isJavaIdentifierPart(char ch)
- Returns:
- true, if character is one of (a, ... z, A, ... Z, 0, ... 9, _)
-
encode
public static java.lang.String encode(byte[] bytes, boolean compress) throws java.io.IOException
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:
java.io.IOException
-
decode
public static byte[] decode(java.lang.String s, boolean uncompress) throws java.io.IOException
Decode a string back to a byte array.- Parameters:
s
- the string to convertuncompress
- use gzip to uncompress the stream of bytes- Throws:
java.io.IOException
-
convertString
public static final java.lang.String convertString(java.lang.String label)
Escape all occurences of newline chars '\n', quotes \", etc.
-
getAnnotationAttributes
public static Attribute[] getAnnotationAttributes(ConstantPoolGen cp, java.util.List vec)
Converts a list of AnnotationGen objects into a set of attributes that can be attached to the class file.- Parameters:
cp
- The constant pool gen where we can create the necessary name refsvec
- A list of AnnotationGen objects
-
getParameterAnnotationAttributes
public static Attribute[] getParameterAnnotationAttributes(ConstantPoolGen cp, java.util.List[] vec)
Annotations against a class are stored in one of four attribute kinds: - RuntimeVisibleParameterAnnotations - RuntimeInvisibleParameterAnnotations
-
-