Source code: openjava/ptree/ArrayAllocationExpression.java
1 /*
2 * ArrayAllocationExpression.java 1.0
3 *
4 *
5 * Jun 20, 1997 by mich
6 * Sep 29, 1997 by bv
7 * Oct 10, 1997 by mich
8 *
9 * @see openjava.ptree.ParseTree
10 * @version 1.0 last updated: Oct 10, 1997
11 * @author Michiaki Tatsubori
12 */
13 package openjava.ptree;
14
15 import openjava.mop.Environment;
16 import openjava.mop.OJClass;
17 import openjava.ptree.util.ParseTreeVisitor;
18
19 /**
20 * The <code>ArrayAllocationExpression</code> class represents
21 * an expression which allocates a new array object.
22 * <br>
23 * This expression is like:
24 * <br><blockquote><pre>
25 * new Object[2][3]
26 * </pre></blockquote><br>
27 * or:
28 * <br><blockquote><pre>
29 * new String[]{ "this", "is", "a", "test" }
30 * </pre></blockquote><br>
31 * The latter is supported from JDK 1.1.
32 *
33 * @see openjava.ptree.Expression
34 * @see openjava.ptree.TypeName
35 * @see openjava.ptree.ExpressionList
36 * @see openjava.ptree.ArrayInitializer
37 */
38 public class ArrayAllocationExpression extends NonLeaf implements Expression {
39
40 /**
41 * Allocates a new ptree object.
42 *
43 * @param typename the type name.
44 * @param dimlist the dimension expression list.
45 */
46 public ArrayAllocationExpression(
47 TypeName typename,
48 ExpressionList dimlist) {
49 this(typename, dimlist, null);
50 }
51
52 /**
53 * Allocates a new ptree object.
54 *
55 * @param typename the type name.
56 * @param dimlist the dimension expression list.
57 * @param ainit the array initializer.
58 * If this is null, no initializer will be
59 * provided this allocation with.
60 */
61 public ArrayAllocationExpression(
62 TypeName typename,
63 ExpressionList dimlist,
64 ArrayInitializer ainit) {
65 super();
66 if (dimlist == null)
67 dimlist = new ExpressionList();
68 set(typename, dimlist, ainit);
69 }
70
71 public ArrayAllocationExpression(OJClass type, ExpressionList args) {
72 this(TypeName.forOJClass(type), args);
73 }
74
75 public ArrayAllocationExpression(
76 OJClass type,
77 ExpressionList args,
78 ArrayInitializer ainit) {
79 this(TypeName.forOJClass(type), args, ainit);
80 }
81
82 ArrayAllocationExpression() {
83 super();
84 }
85
86 /**
87 * Gets the type name of the array.
88 *
89 * @return the type name of the array.
90 */
91 public TypeName getTypeName() {
92 return (TypeName) elementAt(0);
93 }
94
95 /**
96 * Sets the type name of the array.
97 *
98 * @param typename the type name of the array.
99 */
100 public void setTypeName(TypeName typename) {
101 setElementAt(typename, 0);
102 }
103
104 /**
105 * Gets the dimexpr list of the array.
106 *
107 * @return the dimexpr list of the array.
108 */
109 public ExpressionList getDimExprList() {
110 return (ExpressionList) elementAt(1);
111 }
112
113 /**
114 * Sets the dimexpr list of the array.
115 *
116 * @param dimlist the dimexpr list of the array.
117 */
118 public void setDimExprList(ExpressionList dimlist) {
119 setElementAt(dimlist, 1);
120 }
121
122 /**
123 * Gets the initializer of this array allocation.
124 *
125 * @return the initializer.
126 */
127 public ArrayInitializer getInitializer() {
128 return (ArrayInitializer) elementAt(2);
129 }
130
131 /**
132 * Sets the initializer of this array allocation.
133 *
134 * @param ainit the initializer.
135 * If this is null, no initializer will be set.
136 */
137 public void setInitializer(ArrayInitializer ainit) {
138 setElementAt(ainit, 2);
139 }
140
141 public OJClass getType(Environment env) throws Exception {
142 TypeName tname = getTypeName();
143 String dims = TypeName.stringFromDimension(getDimExprList().size());
144 return env.lookupClass(env.toQualifiedName(tname + dims));
145 }
146
147 public void accept(ParseTreeVisitor v) throws ParseTreeException {
148 v.visit(this);
149 }
150
151 }