diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/description/type/TypeDescription.java b/byte-buddy-dep/src/main/java/net/bytebuddy/description/type/TypeDescription.java index 6c85a5b0fdb..5e4fa6cf151 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/description/type/TypeDescription.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/description/type/TypeDescription.java @@ -377,7 +377,7 @@ interface Generic extends TypeDefinition, AnnotatedCodeElement { *
* Returns the owner type of this type. A type's owner type describes a nested type's declaring type. * If it exists, the returned type can be a non-generic or parameterized type. If a class has no - * declaring class, {@code null} is returned. + * declaring type, {@code null} is returned. *
** An owner type is only well-defined for parameterized types ({@link Sort#PARAMETERIZED}), 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 f3e143f3168..817c0a691c8 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 @@ -7088,9 +7088,13 @@ public TypeList.Generic getTypeArguments() { @Override public Generic getOwnerType() { TypeDescription declaringType = typeDescription.getDeclaringType(); - return declaringType == null ? UNDEFINED : declaringType.isGenericDeclaration() - ? new LazyParameterizedReceiverType(declaringType) - : new LazyNonGenericReceiverType(declaringType); + if (declaringType == null) { + return UNDEFINED; + } else { + return !typeDescription.isStatic() && declaringType.isGenericDeclaration() + ? new LazyParameterizedReceiverType(declaringType) + : new LazyNonGenericReceiverType(declaringType); + } } @Override 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 a97b71235c3..43b399e42ff 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 @@ -10,6 +10,7 @@ import static net.bytebuddy.matcher.ElementMatchers.named; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; public abstract class AbstractTypeDescriptionGenericVariableDefiningTest extends AbstractTypeDescriptionGenericTest { @@ -212,6 +213,7 @@ public void testNonGenericTypeAnnotationReceiverTypeOnMethod() throws Exception assertThat(receiverType.getDeclaredAnnotations().size(), is(1)); assertThat(receiverType.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true)); assertThat(receiverType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(0)); + assertThat(receiverType.getOwnerType(), nullValue(TypeDescription.Generic.class)); } @Test @@ -227,6 +229,7 @@ public void testNonGenericTypeAnnotationReceiverTypeOnConstructor() throws Excep assertThat(receiverType.getSort(), is(TypeDefinition.Sort.NON_GENERIC)); assertThat(receiverType.represents(Class.forName(RECEIVER_TYPE_SAMPLE)), is(true)); assertThat(receiverType.getDeclaredAnnotations().size(), is(0)); + assertThat(receiverType.getOwnerType(), nullValue(TypeDescription.Generic.class)); } @Test @@ -246,6 +249,8 @@ public void testNonGenericInnerTypeAnnotationReceiverTypeOnMethod() throws Excep assertThat(receiverType.getDeclaredAnnotations().size(), is(1)); assertThat(receiverType.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true)); assertThat(receiverType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(1)); + assertThat(receiverType.getOwnerType().getSort(), is(TypeDefinition.Sort.NON_GENERIC)); + assertThat(receiverType.getOwnerType().represents(Class.forName(RECEIVER_TYPE_SAMPLE)), is(true)); } @Test @@ -265,6 +270,7 @@ public void testNonGenericInnerTypeAnnotationReceiverTypeOnConstructor() throws assertThat(receiverType.getDeclaredAnnotations().size(), is(1)); assertThat(receiverType.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true)); assertThat(receiverType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(2)); + assertThat(receiverType.getOwnerType(), nullValue(TypeDescription.Generic.class)); } @Test @@ -284,6 +290,8 @@ public void testNonGenericNestedTypeAnnotationReceiverTypeOnMethod() throws Exce assertThat(receiverType.getDeclaredAnnotations().size(), is(1)); assertThat(receiverType.getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true)); assertThat(receiverType.getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(3)); + assertThat(receiverType.getOwnerType().getSort(), is(TypeDefinition.Sort.NON_GENERIC)); + assertThat(receiverType.getOwnerType().represents(Class.forName(RECEIVER_TYPE_SAMPLE)), is(true)); } @Test @@ -299,9 +307,9 @@ public void testNonGenericNestedTypeAnnotationReceiverTypeOnConstructor() throws assertThat(receiverType.getSort(), is(TypeDefinition.Sort.NON_GENERIC)); assertThat(receiverType.represents(Class.forName(RECEIVER_TYPE_SAMPLE)), is(true)); assertThat(receiverType.getDeclaredAnnotations().size(), is(0)); + assertThat(receiverType.getOwnerType(), nullValue(TypeDescription.Generic.class)); } - @Test @SuppressWarnings("unchecked") @JavaVersionRule.Enforce(8) @@ -324,6 +332,8 @@ public void testGenericTypeAnnotationReceiverTypeOnMethod() throws Exception { assertThat(receiverType.getTypeArguments().getOnly().getDeclaredAnnotations().size(), is(1)); assertThat(receiverType.getTypeArguments().getOnly().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true)); assertThat(receiverType.getTypeArguments().getOnly().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(5)); + assertThat(receiverType.getOwnerType().getSort(), is(TypeDefinition.Sort.NON_GENERIC)); + assertThat(receiverType.getOwnerType().represents(Class.forName(RECEIVER_TYPE_SAMPLE)), is(true)); } @Test @@ -338,7 +348,8 @@ public void testGenericTypeAnnotationReceiverTypeOnConstructor() throws Exceptio assertThat(receiverType, notNullValue(TypeDescription.Generic.class)); assertThat(receiverType.getSort(), is(TypeDefinition.Sort.NON_GENERIC)); assertThat(receiverType.asErasure().represents(Class.forName(RECEIVER_TYPE_SAMPLE)), is(true)); - assertThat(receiverType.getDeclaredAnnotations().size(), is(0));; + assertThat(receiverType.getDeclaredAnnotations().size(), is(0)); + assertThat(receiverType.getOwnerType(), nullValue(TypeDescription.Generic.class)); } @Test @@ -373,6 +384,8 @@ public void testGenericInnerTypeAnnotationReceiverTypeOnMethod() throws Exceptio assertThat(receiverType.getOwnerType().getTypeArguments().getOnly().getDeclaredAnnotations().size(), is(1)); assertThat(receiverType.getOwnerType().getTypeArguments().getOnly().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true)); assertThat(receiverType.getOwnerType().getTypeArguments().getOnly().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(7)); + assertThat(receiverType.getOwnerType().getOwnerType().getSort(), is(TypeDefinition.Sort.NON_GENERIC)); + assertThat(receiverType.getOwnerType().getOwnerType().represents(Class.forName(RECEIVER_TYPE_SAMPLE)), is(true)); } @Test @@ -397,6 +410,8 @@ public void testGenericInnerTypeAnnotationReceiverTypeOnConstructor() throws Exc assertThat(receiverType.getTypeArguments().getOnly().getDeclaredAnnotations().size(), is(1)); assertThat(receiverType.getTypeArguments().getOnly().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true)); assertThat(receiverType.getTypeArguments().getOnly().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(11)); + assertThat(receiverType.getOwnerType().getSort(), is(TypeDefinition.Sort.NON_GENERIC)); + assertThat(receiverType.getOwnerType().represents(Class.forName(RECEIVER_TYPE_SAMPLE)), is(true)); } @Test @@ -419,6 +434,8 @@ public void testGenericNestedTypeAnnotationReceiverTypeOnMethod() throws Excepti assertThat(receiverType.getTypeArguments().getOnly().getDeclaredAnnotations().size(), is(1)); assertThat(receiverType.getTypeArguments().getOnly().getDeclaredAnnotations().isAnnotationPresent(typeAnnotation), is(true)); assertThat(receiverType.getTypeArguments().getOnly().getDeclaredAnnotations().ofType(typeAnnotation).getValue(value, Integer.class), is(13)); + assertThat(receiverType.getOwnerType().getSort(), is(TypeDefinition.Sort.NON_GENERIC)); + assertThat(receiverType.getOwnerType().represents(Class.forName(RECEIVER_TYPE_SAMPLE + "$" + GENERIC)), is(true)); } @Test @@ -434,5 +451,7 @@ public void testGenericNestedTypeAnnotationReceiverTypeOnConstructor() throws Ex assertThat(receiverType.getSort(), is(TypeDefinition.Sort.NON_GENERIC)); assertThat(receiverType.asErasure().represents(Class.forName(RECEIVER_TYPE_SAMPLE + "$" + GENERIC)), is(true)); assertThat(receiverType.getDeclaredAnnotations().size(), is(0)); + assertThat(receiverType.getOwnerType().getSort(), is(TypeDefinition.Sort.NON_GENERIC)); + assertThat(receiverType.getOwnerType().represents(Class.forName(RECEIVER_TYPE_SAMPLE)), is(true)); } }