Skip to content

Commit

Permalink
Added guard for type pool receiver type resolution to emitt parameter…
Browse files Browse the repository at this point in the history
…ized owner types for static inner types.
  • Loading branch information
raphw committed Apr 25, 2016
1 parent f24958a commit 7f27e69
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ interface Generic extends TypeDefinition, AnnotatedCodeElement {
* <p>
* 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.
* </p>
* <p>
* An owner type is only well-defined for parameterized types ({@link Sort#PARAMETERIZED}),
Expand Down
10 changes: 7 additions & 3 deletions byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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));
}
}

0 comments on commit 7f27e69

Please sign in to comment.