avoid to select constructor with parameter of its own type

This commit is contained in:
金戟 2021-04-18 09:45:57 +08:00
parent 6d87653fa2
commit 9c09bf9653

View File

@ -2,6 +2,7 @@ package com.alibaba.testable.core.tool;
import com.alibaba.testable.core.exception.ClassConstructionException; import com.alibaba.testable.core.exception.ClassConstructionException;
import com.alibaba.testable.core.model.TestableNull; import com.alibaba.testable.core.model.TestableNull;
import com.alibaba.testable.core.util.CollectionUtil;
import com.alibaba.testable.core.util.TypeUtil; import com.alibaba.testable.core.util.TypeUtil;
import javax.lang.model.type.NullType; import javax.lang.model.type.NullType;
@ -200,7 +201,7 @@ public class OmniConstructor {
Class<?>[] types = constructor.getParameterTypes(); Class<?>[] types = constructor.getParameterTypes();
if (types.length == 1 && types[0].equals(NullType.class)) { if (types.length == 1 && types[0].equals(NullType.class)) {
return constructor; return constructor;
} else if (types.length < minimalParametersSize) { } else if (types.length < minimalParametersSize && !anyMatch(types, clazz)) {
minimalParametersSize = types.length; minimalParametersSize = types.length;
bestConstructor = constructor; bestConstructor = constructor;
} }
@ -208,4 +209,16 @@ public class OmniConstructor {
return bestConstructor; return bestConstructor;
} }
private static boolean anyMatch(Class<?>[] types, Class<?> clazz) {
for (Class<?> t : types) {
if (clazz.getName().equals(t.getName())) {
return true;
}
if (clazz.getSuperclass() != null && clazz.getSuperclass().getName().equals(t.getName())) {
return true;
}
}
return false;
}
} }