Class ClassFileReader

java.lang.Object
org.codehaus.commons.compiler.sandbox.ClassFileReader

public final class ClassFileReader extends Object
A minimal parser for Java class files, as far as needed to determine which classes and members a class refers to.

Parses the constant pool (all tags defined up to Java 25), the name, superclass and interfaces of the class, the access flags, names and descriptors of its fields and methods, and the BootstrapMethods and SourceFile attributes. The bytecode of the methods (Code attributes) is scanned only as far as needed to determine the source lines (from the LineNumberTable attributes) where the constant pool entries are used; see getLineNumbers(MemberReference). All other attributes are skipped.

Class names are reported in the format of Class.getName(), i.e. with dots as package separators (e.g. "java.lang.String", "[Ljava.lang.String;"); field and method descriptors are reported verbatim (e.g. "(Ljava/lang/String;)V").

Invalid or truncated class files cause a ClassFormatError.

  • Constructor Details

    • ClassFileReader

      public ClassFileReader(byte[] classFile)
      Parses the given class file.
      Throws:
      ClassFormatError - The classFile is invalid or truncated
  • Method Details

    • getMajorVersion

      public int getMajorVersion()
    • getMinorVersion

      public int getMinorVersion()
    • getAccessFlags

      public int getAccessFlags()
      Returns:
      The access flags of the class, e.g. Modifier.INTERFACE
    • getClassName

      public String getClassName()
      Returns:
      The name of the class, e.g. "pkg.Outer$Inner"
    • getSuperclassName

      @Nullable public String getSuperclassName()
      Returns:
      The name of the superclass, or null iff this is the class file of java.lang.Object (or of a module descriptor)
    • getInterfaceNames

      public List<String> getInterfaceNames()
    • getFields

    • getMethods

    • getClassReferences

      public List<String> getClassReferences()
      Returns:
      The names of all classes, interfaces and array types referenced by CONSTANT_Class entries, including the class itself, its superclass and its interfaces
    • getMemberReferences

      public List<ClassFileReader.MemberReference> getMemberReferences()
      Returns:
      All CONSTANT_Fieldref, CONSTANT_Methodref and CONSTANT_InterfaceMethodref entries of the constant pool
    • getMethodHandles

      public List<ClassFileReader.MethodHandleReference> getMethodHandles()
      Returns:
      All CONSTANT_MethodHandle entries of the constant pool
    • getDynamicReferences

      public List<ClassFileReader.DynamicReference> getDynamicReferences()
      Returns:
      All CONSTANT_Dynamic and CONSTANT_InvokeDynamic entries of the constant pool
    • getBootstrapMethods

      public List<ClassFileReader.BootstrapMethod> getBootstrapMethods()
      Returns:
      The entries of the BootstrapMethods attribute; empty if the class file has no such attribute
    • getBootstrapMethod

      public ClassFileReader.BootstrapMethod getBootstrapMethod(ClassFileReader.DynamicReference dynamicReference)
      Returns:
      The bootstrap method of the given dynamicReference
    • getSourceFileName

      @Nullable public String getSourceFileName()
      Returns:
      The value of the SourceFile attribute (e.g. "Foo.java"), or null iff the class file has no such attribute
    • getLineNumbers

      public SortedSet<Integer> getLineNumbers(ClassFileReader.MemberReference memberReference)
      Returns the source lines of the instructions that use the given memberReference, i.e. of the field access and method invocation instructions that refer to it, and of the instructions that use a method handle of it, either directly (ldc), or as the bootstrap method or a bootstrap argument of an invokedynamic instruction or a dynamically-computed constant (e.g. a method reference like Runtime::getRuntime).
      Returns:
      The line numbers; empty iff the class file has no LineNumberTable attributes, or iff no instruction uses the memberReference
    • getLineNumbers

      public SortedSet<Integer> getLineNumbers(ClassFileReader.DynamicReference dynamicReference)
      Returns:
      The source lines of the invokedynamic and ldc instructions that use the given dynamicReference; empty iff the class file has no LineNumberTable attributes, or iff no instruction uses the dynamicReference