Add matcher for only concrete classes
This commit is contained in:
parent
d45ead1284
commit
147b4a64f1
|
@ -24,7 +24,7 @@ public class ByteBuddyElementMatchers {
|
||||||
|
|
||||||
public static <T extends TypeDescription> ElementMatcher.Junction<T> safeExtendsClass(
|
public static <T extends TypeDescription> ElementMatcher.Junction<T> safeExtendsClass(
|
||||||
final ElementMatcher<? super TypeDescription> matcher) {
|
final ElementMatcher<? super TypeDescription> matcher) {
|
||||||
return safeHasSuperType(matcher);
|
return new SafeExtendsClassMatcher<>(new SafeErasureMatcher<>(matcher));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T extends TypeDescription> ElementMatcher.Junction<T> safeHasInterface(
|
public static <T extends TypeDescription> ElementMatcher.Junction<T> safeHasInterface(
|
||||||
|
@ -44,22 +44,7 @@ public class ByteBuddyElementMatchers {
|
||||||
*/
|
*/
|
||||||
public static <T extends TypeDescription> ElementMatcher.Junction<T> safeHasSuperType(
|
public static <T extends TypeDescription> ElementMatcher.Junction<T> safeHasSuperType(
|
||||||
final ElementMatcher<? super TypeDescription> matcher) {
|
final ElementMatcher<? super TypeDescription> matcher) {
|
||||||
return safeHasGenericSuperType(new SafeErasureMatcher(matcher));
|
return new SafeHasSuperTypeMatcher<>(new SafeErasureMatcher<>(matcher));
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Matches any type description that declares a super type that matches the provided matcher.
|
|
||||||
* Exceptions during matching process are logged and ignored.
|
|
||||||
*
|
|
||||||
* @param matcher The type to be checked for being a super type of the matched type.
|
|
||||||
* @param <T> The type of the matched object.
|
|
||||||
* @return A matcher that matches any type description that declares a super type that matches the
|
|
||||||
* provided matcher.
|
|
||||||
* @see ElementMatchers#hasGenericSuperType(net.bytebuddy.matcher.ElementMatcher)
|
|
||||||
*/
|
|
||||||
public static <T extends TypeDescription> ElementMatcher.Junction<T> safeHasGenericSuperType(
|
|
||||||
final ElementMatcher<? super TypeDescription.Generic> matcher) {
|
|
||||||
return new SafeHasSuperTypeMatcher<>(matcher);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -139,19 +124,6 @@ public class ByteBuddyElementMatchers {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private TypeDefinition safeGetSuperClass(final TypeDefinition typeDefinition) {
|
|
||||||
try {
|
|
||||||
return typeDefinition.getSuperClass();
|
|
||||||
} catch (final Exception e) {
|
|
||||||
log.debug(
|
|
||||||
"{} trying to get super class for target {}: {}",
|
|
||||||
e.getClass().getSimpleName(),
|
|
||||||
safeTypeDefinitionName(typeDefinition),
|
|
||||||
e.getMessage());
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Matches a type's interfaces against the provided matcher.
|
* Matches a type's interfaces against the provided matcher.
|
||||||
*
|
*
|
||||||
|
@ -371,7 +343,13 @@ public class ByteBuddyElementMatchers {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private TypeDefinition safeGetSuperClass(final TypeDefinition typeDefinition) {
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "hasSuperMethodMatcher(" + matcher + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static TypeDefinition safeGetSuperClass(final TypeDefinition typeDefinition) {
|
||||||
try {
|
try {
|
||||||
return typeDefinition.getSuperClass();
|
return typeDefinition.getSuperClass();
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
|
@ -384,9 +362,27 @@ public class ByteBuddyElementMatchers {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class SafeExtendsClassMatcher<T extends TypeDescription>
|
||||||
|
extends ElementMatcher.Junction.AbstractBase<T> {
|
||||||
|
|
||||||
|
private final ElementMatcher<? super TypeDescription.Generic> matcher;
|
||||||
|
|
||||||
|
public SafeExtendsClassMatcher(final ElementMatcher<? super TypeDescription.Generic> matcher) {
|
||||||
|
this.matcher = matcher;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public boolean matches(final T target) {
|
||||||
return "hasSuperMethodMatcher(" + matcher + ")";
|
// We do not use foreach loop and iterator interface here because we need to catch exceptions
|
||||||
|
// in {@code getSuperClass} calls
|
||||||
|
TypeDefinition typeDefinition = target;
|
||||||
|
while (typeDefinition != null) {
|
||||||
|
if (matcher.matches(typeDefinition.asGenericType())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
typeDefinition = safeGetSuperClass(typeDefinition);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue