mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 17:18:24 +08:00
feat: ClassUtil.newInstance() add create from factory method support
This commit is contained in:
parent
b8b2d1bf21
commit
6d50a6228b
@ -138,6 +138,16 @@ public class ClassUtil {
|
|||||||
}
|
}
|
||||||
return (T) otherConstructor.newInstance(parameters);
|
return (T) otherConstructor.newInstance(parameters);
|
||||||
}
|
}
|
||||||
|
// 没有任何构造函数的情况下,去查找 static 工厂方法,满足 lombok 注解的需求
|
||||||
|
else {
|
||||||
|
Method factoryMethod = ClassUtil.getFirstMethod(clazz, m -> m.getParameterCount() == 0
|
||||||
|
&& clazz.isAssignableFrom(m.getReturnType())
|
||||||
|
&& Modifier.isPublic(m.getModifiers())
|
||||||
|
&& Modifier.isStatic(m.getModifiers()));
|
||||||
|
if (factoryMethod != null) {
|
||||||
|
return (T) factoryMethod.invoke(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
throw new IllegalArgumentException("the class \"" + clazz.getName() + "\" has no constructor.");
|
throw new IllegalArgumentException("the class \"" + clazz.getName() + "\" has no constructor.");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("Can not newInstance class: " + clazz.getName());
|
throw new RuntimeException("Can not newInstance class: " + clazz.getName());
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user