JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/type/TypeParameter.java
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 */
21package com.github.javaparser.ast.type;
22
23import com.github.javaparser.Range;
24import com.github.javaparser.ast.AllFieldsConstructor;
25import com.github.javaparser.ast.NodeList;
26import com.github.javaparser.ast.expr.AnnotationExpr;
27import com.github.javaparser.ast.expr.SimpleName;
28import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations;
29import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
30import com.github.javaparser.ast.observer.ObservableProperty;
31import com.github.javaparser.ast.visitor.GenericVisitor;
32import com.github.javaparser.ast.visitor.VoidVisitor;
33import static com.github.javaparser.utils.Utils.assertNotNull;
34import static java.util.stream.Collectors.joining;
35import com.github.javaparser.ast.Node;
36import com.github.javaparser.ast.visitor.CloneVisitor;
37import com.github.javaparser.metamodel.TypeParameterMetaModel;
38import com.github.javaparser.metamodel.JavaParserMetaModel;
39import com.github.javaparser.TokenRange;
40import com.github.javaparser.resolution.types.ResolvedTypeVariable;
41import java.util.function.Consumer;
42import java.util.Optional;
43import 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 */
57public class TypeParameter extends ReferenceType implements NodeWithSimpleName<TypeParameter>, NodeWithAnnotations<TypeParameter> {
58
59    private SimpleName name;
60
61    private NodeList<ClassOrInterfaceTypetypeBound;
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 namefinal NodeList<ClassOrInterfaceTypetypeBound) {
72        this(null, new SimpleName(name), typeBound, new NodeList<>());
73    }
74
75    @AllFieldsConstructor
76    public TypeParameter(SimpleName nameNodeList<ClassOrInterfaceTypetypeBoundNodeList<AnnotationExprannotations) {
77        this(nullnametypeBoundannotations);
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 tokenRangeSimpleName nameNodeList<ClassOrInterfaceTypetypeBoundNodeList<AnnotationExprannotations) {
85        super(tokenRangeannotations);
86        setName(name);
87        setTypeBound(typeBound);
88        customInitialization();
89    }
90
91    @Override
92    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
93    public <RAR accept(final GenericVisitor<RAvfinal A arg) {
94        return v.visit(this, arg);
95    }
96
97    @Override
98    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
99    public <Avoid accept(final VoidVisitor<Avfinal 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<ClassOrInterfaceTypegetTypeBound() {
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.namename);
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<ClassOrInterfaceTypetypeBound) {
140        assertNotNull(typeBound);
141        if (typeBound == this.typeBound) {
142            return (TypeParameter) this;
143        }
144        notifyPropertyChange(ObservableProperty.TYPE_BOUND, this.typeBoundtypeBound);
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<AnnotationExprannotations) {
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 = 0i < 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 (TypeParameteraccept(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 nodeNode replacementNode) {
194        if (node == null)
195            return false;
196        if (node == name) {
197            setName((SimpleNamereplacementNode);
198            return true;
199        }
200        for (int i = 0i < typeBound.size(); i++) {
201            if (typeBound.get(i) == node) {
202                typeBound.set(i, (ClassOrInterfaceTypereplacementNode);
203                return true;
204            }
205        }
206        return super.replace(nodereplacementNode);
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<TypeParameteraction) {
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<TypeParametertoTypeParameter() {
234        return Optional.of(this);
235    }
236}
237
MembersX
TypeParameter:remove
TypeParameter:typeBound
TypeParameter:TypeParameter
TypeParameter:asString:Block:str
TypeParameter:asString
TypeParameter:getTypeBound
TypeParameter:toTypeParameter
TypeParameter:name
TypeParameter:setName
TypeParameter:clone
TypeParameter:isTypeParameter
TypeParameter:ifTypeParameter
TypeParameter:asTypeParameter
TypeParameter:resolve
TypeParameter:getName
TypeParameter:accept
TypeParameter:getMetaModel
TypeParameter:replace
TypeParameter:setAnnotations
TypeParameter:setTypeBound
Members
X