public final class JCodeModel
extends java.lang.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 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 JExpression
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, ClassType)
.
Modifier and Type | Field | Description |
---|---|---|
JPrimitiveType |
BOOLEAN |
|
static java.util.Map<java.lang.Class<?>,java.lang.Class<?>> |
boxToPrimitive |
The reverse look up for
primitiveToBox |
JPrimitiveType |
BYTE |
|
JPrimitiveType |
CHAR |
|
JPrimitiveType |
DOUBLE |
|
JPrimitiveType |
FLOAT |
|
JPrimitiveType |
INT |
|
protected static 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 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 ) |
JPrimitiveType |
SHORT |
|
JPrimitiveType |
VOID |
Constructor | Description |
---|---|
JCodeModel() |
Modifier and Type | Method | Description |
---|---|---|
JDefinedClass |
_class(int mods,
java.lang.String fullyqualifiedName,
ClassType t) |
Creates a new generated class.
|
JDefinedClass |
_class(java.lang.String fullyqualifiedName) |
Creates a new generated class.
|
JDefinedClass |
_class(java.lang.String fullyqualifiedName,
ClassType t) |
Creates a new generated class.
|
JDefinedClass |
_getClass(java.lang.String fullyQualifiedName) |
Gets a reference to the already created generated class.
|
JModule |
_getModuleInfo() |
Returns existing Java module to be generated.
|
JModule |
_moduleInfo(java.lang.String name) |
Creates and returns Java module to be generated.
|
JPackage |
_package(java.lang.String name) |
Add a package to the list of packages to be generated.
|
void |
_prepareModuleInfo(java.lang.String name,
java.lang.String... requires) |
Creates Java module instance and adds existing packages with classes to the Java module info.
|
JType |
_ref(java.lang.Class<?> c) |
|
void |
_updateModuleInfo(java.lang.String... requires) |
Adds existing packages with classes to the Java module info.
|
JDefinedClass |
anonymousClass(JClass baseType) |
Creates a new anonymous class.
|
JDefinedClass |
anonymousClass(java.lang.Class<?> baseType) |
|
void |
build(CodeWriter out) |
A convenience method for
build(out,out) . |
void |
build(CodeWriter source,
CodeWriter resource) |
Generates Java source code.
|
void |
build(java.io.File destDir) |
A convenience method for
build(destDir,System.out) . |
void |
build(java.io.File srcDir,
java.io.File resourceDir) |
A convenience method for
build(srcDir,resourceDir,System.out) . |
void |
build(java.io.File srcDir,
java.io.File resourceDir,
java.io.PrintStream status) |
Generates Java source code.
|
void |
build(java.io.File destDir,
java.io.PrintStream status) |
Generates Java source code.
|
int |
countArtifacts() |
Returns the number of files to be generated if
build(java.io.File, java.io.PrintStream) is invoked now. |
JClass |
directClass(java.lang.String name) |
Creates a dummy, unknown
JClass that represents a given name. |
JDefinedClass |
newAnonymousClass(JClass baseType) |
Deprecated.
The naming convention doesn't match the rest of the CodeModel.
Use
anonymousClass(JClass) instead. |
java.util.Iterator<JPackage> |
packages() |
Returns an iterator that walks the packages defined using this code
writer.
|
JType |
parseType(java.lang.String name) |
Obtains a type object from a type name.
|
JClass |
ref(java.lang.Class<?> clazz) |
Obtains a reference to an existing class from its Class object.
|
JClass |
ref(java.lang.String fullyQualifiedClassName) |
Obtains a reference to an existing class from its fully-qualified
class name.
|
JPackage |
rootPackage() |
|
JClass |
wildcard() |
Gets a
JClass representation for "?",
which is equivalent to "? extends Object". |
public final JNullType NULL
public final JPrimitiveType VOID
public final JPrimitiveType BOOLEAN
public final JPrimitiveType BYTE
public final JPrimitiveType SHORT
public final JPrimitiveType CHAR
public final JPrimitiveType INT
public final JPrimitiveType FLOAT
public final JPrimitiveType LONG
public final JPrimitiveType DOUBLE
protected static final boolean isCaseSensitiveFileSystem
public static final java.util.Map<java.lang.Class<?>,java.lang.Class<?>> primitiveToBox
Class
(such as Integer.TYPE
to its boxed type (such as Integer.class
)public static final java.util.Map<java.lang.Class<?>,java.lang.Class<?>> boxToPrimitive
primitiveToBox
public JPackage _package(java.lang.String name)
name
- Name of the package. Use "" to indicate the root package.public JModule _moduleInfo(java.lang.String name)
name
- The Name of Java module.public JModule _getModuleInfo()
null
if Java module was not created yet.public void _prepareModuleInfo(java.lang.String name, java.lang.String... requires)
name
- The Name of Java module.requires
- Requires directives to add.java.lang.IllegalStateException
- when Java module instance was not initialized.public void _updateModuleInfo(java.lang.String... requires)
requires
- Requires directives to add.java.lang.IllegalStateException
- when Java module instance was not initialized.public final JPackage rootPackage()
public java.util.Iterator<JPackage> packages()
public JDefinedClass _class(java.lang.String fullyqualifiedName) throws JClassAlreadyExistsException
JClassAlreadyExistsException
- When the specified class/interface was already created.public JClass directClass(java.lang.String name)
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.
public JDefinedClass _class(int mods, java.lang.String fullyqualifiedName, ClassType t) throws JClassAlreadyExistsException
JClassAlreadyExistsException
- When the specified class/interface was already created.public JDefinedClass _class(java.lang.String fullyqualifiedName, ClassType t) throws JClassAlreadyExistsException
JClassAlreadyExistsException
- When the specified class/interface was already created.public JDefinedClass _getClass(java.lang.String fullyQualifiedName)
JPackage._getClass(String)
public JDefinedClass newAnonymousClass(JClass baseType)
anonymousClass(JClass)
instead.public JDefinedClass anonymousClass(JClass baseType)
public JDefinedClass anonymousClass(java.lang.Class<?> baseType)
public void build(java.io.File destDir, java.io.PrintStream status) throws java.io.IOException
build(destDir,destDir,System.out)
.destDir
- source files are generated into this directory.status
- if non-null, progress indication will be sent to this stream.java.io.IOException
public void build(java.io.File srcDir, java.io.File resourceDir, java.io.PrintStream status) throws java.io.IOException
build(CodeWriter,CodeWriter)
.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.java.io.IOException
public void build(java.io.File destDir) throws java.io.IOException
build(destDir,System.out)
.java.io.IOException
public void build(java.io.File srcDir, java.io.File resourceDir) throws java.io.IOException
build(srcDir,resourceDir,System.out)
.java.io.IOException
public void build(CodeWriter out) throws java.io.IOException
build(out,out)
.java.io.IOException
public void build(CodeWriter source, CodeWriter resource) throws java.io.IOException
java.io.IOException
public int countArtifacts()
build(java.io.File, java.io.PrintStream)
is invoked now.public JClass ref(java.lang.Class<?> clazz)
The parameter may not be primitive.
for the version that handles more cases.
public JType _ref(java.lang.Class<?> c)
public JClass ref(java.lang.String fullyQualifiedClassName)
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
.
public JClass wildcard()
JClass
representation for "?",
which is equivalent to "? extends Object".public JType parseType(java.lang.String name) throws java.lang.ClassNotFoundException
This method handles primitive types, arrays, and existing Class
es.
java.lang.ClassNotFoundException
- If the specified type is not found.Copyright © 2018 Oracle Corporation. All rights reserved.