使用getConstructor()在运行时创建Generic类的实例:NoSuchMethodException()

时间:2014-10-07 18:04:44

标签: java generics reflection getconstructor

我正在使用泛型,需要在运行时创建泛型类的实例,所以我尝试使用getConstructor()。不幸的是,尽管有正确的签名,我收到NoSuchMethodException,所以我不知道出了什么问题。我将非常感谢您的建议,以便我能够解决这个问题。 :)我已经为CustomerAssembler提供了构造函数。由于使用了泛型,我需要动态创建此类的实例。我已经包含了我正在使用的代码片段。在其中,我调用了getConstructors()来查看构造函数是否存在以及它们的签名。两个构造函数都存在,我使用了正确的签名,所以我不知道为什么我会一直得到这个例外。 Arggg ...希望你会看到我做错了什么。 :)

感谢您的时间和帮助, 麦克

 // 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] = {java.lang.reflect.Constructor@10796}"public assemblers.CustomerAssembler()"

           [1] = {java.lang.reflect.Constructor@10797}"public assemblers.CustomerAssembler(java.lang.Class,java.lang.Class,java.lang.Class,java.lang.Class,javax.persistence.EntityManager)"
     signature = {java.lang.String@10802}"(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();
    }

1 个答案:

答案 0 :(得分:1)

customerAssemblerClass.getConstructor(
                    Customer.class,
                    CustomerPreviewDTO.class,
                    CustomerDetailDTO.class,
                    CustomerUpdateDTO.class,
                    EntityManager.class);

这将查找具有以下签名的构造函数:

CustomerAssemble(Customer c,
                 CustomerPreviewDTO cpDTO, 
                 CustomerDetailDTO cdDTO, 
                 CustomerUpdateDTO cuDTO, 
                 EntityManager em)

您的构造函数不会将其作为参数。它需要Class的4个实例和EntityManager的实例。

所以代码应该是

customerAssemblerClass.getConstructor(
                    Class.class,
                    Class.class,
                    Class.class,
                    Class.class,
                    EntityManager.class);