JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/body/TypeDeclaration.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.NodeWithJavadoc;
28import com.github.javaparser.ast.nodeTypes.NodeWithMembers;
29import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
30import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithAccessModifiers;
31import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithStaticModifier;
32import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithStrictfpModifier;
33import com.github.javaparser.ast.observer.ObservableProperty;
34import com.github.javaparser.ast.visitor.CloneVisitor;
35import com.github.javaparser.metamodel.JavaParserMetaModel;
36import com.github.javaparser.metamodel.TypeDeclarationMetaModel;
37import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
38import java.util.List;
39import java.util.Optional;
40import java.util.function.Consumer;
41import static com.github.javaparser.utils.Utils.assertNotNull;
42import static java.util.stream.Collectors.toList;
43import com.github.javaparser.ast.Node;
44
45/**
46 * A base class for all types of type declarations.
47 *
48 * @author Julio Vilmar Gesser
49 */
50public abstract class TypeDeclaration<T extends TypeDeclaration<?>> extends BodyDeclaration<T> implements NodeWithSimpleName<T>, NodeWithJavadoc<T>, NodeWithMembers<T>, NodeWithAccessModifiers<T>, NodeWithStaticModifier<T>, NodeWithStrictfpModifier<T> {
51
52    private SimpleName name;
53
54    private NodeList<Modifiermodifiers;
55
56    private NodeList<BodyDeclaration<?>> members;
57
58    public TypeDeclaration() {
59        this(null, new NodeList<>(), new NodeList<>(), new SimpleName(), new NodeList<>());
60    }
61
62    public TypeDeclaration(NodeList<ModifiermodifiersString name) {
63        this(nullmodifiers, new NodeList<>(), new SimpleName(name), new NodeList<>());
64    }
65
66    @AllFieldsConstructor
67    public TypeDeclaration(NodeList<ModifiermodifiersNodeList<AnnotationExprannotationsSimpleName nameNodeList<BodyDeclaration<?>> members) {
68        this(nullmodifiersannotationsnamemembers);
69    }
70
71    /**
72     * This constructor is used by the parser and is considered private.
73     */
74    @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
75    public TypeDeclaration(TokenRange tokenRangeNodeList<ModifiermodifiersNodeList<AnnotationExprannotationsSimpleName nameNodeList<BodyDeclaration<?>> members) {
76        super(tokenRangeannotations);
77        setModifiers(modifiers);
78        setName(name);
79        setMembers(members);
80        customInitialization();
81    }
82
83    /**
84     * Adds the given declaration to the specified type.
85     *
86     * @param decl member declaration
87     */
88    public T addMember(BodyDeclaration<?> decl) {
89        NodeList<BodyDeclaration<?>> members = getMembers();
90        members.add(decl);
91        return (T) this;
92    }
93
94    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
95    public NodeList<BodyDeclaration<?>> getMembers() {
96        return members;
97    }
98
99    /**
100     * Return the modifiers of this type declaration.
101     *
102     * @return modifiers
103     * @see Modifier
104     */
105    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
106    public NodeList<ModifiergetModifiers() {
107        return modifiers;
108    }
109
110    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
111    @SuppressWarnings("unchecked")
112    public T setMembers(final NodeList<BodyDeclaration<?>> members) {
113        assertNotNull(members);
114        if (members == this.members) {
115            return (T) this;
116        }
117        notifyPropertyChange(ObservableProperty.MEMBERS, this.membersmembers);
118        if (this.members != null)
119            this.members.setParentNode(null);
120        this.members = members;
121        setAsParentNodeOf(members);
122        return (T) this;
123    }
124
125    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
126    @SuppressWarnings("unchecked")
127    public T setModifiers(final NodeList<Modifiermodifiers) {
128        assertNotNull(modifiers);
129        if (modifiers == this.modifiers) {
130            return (T) this;
131        }
132        notifyPropertyChange(ObservableProperty.MODIFIERS, this.modifiersmodifiers);
133        if (this.modifiers != null)
134            this.modifiers.setParentNode(null);
135        this.modifiers = modifiers;
136        setAsParentNodeOf(modifiers);
137        return (T) this;
138    }
139
140    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
141    @SuppressWarnings("unchecked")
142    public T setName(final SimpleName name) {
143        assertNotNull(name);
144        if (name == this.name) {
145            return (T) this;
146        }
147        notifyPropertyChange(ObservableProperty.NAME, this.namename);
148        if (this.name != null)
149            this.name.setParentNode(null);
150        this.name = name;
151        setAsParentNodeOf(name);
152        return (T) this;
153    }
154
155    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
156    public SimpleName getName() {
157        return name;
158    }
159
160    @Override
161    @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
162    public boolean remove(Node node) {
163        if (node == null)
164            return false;
165        for (int i = 0i < members.size(); i++) {
166            if (members.get(i) == node) {
167                members.remove(i);
168                return true;
169            }
170        }
171        for (int i = 0i < modifiers.size(); i++) {
172            if (modifiers.get(i) == node) {
173                modifiers.remove(i);
174                return true;
175            }
176        }
177        return super.remove(node);
178    }
179
180    /**
181     * @return is this type's parent a CompilationUnit?
182     */
183    public boolean isTopLevelType() {
184        return getParentNode().map(p -> p instanceof CompilationUnit).orElse(false);
185    }
186
187    /**
188     * @return methods or constructors whose signatures match the passed signature.
189     */
190    public List<CallableDeclaration<?>> getCallablesWithSignature(CallableDeclaration.Signature signature) {
191        return getMembers().stream().filter(m -> m instanceof CallableDeclaration).map(m -> ((CallableDeclaration<?>) m)).filter(m -> m.getSignature().equals(signature)).collect(toList());
192    }
193
194    /**
195     * Returns the fully qualified name of this type, derived only from information available in this compilation unit. (So no symbol solving happens.)
196     * If the declared type is a local class declaration, it will return Optional.empty().
197     * If the declared type is not contained in a compilation unit, it will return Optional.empty().
198     * @see com.github.javaparser.ast.stmt.LocalClassDeclarationStmt
199     */
200    public Optional<StringgetFullyQualifiedName() {
201        if (isTopLevelType()) {
202            return findCompilationUnit().map(cu -> cu.getPackageDeclaration().map(pd -> pd.getNameAsString()).map(pkg -> pkg + "." + getNameAsString()).orElse(getNameAsString()));
203        }
204        return findAncestor(TypeDeclaration.class).map(td -> (TypeDeclaration<?>) td).flatMap(td -> td.getFullyQualifiedName().map(fqn -> fqn + "." + getNameAsString()));
205    }
206
207    /**
208     * @return is this type's parent a TypeDeclaration?
209     * NOTE: many people are confused over terminology. Refer to https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html .
210     */
211    public boolean isNestedType() {
212        return getParentNode().map(p -> p instanceof TypeDeclaration).orElse(false);
213    }
214
215    @Override
216    @Generated("com.github.javaparser.generator.core.node.CloneGenerator")
217    public TypeDeclaration<?> clone() {
218        return (TypeDeclaration<?>) accept(new CloneVisitor(), null);
219    }
220
221    @Override
222    @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator")
223    public TypeDeclarationMetaModel getMetaModel() {
224        return JavaParserMetaModel.typeDeclarationMetaModel;
225    }
226
227    @Override
228    @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
229    public boolean replace(Node nodeNode replacementNode) {
230        if (node == null)
231            return false;
232        for (int i = 0i < members.size(); i++) {
233            if (members.get(i) == node) {
234                members.set(i, (BodyDeclarationreplacementNode);
235                return true;
236            }
237        }
238        for (int i = 0i < modifiers.size(); i++) {
239            if (modifiers.get(i) == node) {
240                modifiers.set(i, (ModifierreplacementNode);
241                return true;
242            }
243        }
244        if (node == name) {
245            setName((SimpleNamereplacementNode);
246            return true;
247        }
248        return super.replace(nodereplacementNode);
249    }
250
251    @Override
252    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
253    public boolean isTypeDeclaration() {
254        return true;
255    }
256
257    @Override
258    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
259    public TypeDeclaration asTypeDeclaration() {
260        return this;
261    }
262
263    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
264    public void ifTypeDeclaration(Consumer<TypeDeclarationaction) {
265        action.accept(this);
266    }
267
268    @Override
269    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
270    public Optional<TypeDeclarationtoTypeDeclaration() {
271        return Optional.of(this);
272    }
273
274    public abstract ResolvedReferenceTypeDeclaration resolve();
275}
276
MembersX
TypeDeclaration:isTopLevelType
TypeDeclaration:getCallablesWithSignature
TypeDeclaration:members
TypeDeclaration:modifiers
TypeDeclaration:getModifiers
TypeDeclaration:getFullyQualifiedName
TypeDeclaration:isNestedType
TypeDeclaration:toTypeDeclaration
TypeDeclaration:setMembers
TypeDeclaration:replace
TypeDeclaration:setModifiers
TypeDeclaration:getMembers
TypeDeclaration:addMember:Block:members
TypeDeclaration:asTypeDeclaration
TypeDeclaration:getMetaModel
TypeDeclaration:ifTypeDeclaration
TypeDeclaration:isTypeDeclaration
TypeDeclaration:getName
TypeDeclaration:resolve
TypeDeclaration:addMember
TypeDeclaration:setName
TypeDeclaration:remove
TypeDeclaration:clone
TypeDeclaration:name
TypeDeclaration:TypeDeclaration
Members
X