001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: package com.sun.codemodel;
021:
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.lang.annotation.Annotation;
025:
026: /**
027: * Enum Constant.
028: *
029: * When used as an {@link JExpression}, this object represents a reference to the enum constant.
030: *
031: * @author
032: * Bhakti Mehta (Bhakti.Mehta@sun.com)
033: */
034: public final class JEnumConstant extends JExpressionImpl implements
035: JDeclaration, JAnnotatable {
036:
037: /**
038: * The constant.
039: */
040: private final String name;
041: /**
042: * The enum class.
043: */
044: private final JDefinedClass type;
045: /**
046: * javadoc comments, if any.
047: */
048: private JDocComment jdoc = null;
049:
050: /**
051: * Annotations on this variable. Lazily created.
052: */
053: private List<JAnnotationUse> annotations = null;
054:
055: /**
056: * List of the constructor argument expressions.
057: * Lazily constructed.
058: */
059: private List<JExpression> args = null;
060:
061: JEnumConstant(JDefinedClass type, String name) {
062: this .name = name;
063: this .type = type;
064: }
065:
066: /**
067: * Add an expression to this constructor's argument list
068: *
069: * @param arg
070: * Argument to add to argument list
071: */
072: public JEnumConstant arg(JExpression arg) {
073: if (arg == null)
074: throw new IllegalArgumentException();
075: if (args == null)
076: args = new ArrayList<JExpression>();
077: args.add(arg);
078: return this ;
079: }
080:
081: /**
082: * Returns the name of this constant.
083: *
084: * @return never null.
085: */
086: public String getName() {
087: return this .type.fullName().concat(".").concat(this .name);
088: }
089:
090: /**
091: * Creates, if necessary, and returns the enum constant javadoc.
092: *
093: * @return JDocComment containing javadocs for this constant.
094: */
095: public JDocComment javadoc() {
096: if (jdoc == null)
097: jdoc = new JDocComment(type.owner());
098: return jdoc;
099: }
100:
101: /**
102: * Adds an annotation to this variable.
103: * @param clazz
104: * The annotation class to annotate the field with
105: */
106: public JAnnotationUse annotate(JClass clazz) {
107: if (annotations == null)
108: annotations = new ArrayList<JAnnotationUse>();
109: JAnnotationUse a = new JAnnotationUse(clazz);
110: annotations.add(a);
111: return a;
112: }
113:
114: /**
115: * Adds an annotation to this variable.
116: *
117: * @param clazz
118: * The annotation class to annotate the field with
119: */
120: public JAnnotationUse annotate(Class<? extends Annotation> clazz) {
121: return annotate(type.owner().ref(clazz));
122: }
123:
124: public <W extends JAnnotationWriter> W annotate2(Class<W> clazz) {
125: return TypedAnnotationWriter.create(clazz, this );
126: }
127:
128: public void declare(JFormatter f) {
129: if (jdoc != null)
130: f.nl().g(jdoc);
131: if (annotations != null) {
132: for (int i = 0; i < annotations.size(); i++)
133: f.g(annotations.get(i)).nl();
134: }
135: f.id(name);
136: if (args != null) {
137: f.p('(').g(args).p(')');
138: }
139: }
140:
141: public void generate(JFormatter f) {
142: f.t(type).p('.').p(name);
143: }
144: }
|