Java Reflection library that has a function to cre

2019-09-21 08:13发布

I am looking for a Java library that provides a function like this, invokeConstructor. (don't want to import clojure.lang)

1条回答
趁早两清
2楼-- · 2019-09-21 08:41

apache commons-beanutils:

Object args[] = ...;
Class<?> argTypes[] = ...;
Object obj = ConstructorUtils.invokeConstructor(clazz, args, argTypes);
Object obj = ConstructorUtils.invokeExactConstructor(clazz, args, argTypes);

The big difference between "invokeConstructor" and "invokeExactConstructor" is that the former will find a type assignment compatible constuctor, while the latter will match only the exact argument types you've supplied. (see java.lang.Class.isAssignableFrom)

查看更多
登录 后发表回答