Java Reflection getConstructor method

我与影子孤独终老i 提交于 2019-12-07 01:21:26

问题


Lets say I have classes A and B, where B extends A. I also have a constructor in B with one argument which is of type A. I also have an object of type B called bObj.

Is there a way to call B.class.getConstructor(new Class[] { bObj.getClass() }) and get the constructor since B extends A? At the moment I'm getting a NoSuchMethodException.

Regards, Stan


回答1:


I suggest you have a look at the ConstructorUtils from Apache Commons Lang.
They have all manners of constructor discovery methods.

Your case should be covered by "Matching Accessible Constructors".

Here is an example of the method used in context. Basically, you call it like this:

 private <T> T instantiate(Class<?> input, Object parameter) {
  try {
    Constructor<?> constructor = ConstructorUtils.getMatchingAccessibleConstructor(input, parameter.getClass());
    return (T) constructor.newInstance(parameter);
  } catch (Exception e) {
    //handle various exceptions
  } 
}



回答2:


No, the methods which search for you expect exact parameters. So when you look for a constructor, method or field using find/search methods of the reflection API, the API uses equals() to find matches.

If you need the same logic that the Java compiler would use, you will need to use a framework like FEST Reflect or commons beanutils. Or you must call getConstructors() and write your own filter code.

At first glance, this seems stupid: If the Java compiler can do it, why can't the Reflection API? There are two reasons: First, the Java runtime doesn't need to search which method to call because the compiler already selected the correct method.

The second reason is that the Reflection API was always "second best". It can do everything but the goal was never to make it really easy/friendly to use (at least, that's what I think every time I use it :-)




回答3:


if your constructor is not public (or if it's class is not public), or not accessible from your code scope, it will not be returned through

Class.getConstructor()

try

Class.getDeclaredConstructor()

instead



来源:https://stackoverflow.com/questions/13508720/java-reflection-getconstructor-method

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