net.bytebuddy.matcher.ElementMatchers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of byte-buddy Show documentation
Show all versions of byte-buddy Show documentation
Byte Buddy is a Java library for creating Java classes at run time.
This artifact is a build of Byte Buddy with all ASM dependencies repackaged into its own name space.
package net.bytebuddy.matcher;
import net.bytebuddy.description.ByteCodeElement;
import net.bytebuddy.description.ModifierReviewable;
import net.bytebuddy.description.NamedElement;
import net.bytebuddy.description.annotation.AnnotatedCodeElement;
import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.annotation.AnnotationList;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.field.FieldList;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.method.MethodList;
import net.bytebuddy.description.method.ParameterDescription;
import net.bytebuddy.description.method.ParameterList;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.TypeList;
import net.bytebuddy.description.type.generic.GenericTypeDescription;
import net.bytebuddy.description.type.generic.GenericTypeList;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import static net.bytebuddy.utility.ByteBuddyCommons.nonNull;
/**
* A utility class that contains a human-readable language for creating {@link net.bytebuddy.matcher.ElementMatcher}s.
*/
public final class ElementMatchers {
/**
* A readable reference to the bootstrap class loader which is represented by {@code null}.
*/
private static final ClassLoader BOOTSTRAP_CLASSLOADER = null;
/**
* A private constructor that must not be invoked.
*/
private ElementMatchers() {
throw new UnsupportedOperationException();
}
/**
* Matches the given value which can also be {@code null} by the {@link java.lang.Object#equals(Object)} method or
* by a null-check.
*
* @param value The value that is to be matched.
* @param The type of the matched object.
* @return A matcher that matches an exact value.
*/
public static ElementMatcher.Junction is(Object value) {
return value == null
? new NullMatcher()
: new EqualityMatcher(value);
}
/**
* Exactly matches a given field as a {@link FieldDescription}.
*
* @param field The field to match by its description
* @param The type of the matched object.
* @return An element matcher that exactly matches the given field.
*/
public static ElementMatcher.Junction is(Field field) {
return definedField(is(new FieldDescription.ForLoadedField(nonNull(field))));
}
/**
* Matches a field in its defined shape.
*
* @param matcher The matcher to apply to the matched field's defined shape.
* @param The matched object's type.
* @return A matcher that matches a matched field's defined shape.
*/
public static ElementMatcher.Junction definedField(ElementMatcher super FieldDescription.InDefinedShape> matcher) {
return new DefinedShapeMatcher(nonNull(matcher));
}
/**
* Validates if a method is represented by the provided field token.
*
* @param fieldToken The field token to match a method against.
* @param The type of the matched object.
* @return A matcher that matches any field that is represented by the provided field description.
*/
public static ElementMatcher.Junction representedBy(FieldDescription.Token fieldToken) {
return fieldRepresentedBy(is(nonNull(fieldToken)));
}
/**
* Matches a field by a token matcher.
*
* @param matcher The matcher to apply to the field's token.
* @param The matched object's type.
* @return A matcher that applies the given matcher to the matched field's token.
*/
public static ElementMatcher.Junction fieldRepresentedBy(ElementMatcher super FieldDescription.Token> matcher) {
return new TokenMatcher(nonNull(matcher));
}
/**
* Exactly matches a given method as a {@link MethodDescription}.
*
* @param method The method to match by its description
* @param The type of the matched object.
* @return An element matcher that exactly matches the given method.
*/
public static ElementMatcher.Junction is(Method method) {
return definedMethod(is(new MethodDescription.ForLoadedMethod(nonNull(method))));
}
/**
* Exactly matches a given constructor as a {@link MethodDescription}.
*
* @param constructor The constructor to match by its description
* @param The type of the matched object.
* @return An element matcher that exactly matches the given constructor.
*/
public static ElementMatcher.Junction is(Constructor> constructor) {
return definedMethod(is(new MethodDescription.ForLoadedConstructor(nonNull(constructor))));
}
/**
* Matches a method in its defined shape.
*
* @param matcher The matcher to apply to the matched method's defined shape.
* @param The matched object's type.
* @return A matcher that matches a matched method's defined shape.
*/
public static ElementMatcher.Junction definedMethod(ElementMatcher super MethodDescription.InDefinedShape> matcher) {
return new DefinedShapeMatcher(nonNull(matcher));
}
/**
* Matches a method by a token matcher.
*
* @param matcher The matcher to apply to the method's token.
* @param The matched object's type.
* @return A matcher that applies the given matcher to the matched method's token.
*/
public static ElementMatcher.Junction methodRepresentedBy(ElementMatcher super MethodDescription.Token> matcher) {
return new TokenMatcher(nonNull(matcher));
}
/**
* Validates if a method is represented by the provided method token.
*
* @param methodToken The method token to match a method against.
* @param The type of the matched object.
* @return A matcher that matches any method that is represented by the provided method description.
*/
public static ElementMatcher.Junction representedBy(MethodDescription.Token methodToken) {
return methodRepresentedBy(is(nonNull(methodToken)));
}
/**
* Matches a parameter in its defined shape.
*
* @param matcher The matcher to apply to the matched parameter's defined shape.
* @param The matched object's type.
* @return A matcher that matches a matched parameter's defined shape.
*/
public static ElementMatcher.Junction definedParameter(
ElementMatcher super ParameterDescription.InDefinedShape> matcher) {
return new DefinedShapeMatcher(nonNull(matcher));
}
/**
* Matches a parameter by a token matcher.
*
* @param matcher The matcher to apply to the parameter's token.
* @param The matched object's type.
* @return A matcher that applies the given matcher to the matched parameter's token.
*/
public static ElementMatcher.Junction parameterRepresentedBy(
ElementMatcher super ParameterDescription.Token> matcher) {
return new TokenMatcher(nonNull(matcher));
}
/**
* Validates if a method is represented by the provided method token.
*
* @param parameterToken The parameter token to match a method against.
* @param The type of the matched object.
* @return A matcher that matches any parameter that is represented by the provided parameter description.
*/
public static ElementMatcher.Junction representedBy(ParameterDescription.Token parameterToken) {
return parameterRepresentedBy(is(nonNull(parameterToken)));
}
/**
* Matches a parameter's type by the given matcher.
*
* @param matcher The matcher to apply to the parameter's type.
* @param The type of the matched object.
* @return A matcher that matches a parameter's type by the given matcher.
*/
public static ElementMatcher.Junction hasType(ElementMatcher super TypeDescription> matcher) {
return hasGenericType(rawType(nonNull(matcher)));
}
/**
* Matches a method parameter by its generic type.
*
* @param matcher The matcher to apply to a parameter's generic type.
* @param The type of the matched object.
* @return A matcher that matches the matched parameter's generic type.
*/
public static ElementMatcher.Junction hasGenericType(ElementMatcher super GenericTypeDescription> matcher) {
return new MethodParameterTypeMatcher(nonNull(matcher));
}
/**
* Exactly matches a given type as a {@link TypeDescription}.
*
* @param type The type to match by its description
* @param The type of the matched object.
* @return An element matcher that exactly matches the given type.
*/
public static ElementMatcher.Junction is(Type type) {
return is(GenericTypeDescription.Sort.describe(nonNull(type)));
}
/**
* Exactly matches a given annotation as an {@link AnnotationDescription}.
*
* @param annotation The annotation to match by its description.
* @param The type of the matched object.
* @return An element matcher that exactly matches the given annotation.
*/
public static ElementMatcher.Junction is(Annotation annotation) {
return is(AnnotationDescription.ForLoadedAnnotation.of(nonNull(annotation)));
}
/**
* Inverts another matcher.
*
* @param matcher The matcher to invert.
* @param The type of the matched object.
* @return An inverted version of the given {@code matcher}.
*/
public static ElementMatcher.Junction not(ElementMatcher super T> matcher) {
return new NegatingMatcher(nonNull(matcher));
}
/**
* Creates a matcher that always returns {@code true}.
*
* @param The type of the matched object.
* @return A matcher that matches anything.
*/
public static ElementMatcher.Junction any() {
return new BooleanMatcher(true);
}
/**
* Creates a matcher that always returns {@code false}.
*
* @param The type of the matched object.
* @return A matcher that matches nothing.
*/
public static ElementMatcher.Junction none() {
return new BooleanMatcher(false);
}
/**
* Creates a matcher that matches any of the given objects by the {@link java.lang.Object#equals(Object)} method.
* None of the values must be {@code null}.
*
* @param value The input values to be compared against.
* @param The type of the matched object.
* @return A matcher that checks for the equality with any of the given objects.
*/
public static ElementMatcher.Junction anyOf(Object... value) {
return anyOf(Arrays.asList(nonNull(value)));
}
/**
* Creates a matcher that matches any of the given objects by the {@link java.lang.Object#equals(Object)} method.
* None of the values must be {@code null}.
*
* @param values The input values to be compared against.
* @param The type of the matched object.
* @return A matcher that checks for the equality with any of the given objects.
*/
public static ElementMatcher.Junction anyOf(Iterable> values) {
ElementMatcher.Junction matcher = none();
for (Object value : values) {
matcher = matcher.or(is(value));
}
return matcher;
}
/**
* Creates a matcher that matches any of the given types as {@link TypeDescription}s
* by the {@link java.lang.Object#equals(Object)} method. None of the values must be {@code null}.
*
* @param value The input values to be compared against.
* @param The type of the matched object.
* @return A matcher that checks for the equality with any of the given objects.
*/
public static ElementMatcher.Junction anyOf(Type... value) {
return anyOf(new GenericTypeList.ForLoadedType(nonNull(value)));
}
/**
* Creates a matcher that matches any of the given constructors as {@link MethodDescription}s
* by the {@link java.lang.Object#equals(Object)} method. None of the values must be {@code null}.
*
* @param value The input values to be compared against.
* @param The type of the matched object.
* @return A matcher that checks for the equality with any of the given objects.
*/
public static ElementMatcher.Junction anyOf(Constructor>... value) {
return definedMethod(anyOf(new MethodList.ForLoadedType(nonNull(value), new Method[0])));
}
/**
* Creates a matcher that matches any of the given methods as {@link MethodDescription}s
* by the {@link java.lang.Object#equals(Object)} method. None of the values must be {@code null}.
*
* @param value The input values to be compared against.
* @param The type of the matched object.
* @return A matcher that checks for the equality with any of the given objects.
*/
public static ElementMatcher.Junction anyOf(Method... value) {
return definedMethod(anyOf(new MethodList.ForLoadedType(new Constructor>[0], nonNull(value))));
}
/**
* Creates a matcher that matches any of the given fields as {@link FieldDescription}s
* by the {@link java.lang.Object#equals(Object)} method. None of the values must be {@code null}.
*
* @param value The input values to be compared against.
* @param The type of the matched object.
* @return A matcher that checks for the equality with any of the given objects.
*/
public static ElementMatcher.Junction anyOf(Field... value) {
return definedField(anyOf(new FieldList.ForLoadedField(nonNull(value))));
}
/**
* Creates a matcher that matches any of the given annotations as {@link AnnotationDescription}s
* by the {@link java.lang.Object#equals(Object)} method. None of the values must be {@code null}.
*
* @param value The input values to be compared against.
* @param The type of the matched object.
* @return A matcher that checks for the equality with any of the given objects.
*/
public static ElementMatcher.Junction anyOf(Annotation... value) {
return anyOf(new AnnotationList.ForLoadedAnnotation(nonNull(value)));
}
/**
* Creates a matcher that matches none of the given objects by the {@link java.lang.Object#equals(Object)} method.
* None of the values must be {@code null}.
*
* @param value The input values to be compared against.
* @param The type of the matched object.
* @return A matcher that checks for the equality with none of the given objects.
*/
public static ElementMatcher.Junction noneOf(Object... value) {
return noneOf(Arrays.asList(value));
}
/**
* Creates a matcher that matches none of the given objects by the {@link java.lang.Object#equals(Object)} method.
* None of the values must be {@code null}.
*
* @param values The input values to be compared against.
* @param The type of the matched object.
* @return A matcher that checks for the equality with none of the given objects.
*/
public static ElementMatcher.Junction noneOf(Iterable> values) {
ElementMatcher.Junction matcher = any();
for (Object value : values) {
matcher = matcher.and(not(is(value)));
}
return matcher;
}
/**
* Creates a matcher that matches none of the given types as {@link TypeDescription}s
* by the {@link java.lang.Object#equals(Object)} method. None of the values must be {@code null}.
*
* @param value The input values to be compared against.
* @param The type of the matched object.
* @return A matcher that checks for the equality with none of the given objects.
*/
public static ElementMatcher.Junction noneOf(Type... value) {
return noneOf(new GenericTypeList.ForLoadedType(nonNull(value)));
}
/**
* Creates a matcher that matches none of the given constructors as {@link MethodDescription}s
* by the {@link java.lang.Object#equals(Object)} method. None of the values must be {@code null}.
*
* @param value The input values to be compared against.
* @param The type of the matched object.
* @return A matcher that checks for the equality with none of the given objects.
*/
public static ElementMatcher.Junction noneOf(Constructor>... value) {
return definedMethod(noneOf(new MethodList.ForLoadedType(nonNull(value), new Method[0])));
}
/**
* Creates a matcher that matches none of the given methods as {@link MethodDescription}s
* by the {@link java.lang.Object#equals(Object)} method. None of the values must be {@code null}.
*
* @param value The input values to be compared against.
* @param The type of the matched object.
* @return A matcher that checks for the equality with none of the given objects.
*/
public static ElementMatcher.Junction noneOf(Method... value) {
return definedMethod(noneOf(new MethodList.ForLoadedType(new Constructor>[0], nonNull(value))));
}
/**
* Creates a matcher that matches none of the given methods as {@link FieldDescription}s
* by the {@link java.lang.Object#equals(Object)} method. None of the values must be {@code null}.
*
* @param value The input values to be compared against.
* @param The type of the matched object.
* @return A matcher that checks for the equality with none of the given objects.
*/
public static ElementMatcher.Junction noneOf(Field... value) {
return definedField(noneOf(new FieldList.ForLoadedField(nonNull(value))));
}
/**
* Creates a matcher that matches none of the given annotations as {@link AnnotationDescription}s
* by the {@link java.lang.Object#equals(Object)} method. None of the values must be {@code null}.
*
* @param value The input values to be compared against.
* @param The type of the matched object.
* @return A matcher that checks for the equality with any of the given objects.
*/
public static ElementMatcher.Junction noneOf(Annotation... value) {
return noneOf(new AnnotationList.ForLoadedAnnotation(nonNull(value)));
}
/**
* 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 The type of the matched object.
* @return A matcher that matches an iterable if at least one element matches the provided matcher.
*/
public static ElementMatcher.Junction> whereAny(ElementMatcher super T> matcher) {
return new CollectionItemMatcher(matcher);
}
/**
* Matches an iterable by assuring that no element of the iterable collection matches the provided matcher.
*
* @param matcher The matcher to apply to each element.
* @param The type of the matched object.
* @return A matcher that matches an iterable if no element matches the provided matcher.
*/
public static ElementMatcher.Junction> whereNone(ElementMatcher super T> matcher) {
return not(whereAny(matcher));
}
/**
* Matches a generic type's raw type against the provided raw type.
*
* @param type The type to match a generic type's erasure against.
* @param The type of the matched object.
* @return A matcher that matches a generic type's raw type against the provided non-generic type.
*/
public static ElementMatcher.Junction rawType(Class> type) {
return rawType(is(nonNull(type)));
}
/**
* Matches a generic type's raw type against the provided raw type.
*
* @param typeDescription The type to match a generic type's erasure against.
* @param The type of the matched object.
* @return A matcher that matches a generic type's raw type against the provided non-generic type.
*/
public static ElementMatcher.Junction rawType(TypeDescription typeDescription) {
return rawType(is(nonNull(typeDescription)));
}
/**
* Converts a matcher for a type description into a matcher for a raw type of the matched generic type against the given matcher. A wildcard
* type which does not define a raw type results in a negative match.
*
* @param matcher The matcher to match the matched object's raw type against.
* @param The type of the matched object.
* @return A type matcher for a generic type that matches the matched type's raw type against the given type description matcher.
*/
public static ElementMatcher.Junction rawType(ElementMatcher super TypeDescription> matcher) {
return new RawTypeMatcher(nonNull(matcher));
}
/**
* Matches an iteration of generic types' erasures against the provided raw types.
*
* @param type The types to match.
* @param The type of the matched object.
* @return A matcher that matches an iteration of generic types' raw types against the provided non-generic types.
*/
public static > ElementMatcher.Junction rawTypes(Class>... type) {
return rawTypes(new TypeList.ForLoadedType(type));
}
/**
* Matches an iteration of generic types' erasures against the provided raw types.
*
* @param typeDescription The types to match.
* @param The type of the matched object.
* @return A matcher that matches an iteration of generic types' raw types against the provided non-generic types.
*/
public static > ElementMatcher.Junction rawTypes(TypeDescription... typeDescription) {
return rawTypes(Arrays.asList(typeDescription));
}
/**
* Matches an iteration of generic types' erasures against the provided raw types.
*
* @param typeDescriptions The types to match.
* @param The type of the matched object.
* @return A matcher that matches an iteration of generic types' raw types against the provided non-generic types.
*/
public static > ElementMatcher.Junction rawTypes(
Iterable extends TypeDescription> typeDescriptions) {
List> typeMatchers = new LinkedList>();
for (GenericTypeDescription typeDescription : typeDescriptions) {
typeMatchers.add(is(nonNull(typeDescription)));
}
return rawTypes(new CollectionOneToOneMatcher(typeMatchers));
}
/**
* Applies the provided matcher to an iteration og generic types' generic types.
*
* @param matcher The matcher to apply at the erased types.
* @param The type of the matched object.
* @return A matcher that matches an iteration of generic types' raw types against the provided matcher.
*/
public static > ElementMatcher.Junction rawTypes(
ElementMatcher super Iterable extends TypeDescription>> matcher) {
return new CollectionRawTypeMatcher(matcher);
}
/**
* Matches a type variable with the given name.
*
* @param symbol The name of the type variable to be match.
* @param The type of the matched object.
* @return A matcher that matches type variables with the given name.
*/
public static ElementMatcher isVariable(String symbol) {
return isVariable(named(nonNull(symbol)));
}
/**
* Matches a type variable with the given name.
*
* @param matcher A matcher for the type variable's name.
* @param The type of the matched object.
* @return A matcher that matches type variables with the given name.
*/
public static ElementMatcher isVariable(ElementMatcher super NamedElement> matcher) {
return new TypeSortMatcher(anyOf(GenericTypeDescription.Sort.VARIABLE,
GenericTypeDescription.Sort.VARIABLE_DETACHED,
GenericTypeDescription.Sort.VARIABLE_SYMBOLIC)).and(matcher);
}
/**
* Matches a {@link NamedElement} for its exact name.
*
* @param name The expected name.
* @param The type of the matched object.
* @return An element matcher for a named element's exact name.
*/
public static ElementMatcher.Junction named(String name) {
return new NameMatcher(new StringMatcher(nonNull(name), StringMatcher.Mode.EQUALS_FULLY));
}
/**
* Matches a {@link NamedElement} for its name. The name's
* capitalization is ignored.
*
* @param name The expected name.
* @param The type of the matched object.
* @return An element matcher for a named element's name.
*/
public static ElementMatcher.Junction namedIgnoreCase(String name) {
return new NameMatcher(new StringMatcher(nonNull(name), StringMatcher.Mode.EQUALS_FULLY_IGNORE_CASE));
}
/**
* Matches a {@link NamedElement} for its name's prefix.
*
* @param prefix The expected name's prefix.
* @param The type of the matched object.
* @return An element matcher for a named element's name's prefix.
*/
public static ElementMatcher.Junction nameStartsWith(String prefix) {
return new NameMatcher(new StringMatcher(nonNull(prefix), StringMatcher.Mode.STARTS_WITH));
}
/**
* Matches a {@link NamedElement} for its name's prefix. The name's
* capitalization is ignored.
*
* @param prefix The expected name's prefix.
* @param The type of the matched object.
* @return An element matcher for a named element's name's prefix.
*/
public static ElementMatcher.Junction nameStartsWithIgnoreCase(String prefix) {
return new NameMatcher(new StringMatcher(nonNull(prefix), StringMatcher.Mode.STARTS_WITH_IGNORE_CASE));
}
/**
* Matches a {@link NamedElement} for its name's suffix.
*
* @param suffix The expected name's suffix.
* @param The type of the matched object.
* @return An element matcher for a named element's name's suffix.
*/
public static ElementMatcher.Junction nameEndsWith(String suffix) {
return new NameMatcher(new StringMatcher(nonNull(suffix), StringMatcher.Mode.ENDS_WITH));
}
/**
* Matches a {@link NamedElement} for its name's suffix. The name's
* capitalization is ignored.
*
* @param suffix The expected name's suffix.
* @param The type of the matched object.
* @return An element matcher for a named element's name's suffix.
*/
public static ElementMatcher.Junction nameEndsWithIgnoreCase(String suffix) {
return new NameMatcher(new StringMatcher(nonNull(suffix), StringMatcher.Mode.ENDS_WITH_IGNORE_CASE));
}
/**
* Matches a {@link NamedElement} for an infix of its name.
*
* @param infix The expected infix of the name.
* @param The type of the matched object.
* @return An element matcher for a named element's name's infix.
*/
public static ElementMatcher.Junction nameContains(String infix) {
return new NameMatcher(new StringMatcher(nonNull(infix), StringMatcher.Mode.CONTAINS));
}
/**
* Matches a {@link NamedElement} for an infix of its name. The name's
* capitalization is ignored.
*
* @param infix The expected infix of the name.
* @param The type of the matched object.
* @return An element matcher for a named element's name's infix.
*/
public static ElementMatcher.Junction nameContainsIgnoreCase(String infix) {
return new NameMatcher(new StringMatcher(nonNull(infix), StringMatcher.Mode.CONTAINS_IGNORE_CASE));
}
/**
* Matches a {@link NamedElement} name against a regular expression.
*
* @param regex The regular expression to match the name against.
* @param The type of the matched object.
* @return An element matcher for a named element's name's against the given regular expression.
*/
public static ElementMatcher.Junction nameMatches(String regex) {
return new NameMatcher(new StringMatcher(nonNull(regex), StringMatcher.Mode.MATCHES));
}
/**
* Matches a {@link ByteCodeElement}'s descriptor against a given value.
*
* @param descriptor The expected descriptor.
* @param The type of the matched object.
* @return A matcher for the given {@code descriptor}.
*/
public static ElementMatcher.Junction hasDescriptor(String descriptor) {
return new DescriptorMatcher(new StringMatcher(nonNull(descriptor), StringMatcher.Mode.EQUALS_FULLY));
}
/**
* Matches a {@link ByteCodeElement} for being declared by a given {@link java.lang.Class}. This matcher matches
* a declared element's raw declaring type.
*
* @param type The type that is expected to declare the matched byte code element.
* @param The type of the matched object.
* @return A matcher for byte code elements being declared by the given {@code type}.
*/
public static ElementMatcher.Junction isDeclaredBy(Class> type) {
return isDeclaredBy(new TypeDescription.ForLoadedType(nonNull(type)));
}
/**
* Matches a {@link ByteCodeElement} for being declared by a given {@link TypeDescription}. This matcher matches
* a declared element's raw declaring type.
*
* @param type The type that is expected to declare the matched byte code element.
* @param The type of the matched object.
* @return A matcher for byte code elements being declared by the given {@code type}.
*/
public static ElementMatcher.Junction isDeclaredBy(TypeDescription type) {
return isDeclaredBy(is(nonNull(type)));
}
/**
* Matches a {@link ByteCodeElement} for being declared by a {@link TypeDescription} that is matched by the given matcher. This matcher matches
* a declared element's raw declaring type.
*
* @param matcher A matcher for the declaring type of the matched byte code element as long as it
* is not {@code null}.
* @param The type of the matched object.
* @return A matcher for byte code elements being declared by a type matched by the given {@code matcher}.
*/
public static ElementMatcher.Junction isDeclaredBy(ElementMatcher super TypeDescription> matcher) {
return isDeclaredByGeneric(rawType(nonNull(matcher)));
}
/**
* Matches a {@link ByteCodeElement} for being declared by a given generic {@link Type}.
*
* @param type The type that is expected to declare the matched byte code element.
* @param The type of the matched object.
* @return A matcher for byte code elements being declared by the given {@code type}.
*/
public static ElementMatcher.Junction isDeclaredByGeneric(Type type) {
return isDeclaredByGeneric(GenericTypeDescription.Sort.describe(nonNull(type)));
}
/**
* Matches a {@link ByteCodeElement} for being declared by a given {@link GenericTypeDescription}.
*
* @param type The type that is expected to declare the matched byte code element.
* @param The type of the matched object.
* @return A matcher for byte code elements being declared by the given {@code type}.
*/
public static ElementMatcher.Junction isDeclaredByGeneric(GenericTypeDescription type) {
return isDeclaredByGeneric(is(nonNull(type)));
}
/**
* Matches a {@link ByteCodeElement} for being declared by a {@link GenericTypeDescription} that is matched by the given matcher.
*
* @param matcher A matcher for the declaring type of the matched byte code element as long as it is not {@code null}.
* @param The type of the matched object.
* @return A matcher for byte code elements being declared by a type matched by the given {@code matcher}.
*/
public static ElementMatcher.Junction isDeclaredByGeneric(ElementMatcher super GenericTypeDescription> matcher) {
return new DeclaringTypeMatcher(nonNull(matcher));
}
/**
* Matches a {@link ByteCodeElement} that is visible to a given {@link java.lang.Class}.
*
* @param type The type that a matched byte code element is expected to be visible to.
* @param The type of the matched object.
* @return A matcher for a byte code element to be visible to a given {@code type}.
*/
public static ElementMatcher.Junction isVisibleTo(Class> type) {
return isVisibleTo(new TypeDescription.ForLoadedType(nonNull(type)));
}
/**
* Matches a {@link ByteCodeElement} that is visible to a given
* {@link TypeDescription}.
*
* @param typeDescription The type that a matched byte code element is expected to be visible to.
* @param The type of the matched object.
* @return A matcher for a byte code element to be visible to a given {@code typeDescription}.
*/
public static ElementMatcher.Junction isVisibleTo(TypeDescription typeDescription) {
return new VisibilityMatcher(nonNull(typeDescription));
}
/**
* Matches a {@link ModifierReviewable} that is {@code abstract}.
*
* @param The type of the matched object.
* @return A matcher for a {@code abstract} modifier reviewable.
*/
public static ElementMatcher.Junction isAbstract() {
return new ModifierMatcher(ModifierMatcher.Mode.ABSTRACT);
}
/**
* Matches an {@link net.bytebuddy.description.annotation.AnnotatedCodeElement} for declared annotations.
* This matcher does not match inherited annotations which only exist for classes. Use
* {@link net.bytebuddy.matcher.ElementMatchers#inheritsAnnotation(Class)} for matching inherited annotations.
*
* @param type The annotation type to match against.
* @param The type of the matched object.
* @return A matcher that validates that an annotated element is annotated with an annotation of {@code type}.
*/
public static ElementMatcher.Junction isAnnotatedWith(Class extends Annotation> type) {
return isAnnotatedWith(new TypeDescription.ForLoadedType(nonNull(type)));
}
/**
* Matches an {@link net.bytebuddy.description.annotation.AnnotatedCodeElement} for declared annotations.
* This matcher does not match inherited annotations which only exist for classes. Use
* {@link net.bytebuddy.matcher.ElementMatchers#inheritsAnnotation(TypeDescription)}
* for matching inherited annotations.
*
* @param typeDescription The annotation type to match against.
* @param The type of the matched object.
* @return A matcher that validates that an annotated element is annotated with an annotation of {@code typeDescription}.
*/
public static ElementMatcher.Junction isAnnotatedWith(TypeDescription typeDescription) {
return isAnnotatedWith(is(typeDescription));
}
/**
* Matches an {@link net.bytebuddy.description.annotation.AnnotatedCodeElement} for declared annotations.
* This matcher does not match inherited annotations which only exist for classes. Use
* {@link net.bytebuddy.matcher.ElementMatchers#inheritsAnnotation(net.bytebuddy.matcher.ElementMatcher)}
* for matching inherited annotations.
*
* @param matcher The matcher to apply to any annotation's type found on the matched annotated element.
* @param The type of the matched object.
* @return A matcher that validates that an annotated element is annotated with an annotation of a type
* that matches the given {@code matcher}.
*/
public static ElementMatcher.Junction isAnnotatedWith(ElementMatcher super TypeDescription> matcher) {
return declaresAnnotation(new AnnotationTypeMatcher(nonNull(matcher)));
}
/**
* Matches an {@link net.bytebuddy.description.annotation.AnnotatedCodeElement} 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 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 ElementMatcher.Junction declaresAnnotation(ElementMatcher super AnnotationDescription> matcher) {
return new DeclaringAnnotationMatcher(new CollectionItemMatcher(nonNull(matcher)));
}
/**
* Matches a {@link ModifierReviewable} that is {@code public}.
*
* @param The type of the matched object.
* @return A matcher for a {@code public} modifier reviewable.
*/
public static ElementMatcher.Junction isPublic() {
return new ModifierMatcher(ModifierMatcher.Mode.PUBLIC);
}
/**
* Matches a {@link ModifierReviewable} that is {@code protected}.
*
* @param The type of the matched object.
* @return A matcher for a {@code protected} modifier reviewable.
*/
public static ElementMatcher.Junction isProtected() {
return new ModifierMatcher(ModifierMatcher.Mode.PROTECTED);
}
/**
* Matches a {@link ModifierReviewable} that is package-private.
*
* @param The type of the matched object.
* @return A matcher for a package-private modifier reviewable.
*/
public static