|
1 /* |
|
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.codemodel.internal; |
|
27 |
|
28 import java.util.ArrayList; |
|
29 import java.util.List; |
|
30 |
|
31 |
|
32 /** |
|
33 * array creation and initialization. |
|
34 */ |
|
35 public final class JArray extends JExpressionImpl { |
|
36 |
|
37 private final JType type; |
|
38 private final JExpression size; |
|
39 private List<JExpression> exprs = null; |
|
40 |
|
41 /** |
|
42 * Add an element to the array initializer |
|
43 */ |
|
44 public JArray add(JExpression e) { |
|
45 if (exprs == null) |
|
46 exprs = new ArrayList<JExpression>(); |
|
47 exprs.add(e); |
|
48 return this; |
|
49 } |
|
50 |
|
51 JArray(JType type, JExpression size) { |
|
52 this.type = type; |
|
53 this.size = size; |
|
54 } |
|
55 |
|
56 public void generate(JFormatter f) { |
|
57 |
|
58 // generally we produce new T[x], but when T is an array type (T=T'[]) |
|
59 // then new T'[][x] is wrong. It has to be new T'[x][]. |
|
60 int arrayCount = 0; |
|
61 JType t = type; |
|
62 |
|
63 while( t.isArray() ) { |
|
64 t = t.elementType(); |
|
65 arrayCount++; |
|
66 } |
|
67 |
|
68 f.p("new").g(t).p('['); |
|
69 if (size != null) |
|
70 f.g(size); |
|
71 f.p(']'); |
|
72 |
|
73 for( int i=0; i<arrayCount; i++ ) |
|
74 f.p("[]"); |
|
75 |
|
76 if ((size == null) || (exprs != null)) |
|
77 f.p('{'); |
|
78 if (exprs != null) { |
|
79 f.g(exprs); |
|
80 } else { |
|
81 f.p(' '); |
|
82 } |
|
83 if ((size == null) || (exprs != null)) |
|
84 f.p('}'); |
|
85 } |
|
86 |
|
87 } |