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.type; |
22 | |
23 | import com.github.javaparser.Range; |
24 | import com.github.javaparser.ast.AllFieldsConstructor; |
25 | import com.github.javaparser.ast.NodeList; |
26 | import com.github.javaparser.ast.expr.AnnotationExpr; |
27 | import com.github.javaparser.ast.expr.SimpleName; |
28 | import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations; |
29 | import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName; |
30 | import com.github.javaparser.ast.observer.ObservableProperty; |
31 | import com.github.javaparser.ast.visitor.GenericVisitor; |
32 | import com.github.javaparser.ast.visitor.VoidVisitor; |
33 | import static com.github.javaparser.utils.Utils.assertNotNull; |
34 | import static java.util.stream.Collectors.joining; |
35 | import com.github.javaparser.ast.Node; |
36 | import com.github.javaparser.ast.visitor.CloneVisitor; |
37 | import com.github.javaparser.metamodel.TypeParameterMetaModel; |
38 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
39 | import com.github.javaparser.TokenRange; |
40 | import com.github.javaparser.resolution.types.ResolvedTypeVariable; |
41 | import java.util.function.Consumer; |
42 | import java.util.Optional; |
43 | import com.github.javaparser.ast.Generated; |
44 | |
45 | /** |
46 | * A type parameter. Examples: |
47 | * <br>{@code <}<b>{@code U}</b>{@code > U getU() { ... }} |
48 | * <br>{@code class D <}<b>{@code @Brain T extends B & A & @Tripe C}</b>{@code > { ... }} |
49 | * <p>U and T are type parameter names. |
50 | * <br>B, A, and C are type parameter bounds. |
51 | * <br>Tripe is an annotation on type parameter bound C. |
52 | * <br>Brain is an annotation on type parameter T. |
53 | * |
54 | * @author Julio Vilmar Gesser |
55 | * @see com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters |
56 | */ |
57 | public class TypeParameter extends ReferenceType implements NodeWithSimpleName<TypeParameter>, NodeWithAnnotations<TypeParameter> { |
58 | |
59 | private SimpleName name; |
60 | |
61 | private NodeList<ClassOrInterfaceType> typeBound; |
62 | |
63 | public TypeParameter() { |
64 | this(null, new SimpleName(), new NodeList<>(), new NodeList<>()); |
65 | } |
66 | |
67 | public TypeParameter(final String name) { |
68 | this(null, new SimpleName(name), new NodeList<>(), new NodeList<>()); |
69 | } |
70 | |
71 | public TypeParameter(final String name, final NodeList<ClassOrInterfaceType> typeBound) { |
72 | this(null, new SimpleName(name), typeBound, new NodeList<>()); |
73 | } |
74 | |
75 | @AllFieldsConstructor |
76 | public TypeParameter(SimpleName name, NodeList<ClassOrInterfaceType> typeBound, NodeList<AnnotationExpr> annotations) { |
77 | this(null, name, typeBound, annotations); |
78 | } |
79 | |
80 | /** |
81 | * This constructor is used by the parser and is considered private. |
82 | */ |
83 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
84 | public TypeParameter(TokenRange tokenRange, SimpleName name, NodeList<ClassOrInterfaceType> typeBound, NodeList<AnnotationExpr> annotations) { |
85 | super(tokenRange, annotations); |
86 | setName(name); |
87 | setTypeBound(typeBound); |
88 | customInitialization(); |
89 | } |
90 | |
91 | @Override |
92 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
93 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
94 | return v.visit(this, arg); |
95 | } |
96 | |
97 | @Override |
98 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
99 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
100 | v.visit(this, arg); |
101 | } |
102 | |
103 | /** |
104 | * Return the name of the paramenter. |
105 | * |
106 | * @return the name of the paramenter |
107 | */ |
108 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
109 | public SimpleName getName() { |
110 | return name; |
111 | } |
112 | |
113 | /** |
114 | * Return the list of {@link ClassOrInterfaceType} that this parameter |
115 | * extends. Return {@code null} null if there are no type. |
116 | * |
117 | * @return list of types that this paramente extends or {@code null} |
118 | */ |
119 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
120 | public NodeList<ClassOrInterfaceType> getTypeBound() { |
121 | return typeBound; |
122 | } |
123 | |
124 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
125 | public TypeParameter setName(final SimpleName name) { |
126 | assertNotNull(name); |
127 | if (name == this.name) { |
128 | return (TypeParameter) 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 TypeParameter setTypeBound(final NodeList<ClassOrInterfaceType> typeBound) { |
140 | assertNotNull(typeBound); |
141 | if (typeBound == this.typeBound) { |
142 | return (TypeParameter) this; |
143 | } |
144 | notifyPropertyChange(ObservableProperty.TYPE_BOUND, this.typeBound, typeBound); |
145 | if (this.typeBound != null) |
146 | this.typeBound.setParentNode(null); |
147 | this.typeBound = typeBound; |
148 | setAsParentNodeOf(typeBound); |
149 | return this; |
150 | } |
151 | |
152 | @Override |
153 | public TypeParameter setAnnotations(NodeList<AnnotationExpr> annotations) { |
154 | super.setAnnotations(annotations); |
155 | return this; |
156 | } |
157 | |
158 | @Override |
159 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
160 | public boolean remove(Node node) { |
161 | if (node == null) |
162 | return false; |
163 | for (int i = 0; i < typeBound.size(); i++) { |
164 | if (typeBound.get(i) == node) { |
165 | typeBound.remove(i); |
166 | return true; |
167 | } |
168 | } |
169 | return super.remove(node); |
170 | } |
171 | |
172 | @Override |
173 | public String asString() { |
174 | StringBuilder str = new StringBuilder(getNameAsString()); |
175 | getTypeBound().ifNonEmpty(l -> str.append(l.stream().map(ClassOrInterfaceType::asString).collect(joining("&", " extends ", "")))); |
176 | return str.toString(); |
177 | } |
178 | |
179 | @Override |
180 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
181 | public TypeParameter clone() { |
182 | return (TypeParameter) accept(new CloneVisitor(), null); |
183 | } |
184 | |
185 | @Override |
186 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
187 | public TypeParameterMetaModel getMetaModel() { |
188 | return JavaParserMetaModel.typeParameterMetaModel; |
189 | } |
190 | |
191 | @Override |
192 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
193 | public boolean replace(Node node, Node replacementNode) { |
194 | if (node == null) |
195 | return false; |
196 | if (node == name) { |
197 | setName((SimpleName) replacementNode); |
198 | return true; |
199 | } |
200 | for (int i = 0; i < typeBound.size(); i++) { |
201 | if (typeBound.get(i) == node) { |
202 | typeBound.set(i, (ClassOrInterfaceType) replacementNode); |
203 | return true; |
204 | } |
205 | } |
206 | return super.replace(node, replacementNode); |
207 | } |
208 | |
209 | @Override |
210 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
211 | public boolean isTypeParameter() { |
212 | return true; |
213 | } |
214 | |
215 | @Override |
216 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
217 | public TypeParameter asTypeParameter() { |
218 | return this; |
219 | } |
220 | |
221 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
222 | public void ifTypeParameter(Consumer<TypeParameter> action) { |
223 | action.accept(this); |
224 | } |
225 | |
226 | @Override |
227 | public ResolvedTypeVariable resolve() { |
228 | return getSymbolResolver().toResolvedType(this, ResolvedTypeVariable.class); |
229 | } |
230 | |
231 | @Override |
232 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
233 | public Optional<TypeParameter> toTypeParameter() { |
234 | return Optional.of(this); |
235 | } |
236 | } |
237 |
Members