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.Name; |
24 | import com.github.javaparser.ast.nodeTypes.NodeWithName; |
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.StaticJavaParser.parseName; |
29 | import static com.github.javaparser.utils.Utils.assertNotNull; |
30 | import com.github.javaparser.ast.visitor.CloneVisitor; |
31 | import com.github.javaparser.metamodel.ImportDeclarationMetaModel; |
32 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
33 | import com.github.javaparser.TokenRange; |
34 | import com.github.javaparser.ast.Node; |
35 | import com.github.javaparser.ast.Generated; |
36 | |
37 | /** |
38 | * An import declaration. |
39 | * <br>{@code import com.github.javaparser.JavaParser;} |
40 | * <br>{@code import com.github.javaparser.*;} |
41 | * <br>{@code import com.github.javaparser.JavaParser.*; } |
42 | * <br>{@code import static com.github.javaparser.JavaParser.*;} |
43 | * <br>{@code import static com.github.javaparser.JavaParser.parse;} |
44 | * |
45 | * <p>The name does not include the asterisk or the static keyword.</p> |
46 | * @author Julio Vilmar Gesser |
47 | */ |
48 | public class ImportDeclaration extends Node implements NodeWithName<ImportDeclaration> { |
49 | |
50 | private Name name; |
51 | |
52 | private boolean isStatic; |
53 | |
54 | private boolean isAsterisk; |
55 | |
56 | private ImportDeclaration() { |
57 | this(null, new Name(), false, false); |
58 | } |
59 | |
60 | public ImportDeclaration(String name, boolean isStatic, boolean isAsterisk) { |
61 | this(null, parseName(name), isStatic, isAsterisk); |
62 | } |
63 | |
64 | @AllFieldsConstructor |
65 | public ImportDeclaration(Name name, boolean isStatic, boolean isAsterisk) { |
66 | this(null, name, isStatic, isAsterisk); |
67 | } |
68 | |
69 | /** |
70 | * This constructor is used by the parser and is considered private. |
71 | */ |
72 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
73 | public ImportDeclaration(TokenRange tokenRange, Name name, boolean isStatic, boolean isAsterisk) { |
74 | super(tokenRange); |
75 | setName(name); |
76 | setStatic(isStatic); |
77 | setAsterisk(isAsterisk); |
78 | customInitialization(); |
79 | } |
80 | |
81 | @Override |
82 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
83 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
84 | return v.visit(this, arg); |
85 | } |
86 | |
87 | @Override |
88 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
89 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
90 | v.visit(this, arg); |
91 | } |
92 | |
93 | /** |
94 | * Retrieves the name of the import (.* is not included.) |
95 | */ |
96 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
97 | public Name getName() { |
98 | return name; |
99 | } |
100 | |
101 | /** |
102 | * Return if the import ends with "*". |
103 | */ |
104 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
105 | public boolean isAsterisk() { |
106 | return isAsterisk; |
107 | } |
108 | |
109 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
110 | public boolean isStatic() { |
111 | return isStatic; |
112 | } |
113 | |
114 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
115 | public ImportDeclaration setAsterisk(final boolean isAsterisk) { |
116 | if (isAsterisk == this.isAsterisk) { |
117 | return (ImportDeclaration) this; |
118 | } |
119 | notifyPropertyChange(ObservableProperty.ASTERISK, this.isAsterisk, isAsterisk); |
120 | this.isAsterisk = isAsterisk; |
121 | return this; |
122 | } |
123 | |
124 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
125 | public ImportDeclaration setName(final Name name) { |
126 | assertNotNull(name); |
127 | if (name == this.name) { |
128 | return (ImportDeclaration) this; |
129 | } |
130 | notifyPropertyChange(ObservableProperty.NAME, this.name, name); |
131 | if (this.name != null) |
132 | this.name.setParentNode(null); |
133 | this.name = name; |
134 | setAsParentNodeOf(name); |
135 | return this; |
136 | } |
137 | |
138 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
139 | public ImportDeclaration setStatic(final boolean isStatic) { |
140 | if (isStatic == this.isStatic) { |
141 | return (ImportDeclaration) this; |
142 | } |
143 | notifyPropertyChange(ObservableProperty.STATIC, this.isStatic, isStatic); |
144 | this.isStatic = isStatic; |
145 | return this; |
146 | } |
147 | |
148 | @Override |
149 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
150 | public boolean remove(Node node) { |
151 | if (node == null) |
152 | return false; |
153 | return super.remove(node); |
154 | } |
155 | |
156 | @Override |
157 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
158 | public ImportDeclaration clone() { |
159 | return (ImportDeclaration) accept(new CloneVisitor(), null); |
160 | } |
161 | |
162 | @Override |
163 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
164 | public ImportDeclarationMetaModel getMetaModel() { |
165 | return JavaParserMetaModel.importDeclarationMetaModel; |
166 | } |
167 | |
168 | @Override |
169 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
170 | public boolean replace(Node node, Node replacementNode) { |
171 | if (node == null) |
172 | return false; |
173 | if (node == name) { |
174 | setName((Name) replacementNode); |
175 | return true; |
176 | } |
177 | return super.replace(node, replacementNode); |
178 | } |
179 | } |
180 |
Members