Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mapping of generic thrown exception types #4813

Merged
merged 4 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private JavaType genericType(GenericsType g, String signature) {
}
}

List<JavaType.FullyQualified> thrownExceptions = null;
List<JavaType> thrownExceptions = null;
if(node.getExceptions() != null) {
for (ClassNode e : node.getExceptions()) {
thrownExceptions = new ArrayList<>(node.getExceptions().length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public JavaType.Primitive primitive(TypeTag tag) {

JavaType returnType = null;
List<JavaType> parameterTypes = null;
List<JavaType.FullyQualified> exceptionTypes = null;
List<JavaType> exceptionTypes = null;

if (selectType instanceof Type.MethodType) {
Type.MethodType methodType = (Type.MethodType) selectType;
Expand All @@ -512,20 +512,8 @@ public JavaType.Primitive primitive(TypeTag tag) {
if (!methodType.thrown.isEmpty()) {
exceptionTypes = new ArrayList<>(methodType.thrown.size());
for (Type exceptionType : methodType.thrown) {
JavaType.FullyQualified javaType = TypeUtils.asFullyQualified(type(exceptionType));
if (javaType == null) {
// if the type cannot be resolved to a class (it might not be on the classpath, or it might have
// been mapped to cyclic)
if (exceptionType instanceof Type.ClassType) {
Symbol.ClassSymbol sym = (Symbol.ClassSymbol) exceptionType.tsym;
javaType = new JavaType.Class(null, Flag.Public.getBitMask(), sym.flatName().toString(), JavaType.Class.Kind.Class,
null, null, null, null, null, null, null);
}
}
if (javaType != null) {
// if the exception type is not resolved, it is not added to the list of exceptions
exceptionTypes.add(javaType);
}
JavaType javaType = type(exceptionType);
exceptionTypes.add(javaType);
}
}
} else if (selectType instanceof Type.UnknownType) {
Expand Down Expand Up @@ -603,7 +591,7 @@ public JavaType.Primitive primitive(TypeTag tag) {
((Type.ForAll) methodSymbol.type).qtype :
methodSymbol.type;

List<JavaType.FullyQualified> exceptionTypes = null;
List<JavaType> exceptionTypes = null;

Type selectType = methodSymbol.type;
if (selectType instanceof Type.ForAll) {
Expand All @@ -615,20 +603,8 @@ public JavaType.Primitive primitive(TypeTag tag) {
if (!methodType.thrown.isEmpty()) {
exceptionTypes = new ArrayList<>(methodType.thrown.size());
for (Type exceptionType : methodType.thrown) {
JavaType.FullyQualified javaType = TypeUtils.asFullyQualified(type(exceptionType));
if (javaType == null) {
// if the type cannot be resolved to a class (it might not be on the classpath, or it might have
// been mapped to cyclic)
if (exceptionType instanceof Type.ClassType) {
Symbol.ClassSymbol sym = (Symbol.ClassSymbol) exceptionType.tsym;
javaType = new JavaType.Class(null, Flag.Public.getBitMask(), sym.flatName().toString(), JavaType.Class.Kind.Class,
null, null, null, null, null, null, null);
}
}
if (javaType != null) {
// if the exception type is not resolved, it is not added to the list of exceptions
exceptionTypes.add(javaType);
}
JavaType javaType = type(exceptionType);
exceptionTypes.add(javaType);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public JavaType.Primitive primitive(TypeTag tag) {

JavaType returnType = null;
List<JavaType> parameterTypes = null;
List<JavaType.FullyQualified> exceptionTypes = null;
List<JavaType> exceptionTypes = null;

if (selectType instanceof Type.MethodType) {
Type.MethodType methodType = (Type.MethodType) selectType;
Expand All @@ -520,20 +520,8 @@ public JavaType.Primitive primitive(TypeTag tag) {
if (!methodType.thrown.isEmpty()) {
exceptionTypes = new ArrayList<>(methodType.thrown.size());
for (Type exceptionType : methodType.thrown) {
JavaType.FullyQualified javaType = TypeUtils.asFullyQualified(type(exceptionType));
if (javaType == null) {
// if the type cannot be resolved to a class (it might not be on the classpath, or it might have
// been mapped to cyclic)
if (exceptionType instanceof Type.ClassType) {
Symbol.ClassSymbol sym = (Symbol.ClassSymbol) exceptionType.tsym;
javaType = new JavaType.Class(null, Flag.Public.getBitMask(), sym.flatName().toString(), JavaType.Class.Kind.Class,
null, null, null, null, null, null, null);
}
}
if (javaType != null) {
// if the exception type is not resolved, it is not added to the list of exceptions
exceptionTypes.add(javaType);
}
JavaType javaType = type(exceptionType);
exceptionTypes.add(javaType);
}
}
} else if (selectType instanceof Type.UnknownType) {
Expand Down Expand Up @@ -590,7 +578,7 @@ public JavaType.Primitive primitive(TypeTag tag) {
} else {
try {
defaultValues = Collections.singletonList(methodSymbol.getDefaultValue().getValue().toString());
} catch(UnsupportedOperationException e) {
} catch (UnsupportedOperationException e) {
// not all Attribute implementations define `getValue()`
}
}
Expand All @@ -611,7 +599,7 @@ public JavaType.Primitive primitive(TypeTag tag) {
((Type.ForAll) methodSymbol.type).qtype :
methodSymbol.type;

List<JavaType.FullyQualified> exceptionTypes = null;
List<JavaType> exceptionTypes = null;

Type selectType = methodSymbol.type;
if (selectType instanceof Type.ForAll) {
Expand All @@ -623,20 +611,8 @@ public JavaType.Primitive primitive(TypeTag tag) {
if (!methodType.thrown.isEmpty()) {
exceptionTypes = new ArrayList<>(methodType.thrown.size());
for (Type exceptionType : methodType.thrown) {
JavaType.FullyQualified javaType = TypeUtils.asFullyQualified(type(exceptionType));
if (javaType == null) {
// if the type cannot be resolved to a class (it might not be on the classpath, or it might have
// been mapped to cyclic)
if (exceptionType instanceof Type.ClassType) {
Symbol.ClassSymbol sym = (Symbol.ClassSymbol) exceptionType.tsym;
javaType = new JavaType.Class(null, Flag.Public.getBitMask(), sym.flatName().toString(), JavaType.Class.Kind.Class,
null, null, null, null, null, null, null);
}
}
if (javaType != null) {
// if the exception type is not resolved, it is not added to the list of exceptions
exceptionTypes.add(javaType);
}
JavaType javaType = type(exceptionType);
exceptionTypes.add(javaType);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ public JavaType.Primitive primitive(TypeTag tag) {

JavaType returnType = null;
List<JavaType> parameterTypes = null;
List<JavaType.FullyQualified> exceptionTypes = null;
List<JavaType> exceptionTypes = null;

if (selectType instanceof Type.MethodType) {
Type.MethodType methodType = (Type.MethodType) selectType;
Expand All @@ -531,20 +531,8 @@ public JavaType.Primitive primitive(TypeTag tag) {
if (!methodType.thrown.isEmpty()) {
exceptionTypes = new ArrayList<>(methodType.thrown.size());
for (Type exceptionType : methodType.thrown) {
JavaType.FullyQualified javaType = TypeUtils.asFullyQualified(type(exceptionType));
if (javaType == null) {
// if the type cannot be resolved to a class (it might not be on the classpath, or it might have
// been mapped to cyclic)
if (exceptionType instanceof Type.ClassType) {
Symbol.ClassSymbol sym = (Symbol.ClassSymbol) exceptionType.tsym;
javaType = new JavaType.Class(null, Flag.Public.getBitMask(), sym.flatName().toString(), JavaType.Class.Kind.Class,
null, null, null, null, null, null, null);
}
}
if (javaType != null) {
// if the exception type is not resolved, it is not added to the list of exceptions
exceptionTypes.add(javaType);
}
JavaType javaType = type(exceptionType);
exceptionTypes.add(javaType);
}
}
} else if (selectType instanceof Type.UnknownType) {
Expand Down Expand Up @@ -622,7 +610,7 @@ public JavaType.Primitive primitive(TypeTag tag) {
((Type.ForAll) methodSymbol.type).qtype :
methodSymbol.type;

List<JavaType.FullyQualified> exceptionTypes = null;
List<JavaType> exceptionTypes = null;

Type selectType = methodSymbol.type;
if (selectType instanceof Type.ForAll) {
Expand All @@ -634,20 +622,8 @@ public JavaType.Primitive primitive(TypeTag tag) {
if (!methodType.thrown.isEmpty()) {
exceptionTypes = new ArrayList<>(methodType.thrown.size());
for (Type exceptionType : methodType.thrown) {
JavaType.FullyQualified javaType = TypeUtils.asFullyQualified(type(exceptionType));
jkschneider marked this conversation as resolved.
Show resolved Hide resolved
if (javaType == null) {
// if the type cannot be resolved to a class (it might not be on the classpath, or it might have
// been mapped to cyclic)
if (exceptionType instanceof Type.ClassType) {
Symbol.ClassSymbol sym = (Symbol.ClassSymbol) exceptionType.tsym;
javaType = new JavaType.Class(null, Flag.Public.getBitMask(), sym.flatName().toString(), JavaType.Class.Kind.Class,
null, null, null, null, null, null, null);
}
}
if (javaType != null) {
// if the exception type is not resolved, it is not added to the list of exceptions
exceptionTypes.add(javaType);
}
JavaType javaType = type(exceptionType);
exceptionTypes.add(javaType);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ public JavaType.Primitive primitive(TypeTag tag) {

JavaType returnType = null;
List<JavaType> parameterTypes = null;
List<JavaType.FullyQualified> exceptionTypes = null;
List<JavaType> exceptionTypes = null;

if (selectType instanceof Type.MethodType) {
Type.MethodType methodType = (Type.MethodType) selectType;
Expand All @@ -514,20 +514,8 @@ public JavaType.Primitive primitive(TypeTag tag) {
if (!methodType.thrown.isEmpty()) {
exceptionTypes = new ArrayList<>(methodType.thrown.size());
for (Type exceptionType : methodType.thrown) {
JavaType.FullyQualified javaType = TypeUtils.asFullyQualified(type(exceptionType));
if (javaType == null) {
// if the type cannot be resolved to a class (it might not be on the classpath, or it might have
// been mapped to cyclic)
if (exceptionType instanceof Type.ClassType) {
Symbol.ClassSymbol sym = (Symbol.ClassSymbol) exceptionType.tsym;
javaType = new JavaType.Class(null, Flag.Public.getBitMask(), sym.flatName().toString(), JavaType.Class.Kind.Class,
null, null, null, null, null, null, null);
}
}
if (javaType != null) {
// if the exception type is not resolved, it is not added to the list of exceptions
exceptionTypes.add(javaType);
}
JavaType javaType = type(exceptionType);
exceptionTypes.add(javaType);
}
}
} else if (selectType instanceof Type.UnknownType) {
Expand Down Expand Up @@ -604,7 +592,7 @@ public JavaType.Primitive primitive(TypeTag tag) {
((Type.ForAll) methodSymbol.type).qtype :
methodSymbol.type;

List<JavaType.FullyQualified> exceptionTypes = null;
List<JavaType> exceptionTypes = null;

Type selectType = methodSymbol.type;
if (selectType instanceof Type.ForAll) {
Expand All @@ -616,20 +604,8 @@ public JavaType.Primitive primitive(TypeTag tag) {
if (!methodType.thrown.isEmpty()) {
exceptionTypes = new ArrayList<>(methodType.thrown.size());
for (Type exceptionType : methodType.thrown) {
JavaType.FullyQualified javaType = TypeUtils.asFullyQualified(type(exceptionType));
if (javaType == null) {
// if the type cannot be resolved to a class (it might not be on the classpath, or it might have
// been mapped to cyclic)
if (exceptionType instanceof Type.ClassType) {
Symbol.ClassSymbol sym = (Symbol.ClassSymbol) exceptionType.tsym;
javaType = new JavaType.Class(null, Flag.Public.getBitMask(), sym.flatName().toString(), JavaType.Class.Kind.Class,
null, null, null, null, null, null, null);
}
}
if (javaType != null) {
// if the exception type is not resolved, it is not added to the list of exceptions
exceptionTypes.add(javaType);
}
JavaType javaType = type(exceptionType);
exceptionTypes.add(javaType);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ default JavaType firstMethodParameter(String methodName) {
return methodType(methodName).getParameterTypes().get(0);
}

@Test
default void declaringTypeRefersToParameterizedClass() {
JavaType.FullyQualified declaringType = methodType("nameShadow").getDeclaringType();
assertThat(declaringType.getTypeParameters())
.describedAs("If it points to the raw class, " +
"method level name shadowing of generic " +
"type variables cannot be detected.")
.isNotEmpty();
}

@Test
default void javaLangObjectHasNoSupertype() {
assertThat(goatType().getSupertype().getSupertype()).isNull();
Expand Down Expand Up @@ -242,4 +252,18 @@ default void recursiveIntersection() {
JavaType.GenericTypeVariable clazz = TypeUtils.asGeneric(firstMethodParameter("recursiveIntersection"));
assertThat(clazz.toString()).isEqualTo("Generic{U extends org.openrewrite.java.JavaTypeGoat$Extension<Generic{U}> & org.openrewrite.java.Intersection<Generic{U}>}");
}

@Test
default void throwsGenericExceptions() {
JavaType.Method method = methodType("throwsGenericException");
JavaType ex = method.getThrownExceptions().get(0);

assertThat(ex).isInstanceOf(JavaType.GenericTypeVariable.class);

JavaType.GenericTypeVariable generic = (JavaType.GenericTypeVariable) ex;
assertThat(generic.getName()).isEqualTo("T");
assertThat(generic.getVariance()).isEqualTo(COVARIANT);
assertThat(TypeUtils.asFullyQualified(generic.getBounds().get(0))
.getFullyQualifiedName()).isEqualTo("java.io.FileNotFoundException");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ private JavaType.Method method(Constructor<?> method, JavaType.FullyQualified de
);
typeCache.put(signature, mappedMethod);

List<JavaType.FullyQualified> thrownExceptions = null;
List<JavaType> thrownExceptions = null;
if (method.getExceptionTypes().length > 0) {
thrownExceptions = new ArrayList<>(method.getExceptionTypes().length);
for (Class<?> e : method.getExceptionTypes()) {
Expand Down Expand Up @@ -451,12 +451,11 @@ private JavaType.Method method(Method method, JavaType.FullyQualified declaringT
);
typeCache.put(signature, mappedMethod);

List<JavaType.FullyQualified> thrownExceptions = null;
List<JavaType> thrownExceptions = null;
if (method.getExceptionTypes().length > 0) {
thrownExceptions = new ArrayList<>(method.getExceptionTypes().length);
for (Class<?> e : method.getExceptionTypes()) {
JavaType.FullyQualified fullyQualified = (JavaType.FullyQualified) type(e);
thrownExceptions.add(fullyQualified);
for (Type e : method.getGenericExceptionTypes()) {
thrownExceptions.add(type(e));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,11 @@ public J visitMethodDeclaration(J.MethodDeclaration method, Integer p) {
// Update method type information to reflect the new checked exceptions
JavaType.Method type = m.getMethodType();
if (type != null) {
List<JavaType.FullyQualified> newThrows = new ArrayList<>();
List<JavaType> newThrows = new ArrayList<>();
List<NameTree> throws_ = (m.getThrows() == null) ? emptyList() : m.getThrows();
for (NameTree t : throws_) {
J.Identifier exceptionIdent = (J.Identifier) t;
newThrows.add((JavaType.FullyQualified) exceptionIdent.getType());
newThrows.add(exceptionIdent.getType());
}
type = type.withThrownExceptions(newThrows);
}
Expand Down
Loading
Loading