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 beneefits:
- 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 java.lang.annotation.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 membersJAnnotationStringValue Captures the value of the annotation.JAnnotationUse Represents an annotation on a program element.JAnnotationValue Things that can be values of an annotation 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.JBlock A block of Java code, which may contain statements and local declarations.JBreak JBreak statementJCase 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 clauseJContinue JContinue statementJDefinedClass A generated Java class/interface/enum/....JDirectClass A specialJClass
that represents an unknown class (except its name.)JDocComment JavaDoc comment.JDoLoop Do loopsJEnumConstant Enum Constant.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.JGenerifiableImpl Implementation ofJGenerifiable
.JInvocation JMethod invocationJJavaName Utility methods that convert arbitrary strings into Java identifiers.JJavaName.Entry JLabel Label that can be used for continue and break.JMethod Java method.JMod Modifier constants.JMods Modifier groups.JNarrowedClass Represents X<Y>.JNullType Special class object that represents the type of "null".JOp JClass for generating expressions containing operatorsJOp.BinaryOp JOp.TernaryOp JOp.TightUnaryOp JOp.UnaryOp JPackage A Java package.JPrimitiveType Java built-in primitive types.JResourceFile Represents a resource file in the application-specific file format.JReturn A return statementJStringLiteral String literal.JSwitch Switch statementJThrow JThrow statementJTryBlock Try statement with Catch and/or Finally clauseJType A representation of a type in codeModel.JTypeVar Type variable used to declare generics.JTypeWildcard Represents a wildcard type like "? extends Foo".JVar Variables and fields.JWhileLoop While statementSecureLoader Class defined for safe calls of getClassLoader methods of any kind (context/system/class classloader.TypedAnnotationWriter<A extends java.lang.annotation.Annotation,W extends JAnnotationWriter<A>> Dynamically implements the typed annotation writer interfaces. -
Enum Summary Enum Description JFormatter.Mode -
Exception Summary Exception Description JClassAlreadyExistsException Indicates that the class is already created.