Skip to content

Commit

Permalink
Fix for mybatis#1794 Comparing TypeVariable must be done by equals()
Browse files Browse the repository at this point in the history
  • Loading branch information
harawata authored and piotr-ponikowski-source committed Jan 4, 2020
1 parent 35acc5a commit 02716da
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Expand Up @@ -179,7 +179,7 @@ private static Type scanSuperTypes(TypeVariable<?> typeVar, Type srcType, Class<
}
if (declaringClass == parentAsClass) {
for (int i = 0; i < parentTypeVars.length; i++) {
if (typeVar == parentTypeVars[i]) {
if (typeVar.equals(parentTypeVars[i])) {
return parentAsType.getActualTypeArguments()[i];
}
}
Expand All @@ -202,7 +202,7 @@ private static ParameterizedType translateParentTypeVars(ParameterizedType srcTy
for (int i = 0; i < parentTypeArgs.length; i++) {
if (parentTypeArgs[i] instanceof TypeVariable) {
for (int j = 0; j < srcTypeVars.length; j++) {
if (srcTypeVars[j] == parentTypeArgs[i]) {
if (srcTypeVars[j].equals(parentTypeArgs[i])) {
noChange = false;
newParentArgs[i] = srcTypeArgs[j];
}
Expand Down
Expand Up @@ -26,6 +26,9 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.ibatis.reflection.typeparam.Calculator;
import org.apache.ibatis.reflection.typeparam.Calculator.SubCalculator;
Expand Down Expand Up @@ -429,4 +432,29 @@ class D extends C<Integer> {}
Field field = A.class.getDeclaredField("id");
assertEquals(Integer.class, TypeParameterResolver.resolveFieldType(field, clazz));
}

@Test
void shouldTypeVariablesBeComparedWithEquals() throws Exception {
// #1794
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<Type> futureA = executor.submit(() -> {
Type retType = TypeParameterResolver.resolveReturnType(IfaceA.class.getMethods()[0], IfaceA.class);
return ((ParameterizedType) retType).getActualTypeArguments()[0];
});
Future<Type> futureB = executor.submit(() -> {
Type retType = TypeParameterResolver.resolveReturnType(IfaceB.class.getMethods()[0], IfaceB.class);
return ((ParameterizedType) retType).getActualTypeArguments()[0];
});
assertEquals(AA.class, futureA.get());
assertEquals(BB.class, futureB.get());
executor.shutdown();
}

// @formatter:off
class AA {}
class BB {}
interface IfaceA extends ParentIface<AA> {}
interface IfaceB extends ParentIface<BB> {}
interface ParentIface<T> {List<T> m();}
// @formatter:on
}

0 comments on commit 02716da

Please sign in to comment.