Skip to content

Commit

Permalink
Fixed tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Jan 11, 2016
1 parent 8132729 commit 42a2bff
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 28 deletions.
14 changes: 7 additions & 7 deletions byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java
Expand Up @@ -1112,20 +1112,20 @@ protected Map<String, List<LazyTypeDescription.AnnotationToken>> getPathMap() {

protected static abstract class DoubleIndexed extends WithIndex {

private final int secondIndex;
private final int preIndex;

public DoubleIndexed(String descriptor, TypePath typePath, int index, int secondIndex) {
public DoubleIndexed(String descriptor, TypePath typePath, int index, int preIndex) {
super(descriptor, typePath, index);
this.secondIndex = secondIndex;
this.preIndex = preIndex;
}

@Override
protected Map<Integer, Map<String, List<LazyTypeDescription.AnnotationToken>>> getIndexedPathMap() {
Map<Integer, Map<Integer, Map<String, List<LazyTypeDescription.AnnotationToken>>>> doubleIndexPathMap = getDoubleIndexedPathMap();
Map<Integer, Map<String, List<LazyTypeDescription.AnnotationToken>>> indexedPathMap = doubleIndexPathMap.get(secondIndex);
Map<Integer, Map<String, List<LazyTypeDescription.AnnotationToken>>> indexedPathMap = doubleIndexPathMap.get(preIndex);
if (indexedPathMap == null) {
indexedPathMap = new HashMap<>();
doubleIndexPathMap.put(secondIndex, indexedPathMap);
doubleIndexPathMap.put(preIndex, indexedPathMap);
}
return indexedPathMap;
}
Expand Down Expand Up @@ -2574,8 +2574,8 @@ public AnnotationVisitor visitTypeAnnotation(int rawTypeReference, TypePath type
case TypeReference.CLASS_TYPE_PARAMETER_BOUND:
annotationRegistrant = new AnnotationRegistrant.ForTypeVariable.WithIndex.DoubleIndexed(descriptor,
typePath,
typeReference.getTypeParameterBoundIndex() - 1,
typeReference.getTypeParameterIndex(),
typeReference.getTypeParameterBoundIndex(),
typeVariableBoundsAnnotationTokens);
break;
default:
Expand Down Expand Up @@ -3028,8 +3028,8 @@ public AnnotationVisitor visitTypeAnnotation(int rawTypeReference, TypePath type
case TypeReference.METHOD_TYPE_PARAMETER_BOUND:
annotationRegistrant = new ForTypeVariable.WithIndex.DoubleIndexed(descriptor,
typePath,
typeReference.getTypeParameterIndex(),
typeReference.getTypeParameterBoundIndex(),
typeReference.getTypeParameterIndex(),
typeVariableBoundAnnotationTokens);
break;
case TypeReference.METHOD_RETURN:
Expand Down
Expand Up @@ -1241,20 +1241,20 @@ public void testTypeAnnotationsFieldType() throws Exception {
assertThat(fieldType.getSort(), is(TypeDefinition.Sort.GENERIC_ARRAY));
assertThat(fieldType.getDeclaredAnnotations().size(), is(1));
assertThat(fieldType.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(fieldType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(13));
assertThat(fieldType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(18));
assertThat(fieldType.getComponentType().getSort(), is(TypeDefinition.Sort.GENERIC_ARRAY));
assertThat(fieldType.getComponentType().getDeclaredAnnotations().size(), is(1));
assertThat(fieldType.getComponentType().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(fieldType.getComponentType().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(14));
assertThat(fieldType.getComponentType().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(19));
assertThat(fieldType.getComponentType().getComponentType().getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
assertThat(fieldType.getComponentType().getComponentType().getDeclaredAnnotations().size(), is(1));
assertThat(fieldType.getComponentType().getComponentType().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(fieldType.getComponentType().getComponentType().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(11));
assertThat(fieldType.getComponentType().getComponentType().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(16));
assertThat(fieldType.getComponentType().getComponentType().getParameters().getOnly().getSort(), is(TypeDefinition.Sort.WILDCARD));
assertThat(fieldType.getComponentType().getComponentType().getParameters().getOnly().getDeclaredAnnotations().size(), is(1));
assertThat(fieldType.getComponentType().getComponentType().getParameters().getOnly().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(fieldType.getComponentType().getComponentType().getParameters().getOnly().getDeclaredAnnotations()
.ofType(typeAnnotation).getValue(value, Integer.class), is(12));
.ofType(typeAnnotation).getValue(value, Integer.class), is(17));
}

@Test
Expand All @@ -1264,11 +1264,11 @@ public void testTypeAnnotationsMethodReturnType() throws Exception {
Class<? extends Annotation> typeAnnotation = (Class<? extends Annotation>) Class.forName(TYPE_ANNOTATION);
MethodDescription.InDefinedShape value = new TypeDescription.ForLoadedType(typeAnnotation).getDeclaredMethods().getOnly();
Class<?> samples = Class.forName(TYPE_ANNOTATION_SAMPLES);
TypeDescription.Generic returnType = describeReturnType(samples.getDeclaredMethod(FOO, Object[][].class));
TypeDescription.Generic returnType = describeReturnType(samples.getDeclaredMethod(FOO, Exception[][].class));
assertThat(returnType.getSort(), is(TypeDefinition.Sort.NON_GENERIC));
assertThat(returnType.getDeclaredAnnotations().size(), is(1));
assertThat(returnType.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(returnType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(17));
assertThat(returnType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(22));
}

@Test
Expand All @@ -1278,19 +1278,19 @@ public void testTypeAnnotationsMethodParameterType() throws Exception {
Class<? extends Annotation> typeAnnotation = (Class<? extends Annotation>) Class.forName(TYPE_ANNOTATION);
MethodDescription.InDefinedShape value = new TypeDescription.ForLoadedType(typeAnnotation).getDeclaredMethods().getOnly();
Class<?> samples = Class.forName(TYPE_ANNOTATION_SAMPLES);
TypeDescription.Generic parameterType = describeParameterType(samples.getDeclaredMethod(FOO, Object[][].class));
TypeDescription.Generic parameterType = describeParameterType(samples.getDeclaredMethod(FOO, Exception[][].class));
assertThat(parameterType.getSort(), is(TypeDefinition.Sort.GENERIC_ARRAY));
assertThat(parameterType.getDeclaredAnnotations().size(), is(1));
assertThat(parameterType.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(parameterType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(19));
assertThat(parameterType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(24));
assertThat(parameterType.getComponentType().getSort(), is(TypeDefinition.Sort.GENERIC_ARRAY));
assertThat(parameterType.getComponentType().getDeclaredAnnotations().size(), is(1));
assertThat(parameterType.getComponentType().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(parameterType.getComponentType().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(20));
assertThat(parameterType.getComponentType().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(25));
assertThat(parameterType.getComponentType().getComponentType().getSort(), is(TypeDefinition.Sort.VARIABLE));
assertThat(parameterType.getComponentType().getComponentType().getDeclaredAnnotations().size(), is(1));
assertThat(parameterType.getComponentType().getComponentType().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(parameterType.getComponentType().getComponentType().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(18));
assertThat(parameterType.getComponentType().getComponentType().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(23));
}

@SuppressWarnings("unused")
Expand Down
Expand Up @@ -6,6 +6,7 @@

import java.lang.annotation.Annotation;

import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -64,17 +65,80 @@ public void testTypeVariableU() throws Exception {
assertThat(u.getUpperBounds().get(0).getDeclaredAnnotations().size(), is(1));
assertThat(u.getUpperBounds().get(0).getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(u.getUpperBounds().get(0).getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(3));
assertThat(u.getUpperBounds().get(0).getUpperBounds().get(0).getSort(), is(TypeDefinition.Sort.WILDCARD));
assertThat(u.getUpperBounds().get(0).getUpperBounds().get(0).getDeclaredAnnotations().size(), is(1));
assertThat(u.getUpperBounds().get(0).getUpperBounds().get(0).getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(u.getUpperBounds().get(0).getUpperBounds().get(0).getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(4));
assertThat(u.getUpperBounds().get(0).getParameters().get(0).getSort(), is(TypeDefinition.Sort.WILDCARD));
assertThat(u.getUpperBounds().get(0).getParameters().get(0).getDeclaredAnnotations().size(), is(1));
assertThat(u.getUpperBounds().get(0).getParameters().get(0).getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(u.getUpperBounds().get(0).getParameters().get(0).getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(4));
assertThat(u.getUpperBounds().get(1).getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
assertThat(u.getUpperBounds().get(1).getDeclaredAnnotations().size(), is(1));
assertThat(u.getUpperBounds().get(1).getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(u.getUpperBounds().get(1).getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(5));
assertThat(u.getUpperBounds().get(1).getUpperBounds().get(0).getSort(), is(TypeDefinition.Sort.WILDCARD));
assertThat(u.getUpperBounds().get(1).getUpperBounds().get(0).getDeclaredAnnotations().size(), is(1));
assertThat(u.getUpperBounds().get(1).getUpperBounds().get(0).getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(u.getUpperBounds().get(1).getUpperBounds().get(0).getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(6));
assertThat(u.getUpperBounds().get(1).getParameters().get(0).getSort(), is(TypeDefinition.Sort.WILDCARD));
assertThat(u.getUpperBounds().get(1).getParameters().get(0).getDeclaredAnnotations().size(), is(1));
assertThat(u.getUpperBounds().get(1).getParameters().get(0).getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(u.getUpperBounds().get(1).getParameters().get(0).getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(6));
}

@Test
@JavaVersionRule.Enforce(8)
@SuppressWarnings("unchecked")
public void testTypeVariableV() throws Exception {
Class<? extends Annotation> typeAnnotation = (Class<? extends Annotation>) Class.forName(TYPE_ANNOTATION);
MethodDescription.InDefinedShape value = new TypeDescription.ForLoadedType(typeAnnotation).getDeclaredMethods().getOnly();
TypeDescription typeDescription = describe(Class.forName(TYPE_ANNOTATION_SAMPLES));
TypeDescription.Generic v = typeDescription.getTypeVariables().filter(named(V)).getOnly();
assertThat(v.getSort(), is(TypeDefinition.Sort.VARIABLE));
assertThat(v.getDeclaredAnnotations().size(), is(1));
assertThat(v.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(v.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(7));
assertThat(v.getUpperBounds().get(0).getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
assertThat(v.getUpperBounds().get(0).getDeclaredAnnotations().size(), is(0));
assertThat(v.getUpperBounds().get(0).getParameters().get(0).getSort(), is(TypeDefinition.Sort.WILDCARD));
assertThat(v.getUpperBounds().get(0).getParameters().get(0).getDeclaredAnnotations().size(), is(1));
assertThat(v.getUpperBounds().get(0).getParameters().get(0).getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(v.getUpperBounds().get(0).getParameters().get(0).getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(8));
assertThat(v.getUpperBounds().get(0).getParameters().get(0).getUpperBounds().getOnly().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
assertThat(v.getUpperBounds().get(0).getParameters().get(0).getUpperBounds().getOnly().getDeclaredAnnotations().size(), is(1));
assertThat(v.getUpperBounds().get(0).getParameters().get(0).getUpperBounds().getOnly().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(v.getUpperBounds().get(0).getParameters().get(0).getUpperBounds().getOnly().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(9));
assertThat(v.getUpperBounds().get(0).getParameters().get(1).getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
assertThat(v.getUpperBounds().get(0).getParameters().get(1).getDeclaredAnnotations().size(), is(1));
assertThat(v.getUpperBounds().get(0).getParameters().get(1).getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(v.getUpperBounds().get(0).getParameters().get(1).getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(10));
assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getSort(), is(TypeDefinition.Sort.WILDCARD));
assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getDeclaredAnnotations().size(), is(1));
assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getDeclaredAnnotations().ofType(typeAnnotation)
.getValue(value, Integer.class), is(11));
assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getLowerBounds().getOnly().getSort(), is(TypeDefinition.Sort.VARIABLE));
assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getLowerBounds().getOnly().getDeclaredAnnotations().size(), is(1));
assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getLowerBounds().getOnly().getDeclaredAnnotations()
.isAnnotationPresent(typeAnnotation), is(true));
assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getLowerBounds().getOnly().getDeclaredAnnotations()
.ofType(typeAnnotation).getValue(value, Integer.class), is(12));
assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getLowerBounds().getOnly().getUpperBounds().get(0)
.getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getLowerBounds().getOnly().getUpperBounds().get(0)
.getDeclaredAnnotations().size(), is(1));
assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getLowerBounds().getOnly().getUpperBounds().get(0)
.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getLowerBounds().getOnly().getUpperBounds().get(0)
.getDeclaredAnnotations().getOnly().prepare(typeAnnotation).getValue(value, Integer.class), is(3));
}

@Test
public void testMethodVariableT() throws Exception {
Class<? extends Annotation> typeAnnotation = (Class<? extends Annotation>) Class.forName(TYPE_ANNOTATION);
MethodDescription.InDefinedShape value = new TypeDescription.ForLoadedType(typeAnnotation).getDeclaredMethods().getOnly();
MethodDescription methodDescription = describe(Class.forName(TYPE_ANNOTATION_SAMPLES)).getDeclaredMethods().filter(isMethod()).getOnly();
TypeDescription.Generic t = methodDescription.getTypeVariables().getOnly();
assertThat(t.getSort(), is(TypeDefinition.Sort.VARIABLE));
assertThat(t.getDeclaredAnnotations().size(), is(1));
assertThat(t.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(t.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(20));
assertThat(t.getUpperBounds().getOnly().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
assertThat(t.getUpperBounds().getOnly().getDeclaredAnnotations().size(), is(1));
assertThat(t.getUpperBounds().getOnly().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(t.getUpperBounds().getOnly().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(21));
}
}

0 comments on commit 42a2bff

Please sign in to comment.