1/*
2 * Copyright (C) 2007-2010 Júlio Vilmar Gesser.
3 * Copyright (C) 2011, 2013-2016 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
22package com.github.javaparser.ast.stmt;
23
24import com.github.javaparser.Range;
25import com.github.javaparser.ast.Modifier;
26import com.github.javaparser.ast.Node;
27import com.github.javaparser.ast.body.Parameter;
28import com.github.javaparser.ast.body.VariableDeclaratorId;
29import com.github.javaparser.ast.expr.AnnotationExpr;
30import com.github.javaparser.ast.nodeTypes.NodeWithBlockStmt;
31import com.github.javaparser.ast.type.ReferenceType;
32import com.github.javaparser.ast.type.Type;
33import com.github.javaparser.ast.visitor.GenericVisitor;
34import com.github.javaparser.ast.visitor.VoidVisitor;
35
36import java.util.EnumSet;
37import java.util.List;
38
39/**
40 * @author Julio Vilmar Gesser
41 */
42public final class CatchClause extends Node implements NodeWithBlockStmt<CatchClause> {
43
44    private Parameter param;
45
46    private BlockStmt catchBlock;
47
48    public CatchClause() {
49    }
50
51    public CatchClause(final Parameter param, final BlockStmt catchBlock) {
52        setParam(param);
53        setBody(catchBlock);
54    }
55
56    public CatchClause(final Range range,
57                       final EnumSet<Modifier> exceptModifier,
58                       final List<AnnotationExpr> exceptAnnotations,
59                       final Type exceptType,
60                       final VariableDeclaratorId exceptId,
61                       final BlockStmt catchBlock) {
62        super(range);
63        setParam(new Parameter(range, exceptModifier, exceptAnnotations, exceptType, null, false, exceptId));
64        setBody(catchBlock);
65    }
66
67	@Override public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) {
68		return v.visit(this, arg);
69	}
70
71	@Override public <A> void accept(final VoidVisitor<A> v, final A arg) {
72		v.visit(this, arg);
73	}
74
75    /**
76     * Use {@link #getBody()} instead
77     */
78    @Deprecated
79	public BlockStmt getCatchBlock() {
80		return catchBlock;
81	}
82
83	/**
84	 * Note that the type of the Parameter can be a UnionType. In this case, any annotations found at the start of the catch(@X A a |...)
85	 * are found directly in the Parameter. Annotations that are on the second or later type - catch(A a | @X B b ...) are found on those types.
86	 */
87	public Parameter getParam() {
88		return param;
89	}
90
91    /**
92     * Use {@link #setBody(BlockStmt)} instead
93     *
94     * @param catchBlock
95     */
96    @Deprecated
97	public CatchClause setCatchBlock(final BlockStmt catchBlock) {
98		this.catchBlock = catchBlock;
99		setAsParentNodeOf(this.catchBlock);
100        return this;
101	}
102
103	public CatchClause setParam(final Parameter param) {
104		this.param = param;
105		setAsParentNodeOf(this.param);
106        return this;
107	}
108
109    @Override
110    public BlockStmt getBody() {
111        return catchBlock;
112    }
113
114    @Override
115    public CatchClause setBody(BlockStmt block) {
116        this.catchBlock = block;
117        setAsParentNodeOf(this.catchBlock);
118        return this;
119    }
120}
121