public final class JCodeModel extends Object
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 accessed 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 IJExpression
s, but
generally you work with CodeModel in a top-down fashion. Because of this
design, most of the CodeModel classes aren't directly instanciable.
Most of the time you'd want to populate new type definitions in a
JCodeModel
. See _class(String, EClassType)
.
Modifier and Type | Field and Description |
---|---|
JPrimitiveType |
BOOLEAN |
static Map<Class<?>,Class<?>> |
boxToPrimitive
The reverse look up for
primitiveToBox . |
JPrimitiveType |
BYTE |
JPrimitiveType |
CHAR |
JPrimitiveType |
DOUBLE |
JPrimitiveType |
FLOAT |
JPrimitiveType |
INT |
protected boolean |
isCaseSensitiveFileSystem
If the flag is true, we will consider two classes "Foo" and "foo" as a
collision.
|
JPrimitiveType |
LONG |
JNullType |
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). |
JPrimitiveType |
SHORT |
JPrimitiveType |
VOID |
Constructor and Description |
---|
JCodeModel() |
Modifier and Type | Method and Description |
---|---|
JDefinedClass |
_class(int nMods,
String sFullyQualifiedClassName)
Creates a new generated class.
|
JDefinedClass |
_class(int nMods,
String sFullyQualifiedClassName,
EClassType eClassType)
Creates a new generated class.
|
JDefinedClass |
_class(String sFullyQualifiedClassName)
Creates a new generated class.
|
JDefinedClass |
_class(String sFullyQualifiedClassName,
EClassType eClassType)
Creates a new generated class.
|
JDefinedClass |
_getClass(String sFullyQualifiedClassName)
Gets a reference to the already created generated class.
|
JPackage |
_package(String name)
Add a package to the list of packages to be generated
|
AbstractJType |
_ref(Class<?> c)
Like
ref(Class) but also handling primitive types! |
JAnonymousClass |
anonymousClass(AbstractJClass aBaseClass)
Creates a new anonymous class.
|
JAnonymousClass |
anonymousClass(Class<?> aBaseClass)
Creates a new anonymous class.
|
void |
build(AbstractCodeWriter out)
A convenience method for
build(out,out) . |
void |
build(AbstractCodeWriter source,
AbstractCodeWriter resource)
Generates Java source code.
|
void |
build(File destDir)
A convenience method for
build(destDir,System.out) . |
void |
build(File srcDir,
File resourceDir)
A convenience method for
build(srcDir,resourceDir,System.out) . |
void |
build(File srcDir,
File resourceDir,
PrintStream status)
Generates Java source code.
|
void |
build(File destDir,
PrintStream status)
Generates Java source code.
|
boolean |
buildsErrorTypeRefs()
Check if any error-types leaked into output Java-sources.
|
int |
countArtifacts() |
JDirectClass |
directClass(EClassType eClassType,
String sName)
Creates a dummy, unknown
JDirectClass that represents a given name. |
JDirectClass |
directClass(String sName)
Creates a dummy, unknown
JDirectClass that represents a given name. |
JErrorClass |
errorClass(String sMessage)
Creates a dummy, error
AbstractJClass that can only be referenced
from hidden classes. |
JErrorClass |
errorClass(String sMessage,
String sName)
Creates a dummy, error
AbstractJClass that can only be referenced
from hidden classes. |
Charset |
getBuildingCharset() |
String |
getBuildingNewLine() |
protected static boolean |
getFileSystemCaseSensitivity() |
Iterator<JPackage> |
packages() |
AbstractJType |
parseType(String name)
Obtains a type object from a type name.
|
AbstractJClass |
ref(Class<?> clazz)
Obtains a reference to an existing class from its Class object.
|
AbstractJClass |
ref(String sFullyQualifiedClassName)
Obtains a reference to an existing class from its fully-qualified class
name.
|
JDefinedClass |
ref(TypeElement element,
Elements elementUtils)
Obtains a reference to a processable class from its TypeElement
description.
|
JDefinedClass |
refWithErrorTypes(TypeElement element,
Elements elementUtils)
Obtains a reference to a processable class from its TypeElement
description.
|
JPackage |
rootPackage() |
JCodeModel |
setBuildingCharset(Charset aCharset)
Set the charset to be used for emitting files.
|
JCodeModel |
setBuildingNewLine(String sNewLine)
Set the new line string to be used for emitting source files.
|
AbstractJClass |
wildcard() |
public static final Map<Class<?>,Class<?>> primitiveToBox
Class
(such as Integer.TYPE
)
to its boxed type (such as Integer.class). It's an unmodifiable
map.public static final Map<Class<?>,Class<?>> boxToPrimitive
primitiveToBox
. It's an unmodifiable map.public final JNullType NULL
public final JPrimitiveType BOOLEAN
public final JPrimitiveType BYTE
public final JPrimitiveType CHAR
public final JPrimitiveType DOUBLE
public final JPrimitiveType FLOAT
public final JPrimitiveType INT
public final JPrimitiveType LONG
public final JPrimitiveType SHORT
public final JPrimitiveType VOID
protected final boolean isCaseSensitiveFileSystem
protected static boolean getFileSystemCaseSensitivity()
@Nonnull public JPackage _package(@Nonnull String name)
name
- Name of the package. Use "" to indicate the root package.@Nonnull public Iterator<JPackage> packages()
@Nonnull public JDefinedClass _class(int nMods, @Nonnull String sFullyQualifiedClassName, @Nonnull EClassType eClassType) throws JClassAlreadyExistsException
nMods
- Modifiers to usesFullyQualifiedClassName
- FQCNeClassType
- Class type to use (enum/class/interface/annotation)JDefinedClass
JClassAlreadyExistsException
- When the specified class/interface was already created.@Nonnull public JDefinedClass _class(@Nonnull String sFullyQualifiedClassName) throws JClassAlreadyExistsException
sFullyQualifiedClassName
- FQCNJDefinedClass
JClassAlreadyExistsException
- When the specified class/interface was already created.@Nonnull public JDefinedClass _class(int nMods, @Nonnull String sFullyQualifiedClassName) throws JClassAlreadyExistsException
nMods
- Modifiers to usesFullyQualifiedClassName
- FQCNJDefinedClass
JClassAlreadyExistsException
- When the specified class/interface was already created.@Nonnull public JDefinedClass _class(@Nonnull String sFullyQualifiedClassName, @Nonnull EClassType eClassType) throws JClassAlreadyExistsException
sFullyQualifiedClassName
- FQCNeClassType
- Class type to use (enum/class/interface/annotation)JDefinedClass
JClassAlreadyExistsException
- When the specified class/interface was already created.@Nonnull public JDirectClass directClass(@Nonnull String sName)
JDirectClass
that represents a given name.
sName
- The fully qualified name of the class. When using type parameters
please use parseType(String)
instead!JDirectClass
@Nonnull public JDirectClass directClass(@Nonnull EClassType eClassType, @Nonnull String sName)
JDirectClass
that represents a given name.
eClassType
- Class type to use.sName
- The fully qualified name of the class. When using type parameters
please use parseType(String)
instead!JDirectClass
@Nonnull public JErrorClass errorClass(@Nonnull String sMessage)
AbstractJClass
that can only be referenced
from hidden classes.
This method is useful when the code generation needs to include some error class that should never leak into actually written code.
Error-types represents holes or place-holders that can't be filled.
References to error-classes can be used in hidden class-models. Such
classes should never be actually written but can be somehow used during
code generation. Use JCodeModel#buildsErrorTypeRefs
method to test
if your generated Java-sources contains references to error-types.
You should probably always check generated code with
JCodeModel#buildsErrorTypeRefs
method if you use any error-types.
Most of error-types methods throws JErrorClassUsedException
unchecked exceptions. Be careful and use AbstractJType#isError
method to check for error-types before actually
using it's methods.
sMessage
- some free form text message to identify source of errorJErrorClass
buildsErrorTypeRefs()
,
JErrorClass
@Nonnull public JErrorClass errorClass(@Nonnull String sMessage, @Nullable String sName)
AbstractJClass
that can only be referenced
from hidden classes.
This method is useful when the code generation needs to include some error class that should never leak into actually written code.
Error-types represents holes or place-holders that can't be filled.
References to error-classes can be used in hidden class-models. Such
classes should never be actually written but can be somehow used during
code generation. Use JCodeModel#buildsErrorTypeRefs
method to test
if your generated Java-sources contains references to error-types.
You should probably always check generated code with
JCodeModel#buildsErrorTypeRefs
method if you use any error-types.
Most of error-types methods throws JErrorClassUsedException
unchecked exceptions. Be careful and use AbstractJType#isError
method to check for error-types before actually
using it's methods.
sName
- name of missing class if it is knownsMessage
- some free form text message to identify source of errorJErrorClass
buildsErrorTypeRefs()
,
JErrorClass
public boolean buildsErrorTypeRefs()
true
if soerrorClass(String)
@Nullable public JDefinedClass _getClass(@Nonnull String sFullyQualifiedClassName)
sFullyQualifiedClassName
- FQCNnull
If the class is not yet created.JPackage._getClass(String)
@Nonnull public JAnonymousClass anonymousClass(@Nonnull AbstractJClass aBaseClass)
aBaseClass
- Base classJAnonymousClass
@Nonnull public JAnonymousClass anonymousClass(@Nonnull Class<?> aBaseClass)
aBaseClass
- Base classJAnonymousClass
@Nullable public Charset getBuildingCharset()
null
means
system default.@Nonnull public JCodeModel setBuildingCharset(@Nullable Charset aCharset)
aCharset
- The charset to be used. May be null
to indicate the use
of the system default.public String getBuildingNewLine()
@Nonnull public JCodeModel setBuildingNewLine(@Nonnull String sNewLine)
sNewLine
- The new line string to be used. May neither be null
nor
empty.public void build(@Nonnull File destDir, @Nullable PrintStream status) throws IOException
build(destDir,destDir,status)
.destDir
- source files and resources are generated into this directory.status
- if non-null, progress indication will be sent to this stream.IOException
- on IO errorpublic void build(@Nonnull File srcDir, @Nonnull File resourceDir, @Nullable PrintStream status) throws IOException
build(AbstractCodeWriter,AbstractCodeWriter)
.srcDir
- Java source files are generated into this directory.resourceDir
- Other resource files are generated into this directory.status
- Progress stream. May be null
.IOException
- on IO error if non-null, progress indication will be sent to this
stream.public void build(@Nonnull File destDir) throws IOException
build(destDir,System.out)
.destDir
- source files and resources are generated into this directory.IOException
- on IO errorpublic void build(@Nonnull File srcDir, @Nonnull File resourceDir) throws IOException
build(srcDir,resourceDir,System.out)
.srcDir
- Java source files are generated into this directory.resourceDir
- Other resource files are generated into this directory.IOException
- on IO errorpublic void build(@Nonnull AbstractCodeWriter out) throws IOException
build(out,out)
.out
- Source code and resource writerIOException
- on IO errorpublic void build(@Nonnull AbstractCodeWriter source, @Nonnull AbstractCodeWriter resource) throws IOException
source
- Source code writerresource
- Resource writerIOException
- on IO error@Nonnegative public int countArtifacts()
build(java.io.File, java.io.PrintStream)
is invoked
now.@Nonnull public AbstractJClass ref(@Nonnull Class<?> clazz)
The parameter may not be primitive.
clazz
- Existing class to referenceJReferencedClass
or a JArrayClass
for the version that handles more cases.
@Nonnull public JDefinedClass ref(@Nonnull TypeElement element, @Nonnull Elements elementUtils) throws ErrorTypeFound, CodeModelBuildingException
Annotation processors can get access of TypeElement
objects during
annotation processing. These TypeElement objects can be used with
jcodemodel as a references to classes.
This method result-class definition can never include references to "error"-types. Error-types araise during annotation processing when something is not fully defined.
You can post-pond annotation processing for later stages of annotation
processor hoping that all error-types will become defined on some
annotation processing stage at last. You can catch ErrorTypeFound
exception to achieve this.
element
- Processable class to referenceelementUtils
- Utility functions to handle Element-objectsErrorTypeFound
- if some classes are not fully defined during annotation processing.CodeModelBuildingException
- In case of an internal error (?)JCodeModelJavaxLangModelAdapter
,
refWithErrorTypes(TypeElement,Elements)
@Nonnull public JDefinedClass refWithErrorTypes(@Nonnull TypeElement element, @Nonnull Elements elementUtils) throws CodeModelBuildingException
Annotation processors can get access of TypeElement objects during annotation processing. These TypeElement objects can be used with jcodemodel as a references to classes.
This method result-class definition can include references to "error"-types. Error-types araise during annotation processing when something is not fully defined.
Sometimes direct treatment of error-types is required. You can use
AbstractJType.isError()
and
buildsErrorTypeRefs()
methods to handle error-types and
to prevent error-types to leak into generated code.
element
- Processable class to referenceelementUtils
- Utility functions to handle Element-objectsCodeModelBuildingException
- In case of an internal error (?)JCodeModelJavaxLangModelAdapter
,
ref(TypeElement, Elements)
,
JErrorClass
,
buildsErrorTypeRefs()
@Nonnull public AbstractJType _ref(@Nonnull Class<?> c)
ref(Class)
but also handling primitive types!c
- Class to be referenced@Nonnull public AbstractJClass ref(@Nonnull String sFullyQualifiedClassName)
Object
,
and return a AbstractJClass
.sFullyQualifiedClassName
- FQCNJReferencedClass
or a JArrayClass
or a
JDirectClass
@Nonnull public AbstractJClass wildcard()
AbstractJClass
representation for "?", which is
equivalent to "? extends Object".@Nonnull public AbstractJType parseType(@Nonnull String name)
This method handles primitive types, arrays, and existing Class
es.
name
- Type name to parseJArrayClass
, a JPrimitiveType
, a
JReferencedClass
, a JNarrowedClass
Copyright © 2013–2017 Philip Helger. All rights reserved.