1 package com.github.javaparser.ast.nodeTypes; 2 3 import java.util.List; 4 5 import com.github.javaparser.ast.Node; 6 import com.github.javaparser.ast.type.ClassOrInterfaceType; 7 import com.github.javaparser.ast.type.ReferenceType; 8 9 public interface NodeWithThrowable<T> { 10 T setThrows(List<ReferenceType> throws_); 11 12 List<ReferenceType> getThrows(); 13 14 /** 15 * Adds this type to the throws clause 16 * 17 * @param throwType the exception type 18 * @return this 19 */ 20 @SuppressWarnings("unchecked") 21 default T addThrows(ReferenceType throwType) { 22 getThrows().add(throwType); 23 throwType.setParentNode((Node) this); 24 return (T) this; 25 } 26 27 /** 28 * Adds this class to the throws clause 29 * 30 * @param clazz the exception class 31 * @return this 32 */ 33 default T addThrows(Class<? extends Throwable> clazz) { 34 ((Node) this).tryAddImportToParentCompilationUnit(clazz); 35 return addThrows(new ClassOrInterfaceType(clazz.getSimpleName())); 36 } 37 38 /** 39 * Check whether this elements throws this exception class 40 * 41 * @param clazz the class of the exception 42 * @return true if found in throws clause, false if not 43 */ 44 public default boolean isThrows(Class<? extends Throwable> clazz) { 45 return isThrows(clazz.getSimpleName()); 46 } 47 48 /** 49 * Check whether this elements throws this exception class 50 * 51 * @param throwableName the class of the exception 52 * @return true if found in throws clause, false if not 53 */ 54 public default boolean isThrows(String throwableName) { 55 return getThrows().stream().anyMatch(t -> t.toString().equals(throwableName)); 56 } 57 } 58