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.stmt; |
22 | |
23 | import com.github.javaparser.TokenRange; |
24 | import com.github.javaparser.ast.*; |
25 | import com.github.javaparser.ast.body.Parameter; |
26 | import com.github.javaparser.ast.expr.AnnotationExpr; |
27 | import com.github.javaparser.ast.expr.SimpleName; |
28 | import com.github.javaparser.ast.nodeTypes.NodeWithBlockStmt; |
29 | import com.github.javaparser.ast.observer.ObservableProperty; |
30 | import com.github.javaparser.ast.type.ClassOrInterfaceType; |
31 | import com.github.javaparser.ast.visitor.CloneVisitor; |
32 | import com.github.javaparser.ast.visitor.GenericVisitor; |
33 | import com.github.javaparser.ast.visitor.VoidVisitor; |
34 | import com.github.javaparser.metamodel.CatchClauseMetaModel; |
35 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
36 | import static com.github.javaparser.utils.Utils.assertNotNull; |
37 | import com.github.javaparser.ast.Node; |
38 | import com.github.javaparser.ast.Generated; |
39 | |
40 | /** |
41 | * The catch part of a try-catch-finally. <br>In {@code try { ... } catch (Exception e) { ... }} the CatchClause |
42 | * is {@code catch (Exception e) { ... }}. Exception e is the parameter. The { ... } is the body. |
43 | * |
44 | * @author Julio Vilmar Gesser |
45 | */ |
46 | public class CatchClause extends Node implements NodeWithBlockStmt<CatchClause> { |
47 | |
48 | private Parameter parameter; |
49 | |
50 | private BlockStmt body; |
51 | |
52 | public CatchClause() { |
53 | this(null, new Parameter(), new BlockStmt()); |
54 | } |
55 | |
56 | public CatchClause(final NodeList<Modifier> exceptModifier, final NodeList<AnnotationExpr> exceptAnnotations, final ClassOrInterfaceType exceptType, final SimpleName exceptName, final BlockStmt body) { |
57 | this(null, new Parameter(null, exceptModifier, exceptAnnotations, exceptType, false, new NodeList<>(), exceptName), body); |
58 | } |
59 | |
60 | @AllFieldsConstructor |
61 | public CatchClause(final Parameter parameter, final BlockStmt body) { |
62 | this(null, parameter, body); |
63 | } |
64 | |
65 | /** |
66 | * This constructor is used by the parser and is considered private. |
67 | */ |
68 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
69 | public CatchClause(TokenRange tokenRange, Parameter parameter, BlockStmt body) { |
70 | super(tokenRange); |
71 | setParameter(parameter); |
72 | setBody(body); |
73 | customInitialization(); |
74 | } |
75 | |
76 | @Override |
77 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
78 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
79 | return v.visit(this, arg); |
80 | } |
81 | |
82 | @Override |
83 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
84 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
85 | v.visit(this, arg); |
86 | } |
87 | |
88 | /** |
89 | * Note that the type of the Parameter can be a UnionType. In this case, any annotations found at the start of the |
90 | * catch(@X A a |...) are found directly in the Parameter. Annotations that are on the second or later type - |
91 | * catch(A a | @X B b ...) are found on those types. |
92 | */ |
93 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
94 | public Parameter getParameter() { |
95 | return parameter; |
96 | } |
97 | |
98 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
99 | public CatchClause setParameter(final Parameter parameter) { |
100 | assertNotNull(parameter); |
101 | if (parameter == this.parameter) { |
102 | return (CatchClause) this; |
103 | } |
104 | notifyPropertyChange(ObservableProperty.PARAMETER, this.parameter, parameter); |
105 | if (this.parameter != null) |
106 | this.parameter.setParentNode(null); |
107 | this.parameter = parameter; |
108 | setAsParentNodeOf(parameter); |
109 | return this; |
110 | } |
111 | |
112 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
113 | public BlockStmt getBody() { |
114 | return body; |
115 | } |
116 | |
117 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
118 | public CatchClause setBody(final BlockStmt body) { |
119 | assertNotNull(body); |
120 | if (body == this.body) { |
121 | return (CatchClause) this; |
122 | } |
123 | notifyPropertyChange(ObservableProperty.BODY, this.body, body); |
124 | if (this.body != null) |
125 | this.body.setParentNode(null); |
126 | this.body = body; |
127 | setAsParentNodeOf(body); |
128 | return this; |
129 | } |
130 | |
131 | @Override |
132 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
133 | public boolean remove(Node node) { |
134 | if (node == null) |
135 | return false; |
136 | return super.remove(node); |
137 | } |
138 | |
139 | @Override |
140 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
141 | public CatchClause clone() { |
142 | return (CatchClause) accept(new CloneVisitor(), null); |
143 | } |
144 | |
145 | @Override |
146 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
147 | public CatchClauseMetaModel getMetaModel() { |
148 | return JavaParserMetaModel.catchClauseMetaModel; |
149 | } |
150 | |
151 | @Override |
152 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
153 | public boolean replace(Node node, Node replacementNode) { |
154 | if (node == null) |
155 | return false; |
156 | if (node == body) { |
157 | setBody((BlockStmt) replacementNode); |
158 | return true; |
159 | } |
160 | if (node == parameter) { |
161 | setParameter((Parameter) replacementNode); |
162 | return true; |
163 | } |
164 | return super.replace(node, replacementNode); |
165 | } |
166 | } |
167 |
Members