I\'m trying to build an automatic testing framework (based on jUnit, but that\'s no important) for my students\' homework. They will have to create constructors for some cla
Here is a generic solution that does not require javassist or another bytecode "manipulator". Although, it assumes that constructors are not doing anything else than simply assigning arguments to corresponding fields, so it simply picks the first constructor and creates an instance with default values (i.e. 0 for int, null for Object etc.).
private T instantiate(Class cls, Map args) throws Exception
{
// Create instance of the given class
final Constructor constr = (Constructor) cls.getConstructors()[0];
final List
P.S. Works with Java 1.5+. The solution also assumes there's no SecurityManager manager that could prevent invocation of f.setAccessible(true).