Java Reflection library that has a function to create NewInstance for any Class with any Constructor [closed]

对着背影说爱祢 提交于 2019-12-25 17:18:44

问题


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


回答1:


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)



来源:https://stackoverflow.com/questions/11488861/java-reflection-library-that-has-a-function-to-create-newinstance-for-any-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!