From 4a7ca30e2e84e56b30e51c34a12b046e597aa460 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Tue, 12 Jan 2016 00:05:36 +0100 Subject: [PATCH] Added adjustment in type pool to consider interface bound. --- .../java/net/bytebuddy/pool/TypePool.java | 51 ++++++++++++++++++- .../AbstractTypeDescriptionGenericTest.java | 28 +++++----- ...escriptionGenericVariableDefiningTest.java | 14 +++-- .../precompiled/TypeAnnotationSamples.java | 13 ++--- 4 files changed, 80 insertions(+), 26 deletions(-) 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 48bd9240ea5..8cb83feb645 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 @@ -4058,6 +4058,8 @@ protected interface GenericTypeToken { */ Generic toGenericType(TypePool typePool, TypeVariableSource typeVariableSource, String typePath, Map> annotationTokens); + boolean isPrimaryBound(TypePool typePool); + interface OfFormalTypeVariable { Generic toGenericType(TypePool typePool, @@ -4171,6 +4173,11 @@ public Generic toGenericType(TypePool typePool, TypeVariableSource typeVariableS typeDescription); } + @Override + public boolean isPrimaryBound(TypePool typePool) { + throw new IllegalStateException("A primitive type cannot be a type variable bound: " + this); + } + @Override public String toString() { return "TypePool.LazyTypeDescription.GenericTypeToken.ForPrimitiveType." + name(); @@ -4232,6 +4239,11 @@ public Generic toGenericType(TypePool typePool, : annotationTokens); } + @Override + public boolean isPrimaryBound(TypePool typePool) { + throw new IllegalStateException("A wildcard type cannot be a type variable bound: " + this); + } + @Override public String toString() { return "TypePool.LazyTypeDescription.GenericTypeToken.ForUnboundWildcard." + name(); @@ -4915,6 +4927,11 @@ public Generic toGenericType(TypePool typePool, TypeVariableSource typeVariableS typePool.describe(name).resolve()); } + @Override + public boolean isPrimaryBound(TypePool typePool) { + return !typePool.describe(name).resolve().isInterface(); + } + @Override public boolean equals(Object other) { return this == other || !(other == null || getClass() != other.getClass()) && name.equals(((ForRawType) other).name); @@ -4998,6 +5015,11 @@ public Generic toGenericType(TypePool typePool, TypeVariableSource typeVariableS } } + @Override + public boolean isPrimaryBound(TypePool typePool) { + return true; + } + @Override public boolean equals(Object other) { return this == other || !(other == null || getClass() != other.getClass()) && symbol.equals(((ForTypeVariable) other).symbol); @@ -5201,7 +5223,9 @@ protected LazyBoundTokenList(TypePool typePool, @Override public Generic get(int index) { - Map> annotationTokens = this.annotationTokens.get(index); + Map> annotationTokens = this.annotationTokens.get(index + (boundTypeTokens.get(0).isPrimaryBound(typePool) + ? 0 + : 1)); return boundTypeTokens.get(index).toGenericType(typePool, typeVariableSource, EMPTY_TYPE_PATH, @@ -5243,6 +5267,11 @@ public Generic toGenericType(TypePool typePool, TypeVariableSource typeVariableS return new LazyGenericArray(typePool, typeVariableSource, typePath, annotationTokens, componentTypeToken); } + @Override + public boolean isPrimaryBound(TypePool typePool) { + throw new IllegalStateException("A generic array type cannot be a type variable bound: " + this); + } + @Override public boolean equals(Object other) { return this == other || !(other == null || getClass() != other.getClass()) @@ -5323,6 +5352,11 @@ public Generic toGenericType(TypePool typePool, TypeVariableSource typeVariableS return new LazyLowerBoundWildcard(typePool, typeVariableSource, typePath, annotationTokens, boundTypeToken); } + @Override + public boolean isPrimaryBound(TypePool typePool) { + throw new IllegalStateException("A wildcard type cannot be a type variable bound: " + this); + } + @Override public boolean equals(Object other) { return this == other || !(other == null || getClass() != other.getClass()) @@ -5409,6 +5443,11 @@ public Generic toGenericType(TypePool typePool, return new LazyLowerBoundWildcard(typePool, typeVariableSource, typePath, annotationTokens, boundTypeToken); } + @Override + public boolean isPrimaryBound(TypePool typePool) { + throw new IllegalStateException("A wildcard type cannot be a type variable bound: " + this); + } + @Override public boolean equals(Object other) { return this == other || !(other == null || getClass() != other.getClass()) @@ -5499,6 +5538,11 @@ public Generic toGenericType(TypePool typePool, TypeVariableSource typeVariableS return new LazyParameterizedType(typePool, typeVariableSource, typePath, annotationTokens, name, parameterTypeTokens); } + @Override + public boolean isPrimaryBound(TypePool typePool) { + return !typePool.describe(name).resolve().isInterface(); + } + @Override public boolean equals(Object other) { return this == other || !(other == null || getClass() != other.getClass()) @@ -5560,6 +5604,11 @@ public Generic toGenericType(TypePool typePool, return new LazyParameterizedType(typePool, typeVariableSource, typePath, annotationTokens, name, parameterTypeTokens, ownerTypeToken); } + @Override + public boolean isPrimaryBound(TypePool typePool) { + return !typePool.describe(name).resolve().isInterface(); + } + /** * A lazy description of a parameterized type with an owner type. */ 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 1c953833d09..471e411e4af 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 @@ -1249,20 +1249,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(18)); + assertThat(fieldType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(21)); 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(19)); + assertThat(fieldType.getComponentType().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(22)); 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(16)); + assertThat(fieldType.getComponentType().getComponentType().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(19)); 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(17)); + .ofType(typeAnnotation).getValue(value, Integer.class), is(20)); } @Test @@ -1276,7 +1276,7 @@ public void testTypeAnnotationsMethodReturnType() throws Exception { 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(22)); + assertThat(returnType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(25)); } @Test @@ -1290,15 +1290,15 @@ public void testTypeAnnotationsMethodParameterType() throws Exception { 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(24)); + assertThat(parameterType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(27)); 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(25)); + assertThat(parameterType.getComponentType().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(28)); 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(23)); + assertThat(parameterType.getComponentType().getComponentType().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(26)); } @Test @@ -1312,7 +1312,7 @@ public void testTypeAnnotationsSuperType() throws Exception { assertThat(superType.getSort(), is(TypeDefinition.Sort.NON_GENERIC)); assertThat(superType.getDeclaredAnnotations().size(), is(1)); assertThat(superType.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true)); - assertThat(superType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(12)); + assertThat(superType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(15)); } @Test @@ -1326,18 +1326,18 @@ public void testTypeAnnotationsInterfaceType() throws Exception { assertThat(firstInterfaceType.getSort(), is(TypeDefinition.Sort.PARAMETERIZED)); assertThat(firstInterfaceType.getDeclaredAnnotations().size(), is(1)); assertThat(firstInterfaceType.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true)); - assertThat(firstInterfaceType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(13)); + assertThat(firstInterfaceType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(16)); assertThat(firstInterfaceType.getParameters().getOnly().getSort(), is(TypeDefinition.Sort.NON_GENERIC)); assertThat(firstInterfaceType.getParameters().getOnly().getDeclaredAnnotations().size(), is(1)); assertThat(firstInterfaceType.getParameters().getOnly().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true)); - assertThat(firstInterfaceType.getParameters().getOnly().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(14)); + assertThat(firstInterfaceType.getParameters().getOnly().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(17)); TypeDescription.Generic secondInterfaceType = describeInterfaceType(samples, 1); assertThat(secondInterfaceType.getSort(), is(TypeDefinition.Sort.PARAMETERIZED)); assertThat(secondInterfaceType.getDeclaredAnnotations().size(), is(0)); assertThat(secondInterfaceType.getParameters().get(0).getSort(), is(TypeDefinition.Sort.NON_GENERIC)); assertThat(secondInterfaceType.getParameters().get(0).getDeclaredAnnotations().size(), is(1)); assertThat(secondInterfaceType.getParameters().get(0).getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true)); - assertThat(secondInterfaceType.getParameters().get(0).getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(15)); + assertThat(secondInterfaceType.getParameters().get(0).getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(18)); assertThat(secondInterfaceType.getParameters().get(1).getSort(), is(TypeDefinition.Sort.NON_GENERIC)); assertThat(secondInterfaceType.getParameters().get(1).getDeclaredAnnotations().size(), is(0)); } @@ -1353,12 +1353,12 @@ public void testTypeAnnotationExceptionType() throws Exception { assertThat(firstExceptionType.getSort(), is(TypeDefinition.Sort.VARIABLE)); assertThat(firstExceptionType.getDeclaredAnnotations().size(), is(1)); assertThat(firstExceptionType.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true)); - assertThat(firstExceptionType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(26)); + assertThat(firstExceptionType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(29)); TypeDescription.Generic secondExceptionType = describeExceptionType(samples.getDeclaredMethod(FOO, Exception[][].class), 1); assertThat(secondExceptionType.getSort(), is(TypeDefinition.Sort.NON_GENERIC)); assertThat(secondExceptionType.getDeclaredAnnotations().size(), is(1)); assertThat(secondExceptionType.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true)); - assertThat(secondExceptionType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(27)); + assertThat(secondExceptionType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(30)); } @Test 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 84e6c8d9285..ed81b235404 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 @@ -117,12 +117,16 @@ public void testTypeVariableV() throws Exception { 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)); + .getSort(), is(TypeDefinition.Sort.NON_GENERIC)); assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getLowerBounds().getOnly().getUpperBounds().get(0) + .getDeclaredAnnotations().size(), is(0)); + assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getLowerBounds().getOnly().getUpperBounds().get(1) + .getSort(), is(TypeDefinition.Sort.PARAMETERIZED)); + assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getLowerBounds().getOnly().getUpperBounds().get(1) .getDeclaredAnnotations().size(), is(1)); - assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getLowerBounds().getOnly().getUpperBounds().get(0) + assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getLowerBounds().getOnly().getUpperBounds().get(1) .getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true)); - assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getLowerBounds().getOnly().getUpperBounds().get(0) + assertThat(v.getUpperBounds().get(0).getParameters().get(1).getParameters().getOnly().getLowerBounds().getOnly().getUpperBounds().get(1) .getDeclaredAnnotations().getOnly().prepare(typeAnnotation).getValue(value, Integer.class), is(3)); } @@ -135,10 +139,10 @@ public void testMethodVariableT() throws Exception { 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.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(23)); 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)); + assertThat(t.getUpperBounds().getOnly().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(24)); } } 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 b990efb8129..57ddde58eff 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,12 +7,13 @@ public abstract class TypeAnnotationSamples<@TypeAnnotation(0) T, S, @TypeAnnotation(2) U extends String & @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) U>>> - extends @TypeAnnotation(12) Object - implements @TypeAnnotation(13) Callable<@TypeAnnotation(14) Object>, Map<@TypeAnnotation(15) String, Object> { + @TypeAnnotation(7) V extends Map<@TypeAnnotation(8) ? extends @TypeAnnotation(9) String, @TypeAnnotation(10) Callable<@TypeAnnotation(11) ? super @TypeAnnotation(12) U>>, + @TypeAnnotation(13) W extends @TypeAnnotation(14) V> + extends @TypeAnnotation(15) Object + implements @TypeAnnotation(16) Callable<@TypeAnnotation(17) Object>, Map<@TypeAnnotation(18) String, Object> { - @TypeAnnotation(16) Callable<@TypeAnnotation(17) ?> @TypeAnnotation(18) [] @TypeAnnotation(19) [] foo; + @TypeAnnotation(19) Callable<@TypeAnnotation(20) ?> @TypeAnnotation(21) [] @TypeAnnotation(22) [] foo; - abstract <@TypeAnnotation(20) T extends @TypeAnnotation(21) Exception> @TypeAnnotation(22) int foo(@TypeAnnotation(23) T @TypeAnnotation(24) [] @TypeAnnotation(25) [] v) - throws @TypeAnnotation(26) T, @TypeAnnotation(27) RuntimeException; + abstract <@TypeAnnotation(23) T extends @TypeAnnotation(24) Exception> @TypeAnnotation(25) int foo(@TypeAnnotation(26) T @TypeAnnotation(27) [] @TypeAnnotation(28) [] v) + throws @TypeAnnotation(29) T, @TypeAnnotation(30) RuntimeException; }