Interface | Description |
---|---|
IJAnnotatable |
Annotatable program elements.
|
IJAnnotationWriter<A extends Annotation> |
Base interface for typed annotation writer.
|
IJAssignmentTarget |
Marker interface for code components that can be placed to the left of '=' in
an assignment.
|
IJClassContainer<CLASSTYPE extends IJClassContainer<CLASSTYPE>> |
The common aspect of a package and a class.
|
IJDeclaration |
Common interface for code components that can generate declarations of
themselves.
|
IJDocCommentable |
Program elements that can have Javadoc
|
IJExpression |
A Java expression.
|
IJExpressionStatement |
Expressions which can stand alone as statements, such as a += b; or ++i;
|
IJGenerable |
Common interface for code components that can generate uses of themselves.
|
IJGenerifiable |
Declarations that can have type variables.
|
IJOwned |
Base interface for objects having a relation to a
JCodeModel . |
IJOwnedMaybe |
Base interface for objects optionally having a relation to a
JCodeModel . |
IJStatement |
Common interface for code components that can generate uses of themselves as
statements.
|
Class | Description |
---|---|
AbstractCodeWriter |
Receives generated code and writes to the appropriate storage.
|
AbstractJAnnotationValue |
Things that can be values of an annotation element.
|
AbstractJAnnotationValueOwned |
Things that can be values of an annotation element and have an owning code
model (see
IJOwned ). |
AbstractJClass |
Represents a Java reference type, such as a class, an interface, an enum, an
array type, a parameterized type.
|
AbstractJClassContainer<CLASSTYPE extends AbstractJClassContainer<CLASSTYPE>> |
A generated Java class/interface/enum/annotation
This class models a declaration, and since a declaration can be always used as a reference, it inherits AbstractJClass . |
AbstractJExpressionAssignmentTargetImpl |
Abstract base class for expressions that also implement
IJAssignmentTarget . |
AbstractJExpressionImpl |
Provides default implementations for
IJExpression . |
AbstractJGenerifiableImpl |
Implementation of
IJGenerifiable . |
AbstractJResourceFile |
Represents a resource file in the application-specific file format.
|
AbstractJType |
A representation of a type in codeModel.
|
JAnnotationArrayMember |
Represents an arrays as annotation members
This class implements
IJAnnotatable to allow new annotations to be
added as a member of the array. |
JAnnotationStringValue |
Captures the value of the annotation.
|
JAnnotationUse |
Represents an annotation on a program element.
|
JAnonymousClass |
Anonymous class quick hack.
|
JArray |
array creation and initialization.
|
JArrayClass |
Array class.
|
JArrayCompRef |
array component reference.
|
JAssignment |
Assignment statements, which are also expressions.
|
JAtom |
JAtoms: Simple code components that merely generate themselves.
|
JAtomDouble |
A special atom for double values
|
JAtomFloat |
A special atom for float values
|
JAtomInt |
A special atom for int values
|
JAtomLong |
A special atom for long values
|
JBlock |
A block of Java code, which may contain statements and local declarations.
|
JBreak |
JBreak statement
|
JCase |
Case statement
|
JCast |
A cast operation.
|
JCatchBlock |
Catch block for a try/catch/finally statement
|
JCodeModel |
Root of the code DOM.
|
JCommentPart |
A part is a part of a javadoc comment, and it is a list of values.
|
JConditional |
If statement, with optional else clause
|
JContinue |
JContinue statement
|
JDefinedClass |
A generated Java class/interface/enum/....
|
JDirectClass |
A special
AbstractJClass that represents an unknown class (except its
name.) |
JDirectStatement |
This class represents a single direct statement.
|
JDocComment |
JavaDoc comment.
|
JDoLoop |
Do loops
|
JEnumConstant |
Enum Constant.
|
JEnumConstantRef |
Enum Constant reference.
|
JErrorClass |
A special
AbstractJClass that represents an error class. |
JExpr |
Factory methods that generate various
IJExpression s. |
JFieldRef |
Field Reference
|
JFieldVar |
A field that can have a
JDocComment associated with it |
JForEach |
ForEach Statement This will generate the code for statement based on the new
j2se 1.5 j.l.s.
|
JForLoop |
For statement
|
JFormatter |
This is a utility class for managing indentation and other basic formatting
for PrintWriter.
|
JInvocation |
JMethod invocation |
JJavaName |
Utility methods that convert arbitrary strings into Java identifiers.
|
JLabel |
Label that can be used for continue and break.
|
JLambda |
This is a single Java 8 lambda expression.
|
JLambdaBlock |
A special JBlock implementation that supports lambda expressions.
|
JLambdaMethodRef |
This is a single Java 8 lambda method reference expression.
|
JLambdaParam |
This represent a single parameter to a Java 8 lambda expression.
|
JMethod |
Java method.
|
JMod |
Modifier constants.
|
JMods |
Modifier groups.
|
JNarrowedClass |
Represents X<Y>.
|
JNullType |
Special class object that represents the type of "null".
|
JOp |
Class for generating expressions containing operators
|
JOpBinary | |
JOpTernary | |
JOpUnary | |
JOpUnaryTight | |
JPackage |
A Java package.
|
JPrimitiveType |
Java built-in primitive types.
|
JReturn |
A return statement
|
JSingleLineCommentStatement |
This class represents a special single-comment "statement"!
|
JStringLiteral |
String literal.
|
JSwitch |
Switch statement
|
JSynchronizedBlock |
Synchronized block within a method statement
|
JThrow |
JThrow statement
|
JTryBlock |
Try statement with Catch and/or Finally clause
|
JTypeVar |
Type variable used to declare generics.
|
JTypeVarClass |
A special type variable that is used inside
JInvocation objects if
the parameter type is an AbstractJClass |
JTypeWildcard |
Represents a wildcard type like "?
|
JVar |
Variables and fields.
|
JWhileLoop |
While statement
|
SourcePrintWriter |
A special version of
PrintWriter that has a customizable new
line string. |
TypedAnnotationWriter<A extends Annotation,W extends IJAnnotationWriter<A>> |
Dynamically implements the typed annotation writer interfaces.
|
Enum | Description |
---|---|
EClassType |
This helps enable whether the JDefinedClass is a Class or Interface or
AnnotationTypeDeclaration or Enum
|
JTypeWildcard.EBoundMode |
Exception | Description |
---|---|
JClassAlreadyExistsException |
Indicates that the class is already created.
|
JErrorClassUsedException |
This exception purely indicates, that the
JErrorClass is used which
is never intended. |
CodeModel is a library that allows you to generate Java source code in a type-safe fashion.
With CodeModel, you build the java source code by first building AST, then writing it out as text files that is Java source files. The AST looks like this:
@DotDiagram
digraph G {
cls1 [label="JDefinedClass"];
cls2 [label="JDefinedClass"];
JCodeModel -> cls1 [label="generated class"];
JCodeModel -> cls2 [label="generated class"];
m1 [label="JMethod"];
m2 [label="JMethod"];
cls1 -> m1;
cls1 -> m2;
cls1 -> JField;
m1 -> JVar [label="method parameter"];
m1 -> JBlock [label="code"];
}
}
You bulid this tree mostly from top-down. So, you first create a new
JDefinedClass
from
JCodeModel
, then you create a
JMethod
from
JDefinedClass
, and so on.
This design brings the following beneefits:
JCodeModel
is the entry point to the library.
See its javadoc for more details about how to use CodeModel.
Generally speaking, CodeModel is expected to be used in an environment where the resource constraint is not severe. Therefore, we haven't spent much effort in trying to make this library lean and mean.
That said, we did some benchmark and performance analysis. In case anyone is interested in making this library better performance wise, here's the findings.
List
s Map
s, and other collections take up
a lot of space. Allocating those things lazily is generally a good idea.
Compared to template-based code generator, the writing operation is slow, as it needs to traverse each AST node. Consider pre-encoding tokens (like 'public') to the target encoding, and consider exploting the subtree equivalence. [at]ArchitectureDocument
Copyright © 2013–2017 Philip Helger. All rights reserved.