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