Skip to content

Commit

Permalink
Refactored method shape matchers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Jul 24, 2015
1 parent 8567c6a commit 58e242f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 63 deletions.
Expand Up @@ -44,7 +44,7 @@ public interface ByteCodeElement extends NamedElement.WithRuntimeName, ModifierR
*/
boolean isVisibleTo(TypeDescription typeDescription);

interface TypeDependant<T extends TypeDependant<? extends T, S>, S extends ByteCodeElement.Token<S>> {
interface TypeDependant<T extends TypeDependant<?, S>, S extends ByteCodeElement.Token<S>> {

T asDefined();

Expand Down
@@ -0,0 +1,18 @@
package net.bytebuddy.matcher;

import net.bytebuddy.description.ByteCodeElement;

public class DefinedShapeMatcher<T extends ByteCodeElement.TypeDependant<S, ?>, S extends ByteCodeElement.TypeDependant<?, ?>>
extends ElementMatcher.Junction.AbstractBase<T> {

private final ElementMatcher<? super S> matcher;

public DefinedShapeMatcher(ElementMatcher<? super S> matcher) {
this.matcher = matcher;
}

@Override
public boolean matches(T target) {
return matcher.matches(target.asDefined());
}
}
Expand Up @@ -26,7 +26,6 @@

import static net.bytebuddy.utility.ByteBuddyCommons.nonNull;


/**
* A utility class that contains a human-readable language for creating {@link net.bytebuddy.matcher.ElementMatcher}s.
*/
Expand Down Expand Up @@ -65,7 +64,7 @@ public static <T> ElementMatcher.Junction<T> is(Object value) {
* @param <T> The type of the matched object.
* @return An element matcher that exactly matches the given field.
*/
public static <T extends FieldDescription.InDefinedShape> ElementMatcher.Junction<T> is(Field field) {
public static <T extends FieldDescription> ElementMatcher.Junction<T> is(Field field) {
return is(new FieldDescription.ForLoadedField(nonNull(field)));
}

Expand All @@ -76,8 +75,12 @@ public static <T extends FieldDescription.InDefinedShape> ElementMatcher.Junctio
* @param <T> The type of the matched object.
* @return An element matcher that matches the given field description.
*/
public static <T extends FieldDescription> ElementMatcher.Junction<T> is(FieldDescription fieldDescription) {
return new EqualityMatcher<T>(nonNull(fieldDescription));
public static <T extends FieldDescription> ElementMatcher.Junction<T> is(FieldDescription.InDefinedShape fieldDescription) {
return definedField(new EqualityMatcher<FieldDescription.InDefinedShape>(nonNull(fieldDescription)));
}

public static <T extends FieldDescription> ElementMatcher.Junction<T> definedField(ElementMatcher<? super FieldDescription.InDefinedShape> matcher) {
return new DefinedShapeMatcher<T, FieldDescription.InDefinedShape>(nonNull(matcher));
}

/**
Expand Down Expand Up @@ -120,8 +123,12 @@ public static <T extends MethodDescription> ElementMatcher.Junction<T> is(Constr
* @param <T> The type of the matched object.
* @return An element matcher that matches the given method description.
*/
public static <T extends MethodDescription> ElementMatcher.Junction<T> is(MethodDescription methodDescription) {
return new EqualityMatcher<T>(nonNull(methodDescription));
public static <T extends MethodDescription> ElementMatcher.Junction<T> is(MethodDescription.InDefinedShape methodDescription) {
return definedMethod(new EqualityMatcher<MethodDescription.InDefinedShape>(nonNull(methodDescription)));
}

public static <T extends MethodDescription> ElementMatcher.Junction<T> definedMethod(ElementMatcher<? super MethodDescription.InDefinedShape> matcher) {
return new DefinedShapeMatcher<T, MethodDescription.InDefinedShape>(nonNull(matcher));
}

/**
Expand All @@ -135,6 +142,15 @@ public static <T extends MethodDescription> ElementMatcher.Junction<T> represent
return new TokenMatcher<T, MethodDescription.Token>(is(nonNull(methodToken)));
}

public static <T extends ParameterDescription> ElementMatcher<T> is(ParameterDescription.InDefinedShape parameterDescription) {
return definedParameter(new EqualityMatcher<ParameterDescription.InDefinedShape>(parameterDescription));
}

public static <T extends ParameterDescription> ElementMatcher.Junction<T> definedParameter(
ElementMatcher<? super ParameterDescription.InDefinedShape> matcher) {
return new DefinedShapeMatcher<T, ParameterDescription.InDefinedShape>(nonNull(matcher));
}

/**
* Validates if a method is represented by the provided method token.
*
Expand Down Expand Up @@ -270,7 +286,7 @@ public static <T extends GenericTypeDescription> ElementMatcher.Junction<T> anyO
* @return A matcher that checks for the equality with any of the given objects.
*/
public static <T extends MethodDescription> ElementMatcher.Junction<T> anyOf(Constructor<?>... value) {
return anyOf(new MethodList.ForLoadedType(nonNull(value), new Method[0]));
return definedMethod(anyOf(new MethodList.ForLoadedType(nonNull(value), new Method[0])));
}

/**
Expand All @@ -282,7 +298,7 @@ public static <T extends MethodDescription> ElementMatcher.Junction<T> anyOf(Con
* @return A matcher that checks for the equality with any of the given objects.
*/
public static <T extends MethodDescription> ElementMatcher.Junction<T> anyOf(Method... value) {
return anyOf(new MethodList.ForLoadedType(new Constructor<?>[0], nonNull(value)));
return definedMethod(anyOf(new MethodList.ForLoadedType(new Constructor<?>[0], nonNull(value))));
}

/**
Expand All @@ -294,7 +310,7 @@ public static <T extends MethodDescription> ElementMatcher.Junction<T> anyOf(Met
* @return A matcher that checks for the equality with any of the given objects.
*/
public static <T extends FieldDescription> ElementMatcher.Junction<T> anyOf(Field... value) {
return anyOf(new FieldList.ForLoadedField(nonNull(value)));
return definedField(anyOf(new FieldList.ForLoadedField(nonNull(value))));
}

/**
Expand Down Expand Up @@ -358,7 +374,7 @@ public static <T extends GenericTypeDescription> ElementMatcher.Junction<T> none
* @return A matcher that checks for the equality with none of the given objects.
*/
public static <T extends MethodDescription> ElementMatcher.Junction<T> noneOf(Constructor<?>... value) {
return noneOf(new MethodList.ForLoadedType(nonNull(value), new Method[0]));
return definedMethod(noneOf(new MethodList.ForLoadedType(nonNull(value), new Method[0])));
}

/**
Expand All @@ -370,7 +386,11 @@ public static <T extends MethodDescription> ElementMatcher.Junction<T> noneOf(Co
* @return A matcher that checks for the equality with none of the given objects.
*/
public static <T extends MethodDescription> ElementMatcher.Junction<T> noneOf(Method... value) {
return noneOf(new MethodList.ForLoadedType(new Constructor<?>[0], nonNull(value)));
return definedMethod(noneOf(new MethodList.ForLoadedType(new Constructor<?>[0], nonNull(value))));
}

public static <T extends MethodDescription> ElementMatcher.Junction<T> noneOf(Field... value) {
return definedMethod(noneOf(new FieldList.ForLoadedField(nonNull(value))));
}

/**
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 58e242f

Please sign in to comment.