/** * Matches an iterable by assuring that at least one element of the iterable collection matches the * provided matcher. * * @param matcher The matcher to apply to each element. * @param <T> The type of the matched object. * @return A matcher that matches an iterable if at least one element matches the provided matcher. */ public static <T> ElementMatcher.Junction<Iterable<? extends T>> whereAny(ElementMatcher<? super T> matcher) { return new CollectionItemMatcher<T>(matcher); }
/** * Matches a type by a another matcher that is applied on any of its declared methods. * * @param matcher The matcher that is applied onto each declared method. * @param <T> The type of the matched object. * @return A matcher that matches any type where another matcher is matched positively on at least on declared methods. */ public static <T extends TypeDefinition> ElementMatcher.Junction<T> declaresMethod(ElementMatcher<? super MethodDescription> matcher) { return new DeclaringMethodMatcher<T>(new CollectionItemMatcher<MethodDescription>(matcher)); }
/** * Matches a list of annotations by a given matcher on a type that declared these annotations or inherited them * from its super classes. * * @param matcher A matcher to apply onto a list of inherited annotations. * @param <T> The type of the matched object. * @return A matcher that matches a list of inherited annotation by a given matcher. */ public static <T extends TypeDescription> ElementMatcher.Junction<T> hasAnnotation(ElementMatcher<? super AnnotationDescription> matcher) { return new InheritedAnnotationMatcher<T>(new CollectionItemMatcher<AnnotationDescription>(matcher)); }
/** * Matches a type by a another matcher that is applied on any of its declared fields. * * @param matcher The matcher that is applied onto each declared field. * @param <T> The type of the matched object. * @return A matcher that matches any type where another matcher is matched positively on at least on declared field. */ public static <T extends TypeDefinition> ElementMatcher.Junction<T> declaresField(ElementMatcher<? super FieldDescription> matcher) { return new DeclaringFieldMatcher<T>(new CollectionItemMatcher<FieldDescription>(matcher)); }
/** * Matches an {@link AnnotationSource} to declare any annotation * that matches the given matcher. Note that this matcher does not match inherited annotations that only exist * for types. Use {@link net.bytebuddy.matcher.ElementMatchers#inheritsAnnotation(net.bytebuddy.matcher.ElementMatcher)} * for matching inherited annotations. * * @param matcher A matcher to apply on any declared annotation of the matched annotated element. * @param <T> The type of the matched object. * @return A matcher that validates that an annotated element is annotated with an annotation that matches * the given {@code matcher}. */ public static <T extends AnnotationSource> ElementMatcher.Junction<T> declaresAnnotation(ElementMatcher<? super AnnotationDescription> matcher) { return new DeclaringAnnotationMatcher<T>(new CollectionItemMatcher<AnnotationDescription>(matcher)); }
/** * 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); }