public <U> U genericFactory(Constructor<U> classConstructor, Object..args)
throws
InstantiationException,
IllegalAccessException,
IllegalArgumentException,
InvocationTargetException {
return classConstructor.newInstance(args);
}
You can get the constructor from the object Class<U>using the method getConstructors. Using the constructor itself, you can get information about the arguments, so additional code outside the factory must be added to fill out the corresponding arguments.
Obviously, this is just as ugly as Peter answered.
source
share