| 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.expr; |
| 22 | |
| 23 | import com.github.javaparser.ast.AllFieldsConstructor; |
| 24 | import com.github.javaparser.ast.Modifier; |
| 25 | import com.github.javaparser.ast.Node; |
| 26 | import com.github.javaparser.ast.NodeList; |
| 27 | import com.github.javaparser.ast.body.VariableDeclarator; |
| 28 | import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations; |
| 29 | import com.github.javaparser.ast.nodeTypes.NodeWithVariables; |
| 30 | import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithFinalModifier; |
| 31 | import com.github.javaparser.ast.observer.ObservableProperty; |
| 32 | import com.github.javaparser.ast.type.Type; |
| 33 | import com.github.javaparser.ast.visitor.CloneVisitor; |
| 34 | import com.github.javaparser.ast.visitor.GenericVisitor; |
| 35 | import com.github.javaparser.ast.visitor.VoidVisitor; |
| 36 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
| 37 | import com.github.javaparser.metamodel.NonEmptyProperty; |
| 38 | import com.github.javaparser.metamodel.VariableDeclarationExprMetaModel; |
| 39 | import java.util.Arrays; |
| 40 | import java.util.stream.Collectors; |
| 41 | import static com.github.javaparser.ast.NodeList.nodeList; |
| 42 | import static com.github.javaparser.utils.Utils.assertNotNull; |
| 43 | import com.github.javaparser.TokenRange; |
| 44 | import java.util.function.Consumer; |
| 45 | import java.util.Optional; |
| 46 | import com.github.javaparser.ast.Generated; |
| 47 | |
| 48 | /** |
| 49 | * A declaration of variables. |
| 50 | * It is an expression, so it can be put in places like the initializer of a for loop, |
| 51 | * or the resources part of the try statement. |
| 52 | * <br>{@code final int x=3, y=55} |
| 53 | * |
| 54 | * <br>All annotations preceding the type will be set on this object, not on the type. |
| 55 | * JavaParser doesn't know if it they are applicable to the method or the type. |
| 56 | * |
| 57 | * @author Julio Vilmar Gesser |
| 58 | */ |
| 59 | public class VariableDeclarationExpr extends Expression implements NodeWithFinalModifier<VariableDeclarationExpr>, NodeWithAnnotations<VariableDeclarationExpr>, NodeWithVariables<VariableDeclarationExpr> { |
| 60 | |
| 61 | private NodeList<Modifier> modifiers; |
| 62 | |
| 63 | private NodeList<AnnotationExpr> annotations; |
| 64 | |
| 65 | @NonEmptyProperty |
| 66 | private NodeList<VariableDeclarator> variables; |
| 67 | |
| 68 | public VariableDeclarationExpr() { |
| 69 | this(null, new NodeList<>(), new NodeList<>(), new NodeList<>()); |
| 70 | } |
| 71 | |
| 72 | public VariableDeclarationExpr(final Type type, String variableName) { |
| 73 | this(null, new NodeList<>(), new NodeList<>(), nodeList(new VariableDeclarator(type, variableName))); |
| 74 | } |
| 75 | |
| 76 | public VariableDeclarationExpr(VariableDeclarator var) { |
| 77 | this(null, new NodeList<>(), new NodeList<>(), nodeList(var)); |
| 78 | } |
| 79 | |
| 80 | public VariableDeclarationExpr(final Type type, String variableName, Modifier... modifiers) { |
| 81 | this(null, Arrays.stream(modifiers).collect(Collectors.toCollection(() -> new NodeList<>())), new NodeList<>(), nodeList(new VariableDeclarator(type, variableName))); |
| 82 | } |
| 83 | |
| 84 | public VariableDeclarationExpr(VariableDeclarator var, Modifier... modifiers) { |
| 85 | this(null, Arrays.stream(modifiers).collect(Collectors.toCollection(() -> new NodeList<>())), new NodeList<>(), nodeList(var)); |
| 86 | } |
| 87 | |
| 88 | public VariableDeclarationExpr(final NodeList<VariableDeclarator> variables) { |
| 89 | this(null, new NodeList<>(), new NodeList<>(), variables); |
| 90 | } |
| 91 | |
| 92 | public VariableDeclarationExpr(final NodeList<Modifier> modifiers, final NodeList<VariableDeclarator> variables) { |
| 93 | this(null, modifiers, new NodeList<>(), variables); |
| 94 | } |
| 95 | |
| 96 | @AllFieldsConstructor |
| 97 | public VariableDeclarationExpr(final NodeList<Modifier> modifiers, final NodeList<AnnotationExpr> annotations, final NodeList<VariableDeclarator> variables) { |
| 98 | this(null, modifiers, annotations, variables); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * This constructor is used by the parser and is considered private. |
| 103 | */ |
| 104 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 105 | public VariableDeclarationExpr(TokenRange tokenRange, NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<VariableDeclarator> variables) { |
| 106 | super(tokenRange); |
| 107 | setModifiers(modifiers); |
| 108 | setAnnotations(annotations); |
| 109 | setVariables(variables); |
| 110 | customInitialization(); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 115 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
| 116 | return v.visit(this, arg); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 121 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
| 122 | v.visit(this, arg); |
| 123 | } |
| 124 | |
| 125 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 126 | public NodeList<AnnotationExpr> getAnnotations() { |
| 127 | return annotations; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Return the modifiers of this variable declaration. |
| 132 | * |
| 133 | * @return modifiers |
| 134 | * @see Modifier |
| 135 | */ |
| 136 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 137 | public NodeList<Modifier> getModifiers() { |
| 138 | return modifiers; |
| 139 | } |
| 140 | |
| 141 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 142 | public NodeList<VariableDeclarator> getVariables() { |
| 143 | return variables; |
| 144 | } |
| 145 | |
| 146 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 147 | public VariableDeclarationExpr setAnnotations(final NodeList<AnnotationExpr> annotations) { |
| 148 | assertNotNull(annotations); |
| 149 | if (annotations == this.annotations) { |
| 150 | return (VariableDeclarationExpr) this; |
| 151 | } |
| 152 | notifyPropertyChange(ObservableProperty.ANNOTATIONS, this.annotations, annotations); |
| 153 | if (this.annotations != null) |
| 154 | this.annotations.setParentNode(null); |
| 155 | this.annotations = annotations; |
| 156 | setAsParentNodeOf(annotations); |
| 157 | return this; |
| 158 | } |
| 159 | |
| 160 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 161 | public VariableDeclarationExpr setModifiers(final NodeList<Modifier> modifiers) { |
| 162 | assertNotNull(modifiers); |
| 163 | if (modifiers == this.modifiers) { |
| 164 | return (VariableDeclarationExpr) this; |
| 165 | } |
| 166 | notifyPropertyChange(ObservableProperty.MODIFIERS, this.modifiers, modifiers); |
| 167 | if (this.modifiers != null) |
| 168 | this.modifiers.setParentNode(null); |
| 169 | this.modifiers = modifiers; |
| 170 | setAsParentNodeOf(modifiers); |
| 171 | return this; |
| 172 | } |
| 173 | |
| 174 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 175 | public VariableDeclarationExpr setVariables(final NodeList<VariableDeclarator> variables) { |
| 176 | assertNotNull(variables); |
| 177 | if (variables == this.variables) { |
| 178 | return (VariableDeclarationExpr) this; |
| 179 | } |
| 180 | notifyPropertyChange(ObservableProperty.VARIABLES, this.variables, variables); |
| 181 | if (this.variables != null) |
| 182 | this.variables.setParentNode(null); |
| 183 | this.variables = variables; |
| 184 | setAsParentNodeOf(variables); |
| 185 | return this; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 190 | public boolean remove(Node node) { |
| 191 | if (node == null) |
| 192 | return false; |
| 193 | for (int i = 0; i < annotations.size(); i++) { |
| 194 | if (annotations.get(i) == node) { |
| 195 | annotations.remove(i); |
| 196 | return true; |
| 197 | } |
| 198 | } |
| 199 | for (int i = 0; i < modifiers.size(); i++) { |
| 200 | if (modifiers.get(i) == node) { |
| 201 | modifiers.remove(i); |
| 202 | return true; |
| 203 | } |
| 204 | } |
| 205 | for (int i = 0; i < variables.size(); i++) { |
| 206 | if (variables.get(i) == node) { |
| 207 | variables.remove(i); |
| 208 | return true; |
| 209 | } |
| 210 | } |
| 211 | return super.remove(node); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 216 | public VariableDeclarationExpr clone() { |
| 217 | return (VariableDeclarationExpr) accept(new CloneVisitor(), null); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 222 | public VariableDeclarationExprMetaModel getMetaModel() { |
| 223 | return JavaParserMetaModel.variableDeclarationExprMetaModel; |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 228 | public boolean replace(Node node, Node replacementNode) { |
| 229 | if (node == null) |
| 230 | return false; |
| 231 | for (int i = 0; i < annotations.size(); i++) { |
| 232 | if (annotations.get(i) == node) { |
| 233 | annotations.set(i, (AnnotationExpr) replacementNode); |
| 234 | return true; |
| 235 | } |
| 236 | } |
| 237 | for (int i = 0; i < modifiers.size(); i++) { |
| 238 | if (modifiers.get(i) == node) { |
| 239 | modifiers.set(i, (Modifier) replacementNode); |
| 240 | return true; |
| 241 | } |
| 242 | } |
| 243 | for (int i = 0; i < variables.size(); i++) { |
| 244 | if (variables.get(i) == node) { |
| 245 | variables.set(i, (VariableDeclarator) replacementNode); |
| 246 | return true; |
| 247 | } |
| 248 | } |
| 249 | return super.replace(node, replacementNode); |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 254 | public boolean isVariableDeclarationExpr() { |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 260 | public VariableDeclarationExpr asVariableDeclarationExpr() { |
| 261 | return this; |
| 262 | } |
| 263 | |
| 264 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 265 | public void ifVariableDeclarationExpr(Consumer<VariableDeclarationExpr> action) { |
| 266 | action.accept(this); |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 271 | public Optional<VariableDeclarationExpr> toVariableDeclarationExpr() { |
| 272 | return Optional.of(this); |
| 273 | } |
| 274 | } |
| 275 |
Members