samedi 9 décembre 2017

Java Reflection: Creating objects with constructor

Team,

I have an interface as below. Now, users can use the below annotation for their methods. Users can provide different classes which are implementing DClass as part of this annotation. EX: testingClass, which is providing its classpath and is an implementation for DClass.

Now using reflection, I can create an instance of testingClass through getInstance(). But, users can provide any implementation of DClass, EX: They can provide class with some constructor arguments. I have to create that object with any constructor arguments/types for the class passed through this annotation. How do we do that?

@Target({ElementType.METHOD})
public @interface ABC
{
Class<? extends DClass> testingClass() default com.company.class;
}

Thanks!





Aucun commentaire:

Enregistrer un commentaire