• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007-2010 Júlio Vilmar Gesser.
3  * Copyright (C) 2011, 2013-2016 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.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.nodeTypes.NodeWithAnnotations;
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 com.github.javaparser.metamodel.JavaParserMetaModel;
33 import com.github.javaparser.metamodel.OptionalProperty;
34 import com.github.javaparser.metamodel.WildcardTypeMetaModel;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.Optional;
38 import javax.annotation.Generated;
39 import com.github.javaparser.TokenRange;
40 import com.github.javaparser.resolution.types.ResolvedUnionType;
41 import com.github.javaparser.resolution.types.ResolvedWildcard;
42 import java.util.function.Consumer;
43 
44 /**
45  * A wildcard type argument.
46  * <br/><code>void printCollection(Collection&lt;<b>?</b>> c) { ... }</code>
47  * <br/><code>boolean addAll(Collection&lt;<b>? extends E</b>> c)</code>
48  * <br/><code>Reference(T referent, ReferenceQueue&lt;<b>? super T</b>> queue)</code>
49  *
50  * @author Julio Vilmar Gesser
51  */
52 public final class WildcardType extends Type implements NodeWithAnnotations<WildcardType> {
53 
54     @OptionalProperty
55     private ReferenceType extendedType;
56 
57     @OptionalProperty
58     private ReferenceType superType;
59 
WildcardType()60     public WildcardType() {
61         this(null, null, null, new NodeList<>());
62     }
63 
WildcardType(final ReferenceType extendedType)64     public WildcardType(final ReferenceType extendedType) {
65         this(null, extendedType, null, new NodeList<>());
66     }
67 
68     @AllFieldsConstructor
WildcardType(final ReferenceType extendedType, final ReferenceType superType, final NodeList<AnnotationExpr> annotations)69     public WildcardType(final ReferenceType extendedType, final ReferenceType superType, final NodeList<AnnotationExpr> annotations) {
70         this(null, extendedType, superType, annotations);
71     }
72 
73     /**
74      * This constructor is used by the parser and is considered private.
75      */
76     @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
WildcardType(TokenRange tokenRange, ReferenceType extendedType, ReferenceType superType, NodeList<AnnotationExpr> annotations)77     public WildcardType(TokenRange tokenRange, ReferenceType extendedType, ReferenceType superType, NodeList<AnnotationExpr> annotations) {
78         super(tokenRange, annotations);
79         setExtendedType(extendedType);
80         setSuperType(superType);
81         customInitialization();
82     }
83 
84     @Override
85     @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
accept(final GenericVisitor<R, A> v, final A arg)86     public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) {
87         return v.visit(this, arg);
88     }
89 
90     @Override
91     @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
accept(final VoidVisitor<A> v, final A arg)92     public <A> void accept(final VoidVisitor<A> v, final A arg) {
93         v.visit(this, arg);
94     }
95 
96     @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
getExtendedType()97     public Optional<ReferenceType> getExtendedType() {
98         return Optional.ofNullable(extendedType);
99     }
100 
101     @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
getSuperType()102     public Optional<ReferenceType> getSuperType() {
103         return Optional.ofNullable(superType);
104     }
105 
106     /**
107      * @deprecated use getExtendedType instead.
108      */
109     @Deprecated
getExtendedTypes()110     public Optional<ReferenceType> getExtendedTypes() {
111         return getExtendedType();
112     }
113 
114     /**
115      * @deprecated use getSuperType instead.
116      */
117     @Deprecated
getSuperTypes()118     public Optional<ReferenceType> getSuperTypes() {
119         return getSuperType();
120     }
121 
122     /**
123      * Sets the extended type
124      *
125      * @param extendedType the extends, can be null
126      * @return this, the WildcardType
127      */
128     @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
setExtendedType(final ReferenceType extendedType)129     public WildcardType setExtendedType(final ReferenceType extendedType) {
130         if (extendedType == this.extendedType) {
131             return (WildcardType) this;
132         }
133         notifyPropertyChange(ObservableProperty.EXTENDED_TYPE, this.extendedType, extendedType);
134         if (this.extendedType != null)
135             this.extendedType.setParentNode(null);
136         this.extendedType = extendedType;
137         setAsParentNodeOf(extendedType);
138         return this;
139     }
140 
141     /**
142      * Sets the extended type
143      *
144      * @param extendedType the extends, can be null
145      * @return this, the WildcardType
146      * @deprecated use setExtendedType instead,
147      */
148     @Deprecated
setExtendedTypes(final ReferenceType extendedType)149     public WildcardType setExtendedTypes(final ReferenceType extendedType) {
150         return setExtendedType(extendedType);
151     }
152 
153     /**
154      * Sets the supertype
155      *
156      * @param superType the super, can be null
157      * @return this, the WildcardType
158      */
159     @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
setSuperType(final ReferenceType superType)160     public WildcardType setSuperType(final ReferenceType superType) {
161         if (superType == this.superType) {
162             return (WildcardType) this;
163         }
164         notifyPropertyChange(ObservableProperty.SUPER_TYPE, this.superType, superType);
165         if (this.superType != null)
166             this.superType.setParentNode(null);
167         this.superType = superType;
168         setAsParentNodeOf(superType);
169         return this;
170     }
171 
172     /**
173      * Sets the supertype
174      *
175      * @param superType the super, can be null
176      * @return this, the WildcardType
177      * @deprecated use setSuperType instead
178      */
179     @Deprecated
setSuperTypes(final ReferenceType superType)180     public WildcardType setSuperTypes(final ReferenceType superType) {
181         return setSuperType(superType);
182     }
183 
184     @Override
setAnnotations(NodeList<AnnotationExpr> annotations)185     public WildcardType setAnnotations(NodeList<AnnotationExpr> annotations) {
186         return (WildcardType) super.setAnnotations(annotations);
187     }
188 
189     @Override
190     @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
remove(Node node)191     public boolean remove(Node node) {
192         if (node == null)
193             return false;
194         if (extendedType != null) {
195             if (node == extendedType) {
196                 removeExtendedType();
197                 return true;
198             }
199         }
200         if (superType != null) {
201             if (node == superType) {
202                 removeSuperType();
203                 return true;
204             }
205         }
206         return super.remove(node);
207     }
208 
209     @Override
asString()210     public String asString() {
211         StringBuilder str = new StringBuilder("?");
212         getExtendedType().ifPresent(t -> str.append(" extends ").append(t.asString()));
213         getSuperType().ifPresent(t -> str.append(" super ").append(t.asString()));
214         return str.toString();
215     }
216 
217     @Deprecated
removeExtendedTypes()218     public WildcardType removeExtendedTypes() {
219         return removeExtendedType();
220     }
221 
222     @Deprecated
removeSuperTypes()223     public WildcardType removeSuperTypes() {
224         return removeSuperType();
225     }
226 
227     @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
removeExtendedType()228     public WildcardType removeExtendedType() {
229         return setExtendedType((ReferenceType) null);
230     }
231 
232     @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
removeSuperType()233     public WildcardType removeSuperType() {
234         return setSuperType((ReferenceType) null);
235     }
236 
237     @Override
238     @Generated("com.github.javaparser.generator.core.node.CloneGenerator")
clone()239     public WildcardType clone() {
240         return (WildcardType) accept(new CloneVisitor(), null);
241     }
242 
243     @Override
244     @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator")
getMetaModel()245     public WildcardTypeMetaModel getMetaModel() {
246         return JavaParserMetaModel.wildcardTypeMetaModel;
247     }
248 
249     @Override
250     @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
replace(Node node, Node replacementNode)251     public boolean replace(Node node, Node replacementNode) {
252         if (node == null)
253             return false;
254         if (extendedType != null) {
255             if (node == extendedType) {
256                 setExtendedType((ReferenceType) replacementNode);
257                 return true;
258             }
259         }
260         if (superType != null) {
261             if (node == superType) {
262                 setSuperType((ReferenceType) replacementNode);
263                 return true;
264             }
265         }
266         return super.replace(node, replacementNode);
267     }
268 
269     /**
270      * This constructor is used by the parser and is considered private.
271      */
272     @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
WildcardType(TokenRange tokenRange, ReferenceType extendedType, ReferenceType superType)273     public WildcardType(TokenRange tokenRange, ReferenceType extendedType, ReferenceType superType) {
274         super(tokenRange);
275         setExtendedType(extendedType);
276         setSuperType(superType);
277         customInitialization();
278     }
279 
280     @Override
281     @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
isWildcardType()282     public boolean isWildcardType() {
283         return true;
284     }
285 
286     @Override
287     @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
asWildcardType()288     public WildcardType asWildcardType() {
289         return this;
290     }
291 
292     @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
ifWildcardType(Consumer<WildcardType> action)293     public void ifWildcardType(Consumer<WildcardType> action) {
294         action.accept(this);
295     }
296 
297     @Override
resolve()298     public ResolvedWildcard resolve() {
299         return getSymbolResolver().toResolvedType(this, ResolvedWildcard.class);
300     }
301 
302     @Override
303     @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
toWildcardType()304     public Optional<WildcardType> toWildcardType() {
305         return Optional.of(this);
306     }
307 }
308