| 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.NodeList; |
| 25 | import com.github.javaparser.ast.observer.ObservableProperty; |
| 26 | import com.github.javaparser.ast.visitor.GenericVisitor; |
| 27 | import com.github.javaparser.ast.visitor.VoidVisitor; |
| 28 | import static com.github.javaparser.utils.Utils.assertNotNull; |
| 29 | import com.github.javaparser.ast.Node; |
| 30 | import com.github.javaparser.ast.visitor.CloneVisitor; |
| 31 | import com.github.javaparser.metamodel.NormalAnnotationExprMetaModel; |
| 32 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
| 33 | import com.github.javaparser.TokenRange; |
| 34 | import java.util.function.Consumer; |
| 35 | import java.util.Optional; |
| 36 | import com.github.javaparser.ast.Generated; |
| 37 | |
| 38 | /** |
| 39 | * An annotation that has zero or more key-value pairs.<br>{@code @Mapping(a=5, d=10)} |
| 40 | * @author Julio Vilmar Gesser |
| 41 | */ |
| 42 | public class NormalAnnotationExpr extends AnnotationExpr { |
| 43 | |
| 44 | private NodeList<MemberValuePair> pairs; |
| 45 | |
| 46 | public NormalAnnotationExpr() { |
| 47 | this(null, new Name(), new NodeList<>()); |
| 48 | } |
| 49 | |
| 50 | @AllFieldsConstructor |
| 51 | public NormalAnnotationExpr(final Name name, final NodeList<MemberValuePair> pairs) { |
| 52 | this(null, name, pairs); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * This constructor is used by the parser and is considered private. |
| 57 | */ |
| 58 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 59 | public NormalAnnotationExpr(TokenRange tokenRange, Name name, NodeList<MemberValuePair> pairs) { |
| 60 | super(tokenRange, name); |
| 61 | setPairs(pairs); |
| 62 | customInitialization(); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 67 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
| 68 | return v.visit(this, arg); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 73 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
| 74 | v.visit(this, arg); |
| 75 | } |
| 76 | |
| 77 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 78 | public NodeList<MemberValuePair> getPairs() { |
| 79 | return pairs; |
| 80 | } |
| 81 | |
| 82 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 83 | public NormalAnnotationExpr setPairs(final NodeList<MemberValuePair> pairs) { |
| 84 | assertNotNull(pairs); |
| 85 | if (pairs == this.pairs) { |
| 86 | return (NormalAnnotationExpr) this; |
| 87 | } |
| 88 | notifyPropertyChange(ObservableProperty.PAIRS, this.pairs, pairs); |
| 89 | if (this.pairs != null) |
| 90 | this.pairs.setParentNode(null); |
| 91 | this.pairs = pairs; |
| 92 | setAsParentNodeOf(pairs); |
| 93 | return this; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * adds a pair to this annotation |
| 98 | * |
| 99 | * @return this, the {@link NormalAnnotationExpr} |
| 100 | */ |
| 101 | public NormalAnnotationExpr addPair(String key, String value) { |
| 102 | return addPair(key, new NameExpr(value)); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * adds a pair to this annotation |
| 107 | * |
| 108 | * @return this, the {@link NormalAnnotationExpr} |
| 109 | */ |
| 110 | public NormalAnnotationExpr addPair(String key, Expression value) { |
| 111 | MemberValuePair memberValuePair = new MemberValuePair(key, value); |
| 112 | getPairs().add(memberValuePair); |
| 113 | return this; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 118 | public boolean remove(Node node) { |
| 119 | if (node == null) |
| 120 | return false; |
| 121 | for (int i = 0; i < pairs.size(); i++) { |
| 122 | if (pairs.get(i) == node) { |
| 123 | pairs.remove(i); |
| 124 | return true; |
| 125 | } |
| 126 | } |
| 127 | return super.remove(node); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 132 | public NormalAnnotationExpr clone() { |
| 133 | return (NormalAnnotationExpr) accept(new CloneVisitor(), null); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 138 | public NormalAnnotationExprMetaModel getMetaModel() { |
| 139 | return JavaParserMetaModel.normalAnnotationExprMetaModel; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 144 | public boolean replace(Node node, Node replacementNode) { |
| 145 | if (node == null) |
| 146 | return false; |
| 147 | for (int i = 0; i < pairs.size(); i++) { |
| 148 | if (pairs.get(i) == node) { |
| 149 | pairs.set(i, (MemberValuePair) replacementNode); |
| 150 | return true; |
| 151 | } |
| 152 | } |
| 153 | return super.replace(node, replacementNode); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 158 | public boolean isNormalAnnotationExpr() { |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 164 | public NormalAnnotationExpr asNormalAnnotationExpr() { |
| 165 | return this; |
| 166 | } |
| 167 | |
| 168 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 169 | public void ifNormalAnnotationExpr(Consumer<NormalAnnotationExpr> action) { |
| 170 | action.accept(this); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 175 | public Optional<NormalAnnotationExpr> toNormalAnnotationExpr() { |
| 176 | return Optional.of(this); |
| 177 | } |
| 178 | } |
| 179 |
Members