Java Reflection calling constructor with primitive types

后端 未结 6 1168
日久生厌
日久生厌 2020-11-29 10:07

I have a method in my test framework that creates an instance of a class, depending on the parameters passed in:

public void test(Object... constructorArgs)          


        
6条回答
  •  醉话见心
    2020-11-29 10:45

    If primitive int value is autoboxed into Integer object, it's not primitive anymore. You can't tell from Integer instance whether it was int at some point.

    I would suggest passing two arrays into test method: one with types and another with values. It'll also remove ambiguity if you have a constructor MyClass(Object) and pass string value (getConstructor would be looking for String constructor).
    Also, you can't tell expected parameter type if parameter value is null.

提交回复
热议问题