JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/PackageDeclaration.java
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 */
21package com.github.javaparser.ast;
22
23import com.github.javaparser.ast.expr.AnnotationExpr;
24import com.github.javaparser.ast.expr.Name;
25import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations;
26import com.github.javaparser.ast.nodeTypes.NodeWithName;
27import com.github.javaparser.ast.observer.ObservableProperty;
28import com.github.javaparser.ast.visitor.GenericVisitor;
29import com.github.javaparser.ast.visitor.VoidVisitor;
30import static com.github.javaparser.utils.Utils.assertNotNull;
31import com.github.javaparser.ast.visitor.CloneVisitor;
32import com.github.javaparser.metamodel.PackageDeclarationMetaModel;
33import com.github.javaparser.metamodel.JavaParserMetaModel;
34import com.github.javaparser.TokenRange;
35import com.github.javaparser.ast.Node;
36import 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 */
45public class PackageDeclaration extends Node implements NodeWithAnnotations<PackageDeclaration>, NodeWithName<PackageDeclaration> {
46
47    private NodeList<AnnotationExprannotations = 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<AnnotationExprannotationsName name) {
61        this(nullannotationsname);
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 tokenRangeNodeList<AnnotationExprannotationsName 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 <RAR accept(final GenericVisitor<RAvfinal A arg) {
78        return v.visit(this, arg);
79    }
80
81    @Override
82    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
83    public <Avoid accept(final VoidVisitor<Avfinal 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<AnnotationExprgetAnnotations() {
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<AnnotationExprannotations) {
113        assertNotNull(annotations);
114        if (annotations == this.annotations) {
115            return (PackageDeclaration) this;
116        }
117        notifyPropertyChange(ObservableProperty.ANNOTATIONS, this.annotationsannotations);
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.namename);
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 = 0i < 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 (PackageDeclarationaccept(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 nodeNode replacementNode) {
173        if (node == null)
174            return false;
175        for (int i = 0i < annotations.size(); i++) {
176            if (annotations.get(i) == node) {
177                annotations.set(i, (AnnotationExprreplacementNode);
178                return true;
179            }
180        }
181        if (node == name) {
182            setName((NamereplacementNode);
183            return true;
184        }
185        return super.replace(nodereplacementNode);
186    }
187}
188
MembersX
PackageDeclaration:annotations
PackageDeclaration:setName
PackageDeclaration:PackageDeclaration
PackageDeclaration:remove
PackageDeclaration:replace
PackageDeclaration:accept
PackageDeclaration:setAnnotations
PackageDeclaration:getMetaModel
PackageDeclaration:name
PackageDeclaration:clone
PackageDeclaration:getName
PackageDeclaration:getAnnotations
Members
X