01: /*
02: * The contents of this file are subject to the terms
03: * of the Common Development and Distribution License
04: * (the "License"). You may not use this file except
05: * in compliance with the License.
06: *
07: * You can obtain a copy of the license at
08: * https://jwsdp.dev.java.net/CDDLv1.0.html
09: * See the License for the specific language governing
10: * permissions and limitations under the License.
11: *
12: * When distributing Covered Code, include this CDDL
13: * HEADER in each file and include the License file at
14: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
15: * add the following below this CDDL HEADER, with the
16: * fields enclosed by brackets "[]" replaced with your
17: * own identifying information: Portions Copyright [yyyy]
18: * [name of copyright owner]
19: */
20:
21: package com.sun.codemodel;
22:
23: import java.util.ArrayList;
24: import java.util.List;
25:
26: /**
27: * array creation and initialization.
28: */
29: public final class JArray extends JExpressionImpl {
30:
31: private final JType type;
32: private final JExpression size;
33: private List<JExpression> exprs = null;
34:
35: /**
36: * Add an element to the array initializer
37: */
38: public JArray add(JExpression e) {
39: if (exprs == null)
40: exprs = new ArrayList<JExpression>();
41: exprs.add(e);
42: return this ;
43: }
44:
45: JArray(JType type, JExpression size) {
46: this .type = type;
47: this .size = size;
48: }
49:
50: public void generate(JFormatter f) {
51:
52: // generally we produce new T[x], but when T is an array type (T=T'[])
53: // then new T'[][x] is wrong. It has to be new T'[x][].
54: int arrayCount = 0;
55: JType t = type;
56:
57: while (t.isArray()) {
58: t = t.elementType();
59: arrayCount++;
60: }
61:
62: f.p("new").g(t).p('[');
63: if (size != null)
64: f.g(size);
65: f.p(']');
66:
67: for (int i = 0; i < arrayCount; i++)
68: f.p("[]");
69:
70: if ((size == null) || (exprs != null))
71: f.p('{');
72: if (exprs != null) {
73: f.g(exprs);
74: } else {
75: f.p(' ');
76: }
77: if ((size == null) || (exprs != null))
78: f.p('}');
79: }
80:
81: }
|