net.bytebuddy.matcher.ElementMatchers Maven / Gradle / Ivy
/*
* Copyright 2014 - Present Rafael Winterhalter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.bytebuddy.matcher;
import net.bytebuddy.ClassFileVersion;
import net.bytebuddy.description.ByteCodeElement;
import net.bytebuddy.description.ModifierReviewable;
import net.bytebuddy.description.NamedElement;
import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.annotation.AnnotationList;
import net.bytebuddy.description.annotation.AnnotationSource;
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.TypeDefinition;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.TypeList;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.utility.JavaModule;
import net.bytebuddy.utility.nullability.MaybeNull;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* A utility class that contains a human-readable language for creating {@link net.bytebuddy.matcher.ElementMatcher}s.
*/
public final class ElementMatchers {
/**
* A private constructor that must not be invoked.
*/
private ElementMatchers() {
throw new UnsupportedOperationException("This class is a utility class and not supposed to be instantiated");
}
/**
* Wraps another matcher to assure that an element is not matched in case that the matching causes an {@link Exception}.
*
* @param matcher The element matcher that potentially throws an exception.
* @param The type of the matched object.
* @return A matcher that returns {@code false} in case that the given matcher throws an exception.
*/
public static ElementMatcher.Junction failSafe(ElementMatcher super T> matcher) {
return new FailSafeMatcher(matcher, false);
}
/**
*
* Wraps another matcher but caches the result of previously matched elements. Caching can be important if a
* matcher requires expensive calculations.
*
*
* Warning: The supplied map can however introduce a memory leak as the matched elements are stored within the map.
* It is therefore important to dereference this matcher at some point or to regularly evict entries from the supplied map.
*
*
* @param matcher The actual matcher for which the results are cached.
* @param map The map for storing results of previously matched elements.
* @param The type of the matched object.
* @return A matcher that stores the results of a previous matching in the supplied map.
*/
public static ElementMatcher.Junction cached(ElementMatcher super T> matcher, ConcurrentMap super T, Boolean> map) {
return new CachingMatcher(matcher, map);
}
/**
*
* Wraps another matcher but caches the result of previously matched elements. Caching can be important if a
* matcher requires expensive calculations.
*
*
* Warning: The cache will hold {@code evictionSize} elements and evict a random element once the cache
* contains more than the specified amount of elements. Cached elements are referenced strongly and might cause
* a memory leak if instance are of a significant size. Using {@link ElementMatchers#cached(ElementMatcher, ConcurrentMap)}
* allows for explicit control over cache eviction.
*
*
* @param matcher The actual matcher for which the results are cached.
* @param evictionSize The maximum amount of elements that are stored in the cache. Must be a positive number.
* @param The type of the matched object.
* @return A matcher that stores the results of a previous matching in the supplied map.
*/
public static ElementMatcher.Junction cached(ElementMatcher super T> matcher, int evictionSize) {
if (evictionSize < 1) {
throw new IllegalArgumentException("Eviction size must be a positive number: " + evictionSize);
}
return new CachingMatcher.WithInlineEviction(matcher, new ConcurrentHashMap(), evictionSize);
}
/**
* 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(@MaybeNull Object value) {
return value == null
? NullMatcher.make()
: new EqualityMatcher(value);
}
/**
* Exactly matches a given field as a {@link FieldDescription} in its defined shape.
*
* @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 in its defined shape.
*/
public static ElementMatcher.Junction is(Field field) {
return is(new FieldDescription.ForLoadedField(field));
}
/**
* Exactly matches a given field as a {@link FieldDescription} in its defined shape.
*
* @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 in its defined shape.
*/
public static ElementMatcher.Junction is(FieldDescription.InDefinedShape field) {
return definedField(new EqualityMatcher(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(matcher);
}
/**
* Exactly matches a given method as a {@link MethodDescription} in its defined shape.
*
* @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 in its defined shape.
*/
public static ElementMatcher.Junction is(Method method) {
return is(new MethodDescription.ForLoadedMethod(method));
}
/**
* Exactly matches a given constructor as a {@link MethodDescription} in its defined shape.
*
* @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 in its defined shape.
*/
public static ElementMatcher.Junction is(Constructor> constructor) {
return is(new MethodDescription.ForLoadedConstructor(constructor));
}
/**
* Exactly matches a given method or constructor as a {@link MethodDescription} in its defined shape.
*
* @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 or constructor in its defined shape.
*/
public static ElementMatcher.Junction is(MethodDescription.InDefinedShape method) {
return definedMethod(new EqualityMatcher(method));
}
/**
* 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(matcher);
}
/**
* Exactly matches a given parameter as a {@link ParameterDescription} in its defined shape.
*
* @param parameter The parameter to match by its description
* @param The type of the matched object.
* @return An element matcher that exactly matches the given parameter in its defined shape.
*/
public static ElementMatcher.Junction is(ParameterDescription.InDefinedShape parameter) {
return definedParameter(new EqualityMatcher(parameter));
}
/**
* 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(matcher);
}
/**
* 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(erasure(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 TypeDescription.Generic> matcher) {
return new MethodParameterTypeMatcher(matcher);
}
/**
* Matches a parameter description for a {@code mandated} parameter.
*
* @param The type of the matched object.
* @return A matcher for a {@code mandated} parameter.
*/
public static ElementMatcher.Junction isMandated() {
return ModifierMatcher.of(ModifierMatcher.Mode.MANDATED);
}
/**
* 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(TypeDefinition.Sort.describe(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(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(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 BooleanMatcher.of(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 BooleanMatcher.of(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}.
*
*
* Important: This method cannot be used interchangeably with any of its overloaded versions which also apply a type
* conversion.
*
*
* @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(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}.
*
*
* Important: This method cannot be used interchangeably with any of the overloaded versions of {@link ElementMatchers#anyOf(Object...)}
* which also apply a type conversion.
*
*
* @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 = null;
for (Object value : values) {
matcher = matcher == null
? ElementMatchers.is(value)
: matcher.or(is(value));
}
return matcher == null
? ElementMatchers.none()
: 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 TypeList.Generic.ForLoadedTypes(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.ForLoadedMethods(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.ForLoadedMethods(new Constructor>[0], 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.ForLoadedFields(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.ForLoadedAnnotations(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 = null;
for (Object value : values) {
matcher = matcher == null
? ElementMatchers.not(is(value))
: matcher.and(not(is(value)));
}
return matcher == null
? ElementMatchers.any()
: 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 TypeList.Generic.ForLoadedTypes(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.ForLoadedMethods(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.ForLoadedMethods(new Constructor>[0], 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.ForLoadedFields(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.ForLoadedAnnotations(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 erasure against the provided type. As a wildcard does not define an erasure, a runtime exception is thrown when
* this matcher is applied to a wildcard.
*
* @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 erasure(Class> type) {
return erasure(is(type));
}
/**
* Matches a generic type's erasure against the provided type. As a wildcard does not define an erasure, a runtime exception is thrown
* when this matcher is applied to a wildcard.
*
* @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 erasure(TypeDescription type) {
return erasure(is(type));
}
/**
* Converts a matcher for a type description into a matcher for the matched type's erasure. As a wildcard does not define an erasure,
* a runtime exception is thrown when this matcher is applied to a wildcard.
*
* @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 erasure(ElementMatcher super TypeDescription> matcher) {
return new ErasureMatcher(matcher);
}
/**
* Matches an iteration of generic types' erasures against the provided types. As a wildcard does not define an erasure, a runtime
* exception is thrown when this matcher is applied to a wildcard.
*
* @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 erasures(Class>... type) {
return erasures(new TypeList.ForLoadedTypes(type));
}
/**
* Matches an iteration of generic types' erasures against the provided types. As a wildcard does not define an erasure, a runtime
* exception is thrown when this matcher is applied to a wildcard.
*
* @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 erasures(TypeDescription... type) {
return erasures(Arrays.asList(type));
}
/**
* Matches an iteration of generic types' erasures against the provided types. As a wildcard does not define an erasure, a runtime
* exception is thrown when this matcher is applied to a wildcard.
*
* @param types 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 erasures(
Iterable extends TypeDescription> types) {
List> typeMatchers = new ArrayList>();
for (TypeDescription type : types) {
typeMatchers.add(is(type));
}
return erasures(new CollectionOneToOneMatcher(typeMatchers));
}
/**
* Applies the provided matchers to an iteration og generic types' erasures. As a wildcard does not define an erasure, a runtime
* exception is thrown when this matcher is applied to a wildcard.
*
* @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 erasures(
ElementMatcher super Iterable extends TypeDescription>> matcher) {
return new CollectionErasureMatcher(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.Junction isVariable(String symbol) {
return isVariable(named(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.Junction isVariable(ElementMatcher super NamedElement> matcher) {
return new TypeSortMatcher(anyOf(TypeDefinition.Sort.VARIABLE, TypeDefinition.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(name, StringMatcher.Mode.EQUALS_FULLY));
}
/**
* Matches a {@link NamedElement} for its membership of a set.
*
* @param names The set of expected names.
* @param The type of the matched object.
* @return An element matcher which matches if the element's name is found in the set.
*/
public static ElementMatcher.Junction namedOneOf(String... names) {
return new NameMatcher(new StringSetMatcher(new HashSet(Arrays.asList(names))));
}
/**
* 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(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(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(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(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(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(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(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(regex, StringMatcher.Mode.MATCHES));
}
/**
* Matches a {@link NamedElement.WithOptionalName} for having an explicit name.
*
* @param The type of the matched object.
* @return An element matcher that checks if the matched optionally named element has an explicit name.
*/
public static ElementMatcher.Junction isNamed() {
return new IsNamedMatcher();
}
/**
* Matches a {@link NamedElement.WithDescriptor}'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(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(TypeDescription.ForLoadedType.of(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(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(erasure(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(TypeDefinition.Sort.describe(type));
}
/**
* Matches a {@link ByteCodeElement} for being declared by a given {@link TypeDescription.Generic}.
*
* @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(TypeDescription.Generic type) {
return isDeclaredByGeneric(is(type));
}
/**
* Matches a {@link ByteCodeElement} for being declared by a {@link TypeDescription.Generic} 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 TypeDescription.Generic> matcher) {
return new DeclaringTypeMatcher(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(TypeDescription.ForLoadedType.of(type));
}
/**
* Matches a {@link ByteCodeElement} that is visible to a given {@link TypeDescription}.
*
* @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(TypeDescription type) {
return new VisibilityMatcher(type);
}
/**
* Matches a {@link ByteCodeElement} that is accessible to a given {@link java.lang.Class}.
*
* @param type The type that a matched byte code element is expected to be accessible to.
* @param The type of the matched object.
* @return A matcher for a byte code element to be accessible to a given {@code type}.
*/
public static ElementMatcher.Junction isAccessibleTo(Class> type) {
return isAccessibleTo(TypeDescription.ForLoadedType.of(type));
}
/**
* Matches a {@link ByteCodeElement} that is accessible to a given {@link java.lang.Class}.
*
* @param type The type that a matched byte code element is expected to be accessible to.
* @param The type of the matched object.
* @return A matcher for a byte code element to be accessible to a given {@code type}.
*/
public static ElementMatcher.Junction isAccessibleTo(TypeDescription type) {
return new AccessibilityMatcher(type);
}
/**
* Matches a {@link ModifierReviewable.OfAbstraction} 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 ModifierMatcher.of(ModifierMatcher.Mode.ABSTRACT);
}
/**
* Matches a {@link ModifierReviewable.OfEnumeration} that is an {@code enum} or a field holding an enum.
*
* @param The type of the matched object.
* @return A matcher for an enum.
*/
public static