Class JCodeModel

java.lang.Object
com.sun.codemodel.JCodeModel

public final class JCodeModel extends Object
Root of the code DOM.

Here's your typical CodeModel application.


 JCodeModel cm = new JCodeModel();

 // generate source code by populating the 'cm' tree.
 cm._class(...);
 ...

 // write them out
 cm.build(new File("."));
 

Every CodeModel node is always owned by one JCodeModel object at any given time (which can be often accesesd by the owner() method.) As such, when you generate Java code, most of the operation works in a top-down fashion. For example, you create a class from JCodeModel, which gives you a JDefinedClass. Then you invoke a method on it to generate a new method, which gives you JMethod, and so on. There are a few exceptions to this, most notably building JExpressions, but generally you work with CodeModel in a top-down fashion. Because of this design, most of the CodeModel classes aren't directly instanciable.

Where to go from here?

Most of the time you'd want to populate new type definitions in a JCodeModel. See _class(String, ClassType).

  • Field Details

  • Constructor Details

    • JCodeModel

      public JCodeModel()
  • Method Details

    • _package

      public JPackage _package(String name)
      Add a package to the list of packages to be generated.
      Parameters:
      name - Name of the package. Use "" to indicate the root package.
      Returns:
      Newly generated package
    • _moduleInfo

      public JModule _moduleInfo(String name)
      Creates and returns Java module to be generated.
      Parameters:
      name - The Name of Java module.
      Returns:
      New Java module.
    • _getModuleInfo

      public JModule _getModuleInfo()
      Returns existing Java module to be generated.
      Returns:
      Java module or null if Java module was not created yet.
    • _prepareModuleInfo

      public void _prepareModuleInfo(String name, String... requires)
      Creates Java module instance and adds existing packages with classes to the Java module info. Used to initialize and build Java module instance with existing packages content.
      Parameters:
      name - The Name of Java module.
      requires - Requires directives to add.
      Throws:
      IllegalStateException - when Java module instance was not initialized.
    • _updateModuleInfo

      public void _updateModuleInfo(String... requires)
      Adds existing packages with classes to the Java module info. Java module instance must exist before calling this method. Used to update Java module instance with existing packages content after it was prepared on client side.
      Parameters:
      requires - Requires directives to add.
      Throws:
      IllegalStateException - when Java module instance was not initialized.
    • rootPackage

      public JPackage rootPackage()
    • packages

      public Iterator<JPackage> packages()
      Returns an iterator that walks the packages defined using this code writer.
    • _class

      public JDefinedClass _class(String fullyqualifiedName) throws JClassAlreadyExistsException
      Creates a new generated class.
      Throws:
      JClassAlreadyExistsException - When the specified class/interface was already created.
    • directClass

      public JClass directClass(String name)
      Creates a dummy, unknown JClass that represents a given name.

      This method is useful when the code generation needs to include the user-specified class that may or may not exist, and only thing known about it is a class name.

    • _class

      public JDefinedClass _class(int mods, String fullyqualifiedName, ClassType t) throws JClassAlreadyExistsException
      Creates a new generated class.
      Throws:
      JClassAlreadyExistsException - When the specified class/interface was already created.
    • _class

      public JDefinedClass _class(String fullyqualifiedName, ClassType t) throws JClassAlreadyExistsException
      Creates a new generated class.
      Throws:
      JClassAlreadyExistsException - When the specified class/interface was already created.
    • _getClass

      public JDefinedClass _getClass(String fullyQualifiedName)
      Gets a reference to the already created generated class.
      Returns:
      null If the class is not yet created.
      See Also:
    • newAnonymousClass

      @Deprecated public JDefinedClass newAnonymousClass(JClass baseType)
      Deprecated.
      The naming convention doesn't match the rest of the CodeModel. Use anonymousClass(JClass) instead.
      Creates a new anonymous class.
    • anonymousClass

      public JDefinedClass anonymousClass(JClass baseType)
      Creates a new anonymous class.
    • anonymousClass

      public JDefinedClass anonymousClass(Class<?> baseType)
    • build

      public void build(File destDir, PrintStream status) throws IOException
      Generates Java source code. A convenience method for build(destDir,destDir,System.out).
      Parameters:
      destDir - source files are generated into this directory.
      status - if non-null, progress indication will be sent to this stream.
      Throws:
      IOException
    • build

      public void build(File srcDir, File resourceDir, PrintStream status) throws IOException
      Generates Java source code. A convenience method that calls build(CodeWriter,CodeWriter).
      Parameters:
      srcDir - Java source files are generated into this directory.
      resourceDir - Other resource files are generated into this directory.
      status - if non-null, progress indication will be sent to this stream.
      Throws:
      IOException
    • build

      public void build(File destDir) throws IOException
      A convenience method for build(destDir,System.out).
      Throws:
      IOException
    • build

      public void build(File srcDir, File resourceDir) throws IOException
      A convenience method for build(srcDir,resourceDir,System.out).
      Throws:
      IOException
    • build

      public void build(CodeWriter out) throws IOException
      A convenience method for build(out,out).
      Throws:
      IOException
    • build

      public void build(CodeWriter source, CodeWriter resource) throws IOException
      Generates Java source code.
      Throws:
      IOException
    • countArtifacts

      public int countArtifacts()
      Returns the number of files to be generated if build(java.io.File, java.io.PrintStream) is invoked now.
    • addClassNameReplacer

      public void addClassNameReplacer(String c1, String c2)
      Specify class names or packages to be replaced when the model is dumped into files.
      Parameters:
      c1 - the regular expression to which class name or package will be replaced.
      c2 - the string to be substituted for the first match.
    • classNameReplacer

      public Map<String,String> classNameReplacer()
      Gives an unmodifiable copy of classNameReplacer
      Returns:
      classNameReplacer
    • ref

      public JClass ref(Class<?> clazz)
      Obtains a reference to an existing class from its Class object.

      The parameter may not be primitive.

      See Also:
    • _ref

      public JType _ref(Class<?> c)
    • ref

      public JClass ref(String fullyQualifiedClassName)
      Obtains a reference to an existing class from its fully-qualified class name.

      First, this method attempts to load the class of the given name. If that fails, we assume that the class is derived straight from Object, and return a JClass.

    • wildcard

      public JClass wildcard()
      Gets a JClass representation for "?", which is equivalent to "? extends Object".
    • parseType

      public JType parseType(String name)
      Obtains a type object from a type name.

      This method handles primitive types, arrays, and existing Classes.