| 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.TokenRange; |
| 24 | import com.github.javaparser.ast.AllFieldsConstructor; |
| 25 | import com.github.javaparser.ast.Generated; |
| 26 | import com.github.javaparser.ast.Node; |
| 27 | import com.github.javaparser.ast.nodeTypes.NodeWithName; |
| 28 | import com.github.javaparser.ast.observer.ObservableProperty; |
| 29 | import com.github.javaparser.ast.visitor.CloneVisitor; |
| 30 | import com.github.javaparser.metamodel.AnnotationExprMetaModel; |
| 31 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
| 32 | import com.github.javaparser.resolution.Resolvable; |
| 33 | import com.github.javaparser.resolution.UnsolvedSymbolException; |
| 34 | import com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration; |
| 35 | import java.util.Optional; |
| 36 | import java.util.function.Consumer; |
| 37 | import static com.github.javaparser.utils.Utils.assertNotNull; |
| 38 | |
| 39 | /** |
| 40 | * A base class for the different types of annotations. |
| 41 | * |
| 42 | * @author Julio Vilmar Gesser |
| 43 | */ |
| 44 | public abstract class AnnotationExpr extends Expression implements NodeWithName<AnnotationExpr>, Resolvable<ResolvedAnnotationDeclaration> { |
| 45 | |
| 46 | protected Name name; |
| 47 | |
| 48 | public AnnotationExpr() { |
| 49 | this(null, new Name()); |
| 50 | } |
| 51 | |
| 52 | @AllFieldsConstructor |
| 53 | public AnnotationExpr(Name name) { |
| 54 | this(null, name); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * This constructor is used by the parser and is considered private. |
| 59 | */ |
| 60 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 61 | public AnnotationExpr(TokenRange tokenRange, Name name) { |
| 62 | super(tokenRange); |
| 63 | setName(name); |
| 64 | customInitialization(); |
| 65 | } |
| 66 | |
| 67 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 68 | public Name getName() { |
| 69 | return name; |
| 70 | } |
| 71 | |
| 72 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 73 | public AnnotationExpr setName(final Name name) { |
| 74 | assertNotNull(name); |
| 75 | if (name == this.name) { |
| 76 | return (AnnotationExpr) this; |
| 77 | } |
| 78 | notifyPropertyChange(ObservableProperty.NAME, this.name, name); |
| 79 | if (this.name != null) |
| 80 | this.name.setParentNode(null); |
| 81 | this.name = name; |
| 82 | setAsParentNodeOf(name); |
| 83 | return this; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 88 | public boolean remove(Node node) { |
| 89 | if (node == null) |
| 90 | return false; |
| 91 | return super.remove(node); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 96 | public AnnotationExpr clone() { |
| 97 | return (AnnotationExpr) accept(new CloneVisitor(), null); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 102 | public AnnotationExprMetaModel getMetaModel() { |
| 103 | return JavaParserMetaModel.annotationExprMetaModel; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 108 | public boolean replace(Node node, Node replacementNode) { |
| 109 | if (node == null) |
| 110 | return false; |
| 111 | if (node == name) { |
| 112 | setName((Name) replacementNode); |
| 113 | return true; |
| 114 | } |
| 115 | return super.replace(node, replacementNode); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 120 | public boolean isAnnotationExpr() { |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 126 | public AnnotationExpr asAnnotationExpr() { |
| 127 | return this; |
| 128 | } |
| 129 | |
| 130 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 131 | public void ifAnnotationExpr(Consumer<AnnotationExpr> action) { |
| 132 | action.accept(this); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Attempts to resolve the declaration corresponding to the annotation expression. If successful, a |
| 137 | * {@link ResolvedAnnotationDeclaration} representing the declaration of the annotation referenced by this |
| 138 | * {@code AnnotationExpr} is returned. Otherwise, an {@link UnsolvedSymbolException} is thrown. |
| 139 | * |
| 140 | * @return a {@link ResolvedAnnotationDeclaration} representing the declaration of the annotation expression. |
| 141 | * @throws UnsolvedSymbolException if the declaration corresponding to the annotation expression could not be |
| 142 | * resolved. |
| 143 | */ |
| 144 | @Override |
| 145 | public ResolvedAnnotationDeclaration resolve() { |
| 146 | return getSymbolResolver().resolveDeclaration(this, ResolvedAnnotationDeclaration.class); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 151 | public Optional<AnnotationExpr> toAnnotationExpr() { |
| 152 | return Optional.of(this); |
| 153 | } |
| 154 | } |
| 155 |
Members