From 8a9c0d24157e267281bf47ec5bedf74de1218f41 Mon Sep 17 00:00:00 2001 From: Joshua Selbo Date: Wed, 6 Mar 2024 09:10:22 -0800 Subject: [PATCH 1/4] Don't resolve parameterized types for RAW_TYPES mode --- .../description/type/TypeDescription.java | 11 +++++-- ...orTypeVariableBindingWithRawTypesTest.java | 31 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 byte-buddy-dep/src/test/java/net/bytebuddy/description/type/TypeDescriptionGenericVisitorSubstitutorForTypeVariableBindingWithRawTypesTest.java 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 a3ec02597c..087e195e41 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 @@ -1903,9 +1903,14 @@ abstract class Substitutor implements Visitor { */ public Generic onParameterizedType(Generic parameterizedType) { Generic ownerType = parameterizedType.getOwnerType(); - List typeArguments = new ArrayList(parameterizedType.getTypeArguments().size()); - for (Generic typeArgument : parameterizedType.getTypeArguments()) { - typeArguments.add(typeArgument.accept(this)); + List typeArguments; + if (TypeDescription.AbstractBase.RAW_TYPES) { + typeArguments = new ArrayList<>(); + } else { + typeArguments = new ArrayList<>(parameterizedType.getTypeArguments().size()); + for (Generic typeArgument : parameterizedType.getTypeArguments()) { + typeArguments.add(typeArgument.accept(this)); + } } return new OfParameterizedType.Latent(parameterizedType.asRawType().accept(this).asErasure(), ownerType == null diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/TypeDescriptionGenericVisitorSubstitutorForTypeVariableBindingWithRawTypesTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/TypeDescriptionGenericVisitorSubstitutorForTypeVariableBindingWithRawTypesTest.java new file mode 100644 index 0000000000..357fafc717 --- /dev/null +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/TypeDescriptionGenericVisitorSubstitutorForTypeVariableBindingWithRawTypesTest.java @@ -0,0 +1,31 @@ +package net.bytebuddy.description.type; + +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class TypeDescriptionGenericVisitorSubstitutorForTypeVariableBindingWithRawTypesTest { + + @Mock + private TypeDescription.Generic visitedType, parameterizedType, rawType; + + @Test + @SuppressWarnings("unchecked") + public void testParameterizedTypeWithRawTypes() throws Exception { + System.setProperty(TypeDefinition.RAW_TYPES_PROPERTY, "true"); + MockitoAnnotations.initMocks(this); + + when(parameterizedType.asRawType()).thenReturn(rawType); + when(rawType.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(rawType); + when(parameterizedType.getTypeArguments()).thenReturn(new TypeList.Generic.Explicit(TypeDescription.Generic.OfNonGenericType.ForLoadedType.of(Object.class))); + TypeDescription.Generic result = new TypeDescription.Generic.Visitor.Substitutor.ForTypeVariableBinding(visitedType).onParameterizedType(parameterizedType); + + assertThat(result.getTypeArguments(), is(new TypeList.Generic.Explicit())); + } +} From 9c115a9e8ab82660f81c2352a9342c917561717d Mon Sep 17 00:00:00 2001 From: Joshua Selbo Date: Wed, 6 Mar 2024 15:50:04 -0800 Subject: [PATCH 2/4] Use Collections.emptyList() --- .../java/net/bytebuddy/description/type/TypeDescription.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 087e195e41..ddfd1ff99b 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 @@ -1905,7 +1905,7 @@ public Generic onParameterizedType(Generic parameterizedType) { Generic ownerType = parameterizedType.getOwnerType(); List typeArguments; if (TypeDescription.AbstractBase.RAW_TYPES) { - typeArguments = new ArrayList<>(); + typeArguments = Collections.emptyList(); } else { typeArguments = new ArrayList<>(parameterizedType.getTypeArguments().size()); for (Generic typeArgument : parameterizedType.getTypeArguments()) { From c107ef29178dc25db3e3c4e7cdbc1ccba92ff30c Mon Sep 17 00:00:00 2001 From: Joshua Selbo Date: Thu, 7 Mar 2024 10:09:03 -0800 Subject: [PATCH 3/4] Specify matcher generic type for Java <8 --- ...torSubstitutorForTypeVariableBindingWithRawTypesTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/TypeDescriptionGenericVisitorSubstitutorForTypeVariableBindingWithRawTypesTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/TypeDescriptionGenericVisitorSubstitutorForTypeVariableBindingWithRawTypesTest.java index 357fafc717..d4912ef1bb 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/TypeDescriptionGenericVisitorSubstitutorForTypeVariableBindingWithRawTypesTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/TypeDescriptionGenericVisitorSubstitutorForTypeVariableBindingWithRawTypesTest.java @@ -1,13 +1,12 @@ package net.bytebuddy.description.type; +import org.hamcrest.CoreMatchers; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class TypeDescriptionGenericVisitorSubstitutorForTypeVariableBindingWithRawTypesTest { @@ -26,6 +25,6 @@ public void testParameterizedTypeWithRawTypes() throws Exception { when(parameterizedType.getTypeArguments()).thenReturn(new TypeList.Generic.Explicit(TypeDescription.Generic.OfNonGenericType.ForLoadedType.of(Object.class))); TypeDescription.Generic result = new TypeDescription.Generic.Visitor.Substitutor.ForTypeVariableBinding(visitedType).onParameterizedType(parameterizedType); - assertThat(result.getTypeArguments(), is(new TypeList.Generic.Explicit())); + assertThat(result.getTypeArguments(), CoreMatchers.is(new TypeList.Generic.Explicit())); } } From 45e0976d113e843405a4431b0323a3e00b38d228 Mon Sep 17 00:00:00 2001 From: Joshua Selbo Date: Thu, 7 Mar 2024 10:09:57 -0800 Subject: [PATCH 4/4] Actually specify it --- ...isitorSubstitutorForTypeVariableBindingWithRawTypesTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/TypeDescriptionGenericVisitorSubstitutorForTypeVariableBindingWithRawTypesTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/TypeDescriptionGenericVisitorSubstitutorForTypeVariableBindingWithRawTypesTest.java index d4912ef1bb..efae67faf0 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/TypeDescriptionGenericVisitorSubstitutorForTypeVariableBindingWithRawTypesTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/TypeDescriptionGenericVisitorSubstitutorForTypeVariableBindingWithRawTypesTest.java @@ -25,6 +25,6 @@ public void testParameterizedTypeWithRawTypes() throws Exception { when(parameterizedType.getTypeArguments()).thenReturn(new TypeList.Generic.Explicit(TypeDescription.Generic.OfNonGenericType.ForLoadedType.of(Object.class))); TypeDescription.Generic result = new TypeDescription.Generic.Visitor.Substitutor.ForTypeVariableBinding(visitedType).onParameterizedType(parameterizedType); - assertThat(result.getTypeArguments(), CoreMatchers.is(new TypeList.Generic.Explicit())); + assertThat(result.getTypeArguments(), CoreMatchers.is(new TypeList.Generic.Explicit())); } }