Class BCELifier

All Implemented Interfaces:
Visitor

public class BCELifier extends EmptyVisitor
This class takes a given JavaClass object and converts it to a Java program that creates that very class using BCEL. This gives new users of BCEL a useful example showing how things are done with BCEL. It does not cover all features of BCEL, but tries to mimic hand-written code as close as possible.
  • Field Details

    • BASE_PACKAGE

      private static final String BASE_PACKAGE
    • CONSTANT_PREFIX

      private static final String CONSTANT_PREFIX
    • clazz

      private final JavaClass clazz
    • printWriter

      private final PrintWriter printWriter
    • constantPoolGen

      private final ConstantPoolGen constantPoolGen
  • Constructor Details

    • BCELifier

      public BCELifier(JavaClass clazz, OutputStream out)
      Constructs a new instance.
      Parameters:
      clazz - Java class to "decompile".
      out - where to print the Java program in UTF-8.
  • Method Details

    • checkJavaName

      private static String checkJavaName(String name)
      Checks that a name from the parsed class file is a dotted sequence of valid Java identifiers before it is emitted in identifier position of the generated source. The class file format allows characters in names (for example braces, parentheses or newlines) that the Java language does not, so an unchecked name from a crafted class file could inject arbitrary code into the generated program.
      Parameters:
      name - the class or package name to check.
      Returns:
      name if it is safe to emit as a Java identifier.
      Throws:
      IllegalArgumentException - Thrown if the name is not a dotted sequence of valid Java identifiers.
    • escape

      private static String[] escape(String[] names)
    • getJavaClass

      static JavaClass getJavaClass(String name) throws ClassNotFoundException, IOException
      Throws:
      ClassNotFoundException
      IOException
    • main

      public static void main(String[] argv) throws Exception
      Default main method.
      Parameters:
      argv - command line arguments.
      Throws:
      Exception - Thrown if an error occurs.
    • printArgumentTypes

      static String printArgumentTypes(Type[] argTypes)
    • printFlags

      static String printFlags(int flags)
    • printFlags

      public static String printFlags(int flags, BCELifier.FLAGS location)
      Return a string with the flag settings
      Parameters:
      flags - The flags field to interpret.
      location - The item type.
      Returns:
      The formatted string.
      Since:
      6.0 made public
    • printType

      static String printType(String signature)
    • printType

      static String printType(Type type)
    • printCreate

      private void printCreate()
    • printMain

      private void printMain()
    • start

      public void start()
      Start Java code generation
    • visitField

      public void visitField(Field field)
      Description copied from interface: Visitor
      Visits a Field.
      Specified by:
      visitField in interface Visitor
      Overrides:
      visitField in class EmptyVisitor
      Parameters:
      field - The field.
    • visitJavaClass

      public void visitJavaClass(JavaClass clazz)
      Description copied from interface: Visitor
      Visits a JavaClass.
      Specified by:
      visitJavaClass in interface Visitor
      Overrides:
      visitJavaClass in class EmptyVisitor
      Parameters:
      clazz - The class.
    • visitMethod

      public void visitMethod(Method method)
      Description copied from interface: Visitor
      Visits a Method.
      Specified by:
      visitMethod in interface Visitor
      Overrides:
      visitMethod in class EmptyVisitor
      Parameters:
      method - The method.
    • visitStackMap

      public void visitStackMap(StackMap stackMap)
      Description copied from interface: Visitor
      Visits a StackMap attribute.
      Specified by:
      visitStackMap in interface Visitor
      Overrides:
      visitStackMap in class EmptyVisitor
      Parameters:
      stackMap - The attribute.
    • visitStackMapEntry

      public void visitStackMapEntry(StackMapEntry stackMapEntry)
      Description copied from interface: Visitor
      Visits a StackMapEntry.
      Specified by:
      visitStackMapEntry in interface Visitor
      Overrides:
      visitStackMapEntry in class EmptyVisitor
      Parameters:
      stackMapEntry - The entry.
    • visitStackMapType

      public void visitStackMapType(StackMapType stackMapType)
      Visits a StackMapType object.
      Specified by:
      visitStackMapType in interface Visitor
      Overrides:
      visitStackMapType in class EmptyVisitor
      Parameters:
      stackMapType - object to visit.
      Since:
      6.7.1
    • visitStackMapTypeArray

      private void visitStackMapTypeArray(StackMapType[] types)