| 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.TokenRange; |
| 24 | import com.github.javaparser.ast.AllFieldsConstructor; |
| 25 | import com.github.javaparser.ast.Modifier; |
| 26 | import com.github.javaparser.ast.Node; |
| 27 | import com.github.javaparser.ast.NodeList; |
| 28 | import com.github.javaparser.ast.expr.AnnotationExpr; |
| 29 | import com.github.javaparser.ast.expr.Expression; |
| 30 | import com.github.javaparser.ast.expr.SimpleName; |
| 31 | import com.github.javaparser.ast.nodeTypes.NodeWithJavadoc; |
| 32 | import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName; |
| 33 | import com.github.javaparser.ast.nodeTypes.NodeWithType; |
| 34 | import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithAbstractModifier; |
| 35 | import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithPublicModifier; |
| 36 | import com.github.javaparser.ast.observer.ObservableProperty; |
| 37 | import com.github.javaparser.ast.type.ClassOrInterfaceType; |
| 38 | import com.github.javaparser.ast.type.Type; |
| 39 | import com.github.javaparser.ast.visitor.CloneVisitor; |
| 40 | import com.github.javaparser.ast.visitor.GenericVisitor; |
| 41 | import com.github.javaparser.ast.visitor.VoidVisitor; |
| 42 | import com.github.javaparser.metamodel.AnnotationMemberDeclarationMetaModel; |
| 43 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
| 44 | import com.github.javaparser.metamodel.OptionalProperty; |
| 45 | import com.github.javaparser.resolution.Resolvable; |
| 46 | import com.github.javaparser.resolution.declarations.ResolvedAnnotationMemberDeclaration; |
| 47 | import java.util.Optional; |
| 48 | import java.util.function.Consumer; |
| 49 | import static com.github.javaparser.utils.Utils.assertNotNull; |
| 50 | import com.github.javaparser.ast.Generated; |
| 51 | |
| 52 | /** |
| 53 | * The "int id();" in {@code @interface X { int id(); }} |
| 54 | * <p> |
| 55 | * <br>All annotations preceding the type will be set on this object, not on the type. JavaParser doesn't know if it |
| 56 | * they are applicable to the method or the type. |
| 57 | * |
| 58 | * @author Julio Vilmar Gesser |
| 59 | */ |
| 60 | public class AnnotationMemberDeclaration extends BodyDeclaration<AnnotationMemberDeclaration> implements NodeWithJavadoc<AnnotationMemberDeclaration>, NodeWithSimpleName<AnnotationMemberDeclaration>, NodeWithType<AnnotationMemberDeclaration, Type>, NodeWithPublicModifier<AnnotationMemberDeclaration>, NodeWithAbstractModifier<AnnotationMemberDeclaration>, Resolvable<ResolvedAnnotationMemberDeclaration> { |
| 61 | |
| 62 | private NodeList<Modifier> modifiers; |
| 63 | |
| 64 | private Type type; |
| 65 | |
| 66 | private SimpleName name; |
| 67 | |
| 68 | @OptionalProperty |
| 69 | private Expression defaultValue; |
| 70 | |
| 71 | public AnnotationMemberDeclaration() { |
| 72 | this(null, new NodeList<>(), new NodeList<>(), new ClassOrInterfaceType(), new SimpleName(), null); |
| 73 | } |
| 74 | |
| 75 | public AnnotationMemberDeclaration(NodeList<Modifier> modifiers, Type type, String name, Expression defaultValue) { |
| 76 | this(null, modifiers, new NodeList<>(), type, new SimpleName(name), defaultValue); |
| 77 | } |
| 78 | |
| 79 | @AllFieldsConstructor |
| 80 | public AnnotationMemberDeclaration(NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, Type type, SimpleName name, Expression defaultValue) { |
| 81 | this(null, modifiers, annotations, type, name, defaultValue); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * This constructor is used by the parser and is considered private. |
| 86 | */ |
| 87 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 88 | public AnnotationMemberDeclaration(TokenRange tokenRange, NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, Type type, SimpleName name, Expression defaultValue) { |
| 89 | super(tokenRange, annotations); |
| 90 | setModifiers(modifiers); |
| 91 | setType(type); |
| 92 | setName(name); |
| 93 | setDefaultValue(defaultValue); |
| 94 | customInitialization(); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 99 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
| 100 | return v.visit(this, arg); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 105 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
| 106 | v.visit(this, arg); |
| 107 | } |
| 108 | |
| 109 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 110 | public Optional<Expression> getDefaultValue() { |
| 111 | return Optional.ofNullable(defaultValue); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Return the modifiers of this member declaration. |
| 116 | * |
| 117 | * @return modifiers |
| 118 | * @see Modifier |
| 119 | */ |
| 120 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 121 | public NodeList<Modifier> getModifiers() { |
| 122 | return modifiers; |
| 123 | } |
| 124 | |
| 125 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 126 | public SimpleName getName() { |
| 127 | return name; |
| 128 | } |
| 129 | |
| 130 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 131 | public Type getType() { |
| 132 | return type; |
| 133 | } |
| 134 | |
| 135 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 136 | public AnnotationMemberDeclaration removeDefaultValue() { |
| 137 | return setDefaultValue((Expression) null); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Sets the default value |
| 142 | * |
| 143 | * @param defaultValue the default value, can be null |
| 144 | * @return this, the AnnotationMemberDeclaration |
| 145 | */ |
| 146 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 147 | public AnnotationMemberDeclaration setDefaultValue(final Expression defaultValue) { |
| 148 | if (defaultValue == this.defaultValue) { |
| 149 | return (AnnotationMemberDeclaration) this; |
| 150 | } |
| 151 | notifyPropertyChange(ObservableProperty.DEFAULT_VALUE, this.defaultValue, defaultValue); |
| 152 | if (this.defaultValue != null) |
| 153 | this.defaultValue.setParentNode(null); |
| 154 | this.defaultValue = defaultValue; |
| 155 | setAsParentNodeOf(defaultValue); |
| 156 | return this; |
| 157 | } |
| 158 | |
| 159 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 160 | public AnnotationMemberDeclaration setModifiers(final NodeList<Modifier> modifiers) { |
| 161 | assertNotNull(modifiers); |
| 162 | if (modifiers == this.modifiers) { |
| 163 | return (AnnotationMemberDeclaration) this; |
| 164 | } |
| 165 | notifyPropertyChange(ObservableProperty.MODIFIERS, this.modifiers, modifiers); |
| 166 | if (this.modifiers != null) |
| 167 | this.modifiers.setParentNode(null); |
| 168 | this.modifiers = modifiers; |
| 169 | setAsParentNodeOf(modifiers); |
| 170 | return this; |
| 171 | } |
| 172 | |
| 173 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 174 | public AnnotationMemberDeclaration setName(final SimpleName name) { |
| 175 | assertNotNull(name); |
| 176 | if (name == this.name) { |
| 177 | return (AnnotationMemberDeclaration) this; |
| 178 | } |
| 179 | notifyPropertyChange(ObservableProperty.NAME, this.name, name); |
| 180 | if (this.name != null) |
| 181 | this.name.setParentNode(null); |
| 182 | this.name = name; |
| 183 | setAsParentNodeOf(name); |
| 184 | return this; |
| 185 | } |
| 186 | |
| 187 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 188 | public AnnotationMemberDeclaration setType(final Type type) { |
| 189 | assertNotNull(type); |
| 190 | if (type == this.type) { |
| 191 | return (AnnotationMemberDeclaration) this; |
| 192 | } |
| 193 | notifyPropertyChange(ObservableProperty.TYPE, this.type, type); |
| 194 | if (this.type != null) |
| 195 | this.type.setParentNode(null); |
| 196 | this.type = type; |
| 197 | setAsParentNodeOf(type); |
| 198 | return this; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 203 | public boolean remove(Node node) { |
| 204 | if (node == null) |
| 205 | return false; |
| 206 | if (defaultValue != null) { |
| 207 | if (node == defaultValue) { |
| 208 | removeDefaultValue(); |
| 209 | return true; |
| 210 | } |
| 211 | } |
| 212 | for (int i = 0; i < modifiers.size(); i++) { |
| 213 | if (modifiers.get(i) == node) { |
| 214 | modifiers.remove(i); |
| 215 | return true; |
| 216 | } |
| 217 | } |
| 218 | return super.remove(node); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 223 | public AnnotationMemberDeclaration clone() { |
| 224 | return (AnnotationMemberDeclaration) accept(new CloneVisitor(), null); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 229 | public AnnotationMemberDeclarationMetaModel getMetaModel() { |
| 230 | return JavaParserMetaModel.annotationMemberDeclarationMetaModel; |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 235 | public boolean replace(Node node, Node replacementNode) { |
| 236 | if (node == null) |
| 237 | return false; |
| 238 | if (defaultValue != null) { |
| 239 | if (node == defaultValue) { |
| 240 | setDefaultValue((Expression) replacementNode); |
| 241 | return true; |
| 242 | } |
| 243 | } |
| 244 | for (int i = 0; i < modifiers.size(); i++) { |
| 245 | if (modifiers.get(i) == node) { |
| 246 | modifiers.set(i, (Modifier) replacementNode); |
| 247 | return true; |
| 248 | } |
| 249 | } |
| 250 | if (node == name) { |
| 251 | setName((SimpleName) replacementNode); |
| 252 | return true; |
| 253 | } |
| 254 | if (node == type) { |
| 255 | setType((Type) replacementNode); |
| 256 | return true; |
| 257 | } |
| 258 | return super.replace(node, replacementNode); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 263 | public boolean isAnnotationMemberDeclaration() { |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 269 | public AnnotationMemberDeclaration asAnnotationMemberDeclaration() { |
| 270 | return this; |
| 271 | } |
| 272 | |
| 273 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 274 | public void ifAnnotationMemberDeclaration(Consumer<AnnotationMemberDeclaration> action) { |
| 275 | action.accept(this); |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public ResolvedAnnotationMemberDeclaration resolve() { |
| 280 | return getSymbolResolver().resolveDeclaration(this, ResolvedAnnotationMemberDeclaration.class); |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 285 | public Optional<AnnotationMemberDeclaration> toAnnotationMemberDeclaration() { |
| 286 | return Optional.of(this); |
| 287 | } |
| 288 | } |
| 289 |
Members