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