From 87af6dfab00a95afa3fb41c9d3802b83edc93194 Mon Sep 17 00:00:00 2001 From: wuyj Date: Wed, 24 Apr 2024 22:07:52 +0800 Subject: [PATCH 1/2] Fix scanSuperTypes function to return Object Class instead of generic types --- .../ibatis/reflection/TypeParameterResolver.java | 3 +++ .../reflection/TypeParameterResolverTest.java | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/main/java/org/apache/ibatis/reflection/TypeParameterResolver.java b/src/main/java/org/apache/ibatis/reflection/TypeParameterResolver.java index ef1c1da0732..40d9fe408e4 100644 --- a/src/main/java/org/apache/ibatis/reflection/TypeParameterResolver.java +++ b/src/main/java/org/apache/ibatis/reflection/TypeParameterResolver.java @@ -206,6 +206,9 @@ private static Type scanSuperTypes(TypeVariable typeVar, Type srcType, Class< if (declaringClass == parentAsClass) { for (int i = 0; i < parentTypeVars.length; i++) { if (typeVar.equals(parentTypeVars[i])) { + if (parentAsType.getActualTypeArguments()[i] instanceof TypeVariable) { + return Object.class; + } return parentAsType.getActualTypeArguments()[i]; } } diff --git a/src/test/java/org/apache/ibatis/reflection/TypeParameterResolverTest.java b/src/test/java/org/apache/ibatis/reflection/TypeParameterResolverTest.java index d8b4760f219..f46953c7102 100644 --- a/src/test/java/org/apache/ibatis/reflection/TypeParameterResolverTest.java +++ b/src/test/java/org/apache/ibatis/reflection/TypeParameterResolverTest.java @@ -261,6 +261,19 @@ void testReturn_Lv2WildcardList() throws Exception { assertEquals(String.class, wildcard.getUpperBounds()[0]); } + @Test + void testReturn_LV1Map() throws Exception { + Class clazz = Level1Mapper.class; + Method method = clazz.getMethod("selectMap"); + Type result = TypeParameterResolver.resolveReturnType(method, clazz); + assertTrue(result instanceof ParameterizedType); + ParameterizedType paramType = (ParameterizedType) result; + assertEquals(Map.class, paramType.getRawType()); + assertEquals(2, paramType.getActualTypeArguments().length); + assertEquals(String.class, paramType.getActualTypeArguments()[0]); + assertEquals(Object.class, paramType.getActualTypeArguments()[1]); + } + @Test void testReturn_LV2Map() throws Exception { Class clazz = Level2Mapper.class; From 8fb229ef382dcb64e56c5ceccecbf081ddedc761 Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Mon, 29 Apr 2024 03:16:42 +0900 Subject: [PATCH 2/2] Minor refactoring, license year --- .../apache/ibatis/reflection/TypeParameterResolver.java | 8 +++----- .../ibatis/reflection/TypeParameterResolverTest.java | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/apache/ibatis/reflection/TypeParameterResolver.java b/src/main/java/org/apache/ibatis/reflection/TypeParameterResolver.java index 40d9fe408e4..3a7dddcbfed 100644 --- a/src/main/java/org/apache/ibatis/reflection/TypeParameterResolver.java +++ b/src/main/java/org/apache/ibatis/reflection/TypeParameterResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2023 the original author or authors. + * Copyright 2009-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -206,10 +206,8 @@ private static Type scanSuperTypes(TypeVariable typeVar, Type srcType, Class< if (declaringClass == parentAsClass) { for (int i = 0; i < parentTypeVars.length; i++) { if (typeVar.equals(parentTypeVars[i])) { - if (parentAsType.getActualTypeArguments()[i] instanceof TypeVariable) { - return Object.class; - } - return parentAsType.getActualTypeArguments()[i]; + Type actualType = parentAsType.getActualTypeArguments()[i]; + return actualType instanceof TypeVariable ? Object.class : actualType; } } } diff --git a/src/test/java/org/apache/ibatis/reflection/TypeParameterResolverTest.java b/src/test/java/org/apache/ibatis/reflection/TypeParameterResolverTest.java index f46953c7102..7a16f2e8574 100644 --- a/src/test/java/org/apache/ibatis/reflection/TypeParameterResolverTest.java +++ b/src/test/java/org/apache/ibatis/reflection/TypeParameterResolverTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2023 the original author or authors. + * Copyright 2009-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.