Skip to content

Commit

Permalink
Further refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Dec 21, 2015
1 parent d7e7064 commit 44076dc
Show file tree
Hide file tree
Showing 24 changed files with 160 additions and 124 deletions.
Expand Up @@ -97,8 +97,8 @@ private Implementation.SpecialMethodInvocation invokeSuper(MethodRebaseResolver.
}

@Override
public GenericTypeDescription getOriginType() {
return instrumentedType.asGenericType();
public TypeDescription getOriginType() {
return instrumentedType;
}

@Override
Expand Down
Expand Up @@ -2,6 +2,7 @@

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.method.MethodList;
import net.bytebuddy.description.type.TypeDefinition;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.generic.GenericTypeDescription;
import net.bytebuddy.dynamic.scaffold.MethodGraph;
Expand Down Expand Up @@ -82,7 +83,7 @@ private Implementation.SpecialMethodInvocation invokeMethod(MethodDescription.To
}

@Override
public GenericTypeDescription getOriginType() {
public TypeDefinition getOriginType() {
return originTypeResolver.identify(instrumentedType);
}

Expand Down Expand Up @@ -123,7 +124,7 @@ public enum OriginTypeResolver {
*/
SUPER_TYPE {
@Override
protected GenericTypeDescription identify(TypeDescription typeDescription) {
protected TypeDefinition identify(TypeDescription typeDescription) {
return typeDescription.getSuperType();
}
},
Expand All @@ -133,8 +134,8 @@ protected GenericTypeDescription identify(TypeDescription typeDescription) {
*/
LEVEL_TYPE {
@Override
protected GenericTypeDescription identify(TypeDescription typeDescription) {
return typeDescription.asGenericType();
protected TypeDefinition identify(TypeDescription typeDescription) {
return typeDescription;
}
};

Expand All @@ -144,7 +145,7 @@ protected GenericTypeDescription identify(TypeDescription typeDescription) {
* @param typeDescription The type description for which an origin type should be identified.
* @return The origin type to the given type description.
*/
protected abstract GenericTypeDescription identify(TypeDescription typeDescription);
protected abstract TypeDefinition identify(TypeDescription typeDescription);

@Override
public String toString() {
Expand Down
Expand Up @@ -6,6 +6,7 @@
import net.bytebuddy.description.method.MethodDescription;
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.generic.GenericTypeDescription;
import net.bytebuddy.description.type.generic.GenericTypeList;
Expand Down Expand Up @@ -265,7 +266,7 @@ interface Target {
*
* @return The origin type of this implementation.
*/
GenericTypeDescription getOriginType();
TypeDefinition getOriginType();

/**
* Creates a special method invocation for invoking the super method of the given method.
Expand Down
@@ -1,5 +1,6 @@
package net.bytebuddy.matcher;

import net.bytebuddy.description.type.TypeDefinition;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.generic.GenericTypeDescription;

Expand All @@ -11,27 +12,27 @@
*
* @param <T> The type of the matched entity.
*/
public class CollectionRawTypeMatcher<T extends Iterable<? extends GenericTypeDescription>> extends ElementMatcher.Junction.AbstractBase<T> {
public class CollectionRawTypeMatcher<T extends Iterable<? extends TypeDefinition>> extends ElementMatcher.Junction.AbstractBase<T> {

/**
* The matcher to be applied to the raw types.
*/
private final ElementMatcher<? super Iterable<? extends TypeDescription>> matcher;
private final ElementMatcher<? super Iterable<? extends TypeDefinition>> matcher;

/**
* Creates a new raw type matcher.
*
* @param matcher The matcher to be applied to the raw types.
*/
public CollectionRawTypeMatcher(ElementMatcher<? super Iterable<? extends TypeDescription>> matcher) {
public CollectionRawTypeMatcher(ElementMatcher<? super Iterable<? extends TypeDefinition>> matcher) {
this.matcher = matcher;
}

@Override
public boolean matches(T target) {
List<TypeDescription> typeDescriptions = new LinkedList<TypeDescription>();
for (GenericTypeDescription typeDescription : target) {
typeDescriptions.add(typeDescription.asErasure());
for (TypeDefinition typeDefinition : target) {
typeDescriptions.add(typeDefinition.asErasure());
}
return matcher.matches(typeDescriptions);
}
Expand Down
Expand Up @@ -2,14 +2,15 @@

import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.field.FieldList;
import net.bytebuddy.description.type.TypeDefinition;
import net.bytebuddy.description.type.generic.GenericTypeDescription;

/**
* An element matcher that checks if a type description declares fields of a given property.
*
* @param <T> The exact type of the annotated element that is matched.
*/
public class DeclaringFieldMatcher<T extends GenericTypeDescription> extends ElementMatcher.Junction.AbstractBase<T> {
public class DeclaringFieldMatcher<T extends TypeDefinition> extends ElementMatcher.Junction.AbstractBase<T> {

/**
* The field matcher to apply to the declared fields of the matched type description.
Expand Down
Expand Up @@ -2,14 +2,15 @@

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.method.MethodList;
import net.bytebuddy.description.type.TypeDefinition;
import net.bytebuddy.description.type.generic.GenericTypeDescription;

/**
* An element matcher that checks if a type description declares methods of a given property.
*
* @param <T> The exact type of the annotated element that is matched.
*/
public class DeclaringMethodMatcher<T extends GenericTypeDescription> extends ElementMatcher.Junction.AbstractBase<T> {
public class DeclaringMethodMatcher<T extends TypeDefinition> extends ElementMatcher.Junction.AbstractBase<T> {

/**
* The field matcher to apply to the declared fields of the matched type description.
Expand Down
Expand Up @@ -409,7 +409,7 @@ public static <T> ElementMatcher.Junction<T> noneOf(Iterable<?> values) {
* @param <T> The type of the matched object.
* @return A matcher that checks for the equality with none of the given objects.
*/
public static <T extends GenericTypeDescription> ElementMatcher.Junction<T> noneOf(Type... value) {
public static <T extends TypeDefinition> ElementMatcher.Junction<T> noneOf(Type... value) {
return noneOf(new GenericTypeList.ForLoadedTypes(nonNull(value)));
}

Expand Down Expand Up @@ -575,7 +575,7 @@ public static <T extends Iterable<? extends GenericTypeDescription>> ElementMatc
* @param <T> The type of the matched object.
* @return A matcher that matches type variables with the given name.
*/
public static <T extends GenericTypeDescription> ElementMatcher<T> isVariable(String symbol) {
public static <T extends TypeDefinition> ElementMatcher<T> isVariable(String symbol) {
return isVariable(named(nonNull(symbol)));
}

Expand All @@ -586,7 +586,7 @@ public static <T extends GenericTypeDescription> ElementMatcher<T> isVariable(St
* @param <T> The type of the matched object.
* @return A matcher that matches type variables with the given name.
*/
public static <T extends GenericTypeDescription> ElementMatcher<T> isVariable(ElementMatcher<? super NamedElement> matcher) {
public static <T extends TypeDefinition> ElementMatcher<T> isVariable(ElementMatcher<? super NamedElement> matcher) {
return new TypeSortMatcher<T>(anyOf(TypeDefinition.Sort.VARIABLE,
TypeDefinition.Sort.VARIABLE_DETACHED,
TypeDefinition.Sort.VARIABLE_SYMBOLIC)).and(matcher);
Expand Down Expand Up @@ -1067,25 +1067,25 @@ public static <T extends MethodDescription> ElementMatcher.Junction<T> takesGene
/**
* Matches a method description that takes the provided generic arguments.
*
* @param typeDescription The arguments to match against the matched method.
* @param typeDefinition The arguments to match against the matched method.
* @param <T> The type of the matched object.
* @return A method matcher that matches a method's generic parameter types against the supplied arguments.
*/
public static <T extends MethodDescription> ElementMatcher.Junction<T> takesGenericArguments(GenericTypeDescription... typeDescription) {
return takesGenericArguments((Arrays.asList(nonNull(typeDescription))));
public static <T extends MethodDescription> ElementMatcher.Junction<T> takesGenericArguments(TypeDefinition... typeDefinition) {
return takesGenericArguments((Arrays.asList(nonNull(typeDefinition))));
}

/**
* Matches a method description that takes the provided generic arguments.
*
* @param typeDescriptions The arguments to match against the matched method.
* @param typeDefinitions The arguments to match against the matched method.
* @param <T> The type of the matched object.
* @return A method matcher that matches a method's generic parameter types against the supplied arguments.
*/
public static <T extends MethodDescription> ElementMatcher.Junction<T> takesGenericArguments(List<? extends GenericTypeDescription> typeDescriptions) {
public static <T extends MethodDescription> ElementMatcher.Junction<T> takesGenericArguments(List<? extends TypeDefinition> typeDefinitions) {
List<ElementMatcher<? super GenericTypeDescription>> typeMatchers = new LinkedList<ElementMatcher<? super GenericTypeDescription>>();
for (GenericTypeDescription typeDescription : typeDescriptions) {
typeMatchers.add(is(nonNull(typeDescription)));
for (TypeDefinition typeDefinition : typeDefinitions) {
typeMatchers.add(is(nonNull(typeDefinition)));
}
return takesGenericArguments(new CollectionOneToOneMatcher<GenericTypeDescription>(typeMatchers));
}
Expand Down Expand Up @@ -1587,7 +1587,7 @@ public static <T extends TypeDescription> ElementMatcher.Junction<T> hasAnnotati
* @param <T> The type of the matched object.
* @return A matcher that matches any type where another matcher is matched positively on at least on declared field.
*/
public static <T extends GenericTypeDescription> ElementMatcher.Junction<T> declaresField(ElementMatcher<? super FieldDescription> fieldMatcher) {
public static <T extends TypeDefinition> ElementMatcher.Junction<T> declaresField(ElementMatcher<? super FieldDescription> fieldMatcher) {
return new DeclaringFieldMatcher<T>(new CollectionItemMatcher<FieldDescription>(nonNull(fieldMatcher)));
}

Expand All @@ -1598,7 +1598,7 @@ public static <T extends GenericTypeDescription> ElementMatcher.Junction<T> decl
* @param <T> The type of the matched object.
* @return A matcher that matches any type where another matcher is matched positively on at least on declared methods.
*/
public static <T extends GenericTypeDescription> ElementMatcher.Junction<T> declaresMethod(ElementMatcher<? super MethodDescription> methodMatcher) {
public static <T extends TypeDefinition> ElementMatcher.Junction<T> declaresMethod(ElementMatcher<? super MethodDescription> methodMatcher) {
return new DeclaringMethodMatcher<T>(new CollectionItemMatcher<MethodDescription>(nonNull(methodMatcher)));
}

Expand All @@ -1609,7 +1609,7 @@ public static <T extends GenericTypeDescription> ElementMatcher.Junction<T> decl
* @param <T> The type of the matched object.
* @return A matcher that matches generic types of the given sort.
*/
public static <T extends GenericTypeDescription> ElementMatcher.Junction<T> ofSort(TypeDefinition.Sort sort) {
public static <T extends TypeDefinition> ElementMatcher.Junction<T> ofSort(TypeDefinition.Sort sort) {
return ofSort(is(nonNull(sort)));
}

Expand All @@ -1620,7 +1620,7 @@ public static <T extends GenericTypeDescription> ElementMatcher.Junction<T> ofSo
* @param <T> The type of the matched object.
* @return A matcher that matches generic types of the given sort.
*/
public static <T extends GenericTypeDescription> ElementMatcher.Junction<T> ofSort(ElementMatcher<? super TypeDefinition.Sort> matcher) {
public static <T extends TypeDefinition> ElementMatcher.Junction<T> ofSort(ElementMatcher<? super TypeDefinition.Sort> matcher) {
return new TypeSortMatcher<T>(nonNull(matcher));
}

Expand Down
@@ -1,5 +1,6 @@
package net.bytebuddy.matcher;

import net.bytebuddy.description.type.TypeDefinition;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.generic.GenericTypeDescription;

Expand All @@ -9,7 +10,7 @@
*
* @param <T> The type of the matched entity.
*/
public class RawTypeMatcher<T extends GenericTypeDescription> extends ElementMatcher.Junction.AbstractBase<T> {
public class RawTypeMatcher<T extends TypeDefinition> extends ElementMatcher.Junction.AbstractBase<T> {

/**
* The matcher to apply to the raw type of the matched element.
Expand Down
Expand Up @@ -8,7 +8,7 @@
*
* @param <T> The type of the matched entity.
*/
public class TypeSortMatcher<T extends GenericTypeDescription> extends ElementMatcher.Junction.AbstractBase<T> {
public class TypeSortMatcher<T extends TypeDefinition> extends ElementMatcher.Junction.AbstractBase<T> {

/**
* An element matcher to be applied to the type's sort.
Expand Down
@@ -1,22 +1,20 @@
package net.bytebuddy.description.type.generic;
package net.bytebuddy.description.type;

import net.bytebuddy.description.type.TypeDefinition;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.generic.GenericTypeDescription;
import net.bytebuddy.test.utility.ObjectPropertyAssertion;
import org.junit.Test;

import java.lang.reflect.Type;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class GenericTypeDescriptionTest {

@Test
public void testObjectProperties() throws Exception {
ObjectPropertyAssertion.of(TypeDefinition.Sort.class).apply();
}
public class TypeDefinitionTest {

@Test(expected = IllegalArgumentException.class)
public void testUnknownType() throws Exception {
Expand All @@ -30,48 +28,47 @@ public void testNegativeArityGenericArray() throws Exception {

@Test
public void testZeroArityReturnsInstance() throws Exception {
GenericTypeDescription genericTypeDescription = mock(GenericTypeDescription.class);
when(genericTypeDescription.getSort()).thenReturn(TypeDefinition.Sort.PARAMETERIZED);
assertThat(GenericTypeDescription.ForGenericArray.Latent.of(genericTypeDescription, 0), is(genericTypeDescription));
GenericTypeDescription typeDescription = mock(GenericTypeDescription.class);
when(typeDescription.getSort()).thenReturn(TypeDefinition.Sort.PARAMETERIZED);
assertThat(GenericTypeDescription.ForGenericArray.Latent.of(typeDescription, 0), sameInstance(typeDescription));
}

@Test
public void testNonGenericArrayType() throws Exception {
TypeDescription typeDescription = mock(TypeDescription.class);
when(typeDescription.asErasure()).thenReturn(typeDescription);
GenericTypeDescription typeDescription = mock(GenericTypeDescription.class);
when(typeDescription.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC);
assertThat(GenericTypeDescription.ForGenericArray.Latent.of(typeDescription, 1).getSort(), is(TypeDefinition.Sort.NON_GENERIC));
assertThat(GenericTypeDescription.ForGenericArray.Latent.of(typeDescription, 1).getComponentType(), is((GenericTypeDescription) typeDescription));
assertThat(GenericTypeDescription.ForGenericArray.Latent.of(typeDescription, 1).getComponentType(), is(typeDescription));
}

@Test
public void testZeroArityIteratesTypesInstance() throws Exception {
GenericTypeDescription genericTypeDescription = mock(GenericTypeDescription.class);
GenericTypeDescription typeDescription = mock(GenericTypeDescription.class);
GenericTypeDescription componentType = mock(GenericTypeDescription.class);
when(genericTypeDescription.getComponentType()).thenReturn(componentType);
when(genericTypeDescription.getSort()).thenReturn(TypeDefinition.Sort.GENERIC_ARRAY);
when(typeDescription.getComponentType()).thenReturn(componentType);
when(typeDescription.getSort()).thenReturn(TypeDefinition.Sort.GENERIC_ARRAY);
when(componentType.getSort()).thenReturn(TypeDefinition.Sort.PARAMETERIZED);
GenericTypeDescription result = GenericTypeDescription.ForGenericArray.Latent.of(genericTypeDescription, 1);
GenericTypeDescription result = GenericTypeDescription.ForGenericArray.Latent.of(typeDescription, 1);
assertThat(result.getSort(), is(TypeDefinition.Sort.GENERIC_ARRAY));
assertThat(result.getComponentType().getSort(), is(TypeDefinition.Sort.GENERIC_ARRAY));
assertThat(result.getComponentType().getComponentType(), is(componentType));
}

@Test
public void testZeroArityIteratesTypesInstanceNonGeneric() throws Exception {
GenericTypeDescription genericTypeDescription = mock(GenericTypeDescription.class);
GenericTypeDescription typeDescription = mock(GenericTypeDescription.class);
GenericTypeDescription componentType = mock(GenericTypeDescription.class);
when(genericTypeDescription.getComponentType()).thenReturn(componentType);
when(genericTypeDescription.getSort()).thenReturn(TypeDefinition.Sort.GENERIC_ARRAY);
when(typeDescription.getComponentType()).thenReturn(componentType);
when(typeDescription.getSort()).thenReturn(TypeDefinition.Sort.GENERIC_ARRAY);
when(componentType.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC);
GenericTypeDescription result = GenericTypeDescription.ForGenericArray.Latent.of(genericTypeDescription, 1);
GenericTypeDescription result = GenericTypeDescription.ForGenericArray.Latent.of(typeDescription, 1);
assertThat(result.getSort(), is(TypeDefinition.Sort.NON_GENERIC));
assertThat(result.getComponentType().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
assertThat(result.getComponentType().getComponentType(), is(componentType));
}

@Test
public void testEquality() throws Exception {
public void testSortIdentification() throws Exception {
assertThat(TypeDefinition.Sort.NON_GENERIC.isNonGeneric(), is(true));
assertThat(TypeDefinition.Sort.WILDCARD.isWildcard(), is(true));
assertThat(TypeDefinition.Sort.PARAMETERIZED.isParameterized(), is(true));
Expand All @@ -80,4 +77,9 @@ public void testEquality() throws Exception {
assertThat(TypeDefinition.Sort.VARIABLE_SYMBOLIC.isSymbolicTypeVariable(), is(true));
assertThat(TypeDefinition.Sort.GENERIC_ARRAY.isGenericArray(), is(true));
}

@Test
public void testObjectProperties() throws Exception {
ObjectPropertyAssertion.of(TypeDefinition.Sort.class).apply();
}
}
@@ -1,5 +1,7 @@
package net.bytebuddy.description.type.generic;

import net.bytebuddy.description.type.TypeDefinition;

import java.util.List;

public class GenericTypeListExplicitTest extends AbstractGenericTypeListTest<GenericTypeDescription> {
Expand Down

0 comments on commit 44076dc

Please sign in to comment.