Class JavaClass

  • All Implemented Interfaces:
    java.lang.Cloneable, java.lang.Comparable<JavaClass>, Node

    public class JavaClass
    extends AccessFlags
    implements java.lang.Cloneable, Node, java.lang.Comparable<JavaClass>
    Represents a Java class, i.e., the data structures, constant pool, fields, methods and commands contained in a Java .class file. See JVM specification for details. The intent of this class is to represent a parsed or otherwise existing class file. Those interested in programatically generating classes should see the ClassGen class.
    See Also:
    ClassGen
    • Constructor Detail

      • JavaClass

        public JavaClass​(int classNameIndex,
                         int superclassNameIndex,
                         java.lang.String fileName,
                         int major,
                         int minor,
                         int access_flags,
                         ConstantPool constantPool,
                         int[] interfaces,
                         Field[] fields,
                         Method[] methods,
                         Attribute[] attributes,
                         byte source)
        Constructor gets all contents as arguments.
        Parameters:
        classNameIndex - Index into constant pool referencing a ConstantClass that represents this class.
        superclassNameIndex - Index into constant pool referencing a ConstantClass that represents this class's superclass.
        fileName - File name
        major - Major compiler version
        minor - Minor compiler version
        access_flags - Access rights defined by bit flags
        constantPool - Array of constants
        interfaces - Implemented interfaces
        fields - Class fields
        methods - Class methods
        attributes - Class attributes
        source - Read from file or generated in memory?
      • JavaClass

        public JavaClass​(int classNameIndex,
                         int superclassNameIndex,
                         java.lang.String fileName,
                         int major,
                         int minor,
                         int access_flags,
                         ConstantPool constantPool,
                         int[] interfaces,
                         Field[] fields,
                         Method[] methods,
                         Attribute[] attributes)
        Constructor gets all contents as arguments.
        Parameters:
        classNameIndex - Class name
        superclassNameIndex - Superclass name
        fileName - File name
        major - Major compiler version
        minor - Minor compiler version
        access_flags - Access rights defined by bit flags
        constantPool - Array of constants
        interfaces - Implemented interfaces
        fields - Class fields
        methods - Class methods
        attributes - Class attributes
    • Method Detail

      • accept

        public void accept​(Visitor v)
        Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
        Specified by:
        accept in interface Node
        Parameters:
        v - Visitor object
      • dump

        public void dump​(java.io.File file)
                  throws java.io.IOException
        Dump class to a file.
        Parameters:
        file - Output file
        Throws:
        java.io.IOException
      • dump

        public void dump​(java.lang.String _file_name)
                  throws java.io.IOException
        Dump class to a file named fileName.
        Parameters:
        _file_name - Output file name
        Throws:
        java.io.IOException
      • getBytes

        public byte[] getBytes()
        Returns:
        class in binary format
      • dump

        public void dump​(java.io.OutputStream file)
                  throws java.io.IOException
        Dump Java class to output stream in binary format.
        Parameters:
        file - Output stream
        Throws:
        java.io.IOException
      • dump

        public void dump​(java.io.DataOutputStream file)
                  throws java.io.IOException
        Dump Java class to output stream in binary format.
        Parameters:
        file - Output stream
        Throws:
        java.io.IOException
      • getAttributes

        public Attribute[] getAttributes()
        Returns:
        Attributes of the class.
      • getAnnotationEntries

        public AnnotationEntry[] getAnnotationEntries()
        Returns:
        Annotations on the class
        Since:
        6.0
      • getClassName

        public java.lang.String getClassName()
        Returns:
        Class name.
      • getPackageName

        public java.lang.String getPackageName()
        Returns:
        Package name.
      • getClassNameIndex

        public int getClassNameIndex()
        Returns:
        Class name index.
      • getConstantPool

        public ConstantPool getConstantPool()
        Returns:
        Constant pool.
      • getFields

        public Field[] getFields()
        Returns:
        Fields, i.e., variables of the class. Like the JVM spec mandates for the classfile format, these fields are those specific to this class, and not those of the superclass or superinterfaces.
      • getFileName

        public java.lang.String getFileName()
        Returns:
        File name of class, aka SourceFile attribute value
      • getInterfaceNames

        public java.lang.String[] getInterfaceNames()
        Returns:
        Names of implemented interfaces.
      • getInterfaceIndices

        public int[] getInterfaceIndices()
        Returns:
        Indices in constant pool of implemented interfaces.
      • getMajor

        public int getMajor()
        Returns:
        Major number of class file version.
      • getMethods

        public Method[] getMethods()
        Returns:
        Methods of the class.
      • getMethod

        public Method getMethod​(java.lang.reflect.Method m)
        Returns:
        A Method corresponding to java.lang.reflect.Method if any
      • getMinor

        public int getMinor()
        Returns:
        Minor number of class file version.
      • getSourceFileName

        public java.lang.String getSourceFileName()
        Returns:
        sbsolute path to file where this class was read from
      • getSuperclassName

        public java.lang.String getSuperclassName()
        returns the super class name of this class. In the case that this class is java.lang.Object, it will return itself (java.lang.Object). This is probably incorrect but isn't fixed at this time to not break existing clients.
        Returns:
        Superclass name.
      • getSuperclassNameIndex

        public int getSuperclassNameIndex()
        Returns:
        Class name index.
      • setAttributes

        public void setAttributes​(Attribute[] attributes)
        Parameters:
        attributes - .
      • setClassName

        public void setClassName​(java.lang.String className)
        Parameters:
        className - .
      • setClassNameIndex

        public void setClassNameIndex​(int classNameIndex)
        Parameters:
        classNameIndex - .
      • setConstantPool

        public void setConstantPool​(ConstantPool constantPool)
        Parameters:
        constantPool - .
      • setFields

        public void setFields​(Field[] fields)
        Parameters:
        fields - .
      • setFileName

        public void setFileName​(java.lang.String fileName)
        Set File name of class, aka SourceFile attribute value
      • setInterfaceNames

        public void setInterfaceNames​(java.lang.String[] interfaceNames)
        Parameters:
        interfaceNames - .
      • setInterfaces

        public void setInterfaces​(int[] interfaces)
        Parameters:
        interfaces - .
      • setMajor

        public void setMajor​(int major)
        Parameters:
        major - .
      • setMethods

        public void setMethods​(Method[] methods)
        Parameters:
        methods - .
      • setMinor

        public void setMinor​(int minor)
        Parameters:
        minor - .
      • setSourceFileName

        public void setSourceFileName​(java.lang.String sourceFileName)
        Set absolute path to file this class was read from.
      • setSuperclassName

        public void setSuperclassName​(java.lang.String superclassName)
        Parameters:
        superclassName - .
      • setSuperclassNameIndex

        public void setSuperclassNameIndex​(int superclassNameIndex)
        Parameters:
        superclassNameIndex - .
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
        Returns:
        String representing class contents.
      • copy

        public JavaClass copy()
        Returns:
        deep copy of this class
      • isSuper

        public final boolean isSuper()
      • isClass

        public final boolean isClass()
      • isAnonymous

        public final boolean isAnonymous()
        Since:
        6.0
      • isNested

        public final boolean isNested()
        Since:
        6.0
      • getSource

        public final byte getSource()
        Returns:
        returns either HEAP (generated), FILE, or ZIP
      • getRepository

        public Repository getRepository()
        Gets the ClassRepository which holds its definition. By default this is the same as SyntheticRepository.getInstance();
      • setRepository

        public void setRepository​(Repository repository)
        Sets the ClassRepository which loaded the JavaClass. Should be called immediately after parsing is done.
      • instanceOf

        public final boolean instanceOf​(JavaClass super_class)
                                 throws java.lang.ClassNotFoundException
        Equivalent to runtime "instanceof" operator.
        Returns:
        true if this JavaClass is derived from the super class
        Throws:
        java.lang.ClassNotFoundException - if superclasses or superinterfaces of this object can't be found
      • implementationOf

        public boolean implementationOf​(JavaClass inter)
                                 throws java.lang.ClassNotFoundException
        Returns:
        true, if this class is an implementation of interface inter
        Throws:
        java.lang.ClassNotFoundException - if superclasses or superinterfaces of this class can't be found
      • getSuperClass

        public JavaClass getSuperClass()
                                throws java.lang.ClassNotFoundException
        Returns:
        the superclass for this JavaClass object, or null if this is java.lang.Object
        Throws:
        java.lang.ClassNotFoundException - if the superclass can't be found
      • getSuperClasses

        public JavaClass[] getSuperClasses()
                                    throws java.lang.ClassNotFoundException
        Returns:
        list of super classes of this class in ascending order, i.e., java.lang.Object is always the last element
        Throws:
        java.lang.ClassNotFoundException - if any of the superclasses can't be found
      • getInterfaces

        public JavaClass[] getInterfaces()
                                  throws java.lang.ClassNotFoundException
        Get interfaces directly implemented by this JavaClass.
        Throws:
        java.lang.ClassNotFoundException
      • getAllInterfaces

        public JavaClass[] getAllInterfaces()
                                     throws java.lang.ClassNotFoundException
        Get all interfaces implemented by this JavaClass (transitively).
        Throws:
        java.lang.ClassNotFoundException
      • getComparator

        public static BCELComparator getComparator()
        Returns:
        Comparison strategy object
      • setComparator

        public static void setComparator​(BCELComparator comparator)
        Parameters:
        comparator - Comparison strategy object
      • equals

        public boolean equals​(java.lang.Object obj)
        Return value as defined by given BCELComparator strategy. By default two JavaClass objects are said to be equal when their class names are equal.
        Overrides:
        equals in class java.lang.Object
        See Also:
        Object.equals(java.lang.Object)
      • compareTo

        public int compareTo​(JavaClass obj)
        Return the natural ordering of two JavaClasses. This ordering is based on the class name
        Specified by:
        compareTo in interface java.lang.Comparable<JavaClass>
        Since:
        6.0
      • hashCode

        public int hashCode()
        Return value as defined by given BCELComparator strategy. By default return the hashcode of the class name.
        Overrides:
        hashCode in class java.lang.Object
        See Also:
        Object.hashCode()