allow constructor with parameter of same type as class under constructing

This commit is contained in:
金戟 2021-04-24 18:46:04 +08:00
parent 4f3e3f0093
commit 8ebe2a5d0c

View File

@ -204,7 +204,7 @@ public class OmniConstructor {
} else {
Object[] args = new Object[types.length];
for (int i = 0; i < types.length; i++) {
args[i] = newInstance(types[i], classPool, level + 1);
args[i] = types[i].equals(clazz) ? null : newInstance(types[i], classPool, level + 1);
}
return constructor.newInstance(args);
}
@ -217,7 +217,7 @@ public class OmniConstructor {
Class<?>[] types = constructor.getParameterTypes();
if (types.length == 1 && types[0].equals(Void.class)) {
return constructor;
} else if (types.length < minimalParametersSize && !anyMatch(types, clazz)) {
} else if (types.length < minimalParametersSize) {
minimalParametersSize = types.length;
bestConstructor = constructor;
}
@ -225,16 +225,4 @@ public class OmniConstructor {
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;
}
}