JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/body/ClassOrInterfaceDeclaration.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.body;
22
23import com.github.javaparser.TokenRange;
24import com.github.javaparser.ast.*;
25import com.github.javaparser.ast.expr.AnnotationExpr;
26import com.github.javaparser.ast.expr.SimpleName;
27import com.github.javaparser.ast.nodeTypes.NodeWithExtends;
28import com.github.javaparser.ast.nodeTypes.NodeWithImplements;
29import com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters;
30import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithAbstractModifier;
31import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithFinalModifier;
32import com.github.javaparser.ast.observer.ObservableProperty;
33import com.github.javaparser.ast.stmt.LocalClassDeclarationStmt;
34import com.github.javaparser.ast.type.ClassOrInterfaceType;
35import com.github.javaparser.ast.type.TypeParameter;
36import com.github.javaparser.ast.visitor.CloneVisitor;
37import com.github.javaparser.ast.visitor.GenericVisitor;
38import com.github.javaparser.ast.visitor.VoidVisitor;
39import com.github.javaparser.metamodel.ClassOrInterfaceDeclarationMetaModel;
40import com.github.javaparser.metamodel.JavaParserMetaModel;
41import com.github.javaparser.resolution.Resolvable;
42import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
43import java.util.Optional;
44import java.util.function.Consumer;
45import static com.github.javaparser.utils.Utils.assertNotNull;
46import com.github.javaparser.ast.Node;
47import com.github.javaparser.ast.Generated;
48
49/**
50 * A definition of a class or interface.<br>{@code class X { ... }}
51 *
52 * @author Julio Vilmar Gesser
53 */
54public class ClassOrInterfaceDeclaration extends TypeDeclaration<ClassOrInterfaceDeclaration> implements NodeWithImplements<ClassOrInterfaceDeclaration>, NodeWithExtends<ClassOrInterfaceDeclaration>, NodeWithTypeParameters<ClassOrInterfaceDeclaration>, NodeWithAbstractModifier<ClassOrInterfaceDeclaration>, NodeWithFinalModifier<ClassOrInterfaceDeclaration>, Resolvable<ResolvedReferenceTypeDeclaration> {
55
56    private boolean isInterface;
57
58    private NodeList<TypeParametertypeParameters;
59
60    // Can contain more than one item if this is an interface
61    private NodeList<ClassOrInterfaceTypeextendedTypes;
62
63    private NodeList<ClassOrInterfaceTypeimplementedTypes;
64
65    public ClassOrInterfaceDeclaration() {
66        this(null, new NodeList<>(), new NodeList<>(), false, new SimpleName(), new NodeList<>(), new NodeList<>(), new NodeList<>(), new NodeList<>());
67    }
68
69    public ClassOrInterfaceDeclaration(final NodeList<Modifiermodifiersfinal boolean isInterfacefinal String name) {
70        this(nullmodifiers, new NodeList<>(), isInterface, new SimpleName(name), new NodeList<>(), new NodeList<>(), new NodeList<>(), new NodeList<>());
71    }
72
73    @AllFieldsConstructor
74    public ClassOrInterfaceDeclaration(final NodeList<Modifiermodifiersfinal NodeList<AnnotationExprannotationsfinal boolean isInterfacefinal SimpleName namefinal NodeList<TypeParametertypeParametersfinal NodeList<ClassOrInterfaceTypeextendedTypesfinal NodeList<ClassOrInterfaceTypeimplementedTypesfinal NodeList<BodyDeclaration<?>> members) {
75        this(nullmodifiersannotationsisInterfacenametypeParametersextendedTypesimplementedTypesmembers);
76    }
77
78    /**
79     * This constructor is used by the parser and is considered private.
80     */
81    @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
82    public ClassOrInterfaceDeclaration(TokenRange tokenRangeNodeList<ModifiermodifiersNodeList<AnnotationExprannotationsboolean isInterfaceSimpleName nameNodeList<TypeParametertypeParametersNodeList<ClassOrInterfaceTypeextendedTypesNodeList<ClassOrInterfaceTypeimplementedTypesNodeList<BodyDeclaration<?>> members) {
83        super(tokenRangemodifiersannotationsnamemembers);
84        setInterface(isInterface);
85        setTypeParameters(typeParameters);
86        setExtendedTypes(extendedTypes);
87        setImplementedTypes(implementedTypes);
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    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
104    public NodeList<ClassOrInterfaceTypegetExtendedTypes() {
105        return extendedTypes;
106    }
107
108    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
109    public NodeList<ClassOrInterfaceTypegetImplementedTypes() {
110        return implementedTypes;
111    }
112
113    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
114    public NodeList<TypeParametergetTypeParameters() {
115        return typeParameters;
116    }
117
118    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
119    public boolean isInterface() {
120        return isInterface;
121    }
122
123    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
124    public ClassOrInterfaceDeclaration setExtendedTypes(final NodeList<ClassOrInterfaceTypeextendedTypes) {
125        assertNotNull(extendedTypes);
126        if (extendedTypes == this.extendedTypes) {
127            return (ClassOrInterfaceDeclaration) this;
128        }
129        notifyPropertyChange(ObservableProperty.EXTENDED_TYPES, this.extendedTypesextendedTypes);
130        if (this.extendedTypes != null)
131            this.extendedTypes.setParentNode(null);
132        this.extendedTypes = extendedTypes;
133        setAsParentNodeOf(extendedTypes);
134        return this;
135    }
136
137    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
138    public ClassOrInterfaceDeclaration setImplementedTypes(final NodeList<ClassOrInterfaceTypeimplementedTypes) {
139        assertNotNull(implementedTypes);
140        if (implementedTypes == this.implementedTypes) {
141            return (ClassOrInterfaceDeclaration) this;
142        }
143        notifyPropertyChange(ObservableProperty.IMPLEMENTED_TYPES, this.implementedTypesimplementedTypes);
144        if (this.implementedTypes != null)
145            this.implementedTypes.setParentNode(null);
146        this.implementedTypes = implementedTypes;
147        setAsParentNodeOf(implementedTypes);
148        return this;
149    }
150
151    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
152    public ClassOrInterfaceDeclaration setInterface(final boolean isInterface) {
153        if (isInterface == this.isInterface) {
154            return (ClassOrInterfaceDeclaration) this;
155        }
156        notifyPropertyChange(ObservableProperty.INTERFACE, this.isInterfaceisInterface);
157        this.isInterface = isInterface;
158        return this;
159    }
160
161    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
162    public ClassOrInterfaceDeclaration setTypeParameters(final NodeList<TypeParametertypeParameters) {
163        assertNotNull(typeParameters);
164        if (typeParameters == this.typeParameters) {
165            return (ClassOrInterfaceDeclaration) this;
166        }
167        notifyPropertyChange(ObservableProperty.TYPE_PARAMETERS, this.typeParameterstypeParameters);
168        if (this.typeParameters != null)
169            this.typeParameters.setParentNode(null);
170        this.typeParameters = typeParameters;
171        setAsParentNodeOf(typeParameters);
172        return this;
173    }
174
175    @Override
176    @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
177    public boolean remove(Node node) {
178        if (node == null)
179            return false;
180        for (int i = 0i < extendedTypes.size(); i++) {
181            if (extendedTypes.get(i) == node) {
182                extendedTypes.remove(i);
183                return true;
184            }
185        }
186        for (int i = 0i < implementedTypes.size(); i++) {
187            if (implementedTypes.get(i) == node) {
188                implementedTypes.remove(i);
189                return true;
190            }
191        }
192        for (int i = 0i < typeParameters.size(); i++) {
193            if (typeParameters.get(i) == node) {
194                typeParameters.remove(i);
195                return true;
196            }
197        }
198        return super.remove(node);
199    }
200
201    /**
202     * @return is this class's parent a LocalClassDeclarationStmt ?
203     */
204    public boolean isLocalClassDeclaration() {
205        return getParentNode().map(p -> p instanceof LocalClassDeclarationStmt).orElse(false);
206    }
207
208    @Override
209    public Optional<StringgetFullyQualifiedName() {
210        if (isLocalClassDeclaration()) {
211            return Optional.empty();
212        }
213        return super.getFullyQualifiedName();
214    }
215
216    /**
217     * @return is this an inner class?
218     * NOTE: many people are confused over terminology. Refer to https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html .
219     */
220    public boolean isInnerClass() {
221        return isNestedType() && !isInterface && !isStatic();
222    }
223
224    @Override
225    @Generated("com.github.javaparser.generator.core.node.CloneGenerator")
226    public ClassOrInterfaceDeclaration clone() {
227        return (ClassOrInterfaceDeclarationaccept(new CloneVisitor(), null);
228    }
229
230    @Override
231    @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator")
232    public ClassOrInterfaceDeclarationMetaModel getMetaModel() {
233        return JavaParserMetaModel.classOrInterfaceDeclarationMetaModel;
234    }
235
236    @Override
237    @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
238    public boolean replace(Node nodeNode replacementNode) {
239        if (node == null)
240            return false;
241        for (int i = 0i < extendedTypes.size(); i++) {
242            if (extendedTypes.get(i) == node) {
243                extendedTypes.set(i, (ClassOrInterfaceTypereplacementNode);
244                return true;
245            }
246        }
247        for (int i = 0i < implementedTypes.size(); i++) {
248            if (implementedTypes.get(i) == node) {
249                implementedTypes.set(i, (ClassOrInterfaceTypereplacementNode);
250                return true;
251            }
252        }
253        for (int i = 0i < typeParameters.size(); i++) {
254            if (typeParameters.get(i) == node) {
255                typeParameters.set(i, (TypeParameterreplacementNode);
256                return true;
257            }
258        }
259        return super.replace(nodereplacementNode);
260    }
261
262    @Override
263    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
264    public boolean isClassOrInterfaceDeclaration() {
265        return true;
266    }
267
268    @Override
269    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
270    public ClassOrInterfaceDeclaration asClassOrInterfaceDeclaration() {
271        return this;
272    }
273
274    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
275    public void ifClassOrInterfaceDeclaration(Consumer<ClassOrInterfaceDeclarationaction) {
276        action.accept(this);
277    }
278
279    @Override
280    public ResolvedReferenceTypeDeclaration resolve() {
281        return getSymbolResolver().resolveDeclaration(this, ResolvedReferenceTypeDeclaration.class);
282    }
283
284    @Override
285    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
286    public Optional<ClassOrInterfaceDeclarationtoClassOrInterfaceDeclaration() {
287        return Optional.of(this);
288    }
289}
290
MembersX
ClassOrInterfaceDeclaration:clone
ClassOrInterfaceDeclaration:isInterface
ClassOrInterfaceDeclaration:setImplementedTypes
ClassOrInterfaceDeclaration:setTypeParameters
ClassOrInterfaceDeclaration:getTypeParameters
ClassOrInterfaceDeclaration:replace
ClassOrInterfaceDeclaration:ifClassOrInterfaceDeclaration
ClassOrInterfaceDeclaration:remove
ClassOrInterfaceDeclaration:implementedTypes
ClassOrInterfaceDeclaration:isInnerClass
ClassOrInterfaceDeclaration:extendedTypes
ClassOrInterfaceDeclaration:resolve
ClassOrInterfaceDeclaration:ClassOrInterfaceDeclaration
ClassOrInterfaceDeclaration:typeParameters
ClassOrInterfaceDeclaration:getExtendedTypes
ClassOrInterfaceDeclaration:setInterface
ClassOrInterfaceDeclaration:toClassOrInterfaceDeclaration
ClassOrInterfaceDeclaration:setExtendedTypes
ClassOrInterfaceDeclaration:isClassOrInterfaceDeclaration
ClassOrInterfaceDeclaration:isLocalClassDeclaration
ClassOrInterfaceDeclaration:getImplementedTypes
ClassOrInterfaceDeclaration:getFullyQualifiedName
ClassOrInterfaceDeclaration:accept
ClassOrInterfaceDeclaration:getMetaModel
ClassOrInterfaceDeclaration:asClassOrInterfaceDeclaration
Members
X