8
|
1 |
/*
|
|
2 |
* Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.codemodel.internal;
|
|
27 |
|
|
28 |
import java.lang.annotation.Annotation;
|
|
29 |
import java.util.ArrayList;
|
|
30 |
import java.util.List;
|
|
31 |
|
|
32 |
|
|
33 |
/**
|
|
34 |
* Variables and fields.
|
|
35 |
*/
|
|
36 |
|
|
37 |
public class JVar extends JExpressionImpl implements JDeclaration, JAssignmentTarget, JAnnotatable {
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Modifiers.
|
|
41 |
*/
|
|
42 |
private JMods mods;
|
|
43 |
|
|
44 |
/**
|
|
45 |
* JType of the variable
|
|
46 |
*/
|
|
47 |
private JType type;
|
|
48 |
|
|
49 |
/**
|
|
50 |
* Name of the variable
|
|
51 |
*/
|
|
52 |
private String name;
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Initialization of the variable in its declaration
|
|
56 |
*/
|
|
57 |
private JExpression init;
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Annotations on this variable. Lazily created.
|
|
61 |
*/
|
|
62 |
private List<JAnnotationUse> annotations = null;
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
/**
|
|
67 |
* JVar constructor
|
|
68 |
*
|
|
69 |
* @param type
|
|
70 |
* Datatype of this variable
|
|
71 |
*
|
|
72 |
* @param name
|
|
73 |
* Name of this variable
|
|
74 |
*
|
|
75 |
* @param init
|
|
76 |
* Value to initialize this variable to
|
|
77 |
*/
|
|
78 |
JVar(JMods mods, JType type, String name, JExpression init) {
|
|
79 |
this.mods = mods;
|
|
80 |
this.type = type;
|
|
81 |
this.name = name;
|
|
82 |
this.init = init;
|
|
83 |
}
|
|
84 |
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Initialize this variable
|
|
88 |
*
|
|
89 |
* @param init
|
|
90 |
* JExpression to be used to initialize this field
|
|
91 |
*/
|
|
92 |
public JVar init(JExpression init) {
|
|
93 |
this.init = init;
|
|
94 |
return this;
|
|
95 |
}
|
|
96 |
|
|
97 |
/**
|
|
98 |
* Get the name of this variable
|
|
99 |
*
|
|
100 |
* @return Name of the variable
|
|
101 |
*/
|
|
102 |
public String name() {
|
|
103 |
return name;
|
|
104 |
}
|
|
105 |
|
|
106 |
/**
|
|
107 |
* Changes the name of this variable.
|
|
108 |
*/
|
|
109 |
public void name(String name) {
|
|
110 |
if(!JJavaName.isJavaIdentifier(name))
|
|
111 |
throw new IllegalArgumentException();
|
|
112 |
this.name = name;
|
|
113 |
}
|
|
114 |
|
|
115 |
/**
|
|
116 |
* Return the type of this variable.
|
|
117 |
* @return
|
|
118 |
* always non-null.
|
|
119 |
*/
|
|
120 |
public JType type() {
|
|
121 |
return type;
|
|
122 |
}
|
|
123 |
|
|
124 |
/**
|
|
125 |
* @return
|
|
126 |
* the current modifiers of this method.
|
|
127 |
* Always return non-null valid object.
|
|
128 |
*/
|
|
129 |
public JMods mods() {
|
|
130 |
return mods;
|
|
131 |
}
|
|
132 |
|
|
133 |
/**
|
|
134 |
* Sets the type of this variable.
|
|
135 |
*
|
|
136 |
* @param newType
|
|
137 |
* must not be null.
|
|
138 |
*
|
|
139 |
* @return
|
|
140 |
* the old type value. always non-null.
|
|
141 |
*/
|
|
142 |
public JType type(JType newType) {
|
|
143 |
JType r = type;
|
|
144 |
if(newType==null)
|
|
145 |
throw new IllegalArgumentException();
|
|
146 |
type = newType;
|
|
147 |
return r;
|
|
148 |
}
|
|
149 |
|
|
150 |
|
|
151 |
/**
|
|
152 |
* Adds an annotation to this variable.
|
|
153 |
* @param clazz
|
|
154 |
* The annotation class to annotate the field with
|
|
155 |
*/
|
|
156 |
public JAnnotationUse annotate(JClass clazz){
|
|
157 |
if(annotations==null)
|
|
158 |
annotations = new ArrayList<JAnnotationUse>();
|
|
159 |
JAnnotationUse a = new JAnnotationUse(clazz);
|
|
160 |
annotations.add(a);
|
|
161 |
return a;
|
|
162 |
}
|
|
163 |
|
|
164 |
/**
|
|
165 |
* Adds an annotation to this variable.
|
|
166 |
*
|
|
167 |
* @param clazz
|
|
168 |
* The annotation class to annotate the field with
|
|
169 |
*/
|
|
170 |
public JAnnotationUse annotate(Class <? extends Annotation> clazz){
|
|
171 |
return annotate(type.owner().ref(clazz));
|
|
172 |
}
|
|
173 |
|
|
174 |
public <W extends JAnnotationWriter> W annotate2(Class<W> clazz) {
|
|
175 |
return TypedAnnotationWriter.create(clazz,this);
|
|
176 |
}
|
|
177 |
|
|
178 |
protected boolean isAnnotated() {
|
|
179 |
return annotations!=null;
|
|
180 |
}
|
|
181 |
|
|
182 |
public void bind(JFormatter f) {
|
|
183 |
if (annotations != null){
|
|
184 |
for( int i=0; i<annotations.size(); i++ )
|
|
185 |
f.g(annotations.get(i)).nl();
|
|
186 |
}
|
|
187 |
f.g(mods).g(type).id(name);
|
|
188 |
if (init != null)
|
|
189 |
f.p('=').g(init);
|
|
190 |
}
|
|
191 |
|
|
192 |
public void declare(JFormatter f) {
|
|
193 |
f.b(this).p(';').nl();
|
|
194 |
}
|
|
195 |
|
|
196 |
public void generate(JFormatter f) {
|
|
197 |
f.id(name);
|
|
198 |
}
|
|
199 |
|
|
200 |
|
|
201 |
public JExpression assign(JExpression rhs) {
|
|
202 |
return JExpr.assign(this,rhs);
|
|
203 |
}
|
|
204 |
public JExpression assignPlus(JExpression rhs) {
|
|
205 |
return JExpr.assignPlus(this,rhs);
|
|
206 |
}
|
|
207 |
|
|
208 |
}
|