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; |
22 | |
23 | import com.github.javaparser.ast.expr.AnnotationExpr; |
24 | import com.github.javaparser.ast.expr.Expression; |
25 | import com.github.javaparser.ast.expr.IntegerLiteralExpr; |
26 | import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations; |
27 | import com.github.javaparser.ast.observer.ObservableProperty; |
28 | import com.github.javaparser.ast.visitor.GenericVisitor; |
29 | import com.github.javaparser.ast.visitor.VoidVisitor; |
30 | import java.util.Optional; |
31 | import static com.github.javaparser.utils.Utils.assertNotNull; |
32 | import com.github.javaparser.ast.visitor.CloneVisitor; |
33 | import com.github.javaparser.metamodel.ArrayCreationLevelMetaModel; |
34 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
35 | import com.github.javaparser.TokenRange; |
36 | import com.github.javaparser.metamodel.OptionalProperty; |
37 | import com.github.javaparser.ast.Node; |
38 | import com.github.javaparser.ast.Generated; |
39 | |
40 | /** |
41 | * In {@code new int[1][2];} there are two ArrayCreationLevel objects, |
42 | * the first one contains the expression "1", |
43 | * the second the expression "2". |
44 | */ |
45 | public class ArrayCreationLevel extends Node implements NodeWithAnnotations<ArrayCreationLevel> { |
46 | |
47 | @OptionalProperty |
48 | private Expression dimension; |
49 | |
50 | private NodeList<AnnotationExpr> annotations = new NodeList<>(); |
51 | |
52 | public ArrayCreationLevel() { |
53 | this(null, null, new NodeList<>()); |
54 | } |
55 | |
56 | public ArrayCreationLevel(int dimension) { |
57 | this(null, new IntegerLiteralExpr("" + dimension), new NodeList<>()); |
58 | } |
59 | |
60 | public ArrayCreationLevel(Expression dimension) { |
61 | this(null, dimension, new NodeList<>()); |
62 | } |
63 | |
64 | @AllFieldsConstructor |
65 | public ArrayCreationLevel(Expression dimension, NodeList<AnnotationExpr> annotations) { |
66 | this(null, dimension, annotations); |
67 | } |
68 | |
69 | /** |
70 | * This constructor is used by the parser and is considered private. |
71 | */ |
72 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
73 | public ArrayCreationLevel(TokenRange tokenRange, Expression dimension, NodeList<AnnotationExpr> annotations) { |
74 | super(tokenRange); |
75 | setDimension(dimension); |
76 | setAnnotations(annotations); |
77 | customInitialization(); |
78 | } |
79 | |
80 | @Override |
81 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
82 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
83 | return v.visit(this, arg); |
84 | } |
85 | |
86 | @Override |
87 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
88 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
89 | v.visit(this, arg); |
90 | } |
91 | |
92 | /** |
93 | * Sets the dimension |
94 | * |
95 | * @param dimension the dimension, can be null |
96 | * @return this, the ArrayCreationLevel |
97 | */ |
98 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
99 | public ArrayCreationLevel setDimension(final Expression dimension) { |
100 | if (dimension == this.dimension) { |
101 | return (ArrayCreationLevel) this; |
102 | } |
103 | notifyPropertyChange(ObservableProperty.DIMENSION, this.dimension, dimension); |
104 | if (this.dimension != null) |
105 | this.dimension.setParentNode(null); |
106 | this.dimension = dimension; |
107 | setAsParentNodeOf(dimension); |
108 | return this; |
109 | } |
110 | |
111 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
112 | public Optional<Expression> getDimension() { |
113 | return Optional.ofNullable(dimension); |
114 | } |
115 | |
116 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
117 | public NodeList<AnnotationExpr> getAnnotations() { |
118 | return annotations; |
119 | } |
120 | |
121 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
122 | public ArrayCreationLevel setAnnotations(final NodeList<AnnotationExpr> annotations) { |
123 | assertNotNull(annotations); |
124 | if (annotations == this.annotations) { |
125 | return (ArrayCreationLevel) this; |
126 | } |
127 | notifyPropertyChange(ObservableProperty.ANNOTATIONS, this.annotations, annotations); |
128 | if (this.annotations != null) |
129 | this.annotations.setParentNode(null); |
130 | this.annotations = annotations; |
131 | setAsParentNodeOf(annotations); |
132 | return this; |
133 | } |
134 | |
135 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
136 | public ArrayCreationLevel removeDimension() { |
137 | return setDimension((Expression) null); |
138 | } |
139 | |
140 | @Override |
141 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
142 | public boolean remove(Node node) { |
143 | if (node == null) |
144 | return false; |
145 | for (int i = 0; i < annotations.size(); i++) { |
146 | if (annotations.get(i) == node) { |
147 | annotations.remove(i); |
148 | return true; |
149 | } |
150 | } |
151 | if (dimension != null) { |
152 | if (node == dimension) { |
153 | removeDimension(); |
154 | return true; |
155 | } |
156 | } |
157 | return super.remove(node); |
158 | } |
159 | |
160 | @Override |
161 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
162 | public ArrayCreationLevel clone() { |
163 | return (ArrayCreationLevel) accept(new CloneVisitor(), null); |
164 | } |
165 | |
166 | @Override |
167 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
168 | public ArrayCreationLevelMetaModel getMetaModel() { |
169 | return JavaParserMetaModel.arrayCreationLevelMetaModel; |
170 | } |
171 | |
172 | @Override |
173 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
174 | public boolean replace(Node node, Node replacementNode) { |
175 | if (node == null) |
176 | return false; |
177 | for (int i = 0; i < annotations.size(); i++) { |
178 | if (annotations.get(i) == node) { |
179 | annotations.set(i, (AnnotationExpr) replacementNode); |
180 | return true; |
181 | } |
182 | } |
183 | if (dimension != null) { |
184 | if (node == dimension) { |
185 | setDimension((Expression) replacementNode); |
186 | return true; |
187 | } |
188 | } |
189 | return super.replace(node, replacementNode); |
190 | } |
191 | } |
192 |
Members