Class JDefinedClass
- java.lang.Object
-
- com.sun.codemodel.JType
-
- com.sun.codemodel.JClass
-
- com.sun.codemodel.JDefinedClass
-
- All Implemented Interfaces:
JAnnotatable
,JClassContainer
,JDeclaration
,JDocCommentable
,JGenerable
,JGenerifiable
,java.lang.Comparable<JType>
- Direct Known Subclasses:
JAnonymousClass
public class JDefinedClass extends JClass implements JDeclaration, JClassContainer, JGenerifiable, JAnnotatable, JDocCommentable
A generated Java class/interface/enum/....This class models a declaration, and since a declaration can be always used as a reference, it inherits
JClass
.Where to go from here?
You'd want to generate fields and methods on a class. See
method(int, JType, String)
andfield(int, JType, String)
.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<JAnnotationUse>
annotations
Annotations on this variable.private java.util.Map<java.lang.String,JDefinedClass>
classes
Nested classes as a map from name to JDefinedClass.private ClassType
classType
Default value is class or interface or annotationTypeDeclaration or enumprivate java.util.List<JMethod>
constructors
Set of constructors for this class, if anyprivate java.lang.String
directBlock
String that will be put directly inside the generated code.private java.util.Map<java.lang.String,JEnumConstant>
enumConstantsByName
Set of enum constants that are keyed by names.(package private) java.util.Map<java.lang.String,JFieldVar>
fields
Fields keyed by their names.private JGenerifiableImpl
generifiable
Helper class to implementJGenerifiable
.private boolean
hideFile
Flag that controls whether this class should be really generated or not.private JBlock
init
Static initializer, if this class has oneprivate java.util.Set<JClass>
interfaces
List of interfaces that this class implementsprivate JDocComment
jdoc
class javadocjava.lang.Object
metadata
Client-app spcific metadata associated with this user-created class.private java.util.List<JMethod>
methods
Set of methods that are members of this classprivate JMods
mods
Modifiers for the class declarationprivate java.lang.String
name
Name of this class.private JClassContainer
outer
If this is a package-member class, this isJPackage
.private JClass
superClass
Name of the super class of this class.-
Fields inherited from class com.sun.codemodel.JClass
EMPTY_ARRAY
-
-
Constructor Summary
Constructors Modifier Constructor Description private
JDefinedClass(int mods, java.lang.String name, JClassContainer parent, JCodeModel owner)
private
JDefinedClass(int mods, java.lang.String name, JClassContainer parent, JCodeModel owner, ClassType classTypeVal)
JClass constructor(package private)
JDefinedClass(JClassContainer parent, int mods, java.lang.String name, ClassType classTypeval)
(package private)
JDefinedClass(JCodeModel owner, int mods, java.lang.String name)
Constructor for creating anonymous inner class.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description JDefinedClass
_annotationTypeDeclaration(java.lang.String name)
Add an annotationType Declaration to this packageJDefinedClass
_class(int mods, java.lang.String name)
Add a new nested class to this class.JDefinedClass
_class(int mods, java.lang.String name, boolean isInterface)
Deprecated.JDefinedClass
_class(int mods, java.lang.String name, ClassType classTypeVal)
Creates a new class/enum/interface/annotation.JDefinedClass
_class(java.lang.String name)
Add a new public nested class to this class.JDefinedClass
_enum(int mods, java.lang.String name)
Add a public enum to this packageJDefinedClass
_enum(java.lang.String name)
Add a public enum to this packageJClass
_extends()
Returns the class extended by this class.JDefinedClass
_extends(JClass superClass)
This class extends the specifed class.JDefinedClass
_extends(java.lang.Class<?> superClass)
java.util.Iterator<JClass>
_implements()
Returns an iterator that walks the nested classes defined in this class.JDefinedClass
_implements(JClass iface)
This class implements the specifed interface.JDefinedClass
_implements(java.lang.Class<?> iface)
JDefinedClass
_interface(int mods, java.lang.String name)
Add an interface to this package.JDefinedClass
_interface(java.lang.String name)
Adds a public interface to this package.JPackage
_package()
Gets the package to which this class belongs.JAnnotationUse
annotate(JClass clazz)
Adding ability to annotate a classJAnnotationUse
annotate(java.lang.Class<? extends java.lang.annotation.Annotation> clazz)
Adding ability to annotate a class<W extends JAnnotationWriter>
Wannotate2(java.lang.Class<W> clazz)
Adds an annotation to this program element and returns a type-safe writer to fill in the values of such annotations.java.util.Collection<JAnnotationUse>
annotations()
java.lang.String
binaryName()
Gets the binary name of the type.java.util.Iterator<JDefinedClass>
classes()
Returns an iterator that walks the nested classes defined in this class.JMethod
constructor(int mods)
Adds a constructor to this class.java.util.Iterator<JMethod>
constructors()
Returns an iterator that walks the constructors defined in this class.void
declare(JFormatter f)
protected void
declareBody(JFormatter f)
prints the body of a class.void
direct(java.lang.String string)
Places the given string directly inside the generated class.JEnumConstant
enumConstant(java.lang.String name)
If the named enum already exists, the reference to it is returned.JFieldVar
field(int mods, JType type, java.lang.String name)
Adds a field to the list of field members of this JDefinedClass.JFieldVar
field(int mods, JType type, java.lang.String name, JExpression init)
Adds a field to the list of field members of this JDefinedClass.JFieldVar
field(int mods, java.lang.Class<?> type, java.lang.String name)
JFieldVar
field(int mods, java.lang.Class<?> type, java.lang.String name, JExpression init)
java.util.Map<java.lang.String,JFieldVar>
fields()
Returns all the fields declred in this class.java.lang.String
fullName()
Gets the fully qualified name of this class.JTypeVar
generify(java.lang.String name)
Adds a new type variable to this declaration.JTypeVar
generify(java.lang.String name, JClass bound)
Adds a new type variable to this declaration with a bound.JTypeVar
generify(java.lang.String name, java.lang.Class<?> bound)
Adds a new type variable to this declaration with a bound.private java.util.Map<java.lang.String,JDefinedClass>
getClasses()
ClassType
getClassType()
JMethod
getConstructor(JType[] argTypes)
Looks for a method that has the specified method signature and return it.JMethod
getMethod(java.lang.String name, JType[] argTypes)
Looks for a method that has the specified method signature and return it.JPackage
getPackage()
Gets the nearest package parent.void
hide()
Mark this file as hidden, so that this file won't be generated.JBlock
init()
Creates, if necessary, and returns the static initializer for this class.boolean
isAbstract()
Checks if this class is an abstract class.boolean
isAnnotationTypeDeclaration()
This method indicates if the interface is an annotationTypeDeclarationboolean
isAnonymous()
Returns true if this is an anonymous class.boolean
isClass()
Returns true if the container is a class.boolean
isHidden()
boolean
isInterface()
Checks if this object represents an interface.boolean
isPackage()
Returns true if the container is a package.JDocComment
javadoc()
Creates, if necessary, and returns the class javadoc for this JDefinedClassJClass[]
listClasses()
Returns all the nested classes defined in this class.JMethod
method(int mods, JType type, java.lang.String name)
Add a method to the list of method members of this JDefinedClass instance.JMethod
method(int mods, java.lang.Class<?> type, java.lang.String name)
java.util.Collection<JMethod>
methods()
Returns the set of methods defined in this class.JMods
mods()
java.lang.String
name()
JClass name accessor.JClass
outer()
Returns the class in which this class is nested, or null if this is a top-level class.JClassContainer
parentContainer()
Parent JClassContainer.void
removeField(JFieldVar field)
Removes aJFieldVar
from this class.protected JClass
substituteParams(JTypeVar[] variables, java.util.List<JClass> bindings)
Substitutes the type variables with their actual arguments.JTypeVar[]
typeParams()
Iterates all the type parameters of this class/interface.-
Methods inherited from class com.sun.codemodel.JClass
array, boxify, dotclass, erasure, generate, getBaseClass, getBaseClass, getPrimitiveType, getTypeParameters, isAssignableFrom, isParameterized, narrow, narrow, narrow, narrow, narrow, narrow, owner, printLink, staticInvoke, staticInvoke, staticRef, staticRef, toString, unboxify, wildcard
-
Methods inherited from class com.sun.codemodel.JType
compareTo, elementType, isArray, isPrimitive, isReference, parse
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.sun.codemodel.JClassContainer
owner
-
-
-
-
Field Detail
-
name
private java.lang.String name
Name of this class. Null if anonymous.
-
mods
private JMods mods
Modifiers for the class declaration
-
superClass
private JClass superClass
Name of the super class of this class.
-
interfaces
private final java.util.Set<JClass> interfaces
List of interfaces that this class implements
-
fields
final java.util.Map<java.lang.String,JFieldVar> fields
Fields keyed by their names.
-
init
private JBlock init
Static initializer, if this class has one
-
jdoc
private JDocComment jdoc
class javadoc
-
constructors
private final java.util.List<JMethod> constructors
Set of constructors for this class, if any
-
methods
private final java.util.List<JMethod> methods
Set of methods that are members of this class
-
classes
private java.util.Map<java.lang.String,JDefinedClass> classes
Nested classes as a map from name to JDefinedClass. The name is all capitalized in a case sensitive file system (JCodeModel.isCaseSensitiveFileSystem
) to avoid conflicts. Lazily created to save footprint.- See Also:
getClasses()
-
hideFile
private boolean hideFile
Flag that controls whether this class should be really generated or not. Sometimes it is useful to generate code that refers to class X, without actually generating the code of X. This flag is used to surpress X.java file in the output.
-
metadata
public java.lang.Object metadata
Client-app spcific metadata associated with this user-created class.
-
directBlock
private java.lang.String directBlock
String that will be put directly inside the generated code. Can be null.
-
outer
private JClassContainer outer
If this is a package-member class, this isJPackage
. If this is a nested class, this isJDefinedClass
. If this is an anonymous class, this constructor shouldn't be used.
-
classType
private final ClassType classType
Default value is class or interface or annotationTypeDeclaration or enum
-
enumConstantsByName
private final java.util.Map<java.lang.String,JEnumConstant> enumConstantsByName
Set of enum constants that are keyed by names. In Java, enum constant order is actually significant, because of order ID they get. So let's preserve the order.
-
annotations
private java.util.List<JAnnotationUse> annotations
Annotations on this variable. Lazily created.
-
generifiable
private final JGenerifiableImpl generifiable
Helper class to implementJGenerifiable
.
-
-
Constructor Detail
-
JDefinedClass
JDefinedClass(JClassContainer parent, int mods, java.lang.String name, ClassType classTypeval)
-
JDefinedClass
JDefinedClass(JCodeModel owner, int mods, java.lang.String name)
Constructor for creating anonymous inner class.
-
JDefinedClass
private JDefinedClass(int mods, java.lang.String name, JClassContainer parent, JCodeModel owner)
-
JDefinedClass
private JDefinedClass(int mods, java.lang.String name, JClassContainer parent, JCodeModel owner, ClassType classTypeVal)
JClass constructor- Parameters:
mods
- Modifiers for this class declarationname
- Name of this class
-
-
Method Detail
-
isAnonymous
public final boolean isAnonymous()
Returns true if this is an anonymous class.
-
_extends
public JDefinedClass _extends(JClass superClass)
This class extends the specifed class.- Parameters:
superClass
- Superclass for this class- Returns:
- This class
-
_extends
public JDefinedClass _extends(java.lang.Class<?> superClass)
-
_extends
public JClass _extends()
Returns the class extended by this class.- Specified by:
_extends
in classJClass
- Returns:
- Returns the JClass representing the superclass of the
entity (class or interface) represented by this
JClass
. Even if no super class is given explicitly or thisJClass
is not a class, this method still returnsJClass
forObject
. If this JClass representsObject
, return null.
-
_implements
public JDefinedClass _implements(JClass iface)
This class implements the specifed interface.- Parameters:
iface
- Interface that this class implements- Returns:
- This class
-
_implements
public JDefinedClass _implements(java.lang.Class<?> iface)
-
_implements
public java.util.Iterator<JClass> _implements()
Returns an iterator that walks the nested classes defined in this class.- Specified by:
_implements
in classJClass
- Returns:
- A non-null valid iterator that iterates all
JClass
objects that represents those interfaces implemented by this object.
-
name
public java.lang.String name()
JClass name accessor.For example, for
java.util.List
, this method returns"List"
"
-
enumConstant
public JEnumConstant enumConstant(java.lang.String name)
If the named enum already exists, the reference to it is returned. Otherwise this method generates a new enum reference with the given name and returns it.- Parameters:
name
- The name of the constant.- Returns:
- The generated type-safe enum constant.
-
fullName
public java.lang.String fullName()
Gets the fully qualified name of this class.
-
binaryName
public java.lang.String binaryName()
Description copied from class:JType
Gets the binary name of the type. See http://java.sun.com/docs/books/jls/third_edition/html/binaryComp.html#44909- Overrides:
binaryName
in classJType
- Returns:
- Name like "Foo$Bar", "int", "java.lang.String", "java.io.File[]". Never null.
-
isInterface
public boolean isInterface()
Description copied from class:JClass
Checks if this object represents an interface.- Specified by:
isInterface
in classJClass
-
isAbstract
public boolean isAbstract()
Description copied from class:JClass
Checks if this class is an abstract class.- Specified by:
isAbstract
in classJClass
-
field
public JFieldVar field(int mods, JType type, java.lang.String name)
Adds a field to the list of field members of this JDefinedClass.- Parameters:
mods
- Modifiers for this fieldtype
- JType of this fieldname
- Name of this field- Returns:
- Newly generated field
-
field
public JFieldVar field(int mods, java.lang.Class<?> type, java.lang.String name)
-
field
public JFieldVar field(int mods, JType type, java.lang.String name, JExpression init)
Adds a field to the list of field members of this JDefinedClass.- Parameters:
mods
- Modifiers for this field.type
- JType of this field.name
- Name of this field.init
- Initial value of this field.- Returns:
- Newly generated field
-
isAnnotationTypeDeclaration
public boolean isAnnotationTypeDeclaration()
This method indicates if the interface is an annotationTypeDeclaration
-
_annotationTypeDeclaration
public JDefinedClass _annotationTypeDeclaration(java.lang.String name) throws JClassAlreadyExistsException
Add an annotationType Declaration to this package- Specified by:
_annotationTypeDeclaration
in interfaceJClassContainer
- Parameters:
name
- Name of the annotation Type declaration to be added to this package- Returns:
- newly created Annotation Type Declaration
- Throws:
JClassAlreadyExistsException
- When the specified class/interface was already created.
-
_enum
public JDefinedClass _enum(java.lang.String name) throws JClassAlreadyExistsException
Add a public enum to this package- Specified by:
_enum
in interfaceJClassContainer
- Parameters:
name
- Name of the enum to be added to this package- Returns:
- newly created Enum
- Throws:
JClassAlreadyExistsException
- When the specified class/interface was already created.
-
_enum
public JDefinedClass _enum(int mods, java.lang.String name) throws JClassAlreadyExistsException
Add a public enum to this package- Parameters:
name
- Name of the enum to be added to this packagemods
- Modifiers for this enum declaration- Returns:
- newly created Enum
- Throws:
JClassAlreadyExistsException
- When the specified class/interface was already created.
-
getClassType
public ClassType getClassType()
-
field
public JFieldVar field(int mods, java.lang.Class<?> type, java.lang.String name, JExpression init)
-
fields
public java.util.Map<java.lang.String,JFieldVar> fields()
Returns all the fields declred in this class. The returnedMap
is a read-only live view.- Returns:
- always non-null.
-
removeField
public void removeField(JFieldVar field)
Removes aJFieldVar
from this class.- Throws:
java.lang.IllegalArgumentException
- if the given field is not a field on this class.
-
init
public JBlock init()
Creates, if necessary, and returns the static initializer for this class.- Returns:
- JBlock containing initialization statements for this class
-
constructor
public JMethod constructor(int mods)
Adds a constructor to this class.- Parameters:
mods
- Modifiers for this constructor
-
constructors
public java.util.Iterator<JMethod> constructors()
Returns an iterator that walks the constructors defined in this class.
-
getConstructor
public JMethod getConstructor(JType[] argTypes)
Looks for a method that has the specified method signature and return it.- Returns:
- null if not found.
-
method
public JMethod method(int mods, JType type, java.lang.String name)
Add a method to the list of method members of this JDefinedClass instance.- Parameters:
mods
- Modifiers for this methodtype
- Return type for this methodname
- Name of the method- Returns:
- Newly generated JMethod
-
method
public JMethod method(int mods, java.lang.Class<?> type, java.lang.String name)
-
methods
public java.util.Collection<JMethod> methods()
Returns the set of methods defined in this class.
-
getMethod
public JMethod getMethod(java.lang.String name, JType[] argTypes)
Looks for a method that has the specified method signature and return it.- Returns:
- null if not found.
-
isClass
public boolean isClass()
Description copied from interface:JClassContainer
Returns true if the container is a class.- Specified by:
isClass
in interfaceJClassContainer
-
isPackage
public boolean isPackage()
Description copied from interface:JClassContainer
Returns true if the container is a package.- Specified by:
isPackage
in interfaceJClassContainer
-
getPackage
public JPackage getPackage()
Description copied from interface:JClassContainer
Gets the nearest package parent.If this.isPackage(), then return this.
- Specified by:
getPackage
in interfaceJClassContainer
-
_class
public JDefinedClass _class(int mods, java.lang.String name) throws JClassAlreadyExistsException
Add a new nested class to this class.- Specified by:
_class
in interfaceJClassContainer
- Parameters:
mods
- Modifiers for this class declarationname
- Name of class to be added to this package- Returns:
- Newly generated class
- Throws:
JClassAlreadyExistsException
- When the specified class/interface was already created.
-
_class
public JDefinedClass _class(int mods, java.lang.String name, boolean isInterface) throws JClassAlreadyExistsException
Deprecated.Create a new class or a new interface.- Specified by:
_class
in interfaceJClassContainer
- Throws:
JClassAlreadyExistsException
-
_class
public JDefinedClass _class(int mods, java.lang.String name, ClassType classTypeVal) throws JClassAlreadyExistsException
Description copied from interface:JClassContainer
Creates a new class/enum/interface/annotation.- Specified by:
_class
in interfaceJClassContainer
- Throws:
JClassAlreadyExistsException
-
_class
public JDefinedClass _class(java.lang.String name) throws JClassAlreadyExistsException
Add a new public nested class to this class.- Specified by:
_class
in interfaceJClassContainer
- Throws:
JClassAlreadyExistsException
- When the specified class/interface was already created.
-
_interface
public JDefinedClass _interface(int mods, java.lang.String name) throws JClassAlreadyExistsException
Add an interface to this package.- Specified by:
_interface
in interfaceJClassContainer
- Parameters:
mods
- Modifiers for this interface declarationname
- Name of interface to be added to this package- Returns:
- Newly generated interface
- Throws:
JClassAlreadyExistsException
- When the specified class/interface was already created.
-
_interface
public JDefinedClass _interface(java.lang.String name) throws JClassAlreadyExistsException
Adds a public interface to this package.- Specified by:
_interface
in interfaceJClassContainer
- Throws:
JClassAlreadyExistsException
- When the specified class/interface was already created.
-
javadoc
public JDocComment javadoc()
Creates, if necessary, and returns the class javadoc for this JDefinedClass- Specified by:
javadoc
in interfaceJDocCommentable
- Returns:
- JDocComment containing javadocs for this class
-
hide
public void hide()
Mark this file as hidden, so that this file won't be generated.This feature could be used to generate code that refers to class X, without actually generating X.java.
-
isHidden
public boolean isHidden()
-
classes
public final java.util.Iterator<JDefinedClass> classes()
Returns an iterator that walks the nested classes defined in this class.- Specified by:
classes
in interfaceJClassContainer
-
getClasses
private java.util.Map<java.lang.String,JDefinedClass> getClasses()
-
listClasses
public final JClass[] listClasses()
Returns all the nested classes defined in this class.
-
outer
public JClass outer()
Description copied from class:JClass
Returns the class in which this class is nested, or null if this is a top-level class.
-
declare
public void declare(JFormatter f)
- Specified by:
declare
in interfaceJDeclaration
-
declareBody
protected void declareBody(JFormatter f)
prints the body of a class.
-
direct
public void direct(java.lang.String string)
Places the given string directly inside the generated class. This method can be used to add methods/fields that are not generated by CodeModel. This method should be used only as the last resort.
-
_package
public final JPackage _package()
Description copied from class:JClass
Gets the package to which this class belongs. TODO: shall we move move this down?
-
parentContainer
public final JClassContainer parentContainer()
Description copied from interface:JClassContainer
Parent JClassContainer. If this is a package, this method returns a parent package, or null if this package is the root package. If this is an outer-most class, this method returns a package to which it belongs. If this is an inner class, this method returns the outer class.- Specified by:
parentContainer
in interfaceJClassContainer
-
generify
public JTypeVar generify(java.lang.String name)
Description copied from interface:JGenerifiable
Adds a new type variable to this declaration.- Specified by:
generify
in interfaceJGenerifiable
-
generify
public JTypeVar generify(java.lang.String name, java.lang.Class<?> bound)
Description copied from interface:JGenerifiable
Adds a new type variable to this declaration with a bound.- Specified by:
generify
in interfaceJGenerifiable
-
generify
public JTypeVar generify(java.lang.String name, JClass bound)
Description copied from interface:JGenerifiable
Adds a new type variable to this declaration with a bound.- Specified by:
generify
in interfaceJGenerifiable
-
typeParams
public JTypeVar[] typeParams()
Description copied from class:JClass
Iterates all the type parameters of this class/interface.For example, if this
JClass
representsSet<T>
, this method returns an array that contains singleJTypeVar
for 'T'.- Specified by:
typeParams
in interfaceJGenerifiable
- Overrides:
typeParams
in classJClass
-
substituteParams
protected JClass substituteParams(JTypeVar[] variables, java.util.List<JClass> bindings)
Description copied from class:JClass
Substitutes the type variables with their actual arguments.For example, when this class is Map<String,Map<V>>, (where V then doing substituteParams( V, Integer ) returns a
JClass
forMap<String,Map<Integer>>
.This method needs to work recursively.
- Specified by:
substituteParams
in classJClass
-
annotate
public JAnnotationUse annotate(java.lang.Class<? extends java.lang.annotation.Annotation> clazz)
Adding ability to annotate a class- Specified by:
annotate
in interfaceJAnnotatable
- Parameters:
clazz
- The annotation class to annotate the class with
-
annotate
public JAnnotationUse annotate(JClass clazz)
Adding ability to annotate a class- Specified by:
annotate
in interfaceJAnnotatable
- Parameters:
clazz
- The annotation class to annotate the class with
-
annotate2
public <W extends JAnnotationWriter> W annotate2(java.lang.Class<W> clazz)
Description copied from interface:JAnnotatable
Adds an annotation to this program element and returns a type-safe writer to fill in the values of such annotations.- Specified by:
annotate2
in interfaceJAnnotatable
-
annotations
public java.util.Collection<JAnnotationUse> annotations()
- Specified by:
annotations
in interfaceJAnnotatable
- Returns:
- Can be empty but never null.
-
mods
public JMods mods()
- Returns:
- the current modifiers of this class. Always return non-null valid object.
-
-