| 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 | |
| 22 | package com.github.javaparser.ast.nodeTypes; |
| 23 | |
| 24 | import com.github.javaparser.ast.Node; |
| 25 | import com.github.javaparser.ast.NodeList; |
| 26 | import com.github.javaparser.ast.type.ReferenceType; |
| 27 | |
| 28 | import static com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType; |
| 29 | |
| 30 | /** |
| 31 | * A node that declares the types of exception it throws. |
| 32 | */ |
| 33 | public interface NodeWithThrownExceptions<N extends Node> { |
| 34 | N setThrownExceptions(NodeList<ReferenceType> thrownExceptions); |
| 35 | |
| 36 | NodeList<ReferenceType> getThrownExceptions(); |
| 37 | |
| 38 | void tryAddImportToParentCompilationUnit(Class<?> clazz); |
| 39 | |
| 40 | default ReferenceType getThrownException(int i) { |
| 41 | return getThrownExceptions().get(i); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Adds this type to the throws clause |
| 46 | * |
| 47 | * @param throwType the exception type |
| 48 | * @return this |
| 49 | */ |
| 50 | @SuppressWarnings("unchecked") |
| 51 | default N addThrownException(ReferenceType throwType) { |
| 52 | getThrownExceptions().add(throwType); |
| 53 | return (N) this; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Adds this class to the throws clause |
| 58 | * |
| 59 | * @param clazz the exception class |
| 60 | * @return this |
| 61 | */ |
| 62 | default N addThrownException(Class<? extends Throwable> clazz) { |
| 63 | tryAddImportToParentCompilationUnit(clazz); |
| 64 | return addThrownException(parseClassOrInterfaceType(clazz.getSimpleName())); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Check whether this elements throws this exception class. |
| 69 | * Note that this is simply a text compare of the simple name of the class, |
| 70 | * no actual type resolution takes place. |
| 71 | * |
| 72 | * @param clazz the class of the exception |
| 73 | * @return true if found in throws clause, false if not |
| 74 | */ |
| 75 | default boolean isThrown(Class<? extends Throwable> clazz) { |
| 76 | return isThrown(clazz.getSimpleName()); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Check whether this elements throws this exception class |
| 81 | * Note that this is simply a text compare, |
| 82 | * no actual type resolution takes place. |
| 83 | * |
| 84 | * @param throwableName the class of the exception |
| 85 | * @return true if found in throws clause, false if not |
| 86 | */ |
| 87 | default boolean isThrown(String throwableName) { |
| 88 | return getThrownExceptions().stream().anyMatch(t -> t.toString().equals(throwableName)); |
| 89 | } |
| 90 | } |
| 91 |
Members