Skip to content

Commit

Permalink
Fixed up type pool implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Jan 12, 2016
1 parent 96c5501 commit a185ce8
Show file tree
Hide file tree
Showing 6 changed files with 926 additions and 275 deletions.
1,102 changes: 834 additions & 268 deletions byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java

Large diffs are not rendered by default.

Expand Up @@ -1396,11 +1396,11 @@ public void testTypeAnnotationOnNonGenericParameterType() throws Exception {
Class<? extends Annotation> typeAnnotation = (Class<? extends Annotation>) Class.forName(TYPE_ANNOTATION); Class<? extends Annotation> typeAnnotation = (Class<? extends Annotation>) Class.forName(TYPE_ANNOTATION);
MethodDescription.InDefinedShape value = new TypeDescription.ForLoadedType(typeAnnotation).getDeclaredMethods().getOnly(); MethodDescription.InDefinedShape value = new TypeDescription.ForLoadedType(typeAnnotation).getDeclaredMethods().getOnly();
Class<?> samples = Class.forName(TYPE_ANNOTATION_OTHER_SAMPLES); Class<?> samples = Class.forName(TYPE_ANNOTATION_OTHER_SAMPLES);
TypeDescription.Generic oarameterType = describeParameterType(samples.getDeclaredMethod(FOO, Void.class), 0); TypeDescription.Generic parameterType = describeParameterType(samples.getDeclaredMethod(FOO, Void.class), 0);
assertThat(oarameterType.getSort(), is(TypeDefinition.Sort.NON_GENERIC)); assertThat(parameterType.getSort(), is(TypeDefinition.Sort.NON_GENERIC));
assertThat(oarameterType.getDeclaredAnnotations().size(), is(1)); assertThat(parameterType.getDeclaredAnnotations().size(), is(1));
assertThat(oarameterType.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true)); assertThat(parameterType.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(oarameterType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(8)); assertThat(parameterType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(8));
} }


@Test @Test
Expand All @@ -1417,6 +1417,76 @@ public void testTypeAnnotationOnNonGenericExceptionType() throws Exception {
assertThat(exceptionType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(9)); assertThat(exceptionType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(9));
} }


@Test
@JavaVersionRule.Enforce(8)
@SuppressWarnings("unchecked")
public void testTypeAnnotationOnNonGenericArrayType() 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(BAR, Void[][].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(35));
assertThat(returnType.getComponentType().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
assertThat(returnType.getComponentType().getDeclaredAnnotations().size(), is(1));
assertThat(returnType.getComponentType().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(returnType.getComponentType().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(36));
assertThat(returnType.getComponentType().getComponentType().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
assertThat(returnType.getComponentType().getComponentType().getDeclaredAnnotations().size(), is(1));
assertThat(returnType.getComponentType().getComponentType().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(returnType.getComponentType().getComponentType().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(34));
TypeDescription.Generic parameterType = describeParameterType(samples.getDeclaredMethod(BAR, Void[][].class), 0);
assertThat(parameterType.getSort(), is(TypeDefinition.Sort.NON_GENERIC));
assertThat(parameterType.getDeclaredAnnotations().size(), is(1));
assertThat(parameterType.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(parameterType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(38));
assertThat(parameterType.getComponentType().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
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(39));
assertThat(parameterType.getComponentType().getComponentType().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
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(37));
}

@Test
@JavaVersionRule.Enforce(8)
@SuppressWarnings("unchecked")
public void testTypeAnnotationOnNonGenericArrayTypeWithGenericSignature() 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(QUX, Void[][].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(41));
assertThat(returnType.getComponentType().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
assertThat(returnType.getComponentType().getDeclaredAnnotations().size(), is(1));
assertThat(returnType.getComponentType().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(returnType.getComponentType().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(42));
assertThat(returnType.getComponentType().getComponentType().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
assertThat(returnType.getComponentType().getComponentType().getDeclaredAnnotations().size(), is(1));
assertThat(returnType.getComponentType().getComponentType().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(returnType.getComponentType().getComponentType().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(40));
TypeDescription.Generic parameterType = describeParameterType(samples.getDeclaredMethod(QUX, Void[][].class), 0);
assertThat(parameterType.getSort(), is(TypeDefinition.Sort.NON_GENERIC));
assertThat(parameterType.getDeclaredAnnotations().size(), is(1));
assertThat(parameterType.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true));
assertThat(parameterType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(44));
assertThat(parameterType.getComponentType().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
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(45));
assertThat(parameterType.getComponentType().getComponentType().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
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(43));
}

@Test @Test
@JavaVersionRule.Enforce(8) @JavaVersionRule.Enforce(8)
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Expand Down
Expand Up @@ -118,7 +118,10 @@ private TypeDescription.Generic.Builder builder(Type type, TypeDescription.Gener
if (type instanceof TypeVariable) { if (type instanceof TypeVariable) {
return TypeDescription.Generic.Builder.typeVariable(((TypeVariable<?>) type).getName()).annotate(annotationReader.asList()); return TypeDescription.Generic.Builder.typeVariable(((TypeVariable<?>) type).getName()).annotate(annotationReader.asList());
} else if (type instanceof Class) { } else if (type instanceof Class) {
return TypeDescription.Generic.Builder.rawType((Class<?>) type).annotate(annotationReader.asList()); Class<?> rawType = (Class<?>) type;
return (rawType.isArray()
? builder(rawType.getComponentType(), annotationReader.ofComponentType()).asArray()
: TypeDescription.Generic.Builder.rawType((Class<?>) type)).annotate(annotationReader.asList());
} else if (type instanceof GenericArrayType) { } else if (type instanceof GenericArrayType) {
return builder(((GenericArrayType) type).getGenericComponentType(), annotationReader.ofComponentType()).asArray().annotate(annotationReader.asList()); return builder(((GenericArrayType) type).getGenericComponentType(), annotationReader.ofComponentType()).asArray().annotate(annotationReader.asList());
} else if (type instanceof ParameterizedType) { } else if (type instanceof ParameterizedType) {
Expand Down
Expand Up @@ -61,6 +61,15 @@ public void testGenericsObjectProperties() throws Exception {
ObjectPropertyAssertion.of(TypePool.Default.GenericTypeExtractor.ForSignature.OfField.class).applyBasic(); ObjectPropertyAssertion.of(TypePool.Default.GenericTypeExtractor.ForSignature.OfField.class).applyBasic();
} }


@Test
public void testAnnotationRegistrantObjectProperties() throws Exception {
ObjectPropertyAssertion.of(TypePool.Default.AnnotationRegistrant.ForByteCodeElement.class).applyBasic();
ObjectPropertyAssertion.of(TypePool.Default.AnnotationRegistrant.ForByteCodeElement.WithIndex.class).applyBasic();
ObjectPropertyAssertion.of(TypePool.Default.AnnotationRegistrant.ForTypeVariable.class).applyBasic();
ObjectPropertyAssertion.of(TypePool.Default.AnnotationRegistrant.ForTypeVariable.WithIndex.class).applyBasic();
ObjectPropertyAssertion.of(TypePool.Default.AnnotationRegistrant.ForTypeVariable.WithIndex.DoubleIndexed.class).applyBasic();
}

@Test @Test
public void testObjectProperties() throws Exception { public void testObjectProperties() throws Exception {
ObjectPropertyAssertion.of(TypePool.Default.class).apply(); ObjectPropertyAssertion.of(TypePool.Default.class).apply();
Expand Down
Expand Up @@ -40,7 +40,6 @@ public void testIllegalResolutionIsNotResolved() throws Exception {
} }


@Test @Test
@Ignore("Java 8 tests")
public void testGenericTypeTokenObjectPropertiesTest() throws Exception { public void testGenericTypeTokenObjectPropertiesTest() throws Exception {
ObjectPropertyAssertion.of(TypePool.LazyTypeDescription.GenericTypeToken.ForPrimitiveType.class).apply(); ObjectPropertyAssertion.of(TypePool.LazyTypeDescription.GenericTypeToken.ForPrimitiveType.class).apply();
ObjectPropertyAssertion.of(TypePool.LazyTypeDescription.GenericTypeToken.ForRawType.class).apply(); ObjectPropertyAssertion.of(TypePool.LazyTypeDescription.GenericTypeToken.ForRawType.class).apply();
Expand Down
Expand Up @@ -18,4 +18,8 @@ public abstract class TypeAnnotationSamples<@TypeAnnotation(0) T,


abstract <@TypeAnnotation(26) T extends @TypeAnnotation(27) Exception> @TypeAnnotation(28) int foo(@TypeAnnotation(29) T @TypeAnnotation(30) [] @TypeAnnotation(31) [] v) abstract <@TypeAnnotation(26) T extends @TypeAnnotation(27) Exception> @TypeAnnotation(28) int foo(@TypeAnnotation(29) T @TypeAnnotation(30) [] @TypeAnnotation(31) [] v)
throws @TypeAnnotation(32) T, @TypeAnnotation(33) RuntimeException; throws @TypeAnnotation(32) T, @TypeAnnotation(33) RuntimeException;

abstract @TypeAnnotation(34) int @TypeAnnotation(35) [] @TypeAnnotation(36) [] bar(@TypeAnnotation(37) Void @TypeAnnotation(38) [] @TypeAnnotation(39) [] v);

abstract <T> @TypeAnnotation(40) int @TypeAnnotation(41) [] @TypeAnnotation(42) [] qux(@TypeAnnotation(43) Void @TypeAnnotation(44) [] @TypeAnnotation(45) [] v);
} }

0 comments on commit a185ce8

Please sign in to comment.