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 com. from com., then you
create a com. from com., and so on.
This design brings the following benefits:
com. 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.
java.s java.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.
| Modifier and Type | Interface and Description |
|---|---|
| public interface | JAnnotatable
Annotatable program elements. |
| public interface | |
| public interface | JAssignmentTarget
Marker interface for code components that can be placed to the left of '=' in an assignment. |
| public interface | JClassContainer
The common aspect of a package and a class. |
| public interface | JDeclaration
Common interface for code components that can generate declarations of themselves. |
| public interface | JDocCommentable
Program elements that can have Javadoc |
| public interface | JExpression
A Java expression. |
| public interface | JGenerable
Common interface for code components that can generate uses of themselves. |
| public interface | JGenerifiable
Declarations that can have type variables. |
| public interface | JStatement
Common interface for code components that can generate uses of themselves as statements. |
| Modifier and Type | Class and Description |
|---|---|
| public class | ClassType
This helps enable whether the JDefinedClass is a Class or Interface or AnnotationTypeDeclaration or Enum |
| public abstract class | CodeWriter
Receives generated code and writes to the appropriate storage. |
| public class | JAnnotationArrayMember
Represents an arrays as annotation members
This class implements |
| public class | JAnnotationClassValue
Captures the Class or Enum value of the annotation. |
| public class | JAnnotationStringValue
Captures the value of the annotation. |
| public class | JAnnotationUse
Represents an annotation on a program element. |
| public abstract class | JAnnotationValue
Things that can be values of an annotation element. |
| pack-priv class | JAnonymousClass
Anonymous class quick hack. |
| public class | JArray
array creation and initialization. |
| pack-priv class | JArrayClass
Array class. |
| public class | JArrayCompRef
array component reference. |
| public class | JAssignment
Assignment statements, which are also expressions. |
| pack-priv class | JAtom
JAtoms: Simple code components that merely generate themselves. |
| public class | JBlock
A block of Java code, which may contain statements and local declarations. |
| pack-priv class | JBreak
JBreak statement |
| public class | JCase
Case statement |
| public class | JCast
A cast operation. |
| public class | JCatchBlock
Catch block for a try/catch/finally statement |
| public abstract class | JClass
Represents a Java reference type, such as a class, an interface, an enum, an array type, a parameterized type. |
| public class | JClassAlreadyExistsException
Indicates that the class is already created. |
| public class | JCodeModel
Root of the code DOM. |
| public class | JCommentPart
A part is a part of a javadoc comment, and it is a list of values. |
| public class | JConditional
If statement, with optional else clause |
| pack-priv class | JContinue
JContinue statement |
| public class | JDefinedClass
A generated Java class/interface/enum/.... |
| pack-priv class | JDirectClass
A special |
| public class | JDocComment
JavaDoc comment. |
| public class | JDoLoop
Do loops |
| public class | JEnumConstant
Enum Constant. |
| public class | JExportsDirective
Represents a Java module |
| public abstract class | JExpr
Factory methods that generate various |
| public abstract class | JExpressionImpl
Provides default implementations for |
| public class | JFieldRef
Field Reference |
| public class | JFieldVar
A field that can have a |
| public class | JForEach
ForEach Statement This will generate the code for statement based on the new j2se 1.5 j.l.s. |
| public class | JForLoop
For statement |
| public class | JFormatter
This is a utility class for managing indentation and other basic formatting for PrintWriter. |
| pack-priv abstract class | JGenerifiableImpl
Implementation of |
| public class | JInvocation
JMethod invocation |
| public class | JJavaName
Utility methods that convert arbitrary strings into Java identifiers. |
| public class | JLabel
Label that can be used for continue and break. |
| public class | JMethod
Java method. |
| public class | JMod
Modifier constants. |
| public class | JMods
Modifier groups. |
| public class | JModule
Represents a Java module. |
| public abstract class | JModuleDirective
Represents a Java module directive. |
| pack-priv class | JNarrowedClass
Represents |
| public class | JNullType
Special class object that represents the type of "null". |
| public abstract class | JOp
JClass for generating expressions containing operators |
| public class | JPackage
A Java package. |
| public class | JPrimitiveType
Java built-in primitive types. |
| public class | JRequiresDirective
Represents a Java module |
| public abstract class | JResourceFile
Represents a resource file in the application-specific file format. |
| pack-priv class | JReturn
A return statement |
| public class | JStringLiteral
String literal. |
| public class | JSwitch
Switch statement |
| pack-priv class | JThrow
JThrow statement |
| public class | JTryBlock
Try statement with Catch and/or Finally clause |
| public abstract class | JType
A representation of a type in codeModel. |
| public class | JTypeVar
Type variable used to declare generics. |
| pack-priv class | JTypeWildcard
Represents a wildcard type like "? |
| public class | JVar
Variables and fields. |
| public class | JWhileLoop
While statement |
| pack-priv class | SecureLoader
Class defined for safe calls of getClassLoader methods of any kind (context/system/class classloader. |
| pack-priv class | TypedAnnotationWriter<A extends Annotation, W extends JAnnotationWriter
Dynamically implements the typed annotation writer interfaces. |