Interface | Description |
---|---|
JAnnotatable |
Annotatable program elements.
|
JAnnotationWriter<A extends Annotation> |
Base interface for typed annotation writer.
|
JAssignmentTarget |
Marker interface for code components that can be placed to
the left of '=' in an assignment.
|
JClassContainer |
The common aspect of a package and a class.
|
JDeclaration |
Common interface for code components that can generate declarations
of themselves.
|
JDocCommentable |
Program elements that can have Javadoc
|
JExpression |
A Java expression.
|
JGenerable |
Common interface for code components that can generate
uses of themselves.
|
JGenerifiable |
Declarations that can have type variables.
|
JStatement |
Common interface for code components that can generate
uses of themselves as statements.
|
Class | Description |
---|---|
ClassType |
This helps enable whether the JDefinedClass is a Class or Interface or
AnnotationTypeDeclaration or Enum
|
CodeWriter |
Receives generated code and writes to the appropriate storage.
|
JAnnotationArrayMember |
Represents an arrays as annotation members
|
JAnnotationUse |
Represents an annotation on a program element.
|
JAnnotationValue |
Things that can be values of an annotation element.
|
JArray |
array creation and initialization.
|
JArrayCompRef |
array component reference.
|
JAssignment |
Assignment statements, which are also expressions.
|
JBlock |
A block of Java code, which may contain statements and local declarations.
|
JCase |
Case statement
|
JCast |
A cast operation.
|
JCatchBlock |
Catch block for a try/catch/finally statement
|
JClass |
Represents a Java reference type, such as a class, an interface,
an enum, an array type, a parameterized type.
|
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
|
JDefinedClass |
A generated Java class/interface/enum/....
|
JDocComment |
JavaDoc comment.
|
JDoLoop |
Do loops
|
JEnumConstant |
Enum Constant.
|
JExportsDirective |
Represents a Java module
exports directive. |
JExpr |
Factory methods that generate various
JExpression s. |
JExpressionImpl |
Provides default implementations for
JExpression . |
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.
|
JMethod |
Java method.
|
JMod |
Modifier constants.
|
JMods |
Modifier groups.
|
JModule |
Represents a Java module.
|
JModuleDirective |
Represents a Java module directive.
|
JNullType |
Special class object that represents the type of "null".
|
JOp |
JClass for generating expressions containing operators
|
JPackage |
A Java package.
|
JPrimitiveType |
Java built-in primitive types.
|
JRequiresDirective |
Represents a Java module
requires directive. |
JResourceFile |
Represents a resource file in the application-specific file format.
|
JStringLiteral |
String literal.
|
JSwitch |
Switch statement
|
JTryBlock |
Try statement with Catch and/or Finally clause
|
JType |
A representation of a type in codeModel.
|
JTypeVar |
Type variable used to declare generics.
|
JVar |
Variables and fields.
|
JWhileLoop |
While statement
|
Enum | Description |
---|---|
JModuleDirective.Type |
Module directive type.
|
Exception | Description |
---|---|
JClassAlreadyExistsException |
Indicates that the class is already created.
|
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:
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 benefits:
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.
Copyright © 2021 JBoss by Red Hat. All rights reserved.