mardi 10 mars 2015

Construct an Object of type determined at runtime?

So I'm trying to test the methods on a certain class (with a default constructor), and the test methods can only be public and of type void, and must start with "test".



public static void main(String[] args) throws ClassNotFoundException {
Class<?> c = null;
c = Class.forName(args[0]);

System.out.println(c.getName());
for (Method m : c.getMethods()) {
if (m.getName().startsWith("test")
&& m.getReturnType().equals(Void.TYPE)
&& m.getModifiers() == Modifier.PUBLIC) {

Object o = null;

try {
o = c.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
System.out.println(ex.getMessage());
}

// ???
}
}
}


So, my checks work, but I do not know how to make a new instance of the class and test the method. How would I do this?






Aucun commentaire:

Enregistrer un commentaire