summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/ptree/VariableDeclaration.java
blob: e29afb3324db72950922672eaa527defd223a74c (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
/*
 * VariableDeclaration.java 1.0
 *
 * This class is made to type ptree-node into the local variable
 * declaration statement in the body of class.
 *
 * 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.ptree.util.ParseTreeVisitor;

/**
 * The VariableDeclaration class presents
 * local variable declaration statement node of parse tree.
 *
 * @see io.devnulllabs.openjava.ptree.ParseTree
 * @see io.devnulllabs.openjava.ptree.NonLeaf
 * @see io.devnulllabs.openjava.ptree.Statement
 * @see io.devnulllabs.openjava.ptree.ModifierList
 * @see io.devnulllabs.openjava.ptree.TypeName
 * @see io.devnulllabs.openjava.ptree.VariableDeclarator
 * @see io.devnulllabs.openjava.ptree.VariableInitializer
 */
public class VariableDeclaration extends NonLeaf implements Statement {
    /**
     * Allocates a new object.
     *
     * @param  modifs  the modifier list of this variable declaration.
     * @param  typespec  the type specifier.
     * @param  vdeclr  the variable declarator.
     */
    public VariableDeclaration(
        ModifierList modifs,
        TypeName typespec,
        VariableDeclarator vdeclr) {
        super();
        if (modifs == null) {
            modifs = new ModifierList();
        }
        set(modifs, typespec, vdeclr);
    }

    /**
     * Allocates a new object.
     *
     * @param  typespec  the type specifier.
     * @param  vdeclr  the variable declarator.
     */
    public VariableDeclaration(TypeName typespec, VariableDeclarator vdeclr) {
        this(new ModifierList(), typespec, vdeclr);
    }

    /**
     * Allocates a new object.
     *
     * @param  modifs  the modifier list of this variable declaration.
     * @param  typespec  the type specifier.
     * @param  vname  the variable name.
     * @param  vinit  the variable initializer.
     */
    public VariableDeclaration(
        ModifierList modifs,
        TypeName typespec,
        String vname,
        VariableInitializer vinit) {
        this(modifs, typespec, new VariableDeclarator(vname, vinit));
    }

    /**
     * Allocates a new object.
     *
     * @param  modifs  the modifier list of this variable declaration.
     * @param  typespec  the type specifier.
     * @param  vname  the variable name.
     * @param  vinit  the variable initializer.
     */
    public VariableDeclaration(
        TypeName typespec,
        String vname,
        VariableInitializer vinit) {
        this(new ModifierList(), typespec, vname, vinit);
    }

    VariableDeclaration() {
        super();
    }

    /**
     * Gets the modifer list of this variable declaration.
     *
     * @return  the modifier list.
     */
    public ModifierList getModifiers() {
        return (ModifierList) elementAt(0);
    }

    /**
     * Sets the modifer list of this variable declaration.
     *
     * @param  modifs  the modifier list to set.
     */
    public void setModifiers(ModifierList modifs) {
        setElementAt(modifs, 0);
    }

    /**
     * Gets the type specifier of this variable declaration.
     * Any modification on obtained objects is never reflected on
     * this object.
     *
     * @return  the type specifier for this variable.
     */
    public TypeName getTypeSpecifier() {
        TypeName result;
        TypeName tn = (TypeName) elementAt(1);
        VariableDeclarator vd = (VariableDeclarator) elementAt(2);
        result = (TypeName) tn.makeCopy();
        result.addDimension(vd.getDimension());
        return result;
    }

    /**
     * Sets the type specifier of this variable declaration.
     *
     * @param  tspec  the type specifier to set.
     */
    public void setTypeSpecifier(TypeName tspec) {
        setElementAt(tspec, 1);
    }

    /**
     * Gets the variable declarator of this variable declaration.
     *
     * @return  the variable declarator.
     */
    public VariableDeclarator getVariableDeclarator() {
        return (VariableDeclarator) elementAt(2);
    }

    /**
     * Sets the variable declarator of this variable declaration.
     *
     * @param  vdeclr  the variable declarator to set.
     */
    public void setVariableDeclarator(VariableDeclarator vdeclr) {
        setElementAt(vdeclr, 2);
    }

    /**
     * Gets declarator name, declarator name includes variable name
     * but its dimension.
     *
     * @return declarator name
     */
    public String getVariable() {
        return getVariableDeclarator().getVariable();
    }

    /**
     * Sets declarator name, declarator name includes variable name
     * but its dimension.
     *
     * @param  name  declarator name to set.
     * @see  io.devnulllabs.openjava.ptree.TypeName
     */
    public void setVariable(String name) {
        getVariableDeclarator().setVariable(name);
    }

    /**
     * Gets variable initializer.
     *
     * @return variable initializer.
     */
    public VariableInitializer getInitializer() {
        return getVariableDeclarator().getInitializer();
    }

    /**
     * Sets variable initializer.
     *
     * @param  vinit  the variable initializer to set.
     */
    public void setInitializer(VariableInitializer vinit) {
        getVariableDeclarator().setInitializer(vinit);
    }

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

}