summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/ptree/AllocationExpression.java
blob: 3aea1e2fd93cc221319283928e735ce1a886dafe (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * AllocationExpression.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>AllocationExpression</code> class represents
 * an expression which allocates a new object with its constructor.
 * <br>
 * This expression is like:
 * <br><blockquote><pre>
 *     new String( "test" )
 * </pre></blockquote><br>
 * or:
 * <br><blockquote><pre>
 *     new String( "test" ){
 *             public void hoge(){ ... }
 *             ...
 *         }
 * </pre></blockquote><br>
 * The latter is supported from JDK 1.1, is called an anoymous class
 * of the inner class.
 *
 * @see io.devnulllabs.openjava.ptree.Expression
 * @see io.devnulllabs.openjava.ptree.TypeName
 * @see io.devnulllabs.openjava.ptree.ExpressionList
 * @see io.devnulllabs.openjava.ptree.MemberDeclarationList
 */
public class AllocationExpression extends NonLeaf implements Expression {

    /**
     * Allocates a new object with the class body.
     *
     * @param  ctype  a class name to be constructed
     * @param  args  argument list of constructor
     * @param  mdlst  the class body.
     *                If this is null, no class body will be provided
     *                this construct expression with.
     */
    public AllocationExpression(
        Expression encloser,
        TypeName typename,
        ExpressionList args,
        MemberDeclarationList mdlst) {
        super();
        if (args == null)
            args = new ExpressionList();
        /* an explicitly specified null has meaning of no body */
        //if (mdlst == null)  mdlst = new MemberDeclarationList();
        set(typename, args, mdlst, encloser);
    }

    /**
     * Allocates a new object with the class body.
     *
     * @param  ctype  a class name to be constructed
     * @param  args  argument list of constructor
     * @param  mdlst  the class body.
     *                If this is null, no class body will be provided
     *                this construct expression with.
     */
    public AllocationExpression(
        TypeName typename,
        ExpressionList args,
        MemberDeclarationList mdlst) {
        this(null, typename, args, mdlst);
    }

    /**
     * Allocates a new object with the class body.
     *
     * @param  ctype  a class name to be constructed
     * @param  args  argument list of constructor
     * @param  mdlst  the class body.
     *                If this is null, no class body will be provided
     *                this construct expression with.
     */
    public AllocationExpression(
        Expression encloser,
        TypeName typename,
        ExpressionList args) {
        this(encloser, typename, args, null);
    }

    /**
     * Allocates a new object without class body.
     *
     * @param  ctype  a class name to be constructed
     * @param  args  argument list of constructor
     */
    public AllocationExpression(TypeName ctype, ExpressionList args) {
        this(ctype, args, null);
    }

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

    AllocationExpression() {
        super();
    }

    /**
     * Gets the expression of enclosing object.
     *
     * @return  the expression of enclosing object
     */
    public Expression getEncloser() {
        return (Expression) elementAt(3);
    }

    /**
     * Sets the expression of enclosing object.
     *
     * @param  encloser  the expression of enclosing object
     */
    public void setEncloser(Expression encloser) {
        setElementAt(encloser, 3);
    }

    /**
     * Gets the class type of this constructor.
     *
     * @return  the class type of this constructor.
     */
    public TypeName getClassType() {
        return (TypeName) elementAt(0);
    }

    /**
     * Sets the class type of this constructor.
     *
     * @param  ctype  the class body to set.
     */
    public void setClassType(TypeName ctype) {
        setElementAt(ctype, 0);
    }

    /**
     * Gets the arguments of this constructor.
     *
     * @return  the arguments as an expressions list.
     */
    public ExpressionList getArguments() {
        return (ExpressionList) elementAt(1);
    }

    /**
     * Sets the arguments of this constructor.
     *
     * @return  the expressions list of arguments.
     */
    public void setArguments(ExpressionList args) {
        if (args == null) {
            args = new ExpressionList();
        }
        setElementAt(args, 1);
    }

    /**
     * Gets the class body of this constructor.
     *
     * @return  the member declaration list as the class body of
     *          this constructor.
     */
    public MemberDeclarationList getClassBody() {
        return (MemberDeclarationList) elementAt(2);
    }

    /**
     * Sets the class body of this constructor.
     *
     * @param  mdlist  the member declaration list of the class body.
     *                 If this is null, the class body will disappear.
     */
    public void setClassBody(MemberDeclarationList mdlist) {
        setElementAt(mdlist, 2);
    }

    public OJClass getType(Environment env) throws Exception {
        String typename = env.toQualifiedName(getClassType().toString());
        OJClass result = env.lookupClass(typename);
        return result;
    }

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

}