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:
021: package com.sun.codemodel;
022:
023: import java.lang.annotation.Annotation;
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: /**
028: * Variables and fields.
029: */
030:
031: public class JVar extends JExpressionImpl implements JDeclaration,
032: JAssignmentTarget, JAnnotatable {
033:
034: /**
035: * Modifiers.
036: */
037: private JMods mods;
038:
039: /**
040: * JType of the variable
041: */
042: private JType type;
043:
044: /**
045: * Name of the variable
046: */
047: private String name;
048:
049: /**
050: * Initialization of the variable in its declaration
051: */
052: private JExpression init;
053:
054: /**
055: * Annotations on this variable. Lazily created.
056: */
057: private List<JAnnotationUse> annotations = null;
058:
059: /**
060: * JVar constructor
061: *
062: * @param type
063: * Datatype of this variable
064: *
065: * @param name
066: * Name of this variable
067: *
068: * @param init
069: * Value to initialize this variable to
070: */
071: JVar(JMods mods, JType type, String name, JExpression init) {
072: this .mods = mods;
073: this .type = type;
074: this .name = name;
075: this .init = init;
076: }
077:
078: /**
079: * Initialize this variable
080: *
081: * @param init
082: * JExpression to be used to initialize this field
083: */
084: public JVar init(JExpression init) {
085: this .init = init;
086: return this ;
087: }
088:
089: /**
090: * Get the name of this variable
091: *
092: * @return Name of the variable
093: */
094: public String name() {
095: return name;
096: }
097:
098: /**
099: * Changes the name of this variable.
100: */
101: public void name(String name) {
102: if (!JJavaName.isJavaIdentifier(name))
103: throw new IllegalArgumentException();
104: this .name = name;
105: }
106:
107: /**
108: * Return the type of this variable.
109: * @return
110: * always non-null.
111: */
112: public JType type() {
113: return type;
114: }
115:
116: /**
117: * @return
118: * the current modifiers of this method.
119: * Always return non-null valid object.
120: */
121: public JMods mods() {
122: return mods;
123: }
124:
125: /**
126: * Sets the type of this variable.
127: *
128: * @param newType
129: * must not be null.
130: *
131: * @return
132: * the old type value. always non-null.
133: */
134: public JType type(JType newType) {
135: JType r = type;
136: if (newType == null)
137: throw new IllegalArgumentException();
138: type = newType;
139: return r;
140: }
141:
142: /**
143: * Adds an annotation to this variable.
144: * @param clazz
145: * The annotation class to annotate the field with
146: */
147: public JAnnotationUse annotate(JClass clazz) {
148: if (annotations == null)
149: annotations = new ArrayList<JAnnotationUse>();
150: JAnnotationUse a = new JAnnotationUse(clazz);
151: annotations.add(a);
152: return a;
153: }
154:
155: /**
156: * Adds an annotation to this variable.
157: *
158: * @param clazz
159: * The annotation class to annotate the field with
160: */
161: public JAnnotationUse annotate(Class<? extends Annotation> clazz) {
162: return annotate(type.owner().ref(clazz));
163: }
164:
165: public <W extends JAnnotationWriter> W annotate2(Class<W> clazz) {
166: return TypedAnnotationWriter.create(clazz, this );
167: }
168:
169: protected boolean isAnnotated() {
170: return annotations != null;
171: }
172:
173: public void bind(JFormatter f) {
174: if (annotations != null) {
175: for (int i = 0; i < annotations.size(); i++)
176: f.g(annotations.get(i)).nl();
177: }
178: f.g(mods).g(type).id(name);
179: if (init != null)
180: f.p('=').g(init);
181: }
182:
183: public void declare(JFormatter f) {
184: f.b(this ).p(';').nl();
185: }
186:
187: public void generate(JFormatter f) {
188: f.id(name);
189: }
190:
191: public JExpression assign(JExpression rhs) {
192: return JExpr.assign(this , rhs);
193: }
194:
195: public JExpression assignPlus(JExpression rhs) {
196: return JExpr.assignPlus(this, rhs);
197: }
198:
199: }
|