Package com.sun.codemodel
Class JPackage
- java.lang.Object
-
- com.sun.codemodel.JPackage
-
- All Implemented Interfaces:
JAnnotatable
,JClassContainer
,JDeclaration
,JDocCommentable
,JGenerable
,java.lang.Comparable<JPackage>
public final class JPackage extends java.lang.Object implements JDeclaration, JGenerable, JClassContainer, JAnnotatable, java.lang.Comparable<JPackage>, JDocCommentable
A Java package.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<JAnnotationUse>
annotations
Lazily created list of package annotations.private java.util.Map<java.lang.String,JDefinedClass>
classes
List of classes contained within this package keyed by their name.private JDocComment
jdoc
package javadoc.private java.lang.String
name
Name of the package.private JCodeModel
owner
private java.util.Set<JResourceFile>
resources
List of resources files inside this package.private java.util.Map<java.lang.String,JDefinedClass>
upperCaseClassMap
AllJClass
s in this package keyed the upper case class name.
-
Constructor Summary
Constructors Constructor Description JPackage(java.lang.String name, JCodeModel cw)
JPackage constructor
-
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 class to this package.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)
Adds a public class to this package.JDefinedClass
_enum(java.lang.String name)
Add a public enum to this packageJDefinedClass
_getClass(java.lang.String name)
Gets a reference to the already createdJDefinedClass
.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.JResourceFile
addResourceFile(JResourceFile rsrc)
Adds a new resource file to this package.JAnnotationUse
annotate(JClass clazz)
Adds an annotation to this program element.JAnnotationUse
annotate(java.lang.Class<? extends java.lang.annotation.Annotation> clazz)
Adds an annotation to this program element.<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()
Read-only live view of all annotations on thisJAnnotatable
(package private) void
build(CodeWriter src, CodeWriter res)
java.util.Iterator<JDefinedClass>
classes()
Returns an iterator that walks the top-level classes defined in this package.int
compareTo(JPackage that)
Order is based on the lexicological order of the package name.(package private) int
countArtifacts()
private JFormatter
createJavaSourceFileWriter(CodeWriter src, java.lang.String className)
void
declare(JFormatter f)
void
generate(JFormatter f)
JPackage
getPackage()
Gets the nearest package parent.boolean
hasResourceFile(java.lang.String name)
Checks if a resource of the given name exists.boolean
isClass()
Returns true if the container is a class.boolean
isDefined(java.lang.String classLocalName)
Checks if a given name is already defined as a class/interfaceboolean
isPackage()
Returns true if the container is a package.boolean
isUnnamed()
Checks if this package is the root, unnamed package.JDocComment
javadoc()
Creates, if necessary, and returns the package javadoc for this JDefinedClass.java.lang.String
name()
Get the name of this packageJCodeModel
owner()
Return the code model root object being used to create this package.JPackage
parent()
Gets the parent package, or null if this class is the root package.JClassContainer
parentContainer()
Parent JClassContainer.java.util.Iterator<JResourceFile>
propertyFiles()
Iterates all resource files in this package.JClass
ref(java.lang.String name)
Reference a class within this package.void
remove(JClass c)
Removes a class from this package.JPackage
subPackage(java.lang.String pkg)
Gets a reference to a sub package of this package.(package private) java.io.File
toPath(java.io.File dir)
Convert the package name to directory path equivalent
-
-
-
Field Detail
-
name
private java.lang.String name
Name of the package. May be the empty string for the root package.
-
owner
private final JCodeModel owner
-
classes
private final java.util.Map<java.lang.String,JDefinedClass> classes
List of classes contained within this package keyed by their name.
-
resources
private final java.util.Set<JResourceFile> resources
List of resources files inside this package.
-
upperCaseClassMap
private final java.util.Map<java.lang.String,JDefinedClass> upperCaseClassMap
AllJClass
s in this package keyed the upper case class name. This field is non-null only on Windows, to detect "Foo" and "foo" as a collision.
-
annotations
private java.util.List<JAnnotationUse> annotations
Lazily created list of package annotations.
-
jdoc
private JDocComment jdoc
package javadoc.
-
-
Constructor Detail
-
JPackage
JPackage(java.lang.String name, JCodeModel cw)
JPackage constructor- Parameters:
name
- Name of packagecw
- The code writer being used to create this package- Throws:
java.lang.IllegalArgumentException
- If each part of the package name is not a valid identifier
-
-
Method Detail
-
parentContainer
public 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
-
parent
public JPackage parent()
Gets the parent package, or null if this class is the root package.
-
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 class to this package.- 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
Adds a public class to this package.- Specified by:
_class
in interfaceJClassContainer
- Throws:
JClassAlreadyExistsException
- When the specified class/interface was already created.
-
_getClass
public JDefinedClass _getClass(java.lang.String name)
Gets a reference to the already createdJDefinedClass
.- Returns:
- null If the class is not yet created.
-
compareTo
public int compareTo(JPackage that)
Order is based on the lexicological order of the package name.- Specified by:
compareTo
in interfacejava.lang.Comparable<JPackage>
-
_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.
-
_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.
-
addResourceFile
public JResourceFile addResourceFile(JResourceFile rsrc)
Adds a new resource file to this package.
-
hasResourceFile
public boolean hasResourceFile(java.lang.String name)
Checks if a resource of the given name exists.
-
propertyFiles
public java.util.Iterator<JResourceFile> propertyFiles()
Iterates all resource files in this package.
-
javadoc
public JDocComment javadoc()
Creates, if necessary, and returns the package javadoc for this JDefinedClass.- Specified by:
javadoc
in interfaceJDocCommentable
- Returns:
- JDocComment containing javadocs for this class
-
remove
public void remove(JClass c)
Removes a class from this package.
-
ref
public JClass ref(java.lang.String name) throws java.lang.ClassNotFoundException
Reference a class within this package.- Throws:
java.lang.ClassNotFoundException
-
subPackage
public JPackage subPackage(java.lang.String pkg)
Gets a reference to a sub package of this package.
-
classes
public java.util.Iterator<JDefinedClass> classes()
Returns an iterator that walks the top-level classes defined in this package.- Specified by:
classes
in interfaceJClassContainer
-
isDefined
public boolean isDefined(java.lang.String classLocalName)
Checks if a given name is already defined as a class/interface
-
isUnnamed
public final boolean isUnnamed()
Checks if this package is the root, unnamed package.
-
name
public java.lang.String name()
Get the name of this package- Returns:
- The name of this package, or the empty string if this is the
null package. For example, this method returns strings like
"java.lang"
-
owner
public final JCodeModel owner()
Return the code model root object being used to create this package.- Specified by:
owner
in interfaceJClassContainer
-
annotate
public JAnnotationUse annotate(JClass clazz)
Description copied from interface:JAnnotatable
Adds an annotation to this program element.- Specified by:
annotate
in interfaceJAnnotatable
- Parameters:
clazz
- The annotation class to annotate the program element with
-
annotate
public JAnnotationUse annotate(java.lang.Class<? extends java.lang.annotation.Annotation> clazz)
Description copied from interface:JAnnotatable
Adds an annotation to this program element.- Specified by:
annotate
in interfaceJAnnotatable
- Parameters:
clazz
- The annotation class to annotate the program element 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()
Description copied from interface:JAnnotatable
Read-only live view of all annotations on thisJAnnotatable
- Specified by:
annotations
in interfaceJAnnotatable
- Returns:
- Can be empty but never null.
-
toPath
java.io.File toPath(java.io.File dir)
Convert the package name to directory path equivalent
-
declare
public void declare(JFormatter f)
- Specified by:
declare
in interfaceJDeclaration
-
generate
public void generate(JFormatter f)
- Specified by:
generate
in interfaceJGenerable
-
build
void build(CodeWriter src, CodeWriter res) throws java.io.IOException
- Throws:
java.io.IOException
-
countArtifacts
int countArtifacts()
-
createJavaSourceFileWriter
private JFormatter createJavaSourceFileWriter(CodeWriter src, java.lang.String className) throws java.io.IOException
- Throws:
java.io.IOException
-
-