From 09dc1a9d65893107fc43ef2951b8b502eb946b04 Mon Sep 17 00:00:00 2001 From: Joshua Selbo Date: Fri, 8 Mar 2024 04:47:44 -0800 Subject: [PATCH] Don't resolve parameterized types for RAW_TYPES mode (#1604) * Don't resolve parameterized types for RAW_TYPES mode * Use Collections.emptyList() * Specify matcher generic type for Java <8 * Actually specify it --------- Co-authored-by: Joshua Selbo --- .../description/type/TypeDescription.java | 11 +++++-- ...orTypeVariableBindingWithRawTypesTest.java | 30 +++++++++++++++++++ 2 files changed, 38 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..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 @@ -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 = Collections.emptyList(); + } 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..efae67faf0 --- /dev/null +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/description/type/TypeDescriptionGenericVisitorSubstitutorForTypeVariableBindingWithRawTypesTest.java @@ -0,0 +1,30 @@ +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.MatcherAssert.assertThat; +import static org.mockito.ArgumentMatchers.any; +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(), CoreMatchers.is(new TypeList.Generic.Explicit())); + } +}