Class JCodeModel


  • public final class JCodeModel
    extends java.lang.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 Detail

      • packages

        private java.util.HashMap<java.lang.String,​JPackage> packages
        The packages that this JCodeWriter contains.
      • refClasses

        private final java.util.HashMap<java.lang.Class<?>,​JCodeModel.JReferencedClass> refClasses
        All JReferencedClasses are pooled here.
      • NULL

        public final JNullType NULL
        Obtains a reference to the special "null" type.
      • isCaseSensitiveFileSystem

        protected static final boolean isCaseSensitiveFileSystem
        If the flag is true, we will consider two classes "Foo" and "foo" as a collision.
      • primitiveToBox

        public static final java.util.Map<java.lang.Class<?>,​java.lang.Class<?>> primitiveToBox
        Conversion from primitive type Class (such as Integer.TYPE to its boxed type (such as Integer.class)
      • boxToPrimitive

        public static final java.util.Map<java.lang.Class<?>,​java.lang.Class<?>> boxToPrimitive
        The reverse look up for primitiveToBox
    • Constructor Detail

      • JCodeModel

        public JCodeModel()
    • Method Detail

      • getFileSystemCaseSensitivity

        private static boolean getFileSystemCaseSensitivity()
      • _package

        public JPackage _package​(java.lang.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
      • rootPackage

        public final JPackage rootPackage()
      • packages

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

        public JClass directClass​(java.lang.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.

      • _getClass

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

        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​(java.lang.Class<?> baseType)
      • build

        public void build​(java.io.File destDir,
                          java.io.PrintStream status)
                   throws java.io.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:
        java.io.IOException
      • build

        public void build​(java.io.File srcDir,
                          java.io.File resourceDir,
                          java.io.PrintStream status)
                   throws java.io.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:
        java.io.IOException
      • build

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

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

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

        public void build​(CodeWriter source,
                          CodeWriter resource)
                   throws java.io.IOException
        Generates Java source code.
        Throws:
        java.io.IOException
      • _ref

        public JType _ref​(java.lang.Class<?> c)
      • ref

        public JClass ref​(java.lang.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​(java.lang.String name)
                        throws java.lang.ClassNotFoundException
        Obtains a type object from a type name.

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

        Throws:
        java.lang.ClassNotFoundException - If the specified type is not found.