/** * Creates a matcher that always returns {@code true}. * * @param <T> The type of the matched object. * @return A matcher that matches anything. */ public static <T> ElementMatcher.Junction<T> any() { return new BooleanMatcher<T>(true); }
/** * Creates a matcher that always returns {@code false}. * * @param <T> The type of the matched object. * @return A matcher that matches nothing. */ public static <T> ElementMatcher.Junction<T> none() { return new BooleanMatcher<T>(false); }
/** * Matches any class loader that is either the given class loader or a child of the given class loader. * * @param classLoader The class loader of which child class loaders are matched. * @param <T> The type of the matched object. * @return A matcher that matches the given class loader and any class loader that is a child of the given * class loader. */ public static <T extends ClassLoader> ElementMatcher.Junction<T> isChildOf(ClassLoader classLoader) { return classLoader == BOOTSTRAP_CLASSLOADER ? new BooleanMatcher<T>(true) : ElementMatchers.<T>hasChild(is(classLoader)); }
/** * Matches a method that declares the given generic exception type as a (erased) exception type. * * @param exceptionType The exception type that is matched. * @param <T> The type of the matched object. * @return A matcher that matches any method that exactly matches the provided exception. */ public static <T extends MethodDescription> ElementMatcher.Junction<T> declaresException(TypeDescription exceptionType) { return exceptionType.isAssignableTo(Throwable.class) ? ElementMatchers.<T>declaresGenericException(new CollectionItemMatcher<TypeDescription.Generic>(erasure(exceptionType))) : new BooleanMatcher<T>(false); }
/** * Matches a {@link MethodDescription} by its capability to throw a given * checked exception. For specifying a non-checked exception, any method is matched. * * @param exceptionType The type of the exception that should be declared by the method to be matched. * @param <T> The type of the matched object. * @return A matcher that matches a method description by its declaration of throwing a checked exception. */ public static <T extends MethodDescription> ElementMatcher.Junction<T> canThrow(TypeDescription exceptionType) { return exceptionType.isAssignableTo(RuntimeException.class) || exceptionType.isAssignableTo(Error.class) ? new BooleanMatcher<T>(true) : ElementMatchers.<T>declaresGenericException(new CollectionItemMatcher<TypeDescription.Generic>(erasure(isSuperTypeOf(exceptionType)))); }
/** * Matches a method that declares the given generic exception type. For non-generic type, this matcher behaves identically to * {@link ElementMatchers#declaresException(TypeDescription)}. For exceptions that are expressed as type variables, only exceptions * that are represented as this type variable are matched. * * @param exceptionType The generic exception type that is matched exactly. * @param <T> The type of the matched object. * @return A matcher that matches any method that exactly matches the provided generic exception. */ public static <T extends MethodDescription> ElementMatcher.Junction<T> declaresGenericException(TypeDescription.Generic exceptionType) { return !exceptionType.getSort().isWildcard() && exceptionType.asErasure().isAssignableTo(Throwable.class) ? ElementMatchers.<T>declaresGenericException(new CollectionItemMatcher<TypeDescription.Generic>(is(exceptionType))) : new BooleanMatcher<T>(false); }