diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java b/byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java index 930309eff81..45f8a97acce 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java @@ -1112,20 +1112,20 @@ protected Map> 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>> getIndexedPathMap() { Map>>> doubleIndexPathMap = getDoubleIndexedPathMap(); - Map>> indexedPathMap = doubleIndexPathMap.get(secondIndex); + Map>> indexedPathMap = doubleIndexPathMap.get(preIndex); if (indexedPathMap == null) { indexedPathMap = new HashMap<>(); - doubleIndexPathMap.put(secondIndex, indexedPathMap); + doubleIndexPathMap.put(preIndex, indexedPathMap); } return indexedPathMap; } @@ -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: @@ -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: diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/AbstractTypeDescriptionGenericTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/AbstractTypeDescriptionGenericTest.java index 72291e7a14e..08720f3a163 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/AbstractTypeDescriptionGenericTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/AbstractTypeDescriptionGenericTest.java @@ -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 @@ -1264,11 +1264,11 @@ public void testTypeAnnotationsMethodReturnType() throws Exception { Class typeAnnotation = (Class) 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 @@ -1278,19 +1278,19 @@ public void testTypeAnnotationsMethodParameterType() throws Exception { Class typeAnnotation = (Class) 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") diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/AbstractTypeDescriptionGenericVariableDefiningTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/AbstractTypeDescriptionGenericVariableDefiningTest.java index 383e2c45e35..f9b802c44f1 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/AbstractTypeDescriptionGenericVariableDefiningTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/AbstractTypeDescriptionGenericVariableDefiningTest.java @@ -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; @@ -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 typeAnnotation = (Class) 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 typeAnnotation = (Class) 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)); } } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/test/precompiled/TypeAnnotationSamples.java b/byte-buddy-dep/src/test/java/net/bytebuddy/test/precompiled/TypeAnnotationSamples.java index 7f171186cb3..a3d2c773bd1 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/test/precompiled/TypeAnnotationSamples.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/test/precompiled/TypeAnnotationSamples.java @@ -7,9 +7,12 @@ public abstract class TypeAnnotationSamples<@TypeAnnotation(0) T, S, @TypeAnnotation(2) U extends @TypeAnnotation(3) Callable<@TypeAnnotation(4) ?> & @TypeAnnotation(5) List<@TypeAnnotation(6) ?>, - @TypeAnnotation(7) V extends Map<@TypeAnnotation(8) ? extends @TypeAnnotation(9) String, @TypeAnnotation(10) Callable<@TypeAnnotation(11) ? super @TypeAnnotation(12) T>>> { + @TypeAnnotation(7) V extends Map<@TypeAnnotation(8) ? extends @TypeAnnotation(9) String, @TypeAnnotation(10) Callable<@TypeAnnotation(11) ? super @TypeAnnotation(12) U>>> + extends @TypeAnnotation(12) Object + implements @TypeAnnotation(13) Callable<@TypeAnnotation(14) Object>, Map<@TypeAnnotation(15) String, Object> { - @TypeAnnotation(13) Callable<@TypeAnnotation(14) ?> @TypeAnnotation(15) [] @TypeAnnotation(16) [] foo; + @TypeAnnotation(16) Callable<@TypeAnnotation(17) ?> @TypeAnnotation(18) [] @TypeAnnotation(19) [] foo; - abstract <@TypeAnnotation(17) T extends @TypeAnnotation(18) Object> @TypeAnnotation(19) int foo(@TypeAnnotation(20) T @TypeAnnotation(21) [] @TypeAnnotation(22) [] v); + abstract <@TypeAnnotation(20) T extends @TypeAnnotation(21) Exception> @TypeAnnotation(22) int foo(@TypeAnnotation(23) T @TypeAnnotation(24) [] @TypeAnnotation(25) [] v) + throws @TypeAnnotation(26) T; }