Package com.sun.codemodel

Library for generating Java source code

See:
          Description

Interface Summary
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.
JExpression A Java expression.
JGenerifiable Declarations that can have type variables.
JStatement Common interface for code components that can generate uses of themselves as statements.
 

Class Summary
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.
JAssignment Assignment statements, which are also expressions.
JBlock A block of Java code, which may contain statements and local declarations.
JCase Case statement
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.
JExpr Factory methods that generate various JExpressions.
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.
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.
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
 

Exception Summary
JClassAlreadyExistsException Indicates that the class is already created.
 

Package com.sun.codemodel Description

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:

The price you pay for that is increased memory footprint and the generation speed. See performance section for more discussions about the performance and possible improvements.

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.

Lists Maps, 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 © 2005-2008 Sun Microsystems. All Rights Reserved.