Class JCodeModel
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.
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 Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate class
References to existing classes.private final class
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal JPrimitiveType
The reverse look up forprimitiveToBox
final JPrimitiveType
final JPrimitiveType
final JPrimitiveType
final JPrimitiveType
final JPrimitiveType
protected static final boolean
If the flag is true, we will consider two classes "Foo" and "foo" as a collision.final JPrimitiveType
final JNullType
Obtains a reference to the special "null" type.The packages that this JCodeWriter contains.Conversion from primitive typeClass
(such asInteger.TYPE
to its boxed type (such as Integer.class)private final HashMap
<Class<?>, JCodeModel.JReferencedClass> All JReferencedClasses are pooled here.final JPrimitiveType
final JPrimitiveType
private JClass
Cached forwildcard()
. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCreates a new generated class.Creates a new generated class.Creates a new generated class.Gets a reference to the already created generated class.Add a package to the list of packages to be generatedanonymousClass
(JClass baseType) Creates a new anonymous class.anonymousClass
(Class<?> baseType) void
build
(CodeWriter out) A convenience method forbuild(out,out)
.void
build
(CodeWriter source, CodeWriter resource) Generates Java source code.void
A convenience method forbuild(destDir,System.out)
.void
A convenience method forbuild(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.int
Returns the number of files to be generated ifbuild(java.io.File, java.io.PrintStream)
is invoked now.directClass
(String name) Creates a dummy, unknownJClass
that represents a given name.private static boolean
newAnonymousClass
(JClass baseType) Deprecated.The naming convention doesn't match the rest of the CodeModel.packages()
Returns an iterator that walks the packages defined using this code writer.Obtains a type object from a type name.Obtains a reference to an existing class from its Class object.Obtains a reference to an existing class from its fully-qualified class name.final JPackage
wildcard()
Gets aJClass
representation for "?", which is equivalent to "? extends Object".
-
Field Details
-
packages
The packages that this JCodeWriter contains. -
refClasses
All JReferencedClasses are pooled here. -
NULL
Obtains a reference to the special "null" type. -
VOID
-
BOOLEAN
-
BYTE
-
SHORT
-
CHAR
-
INT
-
FLOAT
-
LONG
-
DOUBLE
-
isCaseSensitiveFileSystem
protected static final boolean isCaseSensitiveFileSystemIf the flag is true, we will consider two classes "Foo" and "foo" as a collision. -
wildcard
Cached forwildcard()
. -
primitiveToBox
Conversion from primitive typeClass
(such asInteger.TYPE
to its boxed type (such as Integer.class) -
boxToPrimitive
The reverse look up forprimitiveToBox
-
-
Constructor Details
-
JCodeModel
public JCodeModel()
-
-
Method Details
-
getFileSystemCaseSensitivity
private static boolean getFileSystemCaseSensitivity() -
_package
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
-
packages
Returns an iterator that walks the packages defined using this code writer. -
_class
Creates a new generated class.- Throws:
JClassAlreadyExistsException
- When the specified class/interface was already created.
-
directClass
Creates a dummy, unknownJClass
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 JDefinedClass _class(int mods, String fullyqualifiedName, ClassType t) throws JClassAlreadyExistsException Creates a new generated class.- Throws:
JClassAlreadyExistsException
- When the specified class/interface was already created.
-
_class
public JDefinedClass _class(String fullyqualifiedName, ClassType t) throws JClassAlreadyExistsException Creates a new generated class.- Throws:
JClassAlreadyExistsException
- When the specified class/interface was already created.
-
_getClass
Gets a reference to the already created generated class.- Returns:
- null If the class is not yet created.
- See Also:
-
newAnonymousClass
Deprecated.The naming convention doesn't match the rest of the CodeModel. UseanonymousClass(JClass)
instead.Creates a new anonymous class. -
anonymousClass
Creates a new anonymous class. -
anonymousClass
-
build
Generates Java source code. A convenience method forbuild(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:
IOException
-
build
Generates Java source code. A convenience method that callsbuild(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:
IOException
-
build
A convenience method forbuild(destDir,System.out)
.- Throws:
IOException
-
build
A convenience method forbuild(srcDir,resourceDir,System.out)
.- Throws:
IOException
-
build
A convenience method forbuild(out,out)
.- Throws:
IOException
-
build
Generates Java source code.- Throws:
IOException
-
countArtifacts
public int countArtifacts()Returns the number of files to be generated ifbuild(java.io.File, java.io.PrintStream)
is invoked now. -
ref
Obtains a reference to an existing class from its Class object.The parameter may not be primitive.
- See Also:
-
_ref
-
ref
-
wildcard
Gets aJClass
representation for "?", which is equivalent to "? extends Object". -
parseType
Obtains a type object from a type name.This method handles primitive types, arrays, and existing
Class
es.- Throws:
ClassNotFoundException
- If the specified type is not found.
-