Skip to content

Commit

Permalink
Merge pull request #4362 from jlerbsc/master
Browse files Browse the repository at this point in the history
Fix issue 4345 Strange error when trying to find erasure of generic t…
  • Loading branch information
jlerbsc committed Apr 2, 2024
2 parents dabaf7a + eed85f0 commit 4436fbd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
*/
package com.github.javaparser.resolution.types;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.resolution.MethodUsage;
import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration;
Expand All @@ -33,10 +37,6 @@
import com.github.javaparser.resolution.types.parametrization.ResolvedTypeParametrized;
import com.github.javaparser.utils.Pair;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* A ReferenceType like a class, an interface or an enum. Note that this type can contain also the values
* specified for the type parameters.
Expand Down Expand Up @@ -575,18 +575,7 @@ public ResolvedType erasure() {
}

private List<ResolvedType> erasureOfParamaters(ResolvedTypeParametersMap typeParametersMap) {
List<ResolvedType> erasedParameters = new ArrayList<ResolvedType>();
if (!typeParametersMap.isEmpty()) {
// add erased type except java.lang.object
List<ResolvedType> parameters = typeParametersMap.getTypes().stream()
.filter(type -> !type.isReferenceType())
.map(type -> type.erasure())
.filter(erasedType -> !(isJavaObject(erasedType)))
.filter(erasedType -> erasedType != null)
.collect(Collectors.toList());
erasedParameters.addAll(parameters);
}
return erasedParameters;
return new ArrayList<ResolvedType>();
}

private boolean isJavaObject(ResolvedType rt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@

package com.github.javaparser.symbolsolver.model.typesystem;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.io.Serializable;
Expand All @@ -39,7 +44,9 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.github.javaparser.*;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.resolution.TypeSolver;
Expand Down Expand Up @@ -811,7 +818,7 @@ void testDeclaredFields() {
parserConfiguration.setSymbolResolver(new JavaSymbolSolver(typeSolver));

CompilationUnit cu = new JavaParser(parserConfiguration)
.parse(ParseStart.COMPILATION_UNIT, new StringProvider(code)).getResult().get();
.parse(code).getResult().get();

ClassOrInterfaceDeclaration classA = cu.getClassByName("A").get();
ClassOrInterfaceDeclaration classB = cu.getClassByName("B").get();
Expand All @@ -837,7 +844,7 @@ void testGetAllFieldsVisibleToInheritors() {
parserConfiguration.setSymbolResolver(new JavaSymbolSolver(typeSolver));

CompilationUnit cu = new JavaParser(parserConfiguration)
.parse(ParseStart.COMPILATION_UNIT, new StringProvider(code)).getResult().get();
.parse(code).getResult().get();

ClassOrInterfaceDeclaration classA = cu.getClassByName("A").get();
ClassOrInterfaceDeclaration classB = cu.getClassByName("B").get();
Expand Down Expand Up @@ -876,10 +883,28 @@ void erasure_rawtype() {
assertEquals(expected, erasedType.describe());
}

@Test
// The erasure of a parameterized type with bound.
void erasure_type_variable() {
List<ResolvedType> types = declaredTypes(
"class A<T extends Number> {}");
ResolvedType rt = types.get(0);
String expected = "A";
assertEquals(expected, rt.erasure().describe());
}

@Test
// The erasure of a parameterized type
void erasure_parametrizedType() {
ResolvedType parametrizedType = genericType(Map.class.getCanonicalName(), Integer.class.getCanonicalName(), Integer.class.getCanonicalName());
String expected = "java.util.Map";
assertEquals(expected, parametrizedType.erasure().describe());
}

@Test
// The erasure of an array type T[] is |T|[].
void erasure_arraytype() {
// create a type : List <String>
// create a type : List <String>[]
ResolvedType genericList = array(genericType(List.class.getCanonicalName(), String.class.getCanonicalName()));
String expected = "java.util.List[]";
assertEquals(expected, genericList.erasure().describe());
Expand All @@ -888,21 +913,20 @@ void erasure_arraytype() {
@Test
// The erasure of an array type T[] is |T|[].
void erasure_arraytype_with_bound() {
// create a type : List <T extends Serializable>
// create a type : List <T extends Serializable>[]
ResolvedTypeVariable typeArguments = parametrizedType("T", Serializable.class.getCanonicalName());
ResolvedType genericList = array(genericType(List.class.getCanonicalName(), typeArguments));
String expected = "java.util.List<java.io.Serializable>[]";
String expected = "java.util.List[]";
assertEquals(expected, genericList.erasure().describe());
}

@Test
// The erasure of a type variable (§4.4) is the erasure of its leftmost bound.
void erasure_type_variable() {
List<ResolvedType> types = declaredTypes(
"class A<T extends Number> {}");
ResolvedType rt = types.get(0);
String expected = "A<java.lang.Number>";
assertEquals(expected, rt.erasure().describe());
// The erasure of T extends Serializable is the erasure of its leftmost bound.
void erasure_bounded_type_parameter() {
// create a type : T extends Serializable
ResolvedTypeVariable typeArguments = parametrizedType("T", Serializable.class.getCanonicalName());
String expected = "java.io.Serializable";
assertEquals(expected, typeArguments.erasure().describe());
}

@Test
Expand Down

0 comments on commit 4436fbd

Please sign in to comment.