| 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 | */ |
| 21 | package com.github.javaparser.ast.body; |
| 22 | |
| 23 | import com.github.javaparser.ast.*; |
| 24 | import com.github.javaparser.ast.expr.AnnotationExpr; |
| 25 | import com.github.javaparser.ast.expr.SimpleName; |
| 26 | import com.github.javaparser.ast.nodeTypes.*; |
| 27 | import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithAccessModifiers; |
| 28 | import com.github.javaparser.ast.observer.ObservableProperty; |
| 29 | import com.github.javaparser.ast.stmt.BlockStmt; |
| 30 | import com.github.javaparser.ast.type.ReferenceType; |
| 31 | import com.github.javaparser.ast.type.TypeParameter; |
| 32 | import com.github.javaparser.ast.visitor.CloneVisitor; |
| 33 | import com.github.javaparser.ast.visitor.GenericVisitor; |
| 34 | import com.github.javaparser.ast.visitor.VoidVisitor; |
| 35 | import com.github.javaparser.metamodel.ConstructorDeclarationMetaModel; |
| 36 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
| 37 | import com.github.javaparser.TokenRange; |
| 38 | import com.github.javaparser.resolution.Resolvable; |
| 39 | import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration; |
| 40 | import java.util.Optional; |
| 41 | import java.util.function.Consumer; |
| 42 | import static com.github.javaparser.utils.Utils.assertNotNull; |
| 43 | import com.github.javaparser.ast.Node; |
| 44 | import 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 | */ |
| 54 | public 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<Modifier> modifiers, String name) { |
| 67 | this(null, modifiers, new NodeList<>(), new NodeList<>(), new SimpleName(name), new NodeList<>(), new NodeList<>(), new BlockStmt(), null); |
| 68 | } |
| 69 | |
| 70 | public ConstructorDeclaration(NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<TypeParameter> typeParameters, SimpleName name, NodeList<Parameter> parameters, NodeList<ReferenceType> thrownExceptions, BlockStmt body) { |
| 71 | this(null, modifiers, annotations, typeParameters, name, parameters, thrownExceptions, body, null); |
| 72 | } |
| 73 | |
| 74 | @AllFieldsConstructor |
| 75 | public ConstructorDeclaration(NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<TypeParameter> typeParameters, SimpleName name, NodeList<Parameter> parameters, NodeList<ReferenceType> thrownExceptions, BlockStmt body, ReceiverParameter receiverParameter) { |
| 76 | this(null, modifiers, annotations, typeParameters, name, parameters, thrownExceptions, body, receiverParameter); |
| 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 tokenRange, NodeList<Modifier> modifiers, NodeList<AnnotationExpr> annotations, NodeList<TypeParameter> typeParameters, SimpleName name, NodeList<Parameter> parameters, NodeList<ReferenceType> thrownExceptions, BlockStmt body, ReceiverParameter receiverParameter) { |
| 84 | super(tokenRange, modifiers, annotations, typeParameters, name, parameters, thrownExceptions, receiverParameter); |
| 85 | setBody(body); |
| 86 | customInitialization(); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 91 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
| 92 | return v.visit(this, arg); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 97 | public <A> void accept(final VoidVisitor<A> v, final 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.body, body); |
| 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<Modifier> modifiers) { |
| 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<Parameter> parameters) { |
| 138 | return super.setParameters(parameters); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public ConstructorDeclaration setThrownExceptions(final NodeList<ReferenceType> thrownExceptions) { |
| 143 | return super.setThrownExceptions(thrownExceptions); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public ConstructorDeclaration setTypeParameters(final NodeList<TypeParameter> typeParameters) { |
| 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 includingModifiers, boolean includingThrows, boolean 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 (ConstructorDeclaration) accept(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 node, Node replacementNode) { |
| 207 | if (node == null) |
| 208 | return false; |
| 209 | if (node == body) { |
| 210 | setBody((BlockStmt) replacementNode); |
| 211 | return true; |
| 212 | } |
| 213 | return super.replace(node, replacementNode); |
| 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<ConstructorDeclaration> action) { |
| 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<ConstructorDeclaration> toConstructorDeclaration() { |
| 241 | return Optional.of(this); |
| 242 | } |
| 243 | } |
| 244 |
Members