author | asaha |
Fri, 07 Aug 2009 11:27:00 -0700 | |
changeset 3530 | 18fb7507984b |
parent 2678 | 57cf2a1c1a05 |
permissions | -rw-r--r-- |
8 | 1 |
/* |
2678
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
2 |
* Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
8 | 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.util.ArrayList; |
|
29 |
import java.util.List; |
|
30 |
import java.util.Collections; |
|
31 |
||
32 |
/** |
|
33 |
* A block of Java code, which may contain statements and local declarations. |
|
34 |
* |
|
35 |
* <p> |
|
36 |
* {@link JBlock} contains a large number of factory methods that creates new |
|
37 |
* statements/declarations. Those newly created statements/declarations are |
|
38 |
* inserted into the {@link #pos() "current position"}. The position advances |
|
39 |
* one every time you add a new instruction. |
|
40 |
*/ |
|
41 |
public final class JBlock implements JGenerable, JStatement { |
|
42 |
||
43 |
/** |
|
44 |
* Declarations and statements contained in this block. |
|
45 |
* Either {@link JStatement} or {@link JDeclaration}. |
|
46 |
*/ |
|
47 |
private final List<Object> content = new ArrayList<Object>(); |
|
48 |
||
49 |
/** |
|
50 |
* Whether or not this block must be braced and indented |
|
51 |
*/ |
|
52 |
private boolean bracesRequired = true; |
|
53 |
private boolean indentRequired = true; |
|
54 |
||
55 |
/** |
|
56 |
* Current position. |
|
57 |
*/ |
|
58 |
private int pos; |
|
59 |
||
60 |
public JBlock() { |
|
61 |
this(true,true); |
|
62 |
} |
|
63 |
||
64 |
public JBlock(boolean bracesRequired, boolean indentRequired) { |
|
65 |
this.bracesRequired = bracesRequired; |
|
66 |
this.indentRequired = indentRequired; |
|
67 |
} |
|
68 |
||
69 |
/** |
|
70 |
* Returns a read-only view of {@link JStatement}s and {@link JDeclaration} |
|
71 |
* in this block. |
|
72 |
*/ |
|
73 |
public List<Object> getContents() { |
|
74 |
return Collections.unmodifiableList(content); |
|
75 |
} |
|
76 |
||
77 |
private <T> T insert( T statementOrDeclaration ) { |
|
78 |
content.add(pos,statementOrDeclaration); |
|
79 |
pos++; |
|
80 |
return statementOrDeclaration; |
|
81 |
} |
|
82 |
||
83 |
/** |
|
84 |
* Gets the current position to which new statements will be inserted. |
|
85 |
* |
|
86 |
* For example if the value is 0, newly created instructions will be |
|
87 |
* inserted at the very beginning of the block. |
|
88 |
* |
|
89 |
* @see #pos(int) |
|
90 |
*/ |
|
91 |
public int pos() { |
|
92 |
return pos; |
|
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* Sets the current position. |
|
97 |
* |
|
98 |
* @return |
|
99 |
* the old value of the current position. |
|
100 |
* @throws IllegalArgumentException |
|
101 |
* if the new position value is illegal. |
|
102 |
* |
|
103 |
* @see #pos() |
|
104 |
*/ |
|
105 |
public int pos(int newPos) { |
|
106 |
int r = pos; |
|
107 |
if(newPos>content.size() || newPos<0) |
|
108 |
throw new IllegalArgumentException(); |
|
109 |
pos = newPos; |
|
110 |
||
111 |
return r; |
|
112 |
} |
|
113 |
||
3530 | 114 |
/** |
115 |
* Returns true if this block is empty and does not contain |
|
116 |
* any statement. |
|
117 |
*/ |
|
118 |
public boolean isEmpty() { |
|
119 |
return content.isEmpty(); |
|
120 |
} |
|
121 |
||
8 | 122 |
|
123 |
/** |
|
124 |
* Adds a local variable declaration to this block |
|
125 |
* |
|
126 |
* @param type |
|
127 |
* JType of the variable |
|
128 |
* |
|
129 |
* @param name |
|
130 |
* Name of the variable |
|
131 |
* |
|
132 |
* @return Newly generated JVar |
|
133 |
*/ |
|
134 |
public JVar decl(JType type, String name) { |
|
135 |
return decl(JMod.NONE, type, name, null); |
|
136 |
} |
|
137 |
||
138 |
/** |
|
139 |
* Adds a local variable declaration to this block |
|
140 |
* |
|
141 |
* @param type |
|
142 |
* JType of the variable |
|
143 |
* |
|
144 |
* @param name |
|
145 |
* Name of the variable |
|
146 |
* |
|
147 |
* @param init |
|
148 |
* Initialization expression for this variable. May be null. |
|
149 |
* |
|
150 |
* @return Newly generated JVar |
|
151 |
*/ |
|
152 |
public JVar decl(JType type, String name, JExpression init) { |
|
153 |
return decl(JMod.NONE, type, name, init); |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* Adds a local variable declaration to this block |
|
158 |
* |
|
159 |
* @param mods |
|
160 |
* Modifiers for the variable |
|
161 |
* |
|
162 |
* @param type |
|
163 |
* JType of the variable |
|
164 |
* |
|
165 |
* @param name |
|
166 |
* Name of the variable |
|
167 |
* |
|
168 |
* @param init |
|
169 |
* Initialization expression for this variable. May be null. |
|
170 |
* |
|
171 |
* @return Newly generated JVar |
|
172 |
*/ |
|
173 |
public JVar decl(int mods, JType type, String name, JExpression init) { |
|
174 |
JVar v = new JVar(JMods.forVar(mods), type, name, init); |
|
175 |
insert(v); |
|
176 |
bracesRequired = true; |
|
177 |
indentRequired = true; |
|
178 |
return v; |
|
179 |
} |
|
180 |
||
181 |
/** |
|
182 |
* Creates an assignment statement and adds it to this block. |
|
183 |
* |
|
184 |
* @param lhs |
|
185 |
* Assignable variable or field for left hand side of expression |
|
186 |
* |
|
187 |
* @param exp |
|
188 |
* Right hand side expression |
|
189 |
*/ |
|
190 |
public JBlock assign(JAssignmentTarget lhs, JExpression exp) { |
|
191 |
insert(new JAssignment(lhs, exp)); |
|
192 |
return this; |
|
193 |
} |
|
194 |
||
195 |
public JBlock assignPlus(JAssignmentTarget lhs, JExpression exp) { |
|
196 |
insert(new JAssignment(lhs, exp, "+")); |
|
197 |
return this; |
|
198 |
} |
|
199 |
||
200 |
/** |
|
201 |
* Creates an invocation statement and adds it to this block. |
|
202 |
* |
|
203 |
* @param expr |
|
204 |
* JExpression evaluating to the class or object upon which |
|
205 |
* the named method will be invoked |
|
206 |
* |
|
207 |
* @param method |
|
208 |
* Name of method to invoke |
|
209 |
* |
|
210 |
* @return Newly generated JInvocation |
|
211 |
*/ |
|
212 |
public JInvocation invoke(JExpression expr, String method) { |
|
213 |
JInvocation i = new JInvocation(expr, method); |
|
214 |
insert(i); |
|
215 |
return i; |
|
216 |
} |
|
217 |
||
218 |
/** |
|
219 |
* Creates an invocation statement and adds it to this block. |
|
220 |
* |
|
221 |
* @param expr |
|
222 |
* JExpression evaluating to the class or object upon which |
|
223 |
* the method will be invoked |
|
224 |
* |
|
225 |
* @param method |
|
226 |
* JMethod to invoke |
|
227 |
* |
|
228 |
* @return Newly generated JInvocation |
|
229 |
*/ |
|
230 |
public JInvocation invoke(JExpression expr, JMethod method) { |
|
2678
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
231 |
return insert(new JInvocation(expr, method)); |
8 | 232 |
} |
233 |
||
234 |
/** |
|
235 |
* Creates a static invocation statement. |
|
236 |
*/ |
|
237 |
public JInvocation staticInvoke(JClass type, String method) { |
|
2678
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
238 |
return insert(new JInvocation(type, method)); |
8 | 239 |
} |
240 |
||
241 |
/** |
|
242 |
* Creates an invocation statement and adds it to this block. |
|
243 |
* |
|
244 |
* @param method |
|
245 |
* Name of method to invoke |
|
246 |
* |
|
247 |
* @return Newly generated JInvocation |
|
248 |
*/ |
|
249 |
public JInvocation invoke(String method) { |
|
2678
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
250 |
return insert(new JInvocation((JExpression)null, method)); |
8 | 251 |
} |
252 |
||
253 |
/** |
|
254 |
* Creates an invocation statement and adds it to this block. |
|
255 |
* |
|
256 |
* @param method |
|
257 |
* JMethod to invoke |
|
258 |
* |
|
259 |
* @return Newly generated JInvocation |
|
260 |
*/ |
|
261 |
public JInvocation invoke(JMethod method) { |
|
2678
57cf2a1c1a05
6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents:
8
diff
changeset
|
262 |
return insert(new JInvocation((JExpression)null, method)); |
8 | 263 |
} |
264 |
||
265 |
/** |
|
266 |
* Adds a statement to this block |
|
267 |
* |
|
268 |
* @param s |
|
269 |
* JStatement to be added |
|
270 |
* |
|
271 |
* @return This block |
|
272 |
*/ |
|
273 |
public JBlock add(JStatement s) { // ## Needed? |
|
274 |
insert(s); |
|
275 |
return this; |
|
276 |
} |
|
277 |
||
278 |
/** |
|
279 |
* Create an If statement and add it to this block |
|
280 |
* |
|
281 |
* @param expr |
|
282 |
* JExpression to be tested to determine branching |
|
283 |
* |
|
284 |
* @return Newly generated conditional statement |
|
285 |
*/ |
|
286 |
public JConditional _if(JExpression expr) { |
|
287 |
return insert(new JConditional(expr)); |
|
288 |
} |
|
289 |
||
290 |
/** |
|
291 |
* Create a For statement and add it to this block |
|
292 |
* |
|
293 |
* @return Newly generated For statement |
|
294 |
*/ |
|
295 |
public JForLoop _for() { |
|
296 |
return insert(new JForLoop()); |
|
297 |
} |
|
298 |
||
299 |
/** |
|
300 |
* Create a While statement and add it to this block |
|
301 |
* |
|
302 |
* @return Newly generated While statement |
|
303 |
*/ |
|
304 |
public JWhileLoop _while(JExpression test) { |
|
305 |
return insert(new JWhileLoop(test)); |
|
306 |
} |
|
307 |
||
308 |
/** |
|
309 |
* Create a switch/case statement and add it to this block |
|
310 |
*/ |
|
311 |
public JSwitch _switch(JExpression test) { |
|
312 |
return insert(new JSwitch(test)); |
|
313 |
} |
|
314 |
||
315 |
/** |
|
316 |
* Create a Do statement and add it to this block |
|
317 |
* |
|
318 |
* @return Newly generated Do statement |
|
319 |
*/ |
|
320 |
public JDoLoop _do(JExpression test) { |
|
321 |
return insert(new JDoLoop(test)); |
|
322 |
} |
|
323 |
||
324 |
/** |
|
325 |
* Create a Try statement and add it to this block |
|
326 |
* |
|
327 |
* @return Newly generated Try statement |
|
328 |
*/ |
|
329 |
public JTryBlock _try() { |
|
330 |
return insert(new JTryBlock()); |
|
331 |
} |
|
332 |
||
333 |
/** |
|
334 |
* Create a return statement and add it to this block |
|
335 |
*/ |
|
336 |
public void _return() { |
|
337 |
insert(new JReturn(null)); |
|
338 |
} |
|
339 |
||
340 |
/** |
|
341 |
* Create a return statement and add it to this block |
|
342 |
*/ |
|
343 |
public void _return(JExpression exp) { |
|
344 |
insert(new JReturn(exp)); |
|
345 |
} |
|
346 |
||
347 |
/** |
|
348 |
* Create a throw statement and add it to this block |
|
349 |
*/ |
|
350 |
public void _throw(JExpression exp) { |
|
351 |
insert(new JThrow(exp)); |
|
352 |
} |
|
353 |
||
354 |
/** |
|
355 |
* Create a break statement and add it to this block |
|
356 |
*/ |
|
357 |
public void _break() { |
|
358 |
_break(null); |
|
359 |
} |
|
360 |
||
361 |
public void _break(JLabel label) { |
|
362 |
insert(new JBreak(label)); |
|
363 |
} |
|
364 |
||
365 |
/** |
|
366 |
* Create a label, which can be referenced from |
|
367 |
* <code>continue</code> and <code>break</code> statements. |
|
368 |
*/ |
|
369 |
public JLabel label(String name) { |
|
370 |
JLabel l = new JLabel(name); |
|
371 |
insert(l); |
|
372 |
return l; |
|
373 |
} |
|
374 |
||
375 |
/** |
|
376 |
* Create a continue statement and add it to this block |
|
377 |
*/ |
|
378 |
public void _continue(JLabel label) { |
|
379 |
insert(new JContinue(label)); |
|
380 |
} |
|
381 |
||
382 |
public void _continue() { |
|
383 |
_continue(null); |
|
384 |
} |
|
385 |
||
386 |
/** |
|
387 |
* Create a sub-block and add it to this block |
|
388 |
*/ |
|
389 |
public JBlock block() { |
|
390 |
JBlock b = new JBlock(); |
|
391 |
b.bracesRequired = false; |
|
392 |
b.indentRequired = false; |
|
393 |
return insert(b); |
|
394 |
} |
|
395 |
||
396 |
/** |
|
397 |
* Creates a "literal" statement directly. |
|
398 |
* |
|
399 |
* <p> |
|
400 |
* Specified string is printed as-is. |
|
401 |
* This is useful as a short-cut. |
|
402 |
* |
|
403 |
* <p> |
|
404 |
* For example, you can invoke this method as: |
|
405 |
* <code>directStatement("a=b+c;")</code>. |
|
406 |
*/ |
|
407 |
public JStatement directStatement(final String source) { |
|
408 |
JStatement s = new JStatement() { |
|
409 |
public void state(JFormatter f) { |
|
410 |
f.p(source).nl(); |
|
411 |
} |
|
412 |
}; |
|
413 |
add(s); |
|
414 |
return s; |
|
415 |
} |
|
416 |
||
417 |
public void generate(JFormatter f) { |
|
418 |
if (bracesRequired) |
|
419 |
f.p('{').nl(); |
|
420 |
if (indentRequired) |
|
421 |
f.i(); |
|
422 |
generateBody(f); |
|
423 |
if (indentRequired) |
|
424 |
f.o(); |
|
425 |
if (bracesRequired) |
|
426 |
f.p('}'); |
|
427 |
} |
|
428 |
||
429 |
void generateBody(JFormatter f) { |
|
430 |
for (Object o : content) { |
|
431 |
if (o instanceof JDeclaration) |
|
432 |
f.d((JDeclaration) o); |
|
433 |
else |
|
434 |
f.s((JStatement) o); |
|
435 |
} |
|
436 |
} |
|
437 |
||
438 |
/** |
|
439 |
* Creates an enhanced For statement based on j2se 1.5 JLS |
|
440 |
* and add it to this block |
|
441 |
* |
|
442 |
* @return Newly generated enhanced For statement per j2se 1.5 |
|
443 |
* specification |
|
444 |
*/ |
|
445 |
public JForEach forEach(JType varType, String name, JExpression collection) { |
|
446 |
return insert(new JForEach( varType, name, collection)); |
|
447 |
||
448 |
} |
|
449 |
public void state(JFormatter f) { |
|
450 |
f.g(this); |
|
451 |
if (bracesRequired) |
|
452 |
f.nl(); |
|
453 |
} |
|
454 |
||
455 |
} |