Class JClass

  • Direct Known Subclasses:
    JClass.JInnerClass

    public class JClass
    extends JStructure
    A representation of the Java Source code for a Java Class. This is a useful utility when creating in memory source code. This package was modelled after the Java Reflection API as much as possible to reduce the learning curve.
    Version:
    $Revision$ $Date$
    • Field Detail

      • _constructors

        private java.util.List<JConstructor> _constructors
        The list of constructors for this JClass
      • _fields

        private java.util.Map<java.lang.String,​JField> _fields
        The list of member variables (fields) of this JClass
      • _innerClasses

        private java.util.List<JClass> _innerClasses
      • _methods

        private java.util.List<JMethod> _methods
        The list of methods of this JClass
      • _superClass

        private java.lang.String _superClass
        The superclass for this JClass
      • _staticInitializer

        private JSourceCode _staticInitializer
        The source code for static initialization
      • sourceCodeEntries

        private java.util.List<java.lang.String> sourceCodeEntries
    • Constructor Detail

      • JClass

        public JClass​(java.lang.String name)
               throws java.lang.IllegalArgumentException
        Creates a new JClass with the given name
        Parameters:
        name - the name of the JClass to create
        Throws:
        java.lang.IllegalArgumentException - when the given name is not a valid Class name
    • Method Detail

      • addConstructor

        public void addConstructor​(JConstructor constructor)
                            throws java.lang.IllegalArgumentException
        Adds the given Constructor to this classes list of constructors. The constructor must have been created with this JClass' createConstructor.
        Parameters:
        constructor - a constructor
        Throws:
        java.lang.IllegalArgumentException - if constructor is null or invalid.
      • addField

        public void addField​(JField jField)
                      throws java.lang.IllegalArgumentException
        Adds the given JField to this JClass
        Specified by:
        addField in class JStructure
        Parameters:
        jField - , the JField to add
        Throws:
        java.lang.IllegalArgumentException - when the given JField has a name of an existing JField
      • addMember

        public void addMember​(JMember jMember)
                       throws java.lang.IllegalArgumentException
        Adds the given JMember to this JClass
        Specified by:
        addMember in class JStructure
        Parameters:
        jMember - , the JMember to add
        Throws:
        java.lang.IllegalArgumentException - when the given JMember has the same name of an existing JField or JMethod respectively, or if the JMember is of an unrecognized class.
      • addMethod

        public void addMethod​(JMethod jMethod)
        Adds the given JMethod to this JClass
        Parameters:
        jMethod - the JMethod to add
      • addMethod

        public void addMethod​(JMethod jMethod,
                              boolean importReturnType)
                       throws java.lang.IllegalArgumentException
        Adds the given JMethod to this JClass
        Parameters:
        jMethod - , the JMethod to add
        importReturnType - true if we add the importReturnType to the class import lists. It could be useful to set it to false when all types are fully qualified.
        Throws:
        java.lang.IllegalArgumentException - when the given JMethod has the same name of an existing JMethod.
      • addMethods

        public void addMethods​(JMethod[] jMethods)
                        throws java.lang.IllegalArgumentException
        Adds the given array of JMethods to this JClass
        Parameters:
        jMethods - , the JMethod[] to add
        Throws:
        java.lang.IllegalArgumentException - when any of the given JMethods has the same name of an existing JMethod.
      • createConstructor

        public JConstructor createConstructor()
        Creates a new JConstructor and adds it to this JClass.
        Returns:
        the newly created constructor
      • createConstructor

        public JConstructor createConstructor​(JParameter[] params)
        Creates a new JConstructor and adds it to this JClass.
        Parameters:
        params - the parameters
        Returns:
        the newly created constructor
      • createInnerClass

        public JClass createInnerClass​(java.lang.String localname)
        Creates and returns an inner-class for this JClass
        Parameters:
        localname - the name of the class (no package name)
        Returns:
        the new JClass
      • getConstructor

        public JConstructor getConstructor​(int index)
        Returns the constructor at the specified index.
        Parameters:
        index - the index of the constructor to return
        Returns:
        the JConstructor at the specified index.
      • getConstructors

        public JConstructor[] getConstructors()
        Returns the an array of the JConstructors contained within this JClass
        Returns:
        an array of JConstructor
      • getField

        public JField getField​(java.lang.String name)
        Returns the member with the given name, or null if no member was found with the given name
        Specified by:
        getField in class JStructure
        Parameters:
        name - the name of the member to return
        Returns:
        the member with the given name, or null if no member was found with the given name
      • getFields

        public JField[] getFields()
        Returns an array of all the JFields of this JClass
        Specified by:
        getFields in class JStructure
        Returns:
        an array of all the JFields of this JClass
      • getInnerClasses

        public JClass[] getInnerClasses()
        Returns an array of JClass (the inner classes) contained within this JClass.
        Returns:
        an array of JClass contained within this JClass
      • getMethods

        public JMethod[] getMethods()
        Returns an array of all the JMethods of this JClass
        Returns:
        an array of all the JMethods of this JClass
      • getMethod

        public JMethod getMethod​(java.lang.String name,
                                 int startIndex)
        Returns the first occurrence of the method with the given name, starting from the specified index.
        Parameters:
        name - the name of the method to look for
        startIndex - the starting index to begin the search
        Returns:
        the method if found, otherwise null.
      • getMethod

        public JMethod getMethod​(int index)
        Returns the JMethod located at the specified index
        Parameters:
        index - the index of the JMethod to return.
        Returns:
        the JMethod
      • getStaticInitializationCode

        public JSourceCode getStaticInitializationCode()
        Returns the JSourceCode for the static initializer of this JClass
        Returns:
        the JSourceCode for the static initializer of this JClass
      • getSuperClass

        public java.lang.String getSuperClass()
        Gets the super Class that this class extends
        Returns:
        superClass the super Class that this Class extends
      • print

        public void print​(JSourceWriter jsw)
        Prints the source code for this JClass to the given JSourceWriter
        Specified by:
        print in class JStructure
        Parameters:
        jsw - the JSourceWriter to print to. [May not be null]
      • print

        public void print​(JSourceWriter jsw,
                          boolean classOnly)
        Prints the source code for this JClass to the given JSourceWriter
        Parameters:
        jsw - the JSourceWriter to print to. [May not be null]
        classOnly - whether the header, package and imports should be printed too
      • addSourceCode

        public void addSourceCode​(java.lang.String sourceCode)
      • removeConstructor

        public boolean removeConstructor​(JConstructor constructor)
        Removes the given constructor from this JClass
        Parameters:
        constructor - the JConstructor to remove
        Returns:
        true if the constructor was removed, otherwise false.
      • removeField

        public JField removeField​(java.lang.String name)
        Removes the field with the given name from this JClass
        Parameters:
        name - the name of the field to remove
        Returns:
        the removed field
      • removeField

        public boolean removeField​(JField jField)
        Removes the given JField from this JClass
        Parameters:
        jField - , the JField to remove
        Returns:
        {true if and only if the field was successfully removed
      • removeInnerClass

        public boolean removeInnerClass​(JClass jClass)
        Removes the given inner-class (JClass) from this JClass.
        Parameters:
        jClass - the JClass (inner-class) to remove.
        Returns:
        true if the JClass was removed, otherwise false.
      • setSuperClass

        public void setSuperClass​(java.lang.String superClass)
        Sets the super Class that this class extends
        Parameters:
        superClass - the super Class that this Class extends