01: /*
02: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * 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:
31: /**
32: * array creation and initialization.
33: */
34: public final class JArray extends JExpressionImpl {
35:
36: private final JType type;
37: private final JExpression size;
38: private List<JExpression> exprs = null;
39:
40: /**
41: * Add an element to the array initializer
42: */
43: public JArray add(JExpression e) {
44: if (exprs == null)
45: exprs = new ArrayList<JExpression>();
46: exprs.add(e);
47: return this ;
48: }
49:
50: JArray(JType type, JExpression size) {
51: this .type = type;
52: this .size = size;
53: }
54:
55: public void generate(JFormatter f) {
56:
57: // generally we produce new T[x], but when T is an array type (T=T'[])
58: // then new T'[][x] is wrong. It has to be new T'[x][].
59: int arrayCount = 0;
60: JType t = type;
61:
62: while (t.isArray()) {
63: t = t.elementType();
64: arrayCount++;
65: }
66:
67: f.p("new").g(t).p('[');
68: if (size != null)
69: f.g(size);
70: f.p(']');
71:
72: for (int i = 0; i < arrayCount; i++)
73: f.p("[]");
74:
75: if ((size == null) || (exprs != null))
76: f.p('{');
77: if (exprs != null) {
78: f.g(exprs);
79: } else {
80: f.p(' ');
81: }
82: if ((size == null) || (exprs != null))
83: f.p('}');
84: }
85:
86: }
|