Package com.sun.codemodel

Library for generating Java source code


Interface Summary
JAnnotatableAnnotatable program elements.
JAnnotationWriter<A extends Annotation>Base interface for typed annotation writer.
JAssignmentTargetMarker interface for code components that can be placed to the left of '=' in an assignment.
JClassContainerThe common aspect of a package and a class.
JDeclarationCommon interface for code components that can generate declarations of themselves.
JDocCommentableProgram elements that can have Javadoc
JExpressionA Java expression.
JGenerableCommon interface for code components that can generate uses of themselves.
JGenerifiableDeclarations that can have type variables.
JStatementCommon interface for code components that can generate uses of themselves as statements.

Class Summary
ClassTypeThis helps enable whether the JDefinedClass is a Class or Interface or AnnotationTypeDeclaration or Enum
CodeWriterReceives generated code and writes to the appropriate storage.
JAnnotationArrayMemberRepresents an arrays as annotation members
JAnnotationUseRepresents an annotation on a program element.
JAnnotationValueThings that can be values of an annotation element.
JArrayarray creation and initialization.
JAssignmentAssignment statements, which are also expressions.
JBlockA block of Java code, which may contain statements and local declarations.
JCaseCase statement
JCatchBlockCatch block for a try/catch/finally statement
JClassRepresents a Java reference type, such as a class, an interface, an enum, an array type, a parameterized type.
JCodeModelRoot of the code DOM.
JCommentPartA part is a part of a javadoc comment, and it is a list of values.
JConditionalIf statement, with optional else clause
JDefinedClassA generated Java class/interface/enum/....
JDocCommentJavaDoc comment.
JDoLoopDo loops
JEnumConstantEnum Constant.
JExprFactory methods that generate various JExpressions.
JExpressionImplProvides default implementations for JExpression.
JFieldRefField Reference
JFieldVarA field that can have a JDocComment associated with it
JForEachForEach Statement This will generate the code for statement based on the new j2se 1.5 j.l.s.
JForLoopFor statement
JFormatterThis is a utility class for managing indentation and other basic formatting for PrintWriter.
JInvocationJMethod invocation
JJavaNameUtility methods that convert arbitrary strings into Java identifiers.
JLabelLabel that can be used for continue and break.
JMethodJava method.
JModModifier constants.
JModsModifier groups.
JNullTypeSpecial class object that represents the type of "null".
JOpJClass for generating expressions containing operators
JPackageA Java package.
JPrimitiveTypeJava built-in primitive types.
JResourceFileRepresents a resource file in the application-specific file format.
JStringLiteralString literal.
JSwitchSwitch statement
JTryBlockTry statement with Catch and/or Finally clause
JTypeA representation of a type in codeModel.
JTypeVarType variable used to declare generics.
JVarVariables and fields.
JWhileLoopWhile statement

Exception Summary
JClassAlreadyExistsExceptionIndicates 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:

  • 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
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.


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-2011
Oracle. All Rights Reserved.

Add the Maven Dependecy to your project: maven dependecy for com.amazonaws : aws-java-sdk : 1.3.14