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.modules; |
22 | |
23 | import com.github.javaparser.ast.AllFieldsConstructor; |
24 | import com.github.javaparser.ast.Node; |
25 | import com.github.javaparser.ast.NodeList; |
26 | import com.github.javaparser.ast.expr.AnnotationExpr; |
27 | import com.github.javaparser.ast.expr.Name; |
28 | import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations; |
29 | import com.github.javaparser.ast.nodeTypes.NodeWithName; |
30 | import com.github.javaparser.ast.observer.ObservableProperty; |
31 | import com.github.javaparser.ast.visitor.CloneVisitor; |
32 | import com.github.javaparser.ast.visitor.GenericVisitor; |
33 | import com.github.javaparser.ast.visitor.VoidVisitor; |
34 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
35 | import com.github.javaparser.metamodel.ModuleDeclarationMetaModel; |
36 | import static com.github.javaparser.StaticJavaParser.parseModuleDirective; |
37 | import static com.github.javaparser.utils.Utils.assertNotNull; |
38 | import com.github.javaparser.TokenRange; |
39 | import com.github.javaparser.ast.Generated; |
40 | |
41 | /** |
42 | * A Java 9 Jigsaw module declaration. {@code @Foo module com.github.abc { requires a.B; }} |
43 | */ |
44 | public class ModuleDeclaration extends Node implements NodeWithName<ModuleDeclaration>, NodeWithAnnotations<ModuleDeclaration> { |
45 | |
46 | private Name name; |
47 | |
48 | private NodeList<AnnotationExpr> annotations; |
49 | |
50 | private boolean isOpen; |
51 | |
52 | private NodeList<ModuleDirective> directives; |
53 | |
54 | public ModuleDeclaration() { |
55 | this(null, new NodeList<>(), new Name(), false, new NodeList<>()); |
56 | } |
57 | |
58 | public ModuleDeclaration(Name name, boolean isOpen) { |
59 | this(null, new NodeList<>(), name, isOpen, new NodeList<>()); |
60 | } |
61 | |
62 | @AllFieldsConstructor |
63 | public ModuleDeclaration(NodeList<AnnotationExpr> annotations, Name name, boolean isOpen, NodeList<ModuleDirective> directives) { |
64 | this(null, annotations, name, isOpen, directives); |
65 | } |
66 | |
67 | /** |
68 | * This constructor is used by the parser and is considered private. |
69 | */ |
70 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
71 | public ModuleDeclaration(TokenRange tokenRange, NodeList<AnnotationExpr> annotations, Name name, boolean isOpen, NodeList<ModuleDirective> directives) { |
72 | super(tokenRange); |
73 | setAnnotations(annotations); |
74 | setName(name); |
75 | setOpen(isOpen); |
76 | setDirectives(directives); |
77 | customInitialization(); |
78 | } |
79 | |
80 | @Override |
81 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
82 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
83 | return v.visit(this, arg); |
84 | } |
85 | |
86 | @Override |
87 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
88 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
89 | v.visit(this, arg); |
90 | } |
91 | |
92 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
93 | public Name getName() { |
94 | return name; |
95 | } |
96 | |
97 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
98 | public ModuleDeclaration setName(final Name name) { |
99 | assertNotNull(name); |
100 | if (name == this.name) { |
101 | return (ModuleDeclaration) this; |
102 | } |
103 | notifyPropertyChange(ObservableProperty.NAME, this.name, name); |
104 | if (this.name != null) |
105 | this.name.setParentNode(null); |
106 | this.name = name; |
107 | setAsParentNodeOf(name); |
108 | return this; |
109 | } |
110 | |
111 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
112 | public NodeList<AnnotationExpr> getAnnotations() { |
113 | return annotations; |
114 | } |
115 | |
116 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
117 | public ModuleDeclaration setAnnotations(final NodeList<AnnotationExpr> annotations) { |
118 | assertNotNull(annotations); |
119 | if (annotations == this.annotations) { |
120 | return (ModuleDeclaration) this; |
121 | } |
122 | notifyPropertyChange(ObservableProperty.ANNOTATIONS, this.annotations, annotations); |
123 | if (this.annotations != null) |
124 | this.annotations.setParentNode(null); |
125 | this.annotations = annotations; |
126 | setAsParentNodeOf(annotations); |
127 | return this; |
128 | } |
129 | |
130 | @Override |
131 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
132 | public boolean remove(Node node) { |
133 | if (node == null) |
134 | return false; |
135 | for (int i = 0; i < annotations.size(); i++) { |
136 | if (annotations.get(i) == node) { |
137 | annotations.remove(i); |
138 | return true; |
139 | } |
140 | } |
141 | for (int i = 0; i < directives.size(); i++) { |
142 | if (directives.get(i) == node) { |
143 | directives.remove(i); |
144 | return true; |
145 | } |
146 | } |
147 | return super.remove(node); |
148 | } |
149 | |
150 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
151 | public boolean isOpen() { |
152 | return isOpen; |
153 | } |
154 | |
155 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
156 | public ModuleDeclaration setOpen(final boolean isOpen) { |
157 | if (isOpen == this.isOpen) { |
158 | return (ModuleDeclaration) this; |
159 | } |
160 | notifyPropertyChange(ObservableProperty.OPEN, this.isOpen, isOpen); |
161 | this.isOpen = isOpen; |
162 | return this; |
163 | } |
164 | |
165 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
166 | public NodeList<ModuleDirective> getDirectives() { |
167 | return directives; |
168 | } |
169 | |
170 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
171 | public ModuleDeclaration setDirectives(final NodeList<ModuleDirective> directives) { |
172 | assertNotNull(directives); |
173 | if (directives == this.directives) { |
174 | return (ModuleDeclaration) this; |
175 | } |
176 | notifyPropertyChange(ObservableProperty.DIRECTIVES, this.directives, directives); |
177 | if (this.directives != null) |
178 | this.directives.setParentNode(null); |
179 | this.directives = directives; |
180 | setAsParentNodeOf(directives); |
181 | return this; |
182 | } |
183 | |
184 | @Override |
185 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
186 | public ModuleDeclaration clone() { |
187 | return (ModuleDeclaration) accept(new CloneVisitor(), null); |
188 | } |
189 | |
190 | @Override |
191 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
192 | public ModuleDeclarationMetaModel getMetaModel() { |
193 | return JavaParserMetaModel.moduleDeclarationMetaModel; |
194 | } |
195 | |
196 | @Override |
197 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
198 | public boolean replace(Node node, Node replacementNode) { |
199 | if (node == null) |
200 | return false; |
201 | for (int i = 0; i < annotations.size(); i++) { |
202 | if (annotations.get(i) == node) { |
203 | annotations.set(i, (AnnotationExpr) replacementNode); |
204 | return true; |
205 | } |
206 | } |
207 | for (int i = 0; i < directives.size(); i++) { |
208 | if (directives.get(i) == node) { |
209 | directives.set(i, (ModuleDirective) replacementNode); |
210 | return true; |
211 | } |
212 | } |
213 | if (node == name) { |
214 | setName((Name) replacementNode); |
215 | return true; |
216 | } |
217 | return super.replace(node, replacementNode); |
218 | } |
219 | |
220 | /** |
221 | * Add a directive to the module, like "exports R.S to T1.U1, T2.U2;" |
222 | */ |
223 | public ModuleDeclaration addDirective(String directive) { |
224 | return addDirective(parseModuleDirective(directive)); |
225 | } |
226 | |
227 | public ModuleDeclaration addDirective(ModuleDirective directive) { |
228 | getDirectives().add(directive); |
229 | return this; |
230 | } |
231 | } |
232 |
Members