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.
Most of the time you'd want to populate new type definitions in a JCodeModel.
See _class(String, ClassType).
| Modifier and Type | Class and Description |
|---|---|
| private class | JCodeModel.
References to existing classes. |
| private static class | |
| private class | |
| private class |
| Modifier and Type | Field and Description |
|---|---|
| public final JPrimitiveType | |
| public static final Map | boxToPrimitive
The reverse look up for |
| public final JPrimitiveType | |
| public final JPrimitiveType | |
| private final Map | |
| 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 |
| public final JNullType | NULL
Obtains a reference to the special "null" type. |
| private final HashMap | packages
The packages that this JCodeWriter contains. |
| public static final Map | primitiveToBox
Conversion from primitive type |
| private final HashMap | refClasses
All JReferencedClasses are pooled here. |
| public final JPrimitiveType | |
| public final JPrimitiveType | |
| private JClass | wildcard
Cached for |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public JDefinedClass | |
| public JDefinedClass | |
| public JDefinedClass | |
| public JDefinedClass | |
| public JModule | Returns: Java module ornull if Java module was not created yet.Returns existing Java module to be generated. |
| public JModule | Returns: New Java module.The Name of Java module. name)Creates and returns Java module to be generated. |
| public JPackage | |
| 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 | |
| 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 | |
| public JDefinedClass | |
| 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 | |
| public void | |
| public void | |
| public void | |
| public Map | |
| public int | |
| public JClass | |
| 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 | |
| public JType | |
| public JClass | |
| public JClass | |
| public JPackage | |
| public JClass |
| BOOLEAN | back to summary |
|---|---|
| public final JPrimitiveType BOOLEAN | |
| boxToPrimitive | back to summary |
|---|---|
| public static final Map<Class<?>, Class<?>> boxToPrimitive The reverse look up for | |
| BYTE | back to summary |
|---|---|
| public final JPrimitiveType BYTE | |
| CHAR | back to summary |
|---|---|
| public final JPrimitiveType CHAR | |
| classNameReplacer | back to summary |
|---|---|
| private final Map<String, String> classNameReplacer | |
| DOUBLE | back to summary |
|---|---|
| public final JPrimitiveType DOUBLE | |
| FLOAT | back to summary |
|---|---|
| public final JPrimitiveType FLOAT | |
| INT | back to summary |
|---|---|
| public final JPrimitiveType INT | |
| isCaseSensitiveFileSystem | back to summary |
|---|---|
| pack-priv static final boolean isCaseSensitiveFileSystem If the flag is true, we will consider two classes "Foo" and "foo" as a collision. | |
| LONG | back to summary |
|---|---|
| public final JPrimitiveType LONG | |
| module | back to summary |
|---|---|
| private JModule module Java module in | |
| NULL | back to summary |
|---|---|
| public final JNullType NULL Obtains a reference to the special "null" type. | |
| packages | back to summary |
|---|---|
| private final HashMap<String, JPackage> packages The packages that this JCodeWriter contains. | |
| primitiveToBox | back to summary |
|---|---|
| public static final Map<Class<?>, Class<?>> primitiveToBox Conversion from primitive type | |
| refClasses | back to summary |
|---|---|
| private final HashMap<Class<?>, JCodeModel. All JReferencedClasses are pooled here. | |
| SHORT | back to summary |
|---|---|
| public final JPrimitiveType SHORT | |
| VOID | back to summary |
|---|---|
| public final JPrimitiveType VOID | |
| wildcard | back to summary |
|---|---|
| private JClass wildcard Cached for | |
| JCodeModel | back to summary |
|---|---|
| public JCodeModel() | |
| _class | back to summary |
|---|---|
| public JDefinedClass _class(String fullyqualifiedName) throws JClassAlreadyExistsException Creates a new generated class.
| |
| _class | back to summary |
|---|---|
| public JDefinedClass _class(int mods, String fullyqualifiedName, ClassType t) throws JClassAlreadyExistsException Creates a new generated class.
| |
| _class | back to summary |
|---|---|
| public JDefinedClass _class(String fullyqualifiedName, ClassType t) throws JClassAlreadyExistsException Creates a new generated class.
| |
| _getClass | back to summary |
|---|---|
| public JDefinedClass _getClass(String fullyQualifiedName) Gets a reference to the already created generated class.
| |
| _getModuleInfo | back to summary |
|---|---|
| public JModule _getModuleInfo() Returns existing Java module to be generated.
| |
| _moduleInfo | back to summary |
|---|---|
| public JModule _moduleInfo(final String name) Creates and returns Java module to be generated. | |
| _package | back to summary |
|---|---|
| public JPackage _package(String name) Add a package to the list of packages to be generated. | |
| _prepareModuleInfo | back 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.
| |
| _ref | back to summary |
|---|---|
| public JType _ref(Class<?> c) | |
| _updateModuleInfo | back 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.
| |
| addClassNameReplacer | back to summary |
|---|---|
| public void addClassNameReplacer(String c1, String c2) Specify class names or packages to be replaced when the model is dumped into files. | |
| anonymousClass | back to summary |
|---|---|
| public JDefinedClass anonymousClass(JClass baseType) Creates a new anonymous class. | |
| anonymousClass | back to summary |
|---|---|
| public JDefinedClass anonymousClass(Class<?> baseType) | |
| build | back to summary |
|---|---|
| public void build(File destDir, PrintStream status) throws IOException Generates Java source code.
A convenience method for
| |
| build | back to summary |
|---|---|
| public void build(File srcDir, File resourceDir, PrintStream status) throws IOException Generates Java source code.
A convenience method that calls
| |
| build | back to summary |
|---|---|
| public void build(File destDir) throws IOException A convenience method for | |
| build | back to summary |
|---|---|
| public void build(File srcDir, File resourceDir) throws IOException A convenience method for | |
| build | back to summary |
|---|---|
| public void build(CodeWriter out) throws IOException A convenience method for | |
| build | back to summary |
|---|---|
| public void build(CodeWriter source, CodeWriter resource) throws IOException Generates Java source code. | |
| classNameReplacer | back to summary |
|---|---|
| public Map Gives an unmodifiable copy of classNameReplacer | |
| countArtifacts | back to summary |
|---|---|
| public int countArtifacts() Returns the number of files to be generated if
| |
| directClass | back to summary |
|---|---|
| public JClass directClass(String name) Creates a dummy, unknown 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. | |
| getFileSystemCaseSensitivity | back to summary |
|---|---|
| private static boolean getFileSystemCaseSensitivity() | |
| newAnonymousClass | back to summary |
|---|---|
| public JDefinedClass newAnonymousClass(JClass baseType)
Deprecated The naming convention doesn't match the rest of the CodeModel.
Use Creates a new anonymous class.
| |
| packages | back to summary |
|---|---|
| public Iterator Returns an iterator that walks the packages defined using this code writer. | |
| parseType | back to summary |
|---|---|
| public JType parseType(String name) Obtains a type object from a type name.
This method handles primitive types, arrays, and existing | |
| ref | back to summary |
|---|---|
| public JClass ref(Class<?> clazz) Obtains a reference to an existing class from its Class object. The parameter may not be primitive.
| |
| ref | back 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
| |
| rootPackage | back to summary |
|---|---|
| public JPackage rootPackage() | |
| wildcard | back to summary |
|---|---|
| public JClass wildcard() Gets a | |
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.
| Modifier and Type | Field and Description |
|---|---|
| private final Class |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public JClass | _extends()
Implements abstract com. Gets the super class of this class. |
| public Iterator | _implements()
Implements abstract com. Iterates all super interfaces directly implemented by this class/interface. |
| public JPackage | _package()
Implements abstract com. Gets the package to which this class belongs. |
| public String | |
| public void | |
| public String | |
| public JPrimitiveType | getPrimitiveType()
Overrides com. 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. Checks if this class is an abstract class. |
| public boolean | |
| public boolean | isInterface()
Implements abstract com. Checks if this object represents an interface. |
| public String | |
| public JClass | outer()
Overrides com. Returns the class in which this class is nested, or |
| protected JClass | substituteParams(JTypeVar[] variables, List<JClass> bindings)
Implements abstract com. Substitutes the type variables with their actual arguments. |
| public JTypeVar[] | typeParams()
Overrides com. Iterates all the type parameters of this class/interface. |
| _class | back to summary |
|---|---|
| private final Class<?> _class | |
| JReferencedClass | back to summary |
|---|---|
| pack-priv JReferencedClass(Class<?> _clazz) | |
| _extends | back to summary |
|---|---|
| public JClass _extends() Implements abstract com. Doc from com. Gets the super class of this class.
| |
| _implements | back to summary |
|---|---|
| public Iterator Implements abstract com. Doc from com. Iterates all super interfaces directly implemented by this class/interface.
| |
| _package | back to summary |
|---|---|
| public JPackage _package() Implements abstract com. Doc from com. Gets the package to which this class belongs. Todo shall we move move this down?
| |
| binaryName | back to summary |
|---|---|
| public String binaryName() Overrides com. Doc from com. Gets the binary name of the type. See http://java.sun.com/docs/books/jls/third_edition/html/binaryComp.html#44909
| |
| declare | back to summary |
|---|---|
| public void declare(JFormatter f) Implements com.
| |
| fullName | back to summary |
|---|---|
| public String fullName() Implements abstract com. Doc from com. 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.
| |
| getPrimitiveType | back to summary |
|---|---|
| public JPrimitiveType getPrimitiveType() Overrides com. Doc from com. If this class represents one of the wrapper classes defined in the java.lang package, return the corresponding primitive type. Otherwise null.
| |
| isAbstract | back to summary |
|---|---|
| public boolean isAbstract() Implements abstract com. Doc from com. Checks if this class is an abstract class.
| |
| isArray | back to summary |
|---|---|
| public boolean isArray() Overrides com. Doc from com. Tell whether or not this is an array type.
| |
| isInterface | back to summary |
|---|---|
| public boolean isInterface() Implements abstract com. Doc from com. Checks if this object represents an interface.
| |
| name | back to summary |
|---|---|
| public String name() Implements abstract com. Doc from com. Gets the name of this class.
| |
| outer | back to summary |
|---|---|
| public JClass outer() Overrides com. Doc from com. Returns the class in which this class is nested, or
| |
| substituteParams | back to summary |
|---|---|
| protected JClass substituteParams(JTypeVar[] variables, List<JClass> bindings) Implements abstract com. Doc from com. Substitutes the type variables with their actual arguments.
For example, when this class is This method needs to work recursively.
| |
| typeParams | back to summary |
|---|---|
| public JTypeVar[] typeParams() Overrides com. Doc from com. Iterates all the type parameters of this class/interface.
For example, if this represents
| |
| Modifier and Type | Field and Description |
|---|---|
| private final List | |
| private JClass | |
| private final JCodeModel. | |
| private String |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public String |
| childs | back to summary |
|---|---|
| private final List<JCodeModel. | |
| jClass | back to summary |
|---|---|
| private JClass jClass | |
| parent | back to summary |
|---|---|
| private final JCodeModel. | |
| value | back to summary |
|---|---|
| private String value | |
| Node | back to summary |
|---|---|
| public Node(JCodeModel. | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non-
| |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private JCodeModel. | |
| private JClass | |
| private void |
| TreeParser | back to summary |
|---|---|
| private TreeParser() | |
| buildTree | back to summary |
|---|---|
| private JCodeModel. | |
| parseTypeName | back to summary |
|---|---|
| private JClass parseTypeName(String str) | |
| postOrderCreateJClass | back to summary |
|---|---|
| private void postOrderCreateJClass(JCodeModel. | |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private JClass | Returns: the index of the character next to '>'Parses |
| 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.Parses a type name token T (which can be potentially of the form |
| private void |
| TypeNameParser | back to summary |
|---|---|
| public TypeNameParser(String s) | |
| parseArguments | back to summary |
|---|---|
| private JClass parseArguments(JClass rawType) Parses
| |
| parseSuffix | back to summary |
|---|---|
| private JClass parseSuffix(JClass clazz) Parses additional left-associative suffixes, like type arguments and array specifiers. | |
| parseTypeName | back to summary |
|---|---|
| pack-priv JClass parseTypeName() Parses a type name token T (which can be potentially of the form
| |
| ws | back to summary |
|---|---|
| private void ws() Skips whitespaces | |