Package org.plumelib.bcelutil
Class BcelUtil
- java.lang.Object
-
- org.plumelib.bcelutil.BcelUtil
-
public final class BcelUtil extends java.lang.ObjectStatic utility methods for working with BCEL.
-
-
Field Summary
Fields Modifier and Type Field Description static intjavaVersionThe major version number of the Java runtime (JRE), such as 8, 11, or 17.static booleanskipChecksControls whether the checks incheckMgen(org.apache.bcel.generic.MethodGen)are performed.private static org.apache.bcel.generic.TypestringArrayThe type that represents String[].
-
Constructor Summary
Constructors Modifier Constructor Description privateBcelUtil()This class is a collection of methods; it does not represent anything.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description (package private) static java.lang.StringaccessFlagsToString(org.apache.bcel.classfile.Method m)Return a string representation of the access flags of method m.static voidaddToStart(org.apache.bcel.generic.MethodGen mg, org.apache.bcel.generic.InstructionList newList)Adds instructions to the start of a method.static java.lang.StringattributeNameToString(org.apache.bcel.classfile.Attribute a)Return the attribute name for the specified attribute, looked up in the original class file ConstantPool.static java.lang.StringattributeNameToString(org.apache.bcel.classfile.Attribute a, org.apache.bcel.generic.ConstantPoolGen pool)Return the attribute name for the specified attribute, looked up in the given ConstantPoolGen.static org.apache.bcel.generic.TypebinaryNameToType(@BinaryNameOrPrimitiveType java.lang.String classname)Return the type corresponding to a given binary name or primitive type name.static voidcheckMgen(org.apache.bcel.generic.MethodGen mgen)Checks the specified method for consistency.static voidcheckMgens(org.apache.bcel.generic.ClassGen gen)Checks all of the methods in gen for consistency.static voiddump(org.apache.bcel.classfile.JavaClass jc, java.io.File dumpDir)Dumps the contents of the specified class to the specified directory.static voiddump(org.apache.bcel.classfile.JavaClass jc, java.lang.String dumpDir)Dumps the contents of the specified class to the specified directory.(package private) static voiddumpMethods(org.apache.bcel.generic.ClassGen gen)Print the methods in the class, to standard output.static voiddumpStackTrace()Print the current java call stack.static org.apache.bcel.generic.TypefqBinaryNameToType(@FqBinaryName java.lang.String classname)Return the type corresponding to a given fully-qualified binary name.static java.lang.StringgetConstantString(org.apache.bcel.classfile.ConstantPool pool, int index)Returns the constant string at the specified offset.private static intgetJavaVersion()Returns the major version number from the "java.version" system property, such as 8, 11, or 17.static booleaninJdk(@ClassGetName java.lang.String classname)Returns whether or not the class is part of the JDK (rt.jar).static booleaninJdk(org.apache.bcel.generic.ClassGen gen)Returns whether or not the class is part of the JDK (rt.jar).static booleaninJdkInternalform(@InternalForm java.lang.String classname)Returns whether or not the class is part of the JDK (rt.jar).static java.lang.StringinstructionListToString(org.apache.bcel.generic.InstructionList il, org.apache.bcel.generic.ConstantPoolGen pool)Return a printed description of the given instructions.static booleanisClinit(org.apache.bcel.classfile.Method m)Returns whether or not the method is a class initializer.static booleanisClinit(org.apache.bcel.generic.MethodGen mg)Returns whether or not the method is a class initializer.static booleanisConstructor(org.apache.bcel.classfile.Method m)Returns whether or not the method is a constructor.static booleanisConstructor(org.apache.bcel.generic.MethodGen mg)Returns whether or not the method is a constructor.static booleanisLocalVariableTypeTable(org.apache.bcel.classfile.Attribute a, org.apache.bcel.generic.ConstantPoolGen pool)Returns whether or not the specified attribute is a local variable type table.static booleanisMain(org.apache.bcel.generic.MethodGen mg)Returns whether or not this is a standard main method (static, void, name is 'main', and one formal parameter: a string array).static java.lang.StringlocalVariablesToString(org.apache.bcel.generic.MethodGen mg)Return a description of the local variables (one per line).static voidmakeMethodBodyEmpty(org.apache.bcel.generic.MethodGen mg)Empties the method of all code (except for a return).static java.lang.StringmethodDeclarationToString(org.apache.bcel.classfile.Method m)Returns a string describing a method declaration.static org.apache.bcel.generic.Type[]postpendToArray(org.apache.bcel.generic.Type[] types, org.apache.bcel.generic.Type newType)Returns a copy of the given type array, with newType added to the end.static org.apache.bcel.generic.Type[]prependToArray(org.apache.bcel.generic.Type newType, org.apache.bcel.generic.Type[] types)Returns a copy of the given type array, with newType inserted at the beginning.static voidremoveLocalVariableTypeTables(org.apache.bcel.generic.MethodGen mg)Remove the local variable type table attribute (LVTT) from mg.static voidresetLocalsToFormals(org.apache.bcel.generic.MethodGen mg)Sets the locals to be the formal parameters.static java.lang.Class<?>typeToClass(org.apache.bcel.generic.Type type)Returns the class that corresponds to type.static @ClassGetName java.lang.StringtypeToClassgetname(org.apache.bcel.generic.Type type)Returns the Java class name, in the format ofClass.getName(), that corresponds to type.
-
-
-
Field Detail
-
skipChecks
public static boolean skipChecks
Controls whether the checks incheckMgen(org.apache.bcel.generic.MethodGen)are performed.
-
stringArray
private static final org.apache.bcel.generic.Type stringArray
The type that represents String[].
-
javaVersion
public static final int javaVersion
The major version number of the Java runtime (JRE), such as 8, 11, or 17.
-
-
Method Detail
-
getJavaVersion
private static int getJavaVersion()
Returns the major version number from the "java.version" system property, such as 8, 11, or 17.- Returns:
- the major version of the Java runtime
-
methodDeclarationToString
public static java.lang.String methodDeclarationToString(org.apache.bcel.classfile.Method m)
Returns a string describing a method declaration. It contains the access flags (public, private, static, etc), the return type, the method name, and the types of each of its parameters.For example, if the original Java source declaration was: private final String constantToString (int index) Then the output of methodDeclarationToString would be: private final java.lang.String constantToString (int)
- Parameters:
m- the method- Returns:
- a string describing the method declaration
-
accessFlagsToString
static java.lang.String accessFlagsToString(org.apache.bcel.classfile.Method m)
Return a string representation of the access flags of method m. In the string, the flags are space-separated and in a canonical order.- Parameters:
m- the method whose access flags to retrieve- Returns:
- a string representation of the access flags of method m
-
instructionListToString
public static java.lang.String instructionListToString(org.apache.bcel.generic.InstructionList il, org.apache.bcel.generic.ConstantPoolGen pool)Return a printed description of the given instructions.- Parameters:
il- the instructions to describepool- the constant pool the instructions refer to- Returns:
- a printed representation of the instructions in
il
-
localVariablesToString
public static java.lang.String localVariablesToString(org.apache.bcel.generic.MethodGen mg)
Return a description of the local variables (one per line).- Parameters:
mg- the method whose local variables to describe- Returns:
- a description of the local variables (one per line)
-
attributeNameToString
public static java.lang.String attributeNameToString(org.apache.bcel.classfile.Attribute a)
Return the attribute name for the specified attribute, looked up in the original class file ConstantPool.- Parameters:
a- the attribute- Returns:
- the attribute name for the specified attribute
-
attributeNameToString
public static java.lang.String attributeNameToString(org.apache.bcel.classfile.Attribute a, org.apache.bcel.generic.ConstantPoolGen pool)Return the attribute name for the specified attribute, looked up in the given ConstantPoolGen.- Parameters:
a- the attributepool- the constant pool- Returns:
- the attribute name for the specified attribute
-
isConstructor
public static boolean isConstructor(org.apache.bcel.generic.MethodGen mg)
Returns whether or not the method is a constructor.- Parameters:
mg- the MethodGen to test- Returns:
- true iff the method is a constructor
-
isConstructor
public static boolean isConstructor(org.apache.bcel.classfile.Method m)
Returns whether or not the method is a constructor.- Parameters:
m- the Method to test- Returns:
- true iff the method is a constructor
-
isClinit
public static boolean isClinit(org.apache.bcel.generic.MethodGen mg)
Returns whether or not the method is a class initializer.- Parameters:
mg- the method to test- Returns:
- true iff the method is a class initializer
-
isClinit
public static boolean isClinit(org.apache.bcel.classfile.Method m)
Returns whether or not the method is a class initializer.- Parameters:
m- the method to test- Returns:
- true iff the method is a class initializer
-
inJdk
public static boolean inJdk(org.apache.bcel.generic.ClassGen gen)
Returns whether or not the class is part of the JDK (rt.jar).- Parameters:
gen- the class to test- Returns:
- true iff the class is in a package that is in the JDK (rt.jar)
-
inJdk
public static boolean inJdk(@ClassGetName java.lang.String classname)
Returns whether or not the class is part of the JDK (rt.jar).- Parameters:
classname- the class to test, in the format of Class.getName(); the class should not be an array- Returns:
- true iff the class is in a package that is in the JDK (rt.jar)
-
inJdkInternalform
public static boolean inJdkInternalform(@InternalForm java.lang.String classname)
Returns whether or not the class is part of the JDK (rt.jar).- Parameters:
classname- the class to test, in internal form- Returns:
- true iff the class is part of the JDK (rt.jar)
-
isLocalVariableTypeTable
public static boolean isLocalVariableTypeTable(org.apache.bcel.classfile.Attribute a, org.apache.bcel.generic.ConstantPoolGen pool)Returns whether or not the specified attribute is a local variable type table.- Parameters:
a- the attributepool- the constant pool- Returns:
- true iff the attribute is a local variable type table
-
isMain
public static boolean isMain(org.apache.bcel.generic.MethodGen mg)
Returns whether or not this is a standard main method (static, void, name is 'main', and one formal parameter: a string array).- Parameters:
mg- the method to check- Returns:
- true iff the method is a main method
-
checkMgen
public static void checkMgen(org.apache.bcel.generic.MethodGen mgen)
Checks the specified method for consistency.Does nothing if
skipChecksis false.- Parameters:
mgen- the class to check
-
checkMgens
public static void checkMgens(org.apache.bcel.generic.ClassGen gen)
Checks all of the methods in gen for consistency.- Parameters:
gen- the class to check
-
dumpStackTrace
public static void dumpStackTrace()
Print the current java call stack.
-
dumpMethods
static void dumpMethods(org.apache.bcel.generic.ClassGen gen)
Print the methods in the class, to standard output.- Parameters:
gen- the class whose methods to print
-
dump
public static void dump(org.apache.bcel.classfile.JavaClass jc, java.lang.String dumpDir)Dumps the contents of the specified class to the specified directory. The file is named dumpDir/[class].bcel. It contains a synopsis of the fields and methods followed by the JVM code for each method.- Parameters:
jc- JavaClass to dumpdumpDir- directory in which to write the file- See Also:
dump(JavaClass, File)
-
dump
public static void dump(org.apache.bcel.classfile.JavaClass jc, java.io.File dumpDir)Dumps the contents of the specified class to the specified directory. The file is named dumpDir/[class].bcel. It contains a synopsis of the fields and methods followed by the JVM code for each method.- Parameters:
jc- JavaClass to dumpdumpDir- directory in which to write the file
-
addToStart
public static void addToStart(org.apache.bcel.generic.MethodGen mg, org.apache.bcel.generic.InstructionList newList)Adds instructions to the start of a method.- Parameters:
mg- method to be augmentednewList- instructions to prepend to the method
-
getConstantString
public static java.lang.String getConstantString(org.apache.bcel.classfile.ConstantPool pool, int index)Returns the constant string at the specified offset.- Parameters:
pool- the constant poolindex- the index in the constant pool- Returns:
- the constant string at the specified offset in the constant pool
-
resetLocalsToFormals
public static void resetLocalsToFormals(org.apache.bcel.generic.MethodGen mg)
Sets the locals to be the formal parameters. Any other locals are removed. An instruction list with at least one instruction must exist.- Parameters:
mg- the method whose locals to set
-
makeMethodBodyEmpty
public static void makeMethodBodyEmpty(org.apache.bcel.generic.MethodGen mg)
Empties the method of all code (except for a return). This includes line numbers, exceptions, local variables, etc.- Parameters:
mg- the method to clear out
-
removeLocalVariableTypeTables
public static void removeLocalVariableTypeTables(org.apache.bcel.generic.MethodGen mg)
Remove the local variable type table attribute (LVTT) from mg. Evidently some changes require this to be updated, but without BCEL support that would be hard to do. It should be safe to just delete it since it is optional and really only of use to a debugger.- Parameters:
mg- the method to clear out
-
typeToClassgetname
public static @ClassGetName java.lang.String typeToClassgetname(org.apache.bcel.generic.Type type)
Returns the Java class name, in the format ofClass.getName(), that corresponds to type.- Parameters:
type- the type- Returns:
- the Java classname that corresponds to type
-
typeToClass
public static java.lang.Class<?> typeToClass(org.apache.bcel.generic.Type type)
Returns the class that corresponds to type.- Parameters:
type- the type- Returns:
- the Java class that corresponds to type
-
postpendToArray
public static org.apache.bcel.generic.Type[] postpendToArray(org.apache.bcel.generic.Type[] types, org.apache.bcel.generic.Type newType)Returns a copy of the given type array, with newType added to the end.- Parameters:
types- the array to extendnewType- the element to add to the end of the array- Returns:
- a new array, with newType at the end
-
prependToArray
public static org.apache.bcel.generic.Type[] prependToArray(org.apache.bcel.generic.Type newType, org.apache.bcel.generic.Type[] types)Returns a copy of the given type array, with newType inserted at the beginning.- Parameters:
types- the array to extendnewType- the element to add to the beginning of the array- Returns:
- a new array, with newType at the beginning
-
binaryNameToType
public static org.apache.bcel.generic.Type binaryNameToType(@BinaryNameOrPrimitiveType java.lang.String classname)
Return the type corresponding to a given binary name or primitive type name.- Parameters:
classname- the binary name of a class (= fully-qualified name, except for inner classes), or a primitive type name, but not an array- Returns:
- the type corresponding to the given class name
- See Also:
fqBinaryNameToType(java.lang.String)
-
fqBinaryNameToType
public static org.apache.bcel.generic.Type fqBinaryNameToType(@FqBinaryName java.lang.String classname)
Return the type corresponding to a given fully-qualified binary name.- Parameters:
classname- the fully-qualified binary name of a type, which uses "$" rather than "." for nested classes- Returns:
- the type corresponding to the given name
-
-