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.Name; |
27 | import com.github.javaparser.ast.nodeTypes.NodeWithName; |
28 | import com.github.javaparser.ast.observer.ObservableProperty; |
29 | import com.github.javaparser.ast.visitor.CloneVisitor; |
30 | import com.github.javaparser.ast.visitor.GenericVisitor; |
31 | import com.github.javaparser.ast.visitor.VoidVisitor; |
32 | import static com.github.javaparser.utils.Utils.assertNotNull; |
33 | import com.github.javaparser.TokenRange; |
34 | import java.util.function.Consumer; |
35 | import java.util.Optional; |
36 | import com.github.javaparser.metamodel.ModuleProvidesDirectiveMetaModel; |
37 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
38 | import com.github.javaparser.ast.Generated; |
39 | |
40 | /** |
41 | * A provides directive in module-info.java. {@code provides X.Y with Z1.Z2, Z3.Z4;} |
42 | */ |
43 | public class ModuleProvidesDirective extends ModuleDirective implements NodeWithName<ModuleProvidesDirective> { |
44 | |
45 | private Name name; |
46 | |
47 | private NodeList<Name> with; |
48 | |
49 | public ModuleProvidesDirective() { |
50 | this(null, new Name(), new NodeList<>()); |
51 | } |
52 | |
53 | @AllFieldsConstructor |
54 | public ModuleProvidesDirective(Name name, NodeList<Name> with) { |
55 | this(null, name, with); |
56 | } |
57 | |
58 | /** |
59 | * This constructor is used by the parser and is considered private. |
60 | */ |
61 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
62 | public ModuleProvidesDirective(TokenRange tokenRange, Name name, NodeList<Name> with) { |
63 | super(tokenRange); |
64 | setName(name); |
65 | setWith(with); |
66 | customInitialization(); |
67 | } |
68 | |
69 | @Override |
70 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
71 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
72 | return v.visit(this, arg); |
73 | } |
74 | |
75 | @Override |
76 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
77 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
78 | v.visit(this, arg); |
79 | } |
80 | |
81 | @Override |
82 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
83 | public boolean remove(Node node) { |
84 | if (node == null) |
85 | return false; |
86 | for (int i = 0; i < with.size(); i++) { |
87 | if (with.get(i) == node) { |
88 | with.remove(i); |
89 | return true; |
90 | } |
91 | } |
92 | return super.remove(node); |
93 | } |
94 | |
95 | @Override |
96 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
97 | public ModuleProvidesDirective clone() { |
98 | return (ModuleProvidesDirective) accept(new CloneVisitor(), null); |
99 | } |
100 | |
101 | @Override |
102 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
103 | public boolean isModuleProvidesStmt() { |
104 | return true; |
105 | } |
106 | |
107 | @Override |
108 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
109 | public ModuleProvidesDirective asModuleProvidesStmt() { |
110 | return this; |
111 | } |
112 | |
113 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
114 | public void ifModuleProvidesStmt(Consumer<ModuleProvidesDirective> action) { |
115 | action.accept(this); |
116 | } |
117 | |
118 | @Override |
119 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
120 | public Optional<ModuleProvidesDirective> toModuleProvidesStmt() { |
121 | return Optional.of(this); |
122 | } |
123 | |
124 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
125 | public Name getName() { |
126 | return name; |
127 | } |
128 | |
129 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
130 | public ModuleProvidesDirective setName(final Name name) { |
131 | assertNotNull(name); |
132 | if (name == this.name) { |
133 | return (ModuleProvidesDirective) this; |
134 | } |
135 | notifyPropertyChange(ObservableProperty.NAME, this.name, name); |
136 | if (this.name != null) |
137 | this.name.setParentNode(null); |
138 | this.name = name; |
139 | setAsParentNodeOf(name); |
140 | return this; |
141 | } |
142 | |
143 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
144 | public ModuleProvidesDirective setWith(final NodeList<Name> with) { |
145 | assertNotNull(with); |
146 | if (with == this.with) { |
147 | return (ModuleProvidesDirective) this; |
148 | } |
149 | notifyPropertyChange(ObservableProperty.WITH, this.with, with); |
150 | if (this.with != null) |
151 | this.with.setParentNode(null); |
152 | this.with = with; |
153 | setAsParentNodeOf(with); |
154 | return this; |
155 | } |
156 | |
157 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
158 | public NodeList<Name> getWith() { |
159 | return with; |
160 | } |
161 | |
162 | @Override |
163 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
164 | public boolean replace(Node node, Node replacementNode) { |
165 | if (node == null) |
166 | return false; |
167 | if (node == name) { |
168 | setName((Name) replacementNode); |
169 | return true; |
170 | } |
171 | for (int i = 0; i < with.size(); i++) { |
172 | if (with.get(i) == node) { |
173 | with.set(i, (Name) replacementNode); |
174 | return true; |
175 | } |
176 | } |
177 | return super.replace(node, replacementNode); |
178 | } |
179 | |
180 | @Override |
181 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
182 | public boolean isModuleProvidesDirective() { |
183 | return true; |
184 | } |
185 | |
186 | @Override |
187 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
188 | public ModuleProvidesDirective asModuleProvidesDirective() { |
189 | return this; |
190 | } |
191 | |
192 | @Override |
193 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
194 | public Optional<ModuleProvidesDirective> toModuleProvidesDirective() { |
195 | return Optional.of(this); |
196 | } |
197 | |
198 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
199 | public void ifModuleProvidesDirective(Consumer<ModuleProvidesDirective> action) { |
200 | action.accept(this); |
201 | } |
202 | |
203 | @Override |
204 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
205 | public ModuleProvidesDirectiveMetaModel getMetaModel() { |
206 | return JavaParserMetaModel.moduleProvidesDirectiveMetaModel; |
207 | } |
208 | } |
209 |
Members