summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/ptree/ArrayAllocationExpression.java
blob: f5ad74dde0e9fb824ec3e80d56bc3b23dae4bd46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * ArrayAllocationExpression.java 1.0
 *
 *
 * Jun 20, 1997 by mich
 * Sep 29, 1997 by bv
 * Oct 10, 1997 by mich
 *
 * @see io.devnulllabs.openjava.ptree.ParseTree
 * @version 1.0 last updated:  Oct 10, 1997
 * @author  Michiaki Tatsubori
 */
package io.devnulllabs.openjava.ptree;

import io.devnulllabs.openjava.mop.Environment;
import io.devnulllabs.openjava.mop.OJClass;
import io.devnulllabs.openjava.ptree.util.ParseTreeVisitor;

/**
 * The <code>ArrayAllocationExpression</code> class represents
 * an expression which allocates a new array object.
 * <br>
 * This expression is like:
 * <br><blockquote><pre>
 *     new Object[2][3]
 * </pre></blockquote><br>
 * or:
 * <br><blockquote><pre>
 *     new String[]{ "this", "is", "a", "test" }
 * </pre></blockquote><br>
 * The latter is supported from JDK 1.1.
 *
 * @see io.devnulllabs.openjava.ptree.Expression
 * @see io.devnulllabs.openjava.ptree.TypeName
 * @see io.devnulllabs.openjava.ptree.ExpressionList
 * @see io.devnulllabs.openjava.ptree.ArrayInitializer
 */
public class ArrayAllocationExpression extends NonLeaf implements Expression {

    /**
     * Allocates a new ptree object.
     *
     * @param  typename  the type name.
     * @param  dimlist  the dimension expression list.
     */
    public ArrayAllocationExpression(
        TypeName typename,
        ExpressionList dimlist) {
        this(typename, dimlist, null);
    }

    /**
     * Allocates a new ptree object.
     *
     * @param  typename  the type name.
     * @param  dimlist  the dimension expression list.
     * @param  ainit  the array initializer.
     *                If this is null, no initializer will be
     *                provided this allocation with.
     */
    public ArrayAllocationExpression(
        TypeName typename,
        ExpressionList dimlist,
        ArrayInitializer ainit) {
        super();
        if (dimlist == null)
            dimlist = new ExpressionList();
        set(typename, dimlist, ainit);
    }

    public ArrayAllocationExpression(OJClass type, ExpressionList args) {
        this(TypeName.forOJClass(type), args);
    }

    public ArrayAllocationExpression(
        OJClass type,
        ExpressionList args,
        ArrayInitializer ainit) {
        this(TypeName.forOJClass(type), args, ainit);
    }

    ArrayAllocationExpression() {
        super();
    }

    /**
     * Gets the type name of the array.
     *
     * @return  the type name of the array.
     */
    public TypeName getTypeName() {
        return (TypeName) elementAt(0);
    }

    /**
     * Sets the type name of the array.
     *
     * @param  typename  the type name of the array.
     */
    public void setTypeName(TypeName typename) {
        setElementAt(typename, 0);
    }

    /**
     * Gets the dimexpr list of the array.
     *
     * @return  the dimexpr list of the array.
     */
    public ExpressionList getDimExprList() {
        return (ExpressionList) elementAt(1);
    }

    /**
     * Sets the dimexpr list of the array.
     *
     * @param  dimlist  the dimexpr list of the array.
     */
    public void setDimExprList(ExpressionList dimlist) {
        setElementAt(dimlist, 1);
    }

    /**
     * Gets the initializer of this array allocation.
     *
     * @return  the initializer.
     */
    public ArrayInitializer getInitializer() {
        return (ArrayInitializer) elementAt(2);
    }

    /**
     * Sets the initializer of this array allocation.
     *
     * @param  ainit  the initializer.
     *                If this is null, no initializer will be set.
     */
    public void setInitializer(ArrayInitializer ainit) {
        setElementAt(ainit, 2);
    }

    public OJClass getType(Environment env) throws Exception {
        TypeName tname = getTypeName();
        String dims = TypeName.stringFromDimension(getDimExprList().size());
        return env.lookupClass(env.toQualifiedName(tname + dims));
    }

    public void accept(ParseTreeVisitor v) throws ParseTreeException {
        v.visit(this);
    }

}