Package com.sun.codemodel
Library for generating Java source code
.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:
- source code can be written in random order
- generated source code nicely imports other classes
- generated source code is lexically always correct (no unbalanced parenthesis, etc.)
- code generation becomes relatively type-safe
Using CodeModel
JCodeModel
is the entry point to
the library. See its javadoc for more details about how to use
CodeModel.
Performance
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.
-
Interface Summary 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 JavadocJExpression 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 Summary Class Description ClassType This helps enable whether the JDefinedClass is a Class or Interface or AnnotationTypeDeclaration or EnumCodeWriter Receives generated code and writes to the appropriate storage.JAnnotationArrayMember Represents an arrays as annotation membersJAnnotationClassValue Captures the Class or Enum value of the annotation.JAnnotationStringValue Captures the value of the annotation.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 statementJCast A cast operation.JCatchBlock Catch block for a try/catch/finally statementJClass 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 clauseJDefinedClass A generated Java class/interface/enum/....JDocComment JavaDoc comment.JDoLoop Do loopsJEnumConstant Enum Constant.JExportsDirective Represents a Java moduleexports
directive.JExpr Factory methods that generate variousJExpression
s.JExpressionImpl Provides default implementations forJExpression
.JFieldRef Field ReferenceJFieldVar A field that can have aJDocComment
associated with itJForEach ForEach Statement This will generate the code for statement based on the new j2se 1.5 j.l.s.JForLoop For statementJFormatter This is a utility class for managing indentation and other basic formatting for PrintWriter.JInvocation JMethod invocationJJavaName 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 operatorsJPackage A Java package.JPrimitiveType Java built-in primitive types.JRequiresDirective Represents a Java modulerequires
directive.JResourceFile Represents a resource file in the application-specific file format.JStringLiteral String literal.JSwitch Switch statementJTryBlock Try statement with Catch and/or Finally clauseJType A representation of a type in codeModel.JTypeVar Type variable used to declare generics.JVar Variables and fields.JWhileLoop While statement -
Enum Summary Enum Description JModuleDirective.Type Module directive type. -
Exception Summary Exception Description JClassAlreadyExistsException Indicates that the class is already created.