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