I need create a instance of class using reflection. But That class takes interfaces as its constructor param.
Class I want to get instance :
public class CustomerLogic {
private final CustomerRepository customerRepository;
private final SalutationRepository salutationRepository;
@Autowired
public CustomerLogic(CustomerRepository customerRepository, SalutationRepository salutationRepository) {
this.customerRepository = customerRepository;
this.salutationRepository = salutationRepository;
}
}
This is the method i write to get instance of class :
public <T> T getConstructor(String className, Class<T> type){
try {
Constructor<?>[] constructors = new Constructor[0];
Constructor<?> constructor = getConstructor(className, constructors);
List<Object> parameters = new ArrayList<>();
Class<?>[] parameterTypes = constructor.getParameterTypes();
for (Class<?> parameterType : parameterTypes) {
parameters.add(constructor.newInstance(parameterType.getName()));
}
return type.cast(parameters);
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
throw new RuntimeException();
}
}
this method give java.lang.IllegalArgumentException: argument type mismatch
.
Is there any way to do this.
Aucun commentaire:
Enregistrer un commentaire