JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/stmt/CatchClause.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.stmt;
22
23import com.github.javaparser.TokenRange;
24import com.github.javaparser.ast.*;
25import com.github.javaparser.ast.body.Parameter;
26import com.github.javaparser.ast.expr.AnnotationExpr;
27import com.github.javaparser.ast.expr.SimpleName;
28import com.github.javaparser.ast.nodeTypes.NodeWithBlockStmt;
29import com.github.javaparser.ast.observer.ObservableProperty;
30import com.github.javaparser.ast.type.ClassOrInterfaceType;
31import com.github.javaparser.ast.visitor.CloneVisitor;
32import com.github.javaparser.ast.visitor.GenericVisitor;
33import com.github.javaparser.ast.visitor.VoidVisitor;
34import com.github.javaparser.metamodel.CatchClauseMetaModel;
35import com.github.javaparser.metamodel.JavaParserMetaModel;
36import static com.github.javaparser.utils.Utils.assertNotNull;
37import com.github.javaparser.ast.Node;
38import 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 */
46public 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<ModifierexceptModifierfinal NodeList<AnnotationExprexceptAnnotationsfinal ClassOrInterfaceType exceptTypefinal SimpleName exceptNamefinal BlockStmt body) {
57        this(null, new Parameter(nullexceptModifierexceptAnnotationsexceptTypefalse, new NodeList<>(), exceptName), body);
58    }
59
60    @AllFieldsConstructor
61    public CatchClause(final Parameter parameterfinal BlockStmt body) {
62        this(nullparameterbody);
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 tokenRangeParameter parameterBlockStmt 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 <RAR accept(final GenericVisitor<RAvfinal A arg) {
79        return v.visit(this, arg);
80    }
81
82    @Override
83    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
84    public <Avoid accept(final VoidVisitor<Avfinal 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.parameterparameter);
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.bodybody);
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 (CatchClauseaccept(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 nodeNode replacementNode) {
154        if (node == null)
155            return false;
156        if (node == body) {
157            setBody((BlockStmtreplacementNode);
158            return true;
159        }
160        if (node == parameter) {
161            setParameter((ParameterreplacementNode);
162            return true;
163        }
164        return super.replace(nodereplacementNode);
165    }
166}
167
MembersX
CatchClause:remove
CatchClause:accept
CatchClause:getBody
CatchClause:parameter
CatchClause:clone
CatchClause:body
CatchClause:CatchClause
CatchClause:getMetaModel
CatchClause:setParameter
CatchClause:replace
CatchClause:getParameter
CatchClause:setBody
Members
X