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.body; |
22 | |
23 | import com.github.javaparser.TokenRange; |
24 | import com.github.javaparser.ast.AllFieldsConstructor; |
25 | import com.github.javaparser.ast.Node; |
26 | import com.github.javaparser.ast.NodeList; |
27 | import com.github.javaparser.ast.expr.AnnotationExpr; |
28 | import com.github.javaparser.ast.expr.Name; |
29 | import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations; |
30 | import com.github.javaparser.ast.nodeTypes.NodeWithName; |
31 | import com.github.javaparser.ast.nodeTypes.NodeWithType; |
32 | import com.github.javaparser.ast.observer.ObservableProperty; |
33 | import com.github.javaparser.ast.type.ClassOrInterfaceType; |
34 | import com.github.javaparser.ast.type.Type; |
35 | import com.github.javaparser.ast.visitor.CloneVisitor; |
36 | import com.github.javaparser.ast.visitor.GenericVisitor; |
37 | import com.github.javaparser.ast.visitor.VoidVisitor; |
38 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
39 | import static com.github.javaparser.utils.Utils.assertNotNull; |
40 | import com.github.javaparser.metamodel.ReceiverParameterMetaModel; |
41 | import com.github.javaparser.ast.Generated; |
42 | |
43 | /** |
44 | * The rather obscure <a href="http://blog.joda.org/2015/12/explicit-receiver-parameters.html">"receiver parameter" feature of Java</a>. |
45 | * |
46 | * <br>All annotations preceding the type will be set on this object, not on the type. |
47 | * JavaParser doesn't know if it they are applicable to the receiver parameter or the type. |
48 | * |
49 | * @author Julio Vilmar Gesser |
50 | */ |
51 | public class ReceiverParameter extends Node implements NodeWithType<ReceiverParameter, Type>, NodeWithAnnotations<ReceiverParameter>, NodeWithName<ReceiverParameter> { |
52 | |
53 | private Type type; |
54 | |
55 | private NodeList<AnnotationExpr> annotations; |
56 | |
57 | private Name name; |
58 | |
59 | public ReceiverParameter() { |
60 | this(null, new NodeList<>(), new ClassOrInterfaceType(), new Name()); |
61 | } |
62 | |
63 | public ReceiverParameter(Type type, Name name) { |
64 | this(null, new NodeList<>(), type, name); |
65 | } |
66 | |
67 | /** |
68 | * Creates a new {@link ReceiverParameter}. |
69 | * |
70 | * @param type type of the parameter |
71 | * @param name name of the parameter |
72 | */ |
73 | public ReceiverParameter(Type type, String name) { |
74 | this(null, new NodeList<>(), type, new Name(name)); |
75 | } |
76 | |
77 | @AllFieldsConstructor |
78 | public ReceiverParameter(NodeList<AnnotationExpr> annotations, Type type, Name name) { |
79 | this(null, annotations, type, name); |
80 | } |
81 | |
82 | /** |
83 | * This constructor is used by the parser and is considered private. |
84 | */ |
85 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
86 | public ReceiverParameter(TokenRange tokenRange, NodeList<AnnotationExpr> annotations, Type type, Name name) { |
87 | super(tokenRange); |
88 | setAnnotations(annotations); |
89 | setType(type); |
90 | setName(name); |
91 | customInitialization(); |
92 | } |
93 | |
94 | @Override |
95 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
96 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
97 | return v.visit(this, arg); |
98 | } |
99 | |
100 | @Override |
101 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
102 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
103 | v.visit(this, arg); |
104 | } |
105 | |
106 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
107 | public Type getType() { |
108 | return type; |
109 | } |
110 | |
111 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
112 | public ReceiverParameter setType(final Type type) { |
113 | assertNotNull(type); |
114 | if (type == this.type) { |
115 | return (ReceiverParameter) this; |
116 | } |
117 | notifyPropertyChange(ObservableProperty.TYPE, this.type, type); |
118 | if (this.type != null) |
119 | this.type.setParentNode(null); |
120 | this.type = type; |
121 | setAsParentNodeOf(type); |
122 | return this; |
123 | } |
124 | |
125 | /** |
126 | * @return the list returned could be immutable (in that case it will be empty) |
127 | */ |
128 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
129 | public NodeList<AnnotationExpr> getAnnotations() { |
130 | return annotations; |
131 | } |
132 | |
133 | /** |
134 | * @param annotations a null value is currently treated as an empty list. This behavior could change in the future, |
135 | * so please avoid passing null |
136 | */ |
137 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
138 | public ReceiverParameter setAnnotations(final NodeList<AnnotationExpr> annotations) { |
139 | assertNotNull(annotations); |
140 | if (annotations == this.annotations) { |
141 | return (ReceiverParameter) this; |
142 | } |
143 | notifyPropertyChange(ObservableProperty.ANNOTATIONS, this.annotations, annotations); |
144 | if (this.annotations != null) |
145 | this.annotations.setParentNode(null); |
146 | this.annotations = annotations; |
147 | setAsParentNodeOf(annotations); |
148 | return this; |
149 | } |
150 | |
151 | @Override |
152 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
153 | public ReceiverParameter clone() { |
154 | return (ReceiverParameter) accept(new CloneVisitor(), null); |
155 | } |
156 | |
157 | @Override |
158 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
159 | public ReceiverParameterMetaModel getMetaModel() { |
160 | return JavaParserMetaModel.receiverParameterMetaModel; |
161 | } |
162 | |
163 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
164 | public Name getName() { |
165 | return name; |
166 | } |
167 | |
168 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
169 | public ReceiverParameter setName(final Name name) { |
170 | assertNotNull(name); |
171 | if (name == this.name) { |
172 | return (ReceiverParameter) this; |
173 | } |
174 | notifyPropertyChange(ObservableProperty.NAME, this.name, name); |
175 | if (this.name != null) |
176 | this.name.setParentNode(null); |
177 | this.name = name; |
178 | setAsParentNodeOf(name); |
179 | return this; |
180 | } |
181 | |
182 | @Override |
183 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
184 | public boolean remove(Node node) { |
185 | if (node == null) |
186 | return false; |
187 | for (int i = 0; i < annotations.size(); i++) { |
188 | if (annotations.get(i) == node) { |
189 | annotations.remove(i); |
190 | return true; |
191 | } |
192 | } |
193 | return super.remove(node); |
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 | if (node == name) { |
208 | setName((Name) replacementNode); |
209 | return true; |
210 | } |
211 | if (node == type) { |
212 | setType((Type) replacementNode); |
213 | return true; |
214 | } |
215 | return super.replace(node, replacementNode); |
216 | } |
217 | } |
218 |
Members