1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
4 | * |
5 | * This file is part of JavaParser. |
6 | * |
7 | * JavaParser can be used either under the terms of |
8 | * a) the GNU Lesser General Public License as published by |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. |
11 | * b) the terms of the Apache License |
12 | * |
13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
14 | * LICENCE.APACHE. Please refer to those files for details. |
15 | * |
16 | * JavaParser is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU Lesser General Public License for more details. |
20 | */ |
21 | package com.github.javaparser.ast.body; |
22 | |
23 | import com.github.javaparser.ast.AllFieldsConstructor; |
24 | import com.github.javaparser.ast.NodeList; |
25 | import com.github.javaparser.ast.expr.AnnotationExpr; |
26 | import com.github.javaparser.ast.expr.Expression; |
27 | import com.github.javaparser.ast.expr.SimpleName; |
28 | import com.github.javaparser.ast.nodeTypes.NodeWithArguments; |
29 | import com.github.javaparser.ast.nodeTypes.NodeWithJavadoc; |
30 | import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName; |
31 | import com.github.javaparser.ast.observer.ObservableProperty; |
32 | import com.github.javaparser.ast.visitor.GenericVisitor; |
33 | import com.github.javaparser.ast.visitor.VoidVisitor; |
34 | import static com.github.javaparser.utils.Utils.assertNotNull; |
35 | import com.github.javaparser.ast.Node; |
36 | import com.github.javaparser.ast.visitor.CloneVisitor; |
37 | import com.github.javaparser.metamodel.EnumConstantDeclarationMetaModel; |
38 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
39 | import com.github.javaparser.TokenRange; |
40 | import com.github.javaparser.resolution.Resolvable; |
41 | import com.github.javaparser.resolution.declarations.ResolvedEnumConstantDeclaration; |
42 | import java.util.function.Consumer; |
43 | import java.util.Optional; |
44 | import com.github.javaparser.ast.Generated; |
45 | |
46 | /** |
47 | * One of the values an enum can take. A(1) and B(2) in this example: {@code enum X { A(1), B(2) }} |
48 | * |
49 | * @author Julio Vilmar Gesser |
50 | */ |
51 | public class EnumConstantDeclaration extends BodyDeclaration<EnumConstantDeclaration> implements NodeWithJavadoc<EnumConstantDeclaration>, NodeWithSimpleName<EnumConstantDeclaration>, NodeWithArguments<EnumConstantDeclaration>, Resolvable<ResolvedEnumConstantDeclaration> { |
52 | |
53 | private SimpleName name; |
54 | |
55 | private NodeList<Expression> arguments; |
56 | |
57 | private NodeList<BodyDeclaration<?>> classBody; |
58 | |
59 | public EnumConstantDeclaration() { |
60 | this(null, new NodeList<>(), new SimpleName(), new NodeList<>(), new NodeList<>()); |
61 | } |
62 | |
63 | public EnumConstantDeclaration(String name) { |
64 | this(null, new NodeList<>(), new SimpleName(name), new NodeList<>(), new NodeList<>()); |
65 | } |
66 | |
67 | @AllFieldsConstructor |
68 | public EnumConstantDeclaration(NodeList<AnnotationExpr> annotations, SimpleName name, NodeList<Expression> arguments, NodeList<BodyDeclaration<?>> classBody) { |
69 | this(null, annotations, name, arguments, classBody); |
70 | } |
71 | |
72 | /** |
73 | * This constructor is used by the parser and is considered private. |
74 | */ |
75 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
76 | public EnumConstantDeclaration(TokenRange tokenRange, NodeList<AnnotationExpr> annotations, SimpleName name, NodeList<Expression> arguments, NodeList<BodyDeclaration<?>> classBody) { |
77 | super(tokenRange, annotations); |
78 | setName(name); |
79 | setArguments(arguments); |
80 | setClassBody(classBody); |
81 | customInitialization(); |
82 | } |
83 | |
84 | @Override |
85 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
86 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
87 | return v.visit(this, arg); |
88 | } |
89 | |
90 | @Override |
91 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
92 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
93 | v.visit(this, arg); |
94 | } |
95 | |
96 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
97 | public NodeList<Expression> getArguments() { |
98 | return arguments; |
99 | } |
100 | |
101 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
102 | public NodeList<BodyDeclaration<?>> getClassBody() { |
103 | return classBody; |
104 | } |
105 | |
106 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
107 | public SimpleName getName() { |
108 | return name; |
109 | } |
110 | |
111 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
112 | public EnumConstantDeclaration setArguments(final NodeList<Expression> arguments) { |
113 | assertNotNull(arguments); |
114 | if (arguments == this.arguments) { |
115 | return (EnumConstantDeclaration) this; |
116 | } |
117 | notifyPropertyChange(ObservableProperty.ARGUMENTS, this.arguments, arguments); |
118 | if (this.arguments != null) |
119 | this.arguments.setParentNode(null); |
120 | this.arguments = arguments; |
121 | setAsParentNodeOf(arguments); |
122 | return this; |
123 | } |
124 | |
125 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
126 | public EnumConstantDeclaration setClassBody(final NodeList<BodyDeclaration<?>> classBody) { |
127 | assertNotNull(classBody); |
128 | if (classBody == this.classBody) { |
129 | return (EnumConstantDeclaration) this; |
130 | } |
131 | notifyPropertyChange(ObservableProperty.CLASS_BODY, this.classBody, classBody); |
132 | if (this.classBody != null) |
133 | this.classBody.setParentNode(null); |
134 | this.classBody = classBody; |
135 | setAsParentNodeOf(classBody); |
136 | return this; |
137 | } |
138 | |
139 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
140 | public EnumConstantDeclaration setName(final SimpleName name) { |
141 | assertNotNull(name); |
142 | if (name == this.name) { |
143 | return (EnumConstantDeclaration) this; |
144 | } |
145 | notifyPropertyChange(ObservableProperty.NAME, this.name, name); |
146 | if (this.name != null) |
147 | this.name.setParentNode(null); |
148 | this.name = name; |
149 | setAsParentNodeOf(name); |
150 | return this; |
151 | } |
152 | |
153 | @Override |
154 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
155 | public boolean remove(Node node) { |
156 | if (node == null) |
157 | return false; |
158 | for (int i = 0; i < arguments.size(); i++) { |
159 | if (arguments.get(i) == node) { |
160 | arguments.remove(i); |
161 | return true; |
162 | } |
163 | } |
164 | for (int i = 0; i < classBody.size(); i++) { |
165 | if (classBody.get(i) == node) { |
166 | classBody.remove(i); |
167 | return true; |
168 | } |
169 | } |
170 | return super.remove(node); |
171 | } |
172 | |
173 | @Override |
174 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
175 | public EnumConstantDeclaration clone() { |
176 | return (EnumConstantDeclaration) accept(new CloneVisitor(), null); |
177 | } |
178 | |
179 | @Override |
180 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
181 | public EnumConstantDeclarationMetaModel getMetaModel() { |
182 | return JavaParserMetaModel.enumConstantDeclarationMetaModel; |
183 | } |
184 | |
185 | @Override |
186 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
187 | public boolean replace(Node node, Node replacementNode) { |
188 | if (node == null) |
189 | return false; |
190 | for (int i = 0; i < arguments.size(); i++) { |
191 | if (arguments.get(i) == node) { |
192 | arguments.set(i, (Expression) replacementNode); |
193 | return true; |
194 | } |
195 | } |
196 | for (int i = 0; i < classBody.size(); i++) { |
197 | if (classBody.get(i) == node) { |
198 | classBody.set(i, (BodyDeclaration) replacementNode); |
199 | return true; |
200 | } |
201 | } |
202 | if (node == name) { |
203 | setName((SimpleName) replacementNode); |
204 | return true; |
205 | } |
206 | return super.replace(node, replacementNode); |
207 | } |
208 | |
209 | @Override |
210 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
211 | public boolean isEnumConstantDeclaration() { |
212 | return true; |
213 | } |
214 | |
215 | @Override |
216 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
217 | public EnumConstantDeclaration asEnumConstantDeclaration() { |
218 | return this; |
219 | } |
220 | |
221 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
222 | public void ifEnumConstantDeclaration(Consumer<EnumConstantDeclaration> action) { |
223 | action.accept(this); |
224 | } |
225 | |
226 | @Override |
227 | public ResolvedEnumConstantDeclaration resolve() { |
228 | return getSymbolResolver().resolveDeclaration(this, ResolvedEnumConstantDeclaration.class); |
229 | } |
230 | |
231 | @Override |
232 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
233 | public Optional<EnumConstantDeclaration> toEnumConstantDeclaration() { |
234 | return Optional.of(this); |
235 | } |
236 | } |
237 |
Members