java.lang.Object
org.codehaus.modello.plugin.java.javasource.JCompUnit

public class JCompUnit extends Object
A representation of the Java Source code for a Java compilation unit. 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 Summary

    Fields
    Modifier and Type
    Field
    Description
    private List<JClass>
    The set of top-level classes that live in this compilation unit.
    private static final String
    The Id for Source control systems I needed to separate this line to prevent CVS from expanding it here! ;-)
    private String
    The file to which this JCompUnit will be saved
    private JComment
     
    private List<JInterface>
    The set of top-level interfaces that live in this compilation unit.
    private String
    The package for this JCompUnit
  • Constructor Summary

    Constructors
    Constructor
    Description
    JCompUnit(String packageName, String fileName)
    Creates a new JCompUnit
    JCompUnit(JClass jClass)
    Creates a new JCompUnit with the given JClass (which must have been created with either a full class name or package/local name) as the public class.
    JCompUnit(JInterface jInterface)
    Creates a new JCompUnit with the given JInterface as public interface Package and file name are taken from jInterface.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addClass(JClass jClass)
    Adds a JClass which should be printed in this file.
    void
    Adds a JInterface which should be printed in this file.
    void
    Adds the given JStructure (either a JInterface or a JClass) to this JCompUnit.
    Returns the name of the file that this JCompUnit would be printed as, given a call to #print.
    returns a array of String containing all import classes/packages, also imports within the same package of this object.
    protected static String
     
    Returns the name of the package that this JCompUnit is a member of
    private void
     
    void
    Prints the source code for this JClass in the current directory with the default line seperator of the the runtime platform.
    void
    print(String destDir)
    Prints the source code for this JClass with the default line seperator of the the runtime platform.
    void
    print(String destDir, String lineSeparator)
    Prints the source code for this JCompUnit.
    void
    Prints the source code for this JClass.
    final void
    printStructures(JSourceWriter jsw, boolean printPublic)
    Print the source code for the contained JClass objects.
    private void
    Update the names of nested classes and interfaces.
    void
    Sets the header comment for this JCompUnit

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_HEADER

      private static final String DEFAULT_HEADER
      The Id for Source control systems I needed to separate this line to prevent CVS from expanding it here! ;-)
      See Also:
    • packageName

      private String packageName
      The package for this JCompUnit
    • fileName

      private String fileName
      The file to which this JCompUnit will be saved
    • classes

      private List<JClass> classes
      The set of top-level classes that live in this compilation unit.
    • interfaces

      private List<JInterface> interfaces
      The set of top-level interfaces that live in this compilation unit.
  • Constructor Details

    • JCompUnit

      public JCompUnit(String packageName, String fileName)
      Creates a new JCompUnit
      Parameters:
      packageName - the name of the package for this JCompUnit. If packageName is null or empty, no 'package' line will be generated.
      fileName - the name of the file in which this JCompUnit will be stored
    • JCompUnit

      public JCompUnit(JClass jClass)
      Creates a new JCompUnit with the given JClass (which must have been created with either a full class name or package/local name) as the public class. Package and file name are taken from jClass.
      Parameters:
      jClass - the public class for this JCompUnit.
    • JCompUnit

      public JCompUnit(JInterface jInterface)
      Creates a new JCompUnit with the given JInterface as public interface Package and file name are taken from jInterface.
      Parameters:
      jInterface - the public interface for this JCompUnit.
  • Method Details

    • init

      private void init()
    • addStructure

      public void addStructure(JStructure jStructure) throws IllegalArgumentException
      Adds the given JStructure (either a JInterface or a JClass) to this JCompUnit.
      Parameters:
      jStructure - the JStructure to add
      Throws:
      IllegalArgumentException - when the given JStructure has the same name of an existing JStructure or if the class of jStructure is unknown.
    • addClass

      public void addClass(JClass jClass)
      Adds a JClass which should be printed in this file.
      Parameters:
      jClass - the JClass to add
    • addInterface

      public void addInterface(JInterface jInterface)
      Adds a JInterface which should be printed in this file.
      Parameters:
      jInterface - the JInterface to add
    • getImports

      public SortedSet<String> getImports()
      returns a array of String containing all import classes/packages, also imports within the same package of this object.
      Returns:
      a array of String containing all import classes/packages, also imports within the same package of this object.
    • getFilename

      public String getFilename(String destDir)
      Returns the name of the file that this JCompUnit would be printed as, given a call to #print.
      Parameters:
      destDir - the destination directory. This may be null.
      Returns:
      the name of the file that this JCompUnit would be printed as, given a call to #print.
    • getPackageName

      public String getPackageName()
      Returns the name of the package that this JCompUnit is a member of
      Returns:
      the name of the package that this JCompUnit is a member of, or null if there is no current package name defined
    • getPackageFromClassName

      protected static String getPackageFromClassName(String className)
    • print

      public void print()
      Prints the source code for this JClass in the current directory with the default line seperator of the the runtime platform.
      See Also:
    • print

      public void print(String destDir)
      Prints the source code for this JClass with the default line seperator of the the runtime platform.
      Parameters:
      destDir - the destination directory to generate the file.
      See Also:
    • print

      public void print(String destDir, String lineSeparator)
      Prints the source code for this JCompUnit.
      Parameters:
      destDir - the destination directory to generate the file.
      lineSeparator - the line separator to use at the end of each line. If null, then the default line separator for the runtime platform will be used.
    • print

      public void print(JSourceWriter jsw)
      Prints the source code for this JClass.
      Parameters:
      jsw - the JSourceWriter to print to.
    • printStructures

      public final void printStructures(JSourceWriter jsw, boolean printPublic)
      Print the source code for the contained JClass objects.
      Parameters:
      jsw - the JSourceWriter to print to.
      printPublic - if true, print only public classes; if false, print only non-public classes.
    • setHeader

      public void setHeader(JComment comment)
      Sets the header comment for this JCompUnit
      Parameters:
      comment - the comment to display at the top of the source file when printed
    • resolveNames

      private void resolveNames()
      Update the names of nested classes and interfaces.