org.mule.devkit.model.code
Class CodeModel

java.lang.Object
  extended by org.mule.devkit.model.code.CodeModel

public final class CodeModel
extends Object

Root of the code DOM.

Here's your typical CodeModel application.

 CodeModel cm = new CodeModel();

 // 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 CodeModel 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 CodeModel, which gives you a GeneratedClass. Then you invoke a method on it to generate a new method, which gives you GeneratedMethod, and so on.

There are a few exceptions to this, most notably building GeneratedExpressions, 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 CodeModel. See _class(String, GeneratedClassType).


Field Summary
 PrimitiveType BOOLEAN
           
static Map<Class<?>,Class<?>> boxToPrimitive
          The reverse look up for primitiveToBox
 PrimitiveType BYTE
           
 PrimitiveType CHAR
           
 PrimitiveType DOUBLE
           
 PrimitiveType FLOAT
           
 PrimitiveType INT
           
protected static boolean isCaseSensitiveFileSystem
          If the flag is true, we will consider two classes "Foo" and "foo" as a collision.
 PrimitiveType LONG
           
 NullType NULL
          Obtains a reference to the special "null" type.
static Map<Class<?>,Class<?>> primitiveToBox
          Conversion from primitive type Class (such as Integer.TYPE to its boxed type (such as Integer.class)
 PrimitiveType SHORT
           
 PrimitiveType VOID
           
 
Constructor Summary
CodeModel(CodeWriter codeWriter)
           
 
Method Summary
 GeneratedClass _class(int mods, String fullyqualifiedName, GeneratedClassType t)
          Creates a new generated class.
 GeneratedClass _class(String fullyqualifiedName)
          Creates a new generated class.
 GeneratedClass _class(String fullyqualifiedName, GeneratedClassType t)
          Creates a new generated class.
 GeneratedClass _getClass(String fullyQualifiedName)
          Gets a reference to the already created generated class.
 GeneratedPackage _package(String name)
          Add a package to the list of packages to be generated
 Type _ref(Class<?> c)
           
 GeneratedClass anonymousClass(Class<?> baseType)
           
 GeneratedClass anonymousClass(TypeReference baseType)
          Creates a new anonymous class.
 void build()
           
 int countArtifacts()
          Returns the number of files to be generated if build(java.io.File, java.io.PrintStream) is invoked now.
 TypeReference directClass(String name)
          Creates a dummy, unknown TypeReference that represents a given name.
 CodeWriter getCodeWriter()
          Get code writer
 OutputStream getRegistryBootstrapStream()
           
 GeneratedClass newAnonymousClass(TypeReference baseType)
          Deprecated. The naming convention doesn't match the rest of the CodeModel. Use anonymousClass(TypeReference) instead.
 Iterator<GeneratedPackage> packages()
          Returns an iterator that walks the packages defined using this code writer.
 Type parseType(String name)
          Obtains a type object from a type name.
 TypeReference ref(Class<?> clazz)
          Obtains a reference to an existing class from its Class object.
 Type ref(String fullyQualifiedClassName)
          Obtains a reference to an existing class from its fully-qualified class name.
 Type ref(TypeMirror typeMirror)
           
 GeneratedPackage rootPackage()
           
 TypeReference wildcard()
          Gets a TypeReference representation for "?", which is equivalent to "? extends Object".
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NULL

public final NullType NULL
Obtains a reference to the special "null" type.


VOID

public final PrimitiveType VOID

BOOLEAN

public final PrimitiveType BOOLEAN

BYTE

public final PrimitiveType BYTE

SHORT

public final PrimitiveType SHORT

CHAR

public final PrimitiveType CHAR

INT

public final PrimitiveType INT

FLOAT

public final PrimitiveType FLOAT

LONG

public final PrimitiveType LONG

DOUBLE

public final PrimitiveType DOUBLE

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 Map<Class<?>,Class<?>> primitiveToBox
Conversion from primitive type Class (such as Integer.TYPE to its boxed type (such as Integer.class)


boxToPrimitive

public static final Map<Class<?>,Class<?>> boxToPrimitive
The reverse look up for primitiveToBox

Constructor Detail

CodeModel

public CodeModel(CodeWriter codeWriter)
Method Detail

_package

public GeneratedPackage _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

rootPackage

public final GeneratedPackage rootPackage()

packages

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


_class

public GeneratedClass _class(String fullyqualifiedName)
                      throws ClassAlreadyExistsException
Creates a new generated class.

Throws:
ClassAlreadyExistsException - When the specified class/interface was already created.

directClass

public TypeReference directClass(String name)
Creates a dummy, unknown TypeReference 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 GeneratedClass _class(int mods,
                             String fullyqualifiedName,
                             GeneratedClassType t)
                      throws ClassAlreadyExistsException
Creates a new generated class.

Throws:
ClassAlreadyExistsException - When the specified class/interface was already created.

_class

public GeneratedClass _class(String fullyqualifiedName,
                             GeneratedClassType t)
                      throws ClassAlreadyExistsException
Creates a new generated class.

Throws:
ClassAlreadyExistsException - When the specified class/interface was already created.

_getClass

public GeneratedClass _getClass(String fullyQualifiedName)
Gets a reference to the already created generated class.

Returns:
null If the class is not yet created.
See Also:
GeneratedPackage._getClass(String)

newAnonymousClass

public GeneratedClass newAnonymousClass(TypeReference baseType)
Deprecated. The naming convention doesn't match the rest of the CodeModel. Use anonymousClass(TypeReference) instead.

Creates a new anonymous class.


anonymousClass

public GeneratedClass anonymousClass(TypeReference baseType)
Creates a new anonymous class.


anonymousClass

public GeneratedClass anonymousClass(Class<?> baseType)

build

public void build()
           throws IOException
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.


ref

public Type ref(TypeMirror typeMirror)

ref

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

The parameter may not be primitive.

See Also:
for the version that handles more cases.

_ref

public Type _ref(Class<?> c)

ref

public Type 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 TypeReference.


wildcard

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


parseType

public Type parseType(String name)
               throws ClassNotFoundException
Obtains a type object from a type name.

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

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

getRegistryBootstrapStream

public OutputStream getRegistryBootstrapStream()
                                        throws IOException
Throws:
IOException

getCodeWriter

public CodeWriter getCodeWriter()
Get code writer

Returns:
Code writer


Copyright © 2010-2013 MuleSoft, Inc.. All Rights Reserved.