Class ClassFile

  • All Implemented Interfaces:
    AccessFlags

    public class ClassFile
    extends java.lang.Object
    implements AccessFlags
    Class representing a ClassFile structure.
    Version:
    1.0
    • Constructor Summary

      Constructors 
      Constructor Description
      ClassFile​(java.io.DataInputStream in)  
      ClassFile​(java.io.File classFile)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void debugPrint​(java.lang.String text)  
      int getAccessFlags()
      Returns the access flags for this class or interface.
      AttributeInfo getAttribute​(int index)
      Returns the specified attribute of this class file.
      int getAttributeCount()
      Returns the number of attributes of this class file.
      java.lang.String getClassName​(boolean fullyQualified)
      Returns the name of this class or interface.
      protected java.lang.String getClassNameFromConstantPool​(int cpIndex, boolean fullyQualified)
      Given an index into the constant pool of a ConstantClassInfo, this method returns the fully-qualified name of the class it points to.
      int getConstantPoolCount()
      Returns the size of the constant pool, plus 1.
      ConstantPoolInfo getConstantPoolInfo​(int index)
      Returns the constant pool entry at the specified index.
      int getFieldCount()
      Returns the number of fields declared in this class file.
      FieldInfo getFieldInfo​(int index)
      Returns the specified field's information.
      FieldInfo getFieldInfoByName​(java.lang.String name)
      Returns a field's information by name.
      int getImplementedInterfaceCount()
      Returns the number of interfaces this class or interface implements.
      java.lang.String getImplementedInterfaceName​(int index, boolean fullyQualified)
      Returns the specified interface implemented by this class or interface.
      int getMethodCount()
      Returns the number of methods defined/declared in this class or interface.
      MethodInfo getMethodInfo​(int index)
      Returns information about the specified method defined/declared in this class file.
      java.util.List<MethodInfo> getMethodInfoByName​(java.lang.String name)
      Returns all method overloads with the specified name.
      java.util.List<MethodInfo> getMethodInfoByName​(java.lang.String name, int argCount)
      Returns all method overloads with the specified name and number of arguments.
      java.lang.String getPackageName()
      Returns the package for this class or interface.
      java.util.List<java.lang.String> getParamTypes()  
      java.lang.String getSuperClassName​(boolean fullyQualified)
      Returns the fully-qualified name of the superclass of this class or interface.
      java.lang.String getTypeArgument​(java.lang.String typeParam)
      Returns the currently set type argument for the specified type parameter.
      java.lang.String getUtf8ValueFromConstantPool​(int index)
      Returns the string value represented by a ConstantUtf8Info entry in the constant pool.
      java.lang.String getVersionString()
      Returns the version number of this class, as a string.
      private void init​(java.io.DataInputStream in)
      Parses the class file from a given input stream.
      boolean isDeprecated()
      Returns whether this class is deprecated.
      private void readAccessFlags​(java.io.DataInputStream in)
      Reads this class or interface's access flags.
      private AttributeInfo readAttribute​(java.io.DataInputStream in)
      Reads a single attribute of this class file.
      private void readAttributes​(java.io.DataInputStream in)
      Reads this class file's attributes.
      private void readConstantPoolInfos​(java.io.DataInputStream in)
      Reads the constant pool.
      private void readFields​(java.io.DataInputStream in)
      Reads the "fields" information.
      private void readHeader​(java.io.DataInputStream in)
      Reads the 0xCAFEBABE class file header.
      private void readInterfaces​(java.io.DataInputStream in)
      Reads the array of indices into the constant pool for the names of the interfaces implemented by this class or interface.
      private void readMethods​(java.io.DataInputStream in)  
      private void readSuperClass​(java.io.DataInputStream in)  
      private void readThisClass​(java.io.DataInputStream in)  
      private void readVersion​(java.io.DataInputStream in)
      Reads the class file's major and minor version numbers.
      void setTypeParamsToTypeArgs​(java.util.Map<java.lang.String,​java.lang.String> typeMap)
      Sets a mapping of type parameters of this class to type arguments for a particular instance of this class.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • minorVersion

        private int minorVersion
        The class file's minor version number.
      • majorVersion

        private int majorVersion
        The class file's major version number.
      • accessFlags

        private int accessFlags
        Permissions and properties of this class or interface.
      • thisClass

        private int thisClass
        Index into constantPool for a ConstantClassInfo structure representing the class or interface defined in this class file.
      • superClass

        private int superClass
        Index into constantPool for a ConstantClassInfo structure representing the superclass of this class or interface. If this value is 0 then this class must be class java.lang.Object. If this is an interface, then this index must point to information about class java.lang.Object.
      • interfaces

        int[] interfaces
        Indices into constantPool for ConstantClassInfos representing the implemented interfaces of this class or interface.
      • fields

        private FieldInfo[] fields
        Structures giving complete descriptions of the fields in this class or interface.
      • methods

        private MethodInfo[] methods
        Structures giving complete descriptions of the methods in this class or interface.
      • deprecated

        private boolean deprecated
        Whether this class is deprecated.
      • attributes

        private AttributeInfo[] attributes
        Attributes of this class or interface.
      • paramTypes

        private java.util.List<java.lang.String> paramTypes
        Parameter types, such as "String" in List<String>.
      • typeMap

        private java.util.Map<java.lang.String,​java.lang.String> typeMap
        A mapping of type parameters to type arguments. This is set via setTypeParamsToTypeArgs(Map) during code completion of members of an instance variable whose type is represented by this class file. This ClassFile doesn't use this field itself; rather, it's there for consumers (such as the Java code completion API) to use.
      • ENCLOSING_METHOD

        public static final java.lang.String ENCLOSING_METHOD
        See Also:
        Constant Field Values
      • RUNTIME_VISIBLE_ANNOTATIONS

        public static final java.lang.String RUNTIME_VISIBLE_ANNOTATIONS
        See Also:
        Constant Field Values
      • BOOTSTRAP_METHODS

        public static final java.lang.String BOOTSTRAP_METHODS
        See Also:
        Constant Field Values
      • HEADER

        private static final byte[] HEADER
        The 4-byte class file header, "CAFEBABE".
    • Constructor Detail

      • ClassFile

        public ClassFile​(java.io.File classFile)
                  throws java.io.IOException
        Throws:
        java.io.IOException
      • ClassFile

        public ClassFile​(java.io.DataInputStream in)
                  throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • debugPrint

        private void debugPrint​(java.lang.String text)
      • getAccessFlags

        public int getAccessFlags()
        Returns the access flags for this class or interface.
        Returns:
        The access flags, as a bit field.
        See Also:
        AccessFlags
      • getAttribute

        public AttributeInfo getAttribute​(int index)
        Returns the specified attribute of this class file.
        Parameters:
        index - The index of the attribute.
        Returns:
        The attribute.
        See Also:
        getAttributeCount()
      • getAttributeCount

        public int getAttributeCount()
        Returns the number of attributes of this class file.
        Returns:
        The number of attributes.
        See Also:
        getAttribute(int)
      • getClassName

        public java.lang.String getClassName​(boolean fullyQualified)
        Returns the name of this class or interface.
        Parameters:
        fullyQualified - Whether the name should be fully-qualified.
        Returns:
        The name of this class or interface.
        See Also:
        getSuperClassName(boolean)
      • getClassNameFromConstantPool

        protected java.lang.String getClassNameFromConstantPool​(int cpIndex,
                                                                boolean fullyQualified)
        Given an index into the constant pool of a ConstantClassInfo, this method returns the fully-qualified name of the class it points to.
        Parameters:
        cpIndex - The index into the constant pool. Note that this value is 1-based.
        fullyQualified - Whether the returned class name should be fully qualified.
        Returns:
        The fully-qualified class or interface name.
      • getConstantPoolCount

        public int getConstantPoolCount()
        Returns the size of the constant pool, plus 1.
        Returns:
        The size of the constant pool, plus 1.
        See Also:
        getConstantPoolInfo(int)
      • getConstantPoolInfo

        public ConstantPoolInfo getConstantPoolInfo​(int index)
        Returns the constant pool entry at the specified index. Note that constant pool entries are 1-based (that is, valid indices are 1 - getConstantPoolCount()-1).
        Parameters:
        index - The index into the constant pool to retrieve.
        Returns:
        The constant pool entry, or null if index is 0 (e.g. this ClassFile object represents java.lang.Object).
        See Also:
        getConstantPoolCount()
      • getFieldCount

        public int getFieldCount()
        Returns the number of fields declared in this class file.
        Returns:
        The number of fields.
        See Also:
        getFieldInfo(int)
      • getFieldInfoByName

        public FieldInfo getFieldInfoByName​(java.lang.String name)
        Returns a field's information by name.
        Parameters:
        name - The name of the field.
        Returns:
        The field's information.
        See Also:
        getFieldCount(), getFieldInfo(int)
      • getImplementedInterfaceCount

        public int getImplementedInterfaceCount()
        Returns the number of interfaces this class or interface implements.
        Returns:
        The number of implemented interfaces.
        See Also:
        getImplementedInterfaceName(int, boolean)
      • getImplementedInterfaceName

        public java.lang.String getImplementedInterfaceName​(int index,
                                                            boolean fullyQualified)
        Returns the specified interface implemented by this class or interface.
        Parameters:
        index - The index of the interface.
        fullyQualified - Whether the returned interface name should be fully qualified.
        Returns:
        The interface name.
        See Also:
        getImplementedInterfaceCount()
      • getMethodCount

        public int getMethodCount()
        Returns the number of methods defined/declared in this class or interface.
        Returns:
        The number of methods.
        See Also:
        getMethodInfo(int)
      • getMethodInfo

        public MethodInfo getMethodInfo​(int index)
        Returns information about the specified method defined/declared in this class file.
        Parameters:
        index - The index of the method.
        Returns:
        Information about the method.
        See Also:
        getMethodCount()
      • getMethodInfoByName

        public java.util.List<MethodInfo> getMethodInfoByName​(java.lang.String name)
        Returns all method overloads with the specified name.
        Parameters:
        name - The method name.
        Returns:
        Any method overloads with the given name, or null if none. This is a list of MethodInfos.
        See Also:
        getMethodInfoByName(String, int)
      • getMethodInfoByName

        public java.util.List<MethodInfo> getMethodInfoByName​(java.lang.String name,
                                                              int argCount)
        Returns all method overloads with the specified name and number of arguments.
        Parameters:
        name - The method name.
        argCount - The number of arguments. If this is less than zero, all overloads will be returned, regardless of argument count.
        Returns:
        Any method overloads with the given name and argument count, or null if none. This is a list of MethodInfos.
        See Also:
        getMethodInfoByName(String)
      • getPackageName

        public java.lang.String getPackageName()
        Returns the package for this class or interface.
        Returns:
        The package, or null if this class or interface is not in a package.
        See Also:
        getClassName(boolean)
      • getParamTypes

        public java.util.List<java.lang.String> getParamTypes()
      • getSuperClassName

        public java.lang.String getSuperClassName​(boolean fullyQualified)
        Returns the fully-qualified name of the superclass of this class or interface.
        Parameters:
        fullyQualified - Whether the returned value should be fully qualified.
        Returns:
        The name of the superclass of this class or interface. If this is an interface, then "java.lang.Object" is returned. If this class file represents java.lang.Object, then null is returned.
        See Also:
        getClassName(boolean)
      • getTypeArgument

        public java.lang.String getTypeArgument​(java.lang.String typeParam)
        Returns the currently set type argument for the specified type parameter.
        Parameters:
        typeParam - The type parameter.
        Returns:
        The type argument, or "Object" if no type parameters have been set. This is because, if the user types, say, "java.util.List list;" in Java 5+, the type defaults to Object. The code completion API may set the type argument mapping to null if no type arguments are scanned, thus we need to return Object in this case.
        See Also:
        setTypeParamsToTypeArgs(Map)
      • getUtf8ValueFromConstantPool

        public java.lang.String getUtf8ValueFromConstantPool​(int index)
        Returns the string value represented by a ConstantUtf8Info entry in the constant pool.
        Parameters:
        index - The index into the constant pool of a ConstantUtf8Info structure. This should be 1-based.
        Returns:
        The string represented.
      • getVersionString

        public java.lang.String getVersionString()
        Returns the version number of this class, as a string.
        Returns:
        The class's version number, in the form major.minor.
      • init

        private void init​(java.io.DataInputStream in)
                   throws java.io.IOException
        Parses the class file from a given input stream.
        Parameters:
        in - The input stream to read from.
        Throws:
        java.io.IOException - If an error occurs reading the class file.
      • isDeprecated

        public boolean isDeprecated()
        Returns whether this class is deprecated.
        Returns:
        Whether this class is deprecated.
      • readAccessFlags

        private void readAccessFlags​(java.io.DataInputStream in)
                              throws java.io.IOException
        Reads this class or interface's access flags.
        Parameters:
        in - The input stream to read from.
        Throws:
        java.io.IOException - If an error occurs reading the access flags.
      • readAttribute

        private AttributeInfo readAttribute​(java.io.DataInputStream in)
                                     throws java.io.IOException
        Reads a single attribute of this class file.
        Parameters:
        in - The input stream to read from.
        Returns:
        The attribute.
        Throws:
        java.io.IOException - If an IO error occurs.
      • readAttributes

        private void readAttributes​(java.io.DataInputStream in)
                             throws java.io.IOException
        Reads this class file's attributes.
        Parameters:
        in - The input stream to read from.
        Throws:
        java.io.IOException - If an IO error occurs.
      • readConstantPoolInfos

        private void readConstantPoolInfos​(java.io.DataInputStream in)
                                    throws java.io.IOException
        Reads the constant pool.
        Parameters:
        in - The input stream to read from.
        Throws:
        java.io.IOException - If an IO error occurs.
      • readFields

        private void readFields​(java.io.DataInputStream in)
                         throws java.io.IOException
        Reads the "fields" information.
        Parameters:
        in - The input stream to read from.
        Throws:
        java.io.IOException - If an IO error occurs.
      • readHeader

        private void readHeader​(java.io.DataInputStream in)
                         throws java.io.IOException
        Reads the 0xCAFEBABE class file header.
        Parameters:
        in - The input stream to read from.
        Throws:
        java.io.IOException - If the header is invalid.
      • readInterfaces

        private void readInterfaces​(java.io.DataInputStream in)
                             throws java.io.IOException
        Reads the array of indices into the constant pool for the names of the interfaces implemented by this class or interface.
        Parameters:
        in - The input stream to read from.
        Throws:
        java.io.IOException - If an IO error occurs reading the input stream.
      • readMethods

        private void readMethods​(java.io.DataInputStream in)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • readSuperClass

        private void readSuperClass​(java.io.DataInputStream in)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • readThisClass

        private void readThisClass​(java.io.DataInputStream in)
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • readVersion

        private void readVersion​(java.io.DataInputStream in)
                          throws java.io.IOException
        Reads the class file's major and minor version numbers.
        Parameters:
        in - The input stream to read from.
        Throws:
        java.io.IOException - If the version numbers are invalid.
      • setTypeParamsToTypeArgs

        public void setTypeParamsToTypeArgs​(java.util.Map<java.lang.String,​java.lang.String> typeMap)
        Sets a mapping of type parameters of this class to type arguments for a particular instance of this class. Note that ClassFile does not directly use this field; it is there for code completion API's to use to extract the necessary types of arguments, return values, etc., of methods (see the MethodInfo class).
        Parameters:
        typeMap - A mapping of type parameters to type arguments (both Strings).
        See Also:
        getTypeArgument(String)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object