I've been a software developer for more than a year now and I wanted to try and create my own generic factory, to instantiate new objects.
Overall the goal is, to create a generic solution to instantiate different kinds of objects from the Java Collections, with different arguments in the diamonds.
The problem: I'm receiving multiple errors up on calling that method.
java.lang.NoSuchMethodException: java.util.List.<init>(com...AccountDTO)
after I call the method.- Change variable accountDTOs to type
List
, instead ofList<AccountDTO>
warning in the editor.
The code which doesn't work: I've written a brief explanation in a commented line of code
public static <T extends Collection<?>, E> T getInstance(Class<T> t, Class<E> args) {
try {
// This is not working because I can not pass the identifier as a constructor argument
return t.getDeclaredConstructor(args).newInstance(args);
// This is the goal I _kind of_ want to achieve
return new T<E>();
} catch (Exception e) {
e.printStackTrace();
throw new IllegalArgumentException();
}
}
And last but not least, the line of code, which is calling the factory to create a new instance of that object:
List<AccountDTO> accountDTOs = EntityFactory.getInstance(List.class, AccountDTO.class);
Aucun commentaire:
Enregistrer un commentaire