Top Description Inners Fields Constructors Methods
com.sun.codemodel

public final Class JCodeModel

extends Object
Class Inheritance
Imports
java.io.File, .IOException, .PrintStream, java.lang.reflect.Modifier, java.util.ArrayList, .Collections, .HashMap, .Iterator, .LinkedList, .List, .Map, com.sun.codemodel.writer.FileCodeWriter, .ProgressCodeWriter

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).

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
JCodeModel.JReferencedClass

References to existing classes.

private static class
private class
private class

Field Summary

Modifier and TypeField and Description
public final JPrimitiveType
public static final Map<Class<?>, Class<?>>
boxToPrimitive

The reverse look up for primitiveToBox

public final JPrimitiveType
public final JPrimitiveType
private final Map<String, String>
public final JPrimitiveType
public final JPrimitiveType
public final JPrimitiveType
pack-priv static final boolean
isCaseSensitiveFileSystem

If the flag is true, we will consider two classes "Foo" and "foo" as a collision.

public final JPrimitiveType
private JModule
module

Java module in module-info.java file.

public final JNullType
NULL

Obtains a reference to the special "null" type.

private final HashMap<String, JPackage>
packages

The packages that this JCodeWriter contains.

public static final Map<Class<?>, Class<?>>
primitiveToBox

Conversion from primitive type Class (such as Integer#TYPE to its boxed type (such as Integer.class)

private final HashMap<Class<?>, JCodeModel.JReferencedClass>
refClasses

All JReferencedClasses are pooled here.

public final JPrimitiveType
public final JPrimitiveType
private JClass
wildcard

Cached for wildcard().

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public JDefinedClass
_class(String fullyqualifiedName)

Creates a new generated class.

public JDefinedClass
_class(int mods, String fullyqualifiedName, ClassType t)

Creates a new generated class.

public JDefinedClass
_class(String fullyqualifiedName, ClassType t)

Creates a new generated class.

public JDefinedClass

Returns:

null If the class is not yet created.
_getClass
(String fullyQualifiedName)

Gets a reference to the already created generated class.

public JModule

Returns:

Java module or null if Java module was not created yet.
_getModuleInfo
()

Returns existing Java module to be generated.

public JModule

Returns:

New Java module.
_moduleInfo
(final String
The Name of Java module.
name
)

Creates and returns Java module to be generated.

public JPackage

Returns:

Newly generated package
_package
(String
Name of the package. Use "" to indicate the root package.
name
)

Add a package to the list of packages to be generated.

public void
_prepareModuleInfo(final String
The Name of Java module.
name
,
final String...
Requires directives to add.
requires
)

Creates Java module instance and adds existing packages with classes to the Java module info.

public JType
_ref(Class<?> c)

public void
_updateModuleInfo(final String...
Requires directives to add.
requires
)

Adds existing packages with classes to the Java module info.

public void
addClassNameReplacer(String
the regular expression to which class name or package will be replaced.
c1
,
String
the string to be substituted for the first match.
c2
)

Specify class names or packages to be replaced when the model is dumped into files.

public JDefinedClass
anonymousClass(JClass baseType)

Creates a new anonymous class.

public JDefinedClass
anonymousClass(Class<?> baseType)

public void
build(File
source files are generated into this directory.
destDir
,
PrintStream
if non-null, progress indication will be sent to this stream.
status
)

Generates Java source code.

public void
build(File
Java source files are generated into this directory.
srcDir
,
File
Other resource files are generated into this directory.
resourceDir
,
PrintStream
if non-null, progress indication will be sent to this stream.
status
)

Generates Java source code.

public void
build(File destDir)

A convenience method for build(destDir,System.out).

public void
build(File srcDir, File resourceDir)

A convenience method for build(srcDir,resourceDir,System.out).

public void
build(CodeWriter out)

A convenience method for build(out,out).

public void
build(CodeWriter source, CodeWriter resource)

Generates Java source code.

public Map<String, String>

Returns:

classNameReplacer
classNameReplacer
()

Gives an unmodifiable copy of classNameReplacer

public int
countArtifacts()

Returns the number of files to be generated if build is invoked now.

public JClass
directClass(String name)

Creates a dummy, unknown JClass that represents a given name.

private static boolean
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.

public Iterator<JPackage>
packages()

Returns an iterator that walks the packages defined using this code writer.

public JType
parseType(String name)

Obtains a type object from a type name.

public JClass
ref(Class<?> clazz)

Obtains a reference to an existing class from its Class object.

public JClass
ref(String fullyQualifiedClassName)

Obtains a reference to an existing class from its fully-qualified class name.

public JPackage
public JClass
wildcard()

Gets a JClass representation for "?", which is equivalent to "?

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BOOLEANback to summary
public final JPrimitiveType BOOLEAN
boxToPrimitiveback to summary
public static final Map<Class<?>, Class<?>> boxToPrimitive

The reverse look up for primitiveToBox

BYTEback to summary
public final JPrimitiveType BYTE
CHARback to summary
public final JPrimitiveType CHAR
classNameReplacerback to summary
private final Map<String, String> classNameReplacer
DOUBLEback to summary
public final JPrimitiveType DOUBLE
FLOATback to summary
public final JPrimitiveType FLOAT
INTback to summary
public final JPrimitiveType INT
isCaseSensitiveFileSystemback to summary
pack-priv static final boolean isCaseSensitiveFileSystem

If the flag is true, we will consider two classes "Foo" and "foo" as a collision.

LONGback to summary
public final JPrimitiveType LONG
moduleback to summary
private JModule module

Java module in module-info.java file.

NULLback to summary
public final JNullType NULL

Obtains a reference to the special "null" type.

packagesback to summary
private final HashMap<String, JPackage> packages

The packages that this JCodeWriter contains.

primitiveToBoxback to summary
public static final Map<Class<?>, Class<?>> primitiveToBox

Conversion from primitive type Class (such as Integer#TYPE to its boxed type (such as Integer.class)

refClassesback to summary
private final HashMap<Class<?>, JCodeModel.JReferencedClass> refClasses

All JReferencedClasses are pooled here.

SHORTback to summary
public final JPrimitiveType SHORT
VOIDback to summary
public final JPrimitiveType VOID
wildcardback to summary
private JClass wildcard

Cached for wildcard().

Constructor Detail

JCodeModelback to summary
public JCodeModel()

Method Detail

_classback to summary
public JDefinedClass _class(String fullyqualifiedName) throws JClassAlreadyExistsException

Creates a new generated class.

Exceptions
JClassAlreadyExistsException:
When the specified class/interface was already created.
_classback to summary
public JDefinedClass _class(int mods, String fullyqualifiedName, ClassType t) throws JClassAlreadyExistsException

Creates a new generated class.

Exceptions
JClassAlreadyExistsException:
When the specified class/interface was already created.
_classback to summary
public JDefinedClass _class(String fullyqualifiedName, ClassType t) throws JClassAlreadyExistsException

Creates a new generated class.

Exceptions
JClassAlreadyExistsException:
When the specified class/interface was already created.
_getClassback to summary
public JDefinedClass _getClass(String fullyQualifiedName)

Gets a reference to the already created generated class.

Returns:JDefinedClass

null If the class is not yet created.

See Also
JPackage#_getClass(String)
_getModuleInfoback to summary
public JModule _getModuleInfo()

Returns existing Java module to be generated.

Returns:JModule

Java module or null if Java module was not created yet.

_moduleInfoback to summary
public JModule _moduleInfo(final String name)

Creates and returns Java module to be generated.

Parameters
name:String

The Name of Java module.

Returns:JModule

New Java module.

_packageback to summary
public JPackage _package(String name)

Add a package to the list of packages to be generated.

Parameters
name:String

Name of the package. Use "" to indicate the root package.

Returns:JPackage

Newly generated package

_prepareModuleInfoback to summary
public void _prepareModuleInfo(final String name, final 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:String

The Name of Java module.

requires:String[]

Requires directives to add.

Exceptions
IllegalStateException:
when Java module instance was not initialized.
_refback to summary
public JType _ref(Class<?> c)
_updateModuleInfoback to summary
public void _updateModuleInfo(final 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:String[]

Requires directives to add.

Exceptions
IllegalStateException:
when Java module instance was not initialized.
addClassNameReplacerback to summary
public void addClassNameReplacer(String c1, String c2)

Specify class names or packages to be replaced when the model is dumped into files.

Parameters
c1:String

the regular expression to which class name or package will be replaced.

c2:String

the string to be substituted for the first match.

anonymousClassback to summary
public JDefinedClass anonymousClass(JClass baseType)

Creates a new anonymous class.

anonymousClassback to summary
public JDefinedClass anonymousClass(Class<?> baseType)
buildback to summary
public void build(File destDir, PrintStream status) throws IOException

Generates Java source code. A convenience method for build(destDir,destDir,System.out).

Parameters
destDir:File

source files are generated into this directory.

status:PrintStream

if non-null, progress indication will be sent to this stream.

buildback to summary
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:File

Java source files are generated into this directory.

resourceDir:File

Other resource files are generated into this directory.

status:PrintStream

if non-null, progress indication will be sent to this stream.

buildback to summary
public void build(File destDir) throws IOException

A convenience method for build(destDir,System.out).

buildback to summary
public void build(File srcDir, File resourceDir) throws IOException

A convenience method for build(srcDir,resourceDir,System.out).

buildback to summary
public void build(CodeWriter out) throws IOException

A convenience method for build(out,out).

buildback to summary
public void build(CodeWriter source, CodeWriter resource) throws IOException

Generates Java source code.

classNameReplacerback to summary
public Map<String, String> classNameReplacer()

Gives an unmodifiable copy of classNameReplacer

Returns:Map<String, String>

classNameReplacer

countArtifactsback to summary
public int countArtifacts()

Returns the number of files to be generated if build is invoked now.

directClassback to summary
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.

getFileSystemCaseSensitivityback to summary
private static boolean getFileSystemCaseSensitivity()
newAnonymousClassback to summary
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.

Annotations
@Deprecated
packagesback to summary
public Iterator<JPackage> packages()

Returns an iterator that walks the packages defined using this code writer.

parseTypeback to summary
public JType parseType(String name)

Obtains a type object from a type name.

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

refback to summary
public JClass ref(Class<?> clazz)

Obtains a reference to an existing class from its Class object.

The parameter may not be primitive.

See Also
{@link #_ref(Class)} for the version that handles more cases.
refback to summary
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.

rootPackageback to summary
public JPackage rootPackage()
wildcardback to summary
public JClass wildcard()

Gets a JClass representation for "?", which is equivalent to "? extends Object".

com.sun.codemodel back to summary

private Class JCodeModel.JReferencedClass

extends JClass
implements JDeclaration
Class Inheritance
All Implemented Interfaces
com.sun.codemodel.JDeclaration

References to existing classes.

JReferencedClass is kept in a pool so that they are shared. There is one pool for each JCodeModel object.

It is impossible to cache JReferencedClass globally only because there is the _package() method, which obtains the owner JPackage object, which is scoped to JCodeModel.

Field Summary

Modifier and TypeField and Description
private final Class<?>
Inherited from com.sun.codemodel.JClass:
EMPTY_ARRAY

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public JClass
_extends()

Implements abstract com.sun.codemodel.JClass._extends.

Gets the super class of this class.

public Iterator<JClass>
_implements()

Implements abstract com.sun.codemodel.JClass._implements.

Iterates all super interfaces directly implemented by this class/interface.

public JPackage
_package()

Implements abstract com.sun.codemodel.JClass._package.

Gets the package to which this class belongs.

public String
binaryName()

Overrides com.sun.codemodel.JType.binaryName.

Gets the binary name of the type.

public void
public String
fullName()

Implements abstract com.sun.codemodel.JType.fullName.

Gets the full name of the type.

public JPrimitiveType
getPrimitiveType()

Overrides com.sun.codemodel.JClass.getPrimitiveType.

If this class represents one of the wrapper classes defined in the java.lang package, return the corresponding primitive type.

public boolean
isAbstract()

Implements abstract com.sun.codemodel.JClass.isAbstract.

Checks if this class is an abstract class.

public boolean
isArray()

Overrides com.sun.codemodel.JType.isArray.

Tell whether or not this is an array type.

public boolean
isInterface()

Implements abstract com.sun.codemodel.JClass.isInterface.

Checks if this object represents an interface.

public String
name()

Implements abstract com.sun.codemodel.JClass.name.

Gets the name of this class.

public JClass
outer()

Overrides com.sun.codemodel.JClass.outer.

Returns the class in which this class is nested, or null if this is a top-level class.

protected JClass
substituteParams(JTypeVar[] variables, List<JClass> bindings)

Implements abstract com.sun.codemodel.JClass.substituteParams.

Substitutes the type variables with their actual arguments.

public JTypeVar[]
typeParams()

Overrides com.sun.codemodel.JClass.typeParams.

Iterates all the type parameters of this class/interface.

Inherited from com.sun.codemodel.JClass:
arrayboxifydotclasserasuregenerategetBaseClassgetBaseClassgetTypeParametersisAssignableFromisParameterizednarrownarrownarrownarrownarrownarrowownerprintLinkstaticInvokestaticInvokestaticRefstaticReftoStringunboxifywildcard

Field Detail

_classback to summary
private final Class<?> _class

Constructor Detail

JReferencedClassback to summary
pack-priv JReferencedClass(Class<?> _clazz)

Method Detail

_extendsback to summary
public JClass _extends()

Implements abstract com.sun.codemodel.JClass._extends.

Doc from com.sun.codemodel.JClass._extends.

Gets the super class of this class.

Annotations
@Override
_implementsback to summary
public Iterator<JClass> _implements()

Implements abstract com.sun.codemodel.JClass._implements.

Doc from com.sun.codemodel.JClass._implements.

Iterates all super interfaces directly implemented by this class/interface.

Annotations
@Override
_packageback to summary
public JPackage _package()

Implements abstract com.sun.codemodel.JClass._package.

Doc from com.sun.codemodel.JClass._package.

Gets the package to which this class belongs.

Todo

shall we move move this down?

Annotations
@Override
binaryNameback to summary
public String binaryName()

Overrides com.sun.codemodel.JType.binaryName.

Doc from com.sun.codemodel.JType.binaryName.

Gets the binary name of the type. See http://java.sun.com/docs/books/jls/third_edition/html/binaryComp.html#44909

Annotations
@Override
declareback to summary
public void declare(JFormatter f)

Implements com.sun.codemodel.JDeclaration.declare.

Annotations
@Override
fullNameback to summary
public String fullName()

Implements abstract com.sun.codemodel.JType.fullName.

Doc from com.sun.codemodel.JType.fullName.

Gets the full name of the type. See http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#25430 for the details.

Annotations
@Override
getPrimitiveTypeback to summary
public JPrimitiveType getPrimitiveType()

Overrides com.sun.codemodel.JClass.getPrimitiveType.

Doc from com.sun.codemodel.JClass.getPrimitiveType.

If this class represents one of the wrapper classes defined in the java.lang package, return the corresponding primitive type. Otherwise null.

Annotations
@Override
isAbstractback to summary
public boolean isAbstract()

Implements abstract com.sun.codemodel.JClass.isAbstract.

Doc from com.sun.codemodel.JClass.isAbstract.

Checks if this class is an abstract class.

Annotations
@Override
isArrayback to summary
public boolean isArray()

Overrides com.sun.codemodel.JType.isArray.

Doc from com.sun.codemodel.JType.isArray.

Tell whether or not this is an array type.

Annotations
@Override
isInterfaceback to summary
public boolean isInterface()

Implements abstract com.sun.codemodel.JClass.isInterface.

Doc from com.sun.codemodel.JClass.isInterface.

Checks if this object represents an interface.

Annotations
@Override
nameback to summary
public String name()

Implements abstract com.sun.codemodel.JClass.name.

Doc from com.sun.codemodel.JClass.name.

Gets the name of this class.

Annotations
@Override
outerback to summary
public JClass outer()

Overrides com.sun.codemodel.JClass.outer.

Doc from com.sun.codemodel.JClass.outer.

Returns the class in which this class is nested, or null if this is a top-level class.

Annotations
@Override
substituteParamsback to summary
protected JClass substituteParams(JTypeVar[] variables, List<JClass> bindings)

Implements abstract com.sun.codemodel.JClass.substituteParams.

Doc from com.sun.codemodel.JClass.substituteParams.

Substitutes the type variables with their actual arguments.

For example, when this class is Map<String,Map<V>>, (where V then doing substituteParams( V, Integer ) returns a for Map<String,Map<Integer>>.

This method needs to work recursively.

Annotations
@Override
typeParamsback to summary
public JTypeVar[] typeParams()

Overrides com.sun.codemodel.JClass.typeParams.

Doc from com.sun.codemodel.JClass.typeParams.

Iterates all the type parameters of this class/interface.

For example, if this represents Set<T>, this method returns an array that contains single JTypeVar for 'T'.

Annotations
@Override
com.sun.codemodel back to summary

private Class JCodeModel.Node

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final List<JCodeModel.Node>
private JClass
private final JCodeModel.Node
private String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

childsback to summary
private final List<JCodeModel.Node> childs
jClassback to summary
private JClass jClass
parentback to summary
private final JCodeModel.Node parent
valueback to summary
private String value

Constructor Detail

Nodeback to summary
public Node(JCodeModel.Node parent)

Method Detail

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
com.sun.codemodel back to summary

private Class JCodeModel.TreeParser

extends Object
Class Inheritance

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private JCodeModel.Node
private JClass
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

TreeParserback to summary
private TreeParser()

Method Detail

buildTreeback to summary
private JCodeModel.Node buildTree(String str)
parseTypeNameback to summary
private JClass parseTypeName(String str)
postOrderCreateJClassback to summary
private void postOrderCreateJClass(JCodeModel.Node node)
com.sun.codemodel back to summary

private final Class JCodeModel.TypeNameParser

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private int
private final String
s

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private JClass

Returns:

the index of the character next to '>'
parseArguments
(JClass rawType)

Parses '<T1,T2,...,Tn>'

private JClass
parseSuffix(JClass clazz)

Parses additional left-associative suffixes, like type arguments and array specifiers.

pack-priv JClass

Returns:

the index of the character next to T.
parseTypeName
()

Parses a type name token T (which can be potentially of the form T<T1,T2,...

private void
ws()

Skips whitespaces

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

idxback to summary
private int idx
sback to summary
private final String s

Constructor Detail

TypeNameParserback to summary
public TypeNameParser(String s)

Method Detail

parseArgumentsback to summary
private JClass parseArguments(JClass rawType)

Parses '<T1,T2,...,Tn>'

Returns:JClass

the index of the character next to '>'

parseSuffixback to summary
private JClass parseSuffix(JClass clazz)

Parses additional left-associative suffixes, like type arguments and array specifiers.

parseTypeNameback to summary
pack-priv JClass parseTypeName()

Parses a type name token T (which can be potentially of the form T<T1,T2,...>, or ? extends/super T.)

Returns:JClass

the index of the character next to T.

wsback to summary
private void ws()

Skips whitespaces