- java.lang.Object
-
- com.sun.codemodel.JCodeModel
-
public final class JCodeModel extends java.lang.ObjectRoot 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
JCodeModelobject at any given time (which can be often accesesd by theowner()method.) As such, when you generate Java code, most of the operation works in a top-down fashion. For example, you create a class fromJCodeModel, which gives you aJDefinedClass. Then you invoke a method on it to generate a new method, which gives youJMethod, and so on. There are a few exceptions to this, most notably buildingJExpressions, 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).
-
-
Field Summary
Fields Modifier and Type Field Description JPrimitiveTypeBOOLEANstatic java.util.Map<java.lang.Class<?>,java.lang.Class<?>>boxToPrimitiveThe reverse look up forprimitiveToBoxJPrimitiveTypeBYTEJPrimitiveTypeCHARJPrimitiveTypeDOUBLEJPrimitiveTypeFLOATJPrimitiveTypeINTprotected static booleanisCaseSensitiveFileSystemIf the flag is true, we will consider two classes "Foo" and "foo" as a collision.JPrimitiveTypeLONGJNullTypeNULLObtains a reference to the special "null" type.static java.util.Map<java.lang.Class<?>,java.lang.Class<?>>primitiveToBoxConversion from primitive typeClass(such asInteger.TYPEto its boxed type (such asInteger.class)JPrimitiveTypeSHORTJPrimitiveTypeVOID
-
Constructor Summary
Constructors Constructor Description JCodeModel()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods 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.JDefinedClassanonymousClass(JClass baseType)Creates a new anonymous class.JDefinedClassanonymousClass(java.lang.Class<?> baseType)voidbuild(CodeWriter out)A convenience method forbuild(out,out).voidbuild(CodeWriter source, CodeWriter resource)Generates Java source code.voidbuild(java.io.File destDir)A convenience method forbuild(destDir,System.out).voidbuild(java.io.File srcDir, java.io.File resourceDir)A convenience method forbuild(srcDir,resourceDir,System.out).voidbuild(java.io.File srcDir, java.io.File resourceDir, java.io.PrintStream status)Generates Java source code.voidbuild(java.io.File destDir, java.io.PrintStream status)Generates Java source code.intcountArtifacts()Returns the number of files to be generated ifbuild(java.io.File, java.io.PrintStream)is invoked now.JClassdirectClass(java.lang.String name)Creates a dummy, unknownJClassthat represents a given name.JDefinedClassnewAnonymousClass(JClass baseType)Deprecated.The naming convention doesn't match the rest of the CodeModel.java.util.Iterator<JPackage>packages()Returns an iterator that walks the packages defined using this code writer.JTypeparseType(java.lang.String name)Obtains a type object from a type name.JClassref(java.lang.Class<?> clazz)Obtains a reference to an existing class from its Class object.JClassref(java.lang.String fullyQualifiedClassName)Obtains a reference to an existing class from its fully-qualified class name.JPackagerootPackage()JClasswildcard()Gets aJClassrepresentation for "?", which is equivalent to "? extends Object".
-
-
-
Field Detail
-
NULL
public final JNullType NULL
Obtains a reference to the special "null" type.
-
VOID
public final JPrimitiveType VOID
-
BOOLEAN
public final JPrimitiveType BOOLEAN
-
BYTE
public final JPrimitiveType BYTE
-
SHORT
public final JPrimitiveType SHORT
-
CHAR
public final JPrimitiveType CHAR
-
INT
public final JPrimitiveType INT
-
FLOAT
public final JPrimitiveType FLOAT
-
LONG
public final JPrimitiveType LONG
-
DOUBLE
public final JPrimitiveType 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 java.util.Map<java.lang.Class<?>,java.lang.Class<?>> primitiveToBox
Conversion from primitive typeClass(such asInteger.TYPEto its boxed type (such asInteger.class)
-
boxToPrimitive
public static final java.util.Map<java.lang.Class<?>,java.lang.Class<?>> boxToPrimitive
The reverse look up forprimitiveToBox
-
-
Method Detail
-
_package
public JPackage _package(java.lang.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
-
_moduleInfo
public JModule _moduleInfo(java.lang.String name)
Creates and returns Java module to be generated.- Parameters:
name- The Name of Java module.- Returns:
- New Java module.
-
_getModuleInfo
public JModule _getModuleInfo()
Returns existing Java module to be generated.- Returns:
- Java module or
nullif Java module was not created yet.
-
_prepareModuleInfo
public 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. Used to initialize and build Java module instance with existing packages content.- Parameters:
name- The Name of Java module.requires- Requires directives to add.- Throws:
java.lang.IllegalStateException- when Java module instance was not initialized.
-
_updateModuleInfo
public void _updateModuleInfo(java.lang.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- Requires directives to add.- Throws:
java.lang.IllegalStateException- when Java module instance was not initialized.
-
rootPackage
public final JPackage rootPackage()
-
packages
public java.util.Iterator<JPackage> packages()
Returns an iterator that walks the packages defined using this code writer.
-
_class
public JDefinedClass _class(java.lang.String fullyqualifiedName) throws JClassAlreadyExistsException
Creates a new generated class.- Throws:
JClassAlreadyExistsException- When the specified class/interface was already created.
-
directClass
public JClass directClass(java.lang.String name)
Creates a dummy, unknownJClassthat 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, java.lang.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(java.lang.String fullyqualifiedName, ClassType t) throws JClassAlreadyExistsException
Creates a new generated class.- Throws:
JClassAlreadyExistsException- When the specified class/interface was already created.
-
_getClass
public JDefinedClass _getClass(java.lang.String fullyQualifiedName)
Gets a reference to the already created generated class.- Returns:
- null If the class is not yet created.
- See Also:
JPackage._getClass(String)
-
newAnonymousClass
public JDefinedClass newAnonymousClass(JClass baseType)
Deprecated.The naming convention doesn't match the rest of the CodeModel. UseanonymousClass(JClass)instead.Creates a new anonymous class.
-
anonymousClass
public JDefinedClass anonymousClass(JClass baseType)
Creates a new anonymous class.
-
anonymousClass
public JDefinedClass anonymousClass(java.lang.Class<?> baseType)
-
build
public void build(java.io.File destDir, java.io.PrintStream status) throws java.io.IOExceptionGenerates 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:
java.io.IOException
-
build
public void build(java.io.File srcDir, java.io.File resourceDir, java.io.PrintStream status) throws java.io.IOExceptionGenerates 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:
java.io.IOException
-
build
public void build(java.io.File destDir) throws java.io.IOExceptionA convenience method forbuild(destDir,System.out).- Throws:
java.io.IOException
-
build
public void build(java.io.File srcDir, java.io.File resourceDir) throws java.io.IOExceptionA convenience method forbuild(srcDir,resourceDir,System.out).- Throws:
java.io.IOException
-
build
public void build(CodeWriter out) throws java.io.IOException
A convenience method forbuild(out,out).- Throws:
java.io.IOException
-
build
public void build(CodeWriter source, CodeWriter resource) throws java.io.IOException
Generates Java source code.- Throws:
java.io.IOException
-
countArtifacts
public int countArtifacts()
Returns the number of files to be generated ifbuild(java.io.File, java.io.PrintStream)is invoked now.
-
ref
public JClass ref(java.lang.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 JType _ref(java.lang.Class<?> c)
-
ref
public JClass ref(java.lang.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 aJClass.
-
wildcard
public JClass wildcard()
Gets aJClassrepresentation for "?", which is equivalent to "? extends Object".
-
parseType
public JType parseType(java.lang.String name) throws java.lang.ClassNotFoundException
Obtains a type object from a type name.This method handles primitive types, arrays, and existing
Classes.- Throws:
java.lang.ClassNotFoundException- If the specified type is not found.
-
-