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.
-
ClassDescriptionThis helps enable whether the JDefinedClass is a Class or Interface or AnnotationTypeDeclaration or EnumReceives generated code and writes to the appropriate storage.Annotatable program elements.Represents an arrays as annotation membersCaptures the Class or Enum value of the annotation.Captures the value of the annotation.Represents an annotation on a program element.Things that can be values of an annotation element.JAnnotationWriter<A extends Annotation>Base interface for typed annotation writer.array creation and initialization.array component reference.Assignment statements, which are also expressions.Marker interface for code components that can be placed to the left of '=' in an assignment.A block of Java code, which may contain statements and local declarations.Case statementA cast operation.Catch block for a try/catch/finally statementRepresents a Java reference type, such as a class, an interface, an enum, an array type, a parameterized type.Indicates that the class is already created.The common aspect of a package and a class.Root of the code DOM.A part is a part of a javadoc comment, and it is a list of values.If statement, with optional else clauseCommon interface for code components that can generate declarations of themselves.A generated Java class/interface/enum/....JavaDoc comment.Program elements that can have JavadocDo loopsEnum Constant.Represents a Java module
exports
directive.Factory methods that generate variousJExpression
s.A Java expression.Provides default implementations forJExpression
.Field ReferenceA field that can have aJDocComment
associated with itForEach Statement This will generate the code for statement based on the new j2se 1.5 j.l.s.For statementThis is a utility class for managing indentation and other basic formatting for PrintWriter.Common interface for code components that can generate uses of themselves.Declarations that can have type variables.JMethod invocationUtility methods that convert arbitrary strings into Java identifiers.Label that can be used for continue and break.Java method.Modifier constants.Modifier groups.Represents a Java module.Represents a Java module directive.Module directive type.Special class object that represents the type of "null".JClass for generating expressions containing operatorsA Java package.Java built-in primitive types.Represents a Java modulerequires
directive.Represents a resource file in the application-specific file format.Common interface for code components that can generate uses of themselves as statements.String literal.Switch statementTry statement with Catch and/or Finally clauseA representation of a type in codeModel.Type variable used to declare generics.Variables and fields.While statement