JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/body/ConstructorDeclaration.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.ast.*;
24import com.github.javaparser.ast.expr.AnnotationExpr;
25import com.github.javaparser.ast.expr.SimpleName;
26import com.github.javaparser.ast.nodeTypes.*;
27import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithAccessModifiers;
28import com.github.javaparser.ast.observer.ObservableProperty;
29import com.github.javaparser.ast.stmt.BlockStmt;
30import com.github.javaparser.ast.type.ReferenceType;
31import com.github.javaparser.ast.type.TypeParameter;
32import com.github.javaparser.ast.visitor.CloneVisitor;
33import com.github.javaparser.ast.visitor.GenericVisitor;
34import com.github.javaparser.ast.visitor.VoidVisitor;
35import com.github.javaparser.metamodel.ConstructorDeclarationMetaModel;
36import com.github.javaparser.metamodel.JavaParserMetaModel;
37import com.github.javaparser.TokenRange;
38import com.github.javaparser.resolution.Resolvable;
39import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration;
40import java.util.Optional;
41import java.util.function.Consumer;
42import static com.github.javaparser.utils.Utils.assertNotNull;
43import com.github.javaparser.ast.Node;
44import com.github.javaparser.ast.Generated;
45
46/**
47 * A constructor declaration: {@code class X { X() { } }} where X(){} is the constructor declaration.
48 * <p>
49 * <br>All annotations preceding the name will be set on this object, not on the class.
50 * JavaParser doesn't know if it they are applicable to the method or the class.
51 *
52 * @author Julio Vilmar Gesser
53 */
54public class ConstructorDeclaration extends CallableDeclaration<ConstructorDeclaration> implements NodeWithBlockStmt<ConstructorDeclaration>, NodeWithAccessModifiers<ConstructorDeclaration>, NodeWithJavadoc<ConstructorDeclaration>, NodeWithSimpleName<ConstructorDeclaration>, NodeWithParameters<ConstructorDeclaration>, NodeWithThrownExceptions<ConstructorDeclaration>, NodeWithTypeParameters<ConstructorDeclaration>, Resolvable<ResolvedConstructorDeclaration> {
55
56    private BlockStmt body;
57
58    public ConstructorDeclaration() {
59        this(null, new NodeList<>(), new NodeList<>(), new NodeList<>(), new SimpleName(), new NodeList<>(), new NodeList<>(), new BlockStmt(), null);
60    }
61
62    public ConstructorDeclaration(String name) {
63        this(null, new NodeList<>(new Modifier()), new NodeList<>(), new NodeList<>(), new SimpleName(name), new NodeList<>(), new NodeList<>(), new BlockStmt(), null);
64    }
65
66    public ConstructorDeclaration(NodeList<ModifiermodifiersString name) {
67        this(nullmodifiers, new NodeList<>(), new NodeList<>(), new SimpleName(name), new NodeList<>(), new NodeList<>(), new BlockStmt(), null);
68    }
69
70    public ConstructorDeclaration(NodeList<ModifiermodifiersNodeList<AnnotationExprannotationsNodeList<TypeParametertypeParametersSimpleName nameNodeList<ParameterparametersNodeList<ReferenceTypethrownExceptionsBlockStmt body) {
71        this(nullmodifiersannotationstypeParametersnameparametersthrownExceptionsbodynull);
72    }
73
74    @AllFieldsConstructor
75    public ConstructorDeclaration(NodeList<ModifiermodifiersNodeList<AnnotationExprannotationsNodeList<TypeParametertypeParametersSimpleName nameNodeList<ParameterparametersNodeList<ReferenceTypethrownExceptionsBlockStmt bodyReceiverParameter receiverParameter) {
76        this(nullmodifiersannotationstypeParametersnameparametersthrownExceptionsbodyreceiverParameter);
77    }
78
79    /**
80     * This constructor is used by the parser and is considered private.
81     */
82    @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
83    public ConstructorDeclaration(TokenRange tokenRangeNodeList<ModifiermodifiersNodeList<AnnotationExprannotationsNodeList<TypeParametertypeParametersSimpleName nameNodeList<ParameterparametersNodeList<ReferenceTypethrownExceptionsBlockStmt bodyReceiverParameter receiverParameter) {
84        super(tokenRangemodifiersannotationstypeParametersnameparametersthrownExceptionsreceiverParameter);
85        setBody(body);
86        customInitialization();
87    }
88
89    @Override
90    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
91    public <RAR accept(final GenericVisitor<RAvfinal A arg) {
92        return v.visit(this, arg);
93    }
94
95    @Override
96    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
97    public <Avoid accept(final VoidVisitor<Avfinal A arg) {
98        v.visit(this, arg);
99    }
100
101    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
102    public BlockStmt getBody() {
103        return body;
104    }
105
106    /**
107     * Sets the body
108     *
109     * @param body the body, can not be null
110     * @return this, the ConstructorDeclaration
111     */
112    @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
113    public ConstructorDeclaration setBody(final BlockStmt body) {
114        assertNotNull(body);
115        if (body == this.body) {
116            return (ConstructorDeclaration) this;
117        }
118        notifyPropertyChange(ObservableProperty.BODY, this.bodybody);
119        if (this.body != null)
120            this.body.setParentNode(null);
121        this.body = body;
122        setAsParentNodeOf(body);
123        return this;
124    }
125
126    @Override
127    public ConstructorDeclaration setModifiers(final NodeList<Modifiermodifiers) {
128        return super.setModifiers(modifiers);
129    }
130
131    @Override
132    public ConstructorDeclaration setName(final SimpleName name) {
133        return super.setName(name);
134    }
135
136    @Override
137    public ConstructorDeclaration setParameters(final NodeList<Parameterparameters) {
138        return super.setParameters(parameters);
139    }
140
141    @Override
142    public ConstructorDeclaration setThrownExceptions(final NodeList<ReferenceTypethrownExceptions) {
143        return super.setThrownExceptions(thrownExceptions);
144    }
145
146    @Override
147    public ConstructorDeclaration setTypeParameters(final NodeList<TypeParametertypeParameters) {
148        return super.setTypeParameters(typeParameters);
149    }
150
151    /**
152     * The declaration returned has this schema:
153     * <p>
154     * [accessSpecifier] className ([paramType [paramName]])
155     * [throws exceptionsList]
156     */
157    @Override
158    public String getDeclarationAsString(boolean includingModifiersboolean includingThrowsboolean includingParameterName) {
159        StringBuilder sb = new StringBuilder();
160        if (includingModifiers) {
161            AccessSpecifier accessSpecifier = getAccessSpecifier();
162            sb.append(accessSpecifier.asString()).append(" ");
163        }
164        sb.append(getName());
165        sb.append("(");
166        boolean firstParam = true;
167        for (Parameter param : getParameters()) {
168            if (firstParam) {
169                firstParam = false;
170            } else {
171                sb.append(", ");
172            }
173            if (includingParameterName) {
174                sb.append(param.toString(prettyPrinterNoCommentsConfiguration));
175            } else {
176                sb.append(param.getType().toString(prettyPrinterNoCommentsConfiguration));
177            }
178        }
179        sb.append(")");
180        sb.append(appendThrowsIfRequested(includingThrows));
181        return sb.toString();
182    }
183
184    @Override
185    @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
186    public boolean remove(Node node) {
187        if (node == null)
188            return false;
189        return super.remove(node);
190    }
191
192    @Override
193    @Generated("com.github.javaparser.generator.core.node.CloneGenerator")
194    public ConstructorDeclaration clone() {
195        return (ConstructorDeclarationaccept(new CloneVisitor(), null);
196    }
197
198    @Override
199    @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator")
200    public ConstructorDeclarationMetaModel getMetaModel() {
201        return JavaParserMetaModel.constructorDeclarationMetaModel;
202    }
203
204    @Override
205    @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
206    public boolean replace(Node nodeNode replacementNode) {
207        if (node == null)
208            return false;
209        if (node == body) {
210            setBody((BlockStmtreplacementNode);
211            return true;
212        }
213        return super.replace(nodereplacementNode);
214    }
215
216    @Override
217    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
218    public boolean isConstructorDeclaration() {
219        return true;
220    }
221
222    @Override
223    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
224    public ConstructorDeclaration asConstructorDeclaration() {
225        return this;
226    }
227
228    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
229    public void ifConstructorDeclaration(Consumer<ConstructorDeclarationaction) {
230        action.accept(this);
231    }
232
233    @Override
234    public ResolvedConstructorDeclaration resolve() {
235        return getSymbolResolver().resolveDeclaration(this, ResolvedConstructorDeclaration.class);
236    }
237
238    @Override
239    @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
240    public Optional<ConstructorDeclarationtoConstructorDeclaration() {
241        return Optional.of(this);
242    }
243}
244
MembersX
ConstructorDeclaration:setName
ConstructorDeclaration:resolve
ConstructorDeclaration:getDeclarationAsString:Block:Block:accessSpecifier
ConstructorDeclaration:getBody
ConstructorDeclaration:toConstructorDeclaration
ConstructorDeclaration:ConstructorDeclaration
ConstructorDeclaration:accept
ConstructorDeclaration:getDeclarationAsString:Block:sb
ConstructorDeclaration:replace
ConstructorDeclaration:setBody
ConstructorDeclaration:body
ConstructorDeclaration:setModifiers
ConstructorDeclaration:setThrownExceptions
ConstructorDeclaration:remove
ConstructorDeclaration:ifConstructorDeclaration
ConstructorDeclaration:setTypeParameters
ConstructorDeclaration:getDeclarationAsString
ConstructorDeclaration:getDeclarationAsString:Block:firstParam
ConstructorDeclaration:setParameters
ConstructorDeclaration:asConstructorDeclaration
ConstructorDeclaration:getMetaModel
ConstructorDeclaration:clone
ConstructorDeclaration:isConstructorDeclaration
Members
X