Skip to content

Commit

Permalink
Don't resolve parameterized types for RAW_TYPES mode (#1604)
Browse files Browse the repository at this point in the history
* 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 <jselbo@meta.com>
  • Loading branch information
jselbo and Joshua Selbo authored Mar 8, 2024
1 parent ddc7698 commit 09dc1a9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1903,9 +1903,14 @@ abstract class Substitutor implements Visitor<Generic> {
*/
public Generic onParameterizedType(Generic parameterizedType) {
Generic ownerType = parameterizedType.getOwnerType();
List<Generic> typeArguments = new ArrayList<Generic>(parameterizedType.getTypeArguments().size());
for (Generic typeArgument : parameterizedType.getTypeArguments()) {
typeArguments.add(typeArgument.accept(this));
List<Generic> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.<TypeList.Generic>is(new TypeList.Generic.Explicit()));
}
}

0 comments on commit 09dc1a9

Please sign in to comment.