Class BcelUtil


  • public final class BcelUtil
    extends Object
    Static utility methods for working with BCEL.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private BcelUtil()
      This class is a collection of methods; it does not represent anything.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      (package private) static String accessFlagsToString​(org.apache.bcel.classfile.Method m)
      Return a string representation of the access flags of method m.
      static void addToStart​(org.apache.bcel.generic.MethodGen mg, org.apache.bcel.generic.InstructionList newList)
      Adds instructions to the start of a method.
      static String attributeNameToString​(org.apache.bcel.classfile.Attribute a)
      Return the attribute name for the specified attribute, looked up in the original class file ConstantPool.
      static 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.
      static org.apache.bcel.generic.Type binaryNameToType​(@BinaryNameOrPrimitiveType String classname)
      Return the type corresponding to a given binary name or primitive type name.
      static void checkMgen​(org.apache.bcel.generic.MethodGen mgen)
      Checks the specified method for consistency.
      static void checkMgens​(org.apache.bcel.generic.ClassGen gen)
      Checks all of the methods in gen for consistency.
      static org.apache.bcel.generic.Type classnameToType​(@BinaryNameOrPrimitiveType String classname)
      static void dump​(org.apache.bcel.classfile.JavaClass jc, File dumpDir)
      Dumps the contents of the specified class to the specified directory.
      static void dump​(org.apache.bcel.classfile.JavaClass jc, String dumpDir)
      Dumps the contents of the specified class to the specified directory.
      (package private) static void dumpMethods​(org.apache.bcel.generic.ClassGen gen)
      Print the methods in the class, to standard output.
      static void dumpStackTrace()
      Print the current java call stack.
      static org.apache.bcel.generic.Type fqBinaryNameToType​(@FqBinaryName String classname)
      Return the type corresponding to a given fully-qualified binary name.
      static String getConstantString​(org.apache.bcel.classfile.ConstantPool pool, int index)
      Returns the constant string at the specified offset.
      private static int getJavaVersion()
      Extract the major version number from the "java.version" system property.
      static boolean inJdk​(@ClassGetName String classname)
      Returns whether or not the class is part of the JDK (rt.jar).
      static boolean inJdk​(org.apache.bcel.generic.ClassGen gen)
      Returns whether or not the class is part of the JDK (rt.jar).
      static boolean inJdkInternalform​(@InternalForm String classname)
      Returns whether or not the class is part of the JDK (rt.jar).
      static String instructionListToString​(org.apache.bcel.generic.InstructionList il, org.apache.bcel.generic.ConstantPoolGen pool)
      Return a printed description of the given instructions.
      static boolean isClinit​(org.apache.bcel.classfile.Method m)
      Returns whether or not the method is a class initializer.
      static boolean isClinit​(org.apache.bcel.generic.MethodGen mg)
      Returns whether or not the method is a class initializer.
      static boolean isConstructor​(org.apache.bcel.classfile.Method m)
      Returns whether or not the method is a constructor.
      static boolean isConstructor​(org.apache.bcel.generic.MethodGen mg)
      Returns whether or not the method is a constructor.
      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.
      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).
      static String localVariablesToString​(org.apache.bcel.generic.MethodGen mg)
      Return a description of the local variables (one per line).
      static void makeMethodBodyEmpty​(org.apache.bcel.generic.MethodGen mg)
      Empties the method of all code (except for a return).
      static String methodDeclarationToString​(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 void removeLocalVariableTypeTables​(org.apache.bcel.generic.MethodGen mg)
      Remove the local variable type table attribute (LVTT) from mg.
      static void resetLocalsToFormals​(org.apache.bcel.generic.MethodGen mg)
      Sets the locals to be the formal parameters.
      static Class<?> typeToClass​(org.apache.bcel.generic.Type type)
      Returns the class that corresponds to type.
      static @ClassGetName String typeToClassgetname​(org.apache.bcel.generic.Type type)
      Returns the Java class name, in the format of Class.getName(), that corresponds to type.
    • Field Detail

      • 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.
    • Constructor Detail

      • BcelUtil

        private BcelUtil()
        This class is a collection of methods; it does not represent anything.
    • Method Detail

      • getJavaVersion

        private static int getJavaVersion()
        Extract the major version number from the "java.version" system property.
        Returns:
        the major version of the Java runtime
      • methodDeclarationToString

        public static 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 orignal Java source declarationwas: 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 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 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 describe
        pool - the constant pool the instructions refer to
        Returns:
        a printed representation of the instructions in il
      • localVariablesToString

        public static 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 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 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 attribute
        pool - 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 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 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 attribute
        pool - 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 skipChecks is 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,
                                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 dump
        dumpDir - directory in which to write the file
        See Also:
        dump(JavaClass, File)
      • dump

        public static void dump​(org.apache.bcel.classfile.JavaClass jc,
                                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 dump
        dumpDir - 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 augmented
        newList - instructions to prepend to the method
      • getConstantString

        public static String getConstantString​(org.apache.bcel.classfile.ConstantPool pool,
                                               int index)
        Returns the constant string at the specified offset.
        Parameters:
        pool - the constant pool
        index - 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 String typeToClassgetname​(org.apache.bcel.generic.Type type)
        Returns the Java class name, in the format of Class.getName(), that corresponds to type.
        Parameters:
        type - the type
        Returns:
        the Java classname that corresponds to type
      • typeToClass

        public static 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 extend
        newType - 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 extend
        newType - the element to add to the beginning of the array
        Returns:
        a new array, with newType at the beginning
      • classnameToType

        @Deprecated
        public static org.apache.bcel.generic.Type classnameToType​(@BinaryNameOrPrimitiveType 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)
      • binaryNameToType

        public static org.apache.bcel.generic.Type binaryNameToType​(@BinaryNameOrPrimitiveType 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 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