Skip to content

Commit

Permalink
Added missing documentation for element matchers. Fixed test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Sep 15, 2015
1 parent 22bddce commit 16ea4d1
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 27 deletions.
Expand Up @@ -93,6 +93,13 @@ public static <T extends FieldDescription> ElementMatcher.Junction<T> represente
return fieldRepresentedBy(is(nonNull(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 <T> The matched object's type.
* @return A matcher that applies the given matcher to the matched field's token.
*/
public static <T extends FieldDescription> ElementMatcher.Junction<T> fieldRepresentedBy(ElementMatcher<? super FieldDescription.Token> matcher) { public static <T extends FieldDescription> ElementMatcher.Junction<T> fieldRepresentedBy(ElementMatcher<? super FieldDescription.Token> matcher) {
return new TokenMatcher<T, FieldDescription.Token>(nonNull(matcher)); return new TokenMatcher<T, FieldDescription.Token>(nonNull(matcher));
} }
Expand Down Expand Up @@ -130,6 +137,13 @@ public static <T extends MethodDescription> ElementMatcher.Junction<T> definedMe
return new DefinedShapeMatcher<T, MethodDescription.InDefinedShape>(nonNull(matcher)); return new DefinedShapeMatcher<T, MethodDescription.InDefinedShape>(nonNull(matcher));
} }


/**
* Matches a method by a token matcher.
*
* @param matcher The matcher to apply to the method's token.
* @param <T> The matched object's type.
* @return A matcher that applies the given matcher to the matched method's token.
*/
public static <T extends MethodDescription> ElementMatcher.Junction<T> methodRepresentedBy(ElementMatcher<? super MethodDescription.Token> matcher) { public static <T extends MethodDescription> ElementMatcher.Junction<T> methodRepresentedBy(ElementMatcher<? super MethodDescription.Token> matcher) {
return new TokenMatcher<T, MethodDescription.Token>(nonNull(matcher)); return new TokenMatcher<T, MethodDescription.Token>(nonNull(matcher));
} }
Expand Down Expand Up @@ -157,7 +171,15 @@ public static <T extends ParameterDescription> ElementMatcher.Junction<T> define
return new DefinedShapeMatcher<T, ParameterDescription.InDefinedShape>(nonNull(matcher)); return new DefinedShapeMatcher<T, ParameterDescription.InDefinedShape>(nonNull(matcher));
} }


public static <T extends ParameterDescription> ElementMatcher.Junction<T> parameterRepresentedBy(ElementMatcher<? super ParameterDescription.Token> matcher) { /**
* Matches a parameter by a token matcher.
*
* @param matcher The matcher to apply to the parameter's token.
* @param <T> The matched object's type.
* @return A matcher that applies the given matcher to the matched parameter's token.
*/
public static <T extends ParameterDescription> ElementMatcher.Junction<T> parameterRepresentedBy(
ElementMatcher<? super ParameterDescription.Token> matcher) {
return new TokenMatcher<T, ParameterDescription.Token>(nonNull(matcher)); return new TokenMatcher<T, ParameterDescription.Token>(nonNull(matcher));
} }


Expand All @@ -172,18 +194,24 @@ public static <T extends ParameterDescription> ElementMatcher.Junction<T> repres
return parameterRepresentedBy(is(nonNull(parameterToken))); return parameterRepresentedBy(is(nonNull(parameterToken)));
} }


public static <T extends ParameterDescription> ElementMatcher.Junction<T> hasType(Class<?> type) { /**
return hasType(is(nonNull(type))); * Matches a parameter's type by the given matcher.
} *

* @param matcher
* @param <T>
* @return
*/
public static <T extends ParameterDescription> ElementMatcher.Junction<T> hasType(ElementMatcher<? super TypeDescription> matcher) { public static <T extends ParameterDescription> ElementMatcher.Junction<T> hasType(ElementMatcher<? super TypeDescription> matcher) {
return hasGenericType(rawType(nonNull(matcher))); return hasGenericType(rawType(nonNull(matcher)));
} }


public static <T extends ParameterDescription> ElementMatcher.Junction<T> hasGenericType(Type type) { /**
return hasGenericType(is(nonNull(type))); * Matches a method parameter by its generic type.
} *

* @param matcher The matcher to apply to a parameter's generic type.
* @param <T> The type of the matched object.
* @return A matcher that matches the matched parameter's generic type.
*/
public static <T extends ParameterDescription> ElementMatcher.Junction<T> hasGenericType(ElementMatcher<? super GenericTypeDescription> matcher) { public static <T extends ParameterDescription> ElementMatcher.Junction<T> hasGenericType(ElementMatcher<? super GenericTypeDescription> matcher) {
return new MethodParameterTypeMatcher<T>(nonNull(matcher)); return new MethodParameterTypeMatcher<T>(nonNull(matcher));
} }
Expand Down Expand Up @@ -954,13 +982,19 @@ public static <T extends MethodDescription> ElementMatcher.Junction<T> returns(T
return returns(is(nonNull(typeDescription))); return returns(is(nonNull(typeDescription)));
} }


/**
* Matches a method's return type's erasure by the given matcher.
*
* @param matcher The matcher to apply to a method's return type's erasure.
* @param <T> The type of the matched object.
* @return A matcher that matches the matched method's return type's erasure.
*/
public static <T extends MethodDescription> ElementMatcher.Junction<T> returns(ElementMatcher<? super TypeDescription> matcher) { public static <T extends MethodDescription> ElementMatcher.Junction<T> returns(ElementMatcher<? super TypeDescription> matcher) {
return returnsGeneric(rawType(nonNull(matcher))); return returnsGeneric(rawType(nonNull(matcher)));
} }


/** /**
* Matches {@link MethodDescription}s that matches a matched method's * Matches {@link MethodDescription}s that matches a matched method's return type.
* return type.
* *
* @param matcher A matcher to apply onto a matched method's return type. * @param matcher A matcher to apply onto a matched method's return type.
* @param <T> The type of the matched object. * @param <T> The type of the matched object.
Expand Down
Expand Up @@ -4,7 +4,7 @@
import net.bytebuddy.description.type.generic.GenericTypeDescription; import net.bytebuddy.description.type.generic.GenericTypeDescription;


/** /**
* An element matcher that matches a method's parameter types. * An element matcher that matches a method's parameter's type.
* *
* @param <T> The type of the matched entity. * @param <T> The type of the matched entity.
*/ */
Expand All @@ -16,7 +16,7 @@ public class MethodParameterTypeMatcher<T extends ParameterDescription> extends
private final ElementMatcher<? super GenericTypeDescription> matcher; private final ElementMatcher<? super GenericTypeDescription> matcher;


/** /**
* Creates a new matcher for a method's parameter types. * Creates a new matcher for a method's parameter's type.
* *
* @param matcher The matcher to apply to the type of the parameter. * @param matcher The matcher to apply to the type of the parameter.
*/ */
Expand Down
@@ -1,28 +1,25 @@
package net.bytebuddy.matcher; package net.bytebuddy.matcher;


import net.bytebuddy.description.method.ParameterList; import net.bytebuddy.description.method.ParameterDescription;
import net.bytebuddy.description.type.generic.GenericTypeDescription; import net.bytebuddy.description.type.generic.GenericTypeDescription;
import net.bytebuddy.description.type.generic.GenericTypeList;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mock; import org.mockito.Mock;


import java.util.List;

import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;


public class MethodParameterTypeMatcherTest extends AbstractElementMatcherTest<MethodParameterTypeMatcher<?>> { public class MethodParameterTypeMatcherTest extends AbstractElementMatcherTest<MethodParameterTypeMatcher<?>> {


@Mock @Mock
private ElementMatcher<? super List<? extends GenericTypeDescription>> parameterMatcher; private ElementMatcher<? super GenericTypeDescription> parameterMatcher;


@Mock @Mock
private GenericTypeList typeList; private GenericTypeDescription typeDescription;


@Mock @Mock
private ParameterList parameterList; private ParameterDescription parameterDescription;


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public MethodParameterTypeMatcherTest() { public MethodParameterTypeMatcherTest() {
Expand All @@ -31,22 +28,22 @@ public MethodParameterTypeMatcherTest() {


@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
when(parameterList.asTypeList()).thenReturn(typeList); when(parameterDescription.getType()).thenReturn(typeDescription);
} }


@Test @Test
public void testMatch() throws Exception { public void testMatch() throws Exception {
when(parameterMatcher.matches(typeList)).thenReturn(true); when(parameterMatcher.matches(typeDescription)).thenReturn(true);
assertThat(new MethodParameterTypesMatcher<ParameterList<?>>(parameterMatcher).matches(parameterList), is(true)); assertThat(new MethodParameterTypeMatcher<ParameterDescription>(parameterMatcher).matches(parameterDescription), is(true));
verify(parameterMatcher).matches(typeList); verify(parameterMatcher).matches(typeDescription);
verifyNoMoreInteractions(parameterMatcher); verifyNoMoreInteractions(parameterMatcher);
} }


@Test @Test
public void testNoMatch() throws Exception { public void testNoMatch() throws Exception {
when(parameterMatcher.matches(typeList)).thenReturn(false); when(parameterMatcher.matches(typeDescription)).thenReturn(false);
assertThat(new MethodParameterTypesMatcher<ParameterList<?>>(parameterMatcher).matches(parameterList), is(false)); assertThat(new MethodParameterTypeMatcher<ParameterDescription>(parameterMatcher).matches(parameterDescription), is(false));
verify(parameterMatcher).matches(typeList); verify(parameterMatcher).matches(typeDescription);
verifyNoMoreInteractions(parameterMatcher); verifyNoMoreInteractions(parameterMatcher);
} }
} }
@@ -0,0 +1,52 @@
package net.bytebuddy.matcher;

import net.bytebuddy.description.method.ParameterList;
import net.bytebuddy.description.type.generic.GenericTypeDescription;
import net.bytebuddy.description.type.generic.GenericTypeList;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.*;

public class MethodParameterTypesMatcherTest extends AbstractElementMatcherTest<MethodParameterTypesMatcher<?>> {

@Mock
private ElementMatcher<? super List<? extends GenericTypeDescription>> parameterMatcher;

@Mock
private GenericTypeList typeList;

@Mock
private ParameterList parameterList;

@SuppressWarnings("unchecked")
public MethodParameterTypesMatcherTest() {
super((Class<MethodParameterTypesMatcher<?>>) (Object) MethodParameterTypesMatcher.class, "hasTypes");
}

@Before
public void setUp() throws Exception {
when(parameterList.asTypeList()).thenReturn(typeList);
}

@Test
public void testMatch() throws Exception {
when(parameterMatcher.matches(typeList)).thenReturn(true);
assertThat(new MethodParameterTypesMatcher<ParameterList<?>>(parameterMatcher).matches(parameterList), is(true));
verify(parameterMatcher).matches(typeList);
verifyNoMoreInteractions(parameterMatcher);
}

@Test
public void testNoMatch() throws Exception {
when(parameterMatcher.matches(typeList)).thenReturn(false);
assertThat(new MethodParameterTypesMatcher<ParameterList<?>>(parameterMatcher).matches(parameterList), is(false));
verify(parameterMatcher).matches(typeList);
verifyNoMoreInteractions(parameterMatcher);
}
}

0 comments on commit 16ea4d1

Please sign in to comment.