0
使用泛型并需要在运行时创建泛型类的实例,所以我试图使用getConstructor()。不幸的是,尽管有正确的签名,我仍然收到了NoSuchMethodException,所以我不知道什么是错的。我会很感激你的建议,所以我可以超越这个问题。 :)我已经提供了CustomerAssembler的构造函数。由于泛型被使用,我需要动态地创建这个类的一个实例。我已经包含了我正在使用的代码片段。其中,我调用getConstructors()来查看构造函数是否存在以及它们的签名。这两个构造函数都存在,而且我使用了正确的签名,所以我不知道为什么我一直在收到这个异常。 Arggg ...希望你会看到我做错了什么。 :)使用getConstructor()在运行时创建Generic类的实例:NoSuchMethodException()
谢谢您的时间和帮助, 迈克
// Here are the constructors for CustomerAssembler.
public CustomerAssembler()
{
super();
}
public CustomerAssembler(
Class<Customer> entityClass,
Class<CustomerPreviewDTO> entityPreviewDTOClass,
Class<CustomerDetailDTO> entityDetailDTOClass,
Class<CustomerUpdateDTO> entityUpdateDTOClass,
EntityManager entityManager)
{
super(entityClass, entityPreviewDTOClass, entityDetailDTOClass, entityUpdateDTOClass, entityManager);
}
这里是个例外:NoSuchMethodException:
java.lang.NoSuchMethodException: assemblers.CustomerAssembler.<init>(entities.Customer, dtos.CustomerPreviewDTO, dtos.CustomerDetailDTO, dtos.CustomerUpdateDTO, javax.persistence.EntityManager)
这里是代码...
try
{
Class<CustomerAssembler> customerAssemblerClass = CustomerAssembler.class;
Constructor<CustomerAssembler>[] lst = (Constructor<CustomerAssembler>[]) this.customerAssemblerClass.getConstructors();
/* See what the signature is for the non-default constructor, so I can make sure that
getConstructor() is configured properly. Here is what was reported in the debugger:
[0] = {[email protected]}"public assemblers.CustomerAssembler()"
[1] = {[email protected]}"public assemblers.CustomerAssembler(java.lang.Class,java.lang.Class,java.lang.Class,java.lang.Class,javax.persistence.EntityManager)"
signature = {[email protected]}"(Ljava/lang/Class<Lentities/Customer;>
Ljava/lang/Class<dtos/CustomerPreviewDTO;>
Ljava/lang/Class<dtos/CustomerDetailDTO;>
Ljava/lang/Class<dtos/CustomerUpdateDTO;>
Ljavax/persistence/EntityManager;)V"
*/
// Configure our constructor call... this.contactAssemblerClass
Constructor<CustomerAssembler> ca =
customerAssemblerClass.getConstructor(
Customer.class,
CustomerPreviewDTO.class,
CustomerDetailDTO.class,
CustomerUpdateDTO.class,
EntityManager.class);
// Create an instance here...
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
catch (InstantiationException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
啊!谢谢,JB!我应该从异常信息中意识到我的错误!我已将代码更改为您推荐的签名,现在它找到了构造函数! :)我不确定在做出这个改变后询问后续问题是否合适,或者我是否应该提出一个新问题......我会问,但如果不合适,请让我知道,并且我将打开一个新的。得到构造函数之后,我需要创建一个实例,所以我完成了以下操作,但是它失败了,出现IllegalArgumentException:参数类型不匹配。 – 2014-10-08 04:39:44
我传递了所需的类,所以我不确定是什么错。我会很感激你的评论。再次感谢您的回应! this.contactAssembler = ca.newInstance( Customer.class, CustomerPreviewDTO.class, CustomerDetailDTO.class, CustomerUpdateDTO.class, EntityManager.class); – 2014-10-08 04:40:48
您的构造函数将4个类作为参数:Customer.class,CustomerPreviewDTO.class,CustomerDetailDTO.class,CustomerUpdateDTO.class。但最后一个参数不是一个类。它是EntityManager的*实例*。 – 2014-10-08 05:50:03