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.AllFieldsConstructor; |
24 | import com.github.javaparser.ast.nodeTypes.NodeWithBlockStmt; |
25 | import com.github.javaparser.ast.nodeTypes.NodeWithJavadoc; |
26 | import com.github.javaparser.ast.observer.ObservableProperty; |
27 | import com.github.javaparser.ast.stmt.BlockStmt; |
28 | import com.github.javaparser.ast.visitor.GenericVisitor; |
29 | import com.github.javaparser.ast.visitor.VoidVisitor; |
30 | import static com.github.javaparser.utils.Utils.assertNotNull; |
31 | import com.github.javaparser.ast.Node; |
32 | import com.github.javaparser.ast.visitor.CloneVisitor; |
33 | import com.github.javaparser.metamodel.InitializerDeclarationMetaModel; |
34 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
35 | import com.github.javaparser.TokenRange; |
36 | import java.util.function.Consumer; |
37 | import java.util.Optional; |
38 | import com.github.javaparser.ast.Generated; |
39 | |
40 | /** |
41 | * A (possibly static) initializer body. "static { a=3; }" in this example: {@code class X { static { a=3; } } } |
42 | * |
43 | * @author Julio Vilmar Gesser |
44 | */ |
45 | public class InitializerDeclaration extends BodyDeclaration<InitializerDeclaration> implements NodeWithJavadoc<InitializerDeclaration>, NodeWithBlockStmt<InitializerDeclaration> { |
46 | |
47 | private boolean isStatic; |
48 | |
49 | private BlockStmt body; |
50 | |
51 | public InitializerDeclaration() { |
52 | this(null, false, new BlockStmt()); |
53 | } |
54 | |
55 | @AllFieldsConstructor |
56 | public InitializerDeclaration(boolean isStatic, BlockStmt body) { |
57 | this(null, isStatic, body); |
58 | } |
59 | |
60 | /** |
61 | * This constructor is used by the parser and is considered private. |
62 | */ |
63 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
64 | public InitializerDeclaration(TokenRange tokenRange, boolean isStatic, BlockStmt body) { |
65 | super(tokenRange); |
66 | setStatic(isStatic); |
67 | setBody(body); |
68 | customInitialization(); |
69 | } |
70 | |
71 | @Override |
72 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
73 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
74 | return v.visit(this, arg); |
75 | } |
76 | |
77 | @Override |
78 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
79 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
80 | v.visit(this, arg); |
81 | } |
82 | |
83 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
84 | public BlockStmt getBody() { |
85 | return body; |
86 | } |
87 | |
88 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
89 | public boolean isStatic() { |
90 | return isStatic; |
91 | } |
92 | |
93 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
94 | public InitializerDeclaration setBody(final BlockStmt body) { |
95 | assertNotNull(body); |
96 | if (body == this.body) { |
97 | return (InitializerDeclaration) this; |
98 | } |
99 | notifyPropertyChange(ObservableProperty.BODY, this.body, body); |
100 | if (this.body != null) |
101 | this.body.setParentNode(null); |
102 | this.body = body; |
103 | setAsParentNodeOf(body); |
104 | return this; |
105 | } |
106 | |
107 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
108 | public InitializerDeclaration setStatic(final boolean isStatic) { |
109 | if (isStatic == this.isStatic) { |
110 | return (InitializerDeclaration) this; |
111 | } |
112 | notifyPropertyChange(ObservableProperty.STATIC, this.isStatic, isStatic); |
113 | this.isStatic = isStatic; |
114 | return this; |
115 | } |
116 | |
117 | @Override |
118 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
119 | public boolean remove(Node node) { |
120 | if (node == null) |
121 | return false; |
122 | return super.remove(node); |
123 | } |
124 | |
125 | @Override |
126 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
127 | public InitializerDeclaration clone() { |
128 | return (InitializerDeclaration) accept(new CloneVisitor(), null); |
129 | } |
130 | |
131 | @Override |
132 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
133 | public InitializerDeclarationMetaModel getMetaModel() { |
134 | return JavaParserMetaModel.initializerDeclarationMetaModel; |
135 | } |
136 | |
137 | @Override |
138 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
139 | public boolean replace(Node node, Node replacementNode) { |
140 | if (node == null) |
141 | return false; |
142 | if (node == body) { |
143 | setBody((BlockStmt) replacementNode); |
144 | return true; |
145 | } |
146 | return super.replace(node, replacementNode); |
147 | } |
148 | |
149 | @Override |
150 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
151 | public boolean isInitializerDeclaration() { |
152 | return true; |
153 | } |
154 | |
155 | @Override |
156 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
157 | public InitializerDeclaration asInitializerDeclaration() { |
158 | return this; |
159 | } |
160 | |
161 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
162 | public void ifInitializerDeclaration(Consumer<InitializerDeclaration> action) { |
163 | action.accept(this); |
164 | } |
165 | |
166 | @Override |
167 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
168 | public Optional<InitializerDeclaration> toInitializerDeclaration() { |
169 | return Optional.of(this); |
170 | } |
171 | } |
172 |
Members