Constructor
Constructor Manipulation using Mirror.
Reflecting
Reflecting a constructor by arguments (will return null if not found):
Class<T> clazz;
Constructor<T> c = new Mirror().on(clazz).reflect().constructor()
.withArgs(String.class, Object.class);
Reflecting the only constructor of a class (will throw MirrorException if more than one is found):
Class<T> clazz;
Constructor<T> m = new Mirror().on(clazz).reflect().constructor().withAnyArgs();
Reflecting all constructors of a class (will return an empty list if none found):
Class<T> clazz;
List<Constructor<T>> l = new Mirror().on(clazz).reflectAll().constructors();
Reflecting all constructors of a class that matches a Matcher<Constructor> (will return an empty list if none found):
Class<T> clazz;
List<Constructor<T>> l = new Mirror().on(clazz).reflectAll()
.constructors().matching(new YourCustomMatcher());
You can also map your constructors to other types:
Class clazz;
List<String> l = new Mirror().on(clazz).reflectAll()
.constructors().mappingTo(new YourConstructorToStringMapper());
Invoking Constructors
Invoking a constructor:
Class<T> clazz;
T t = new Mirror().on(clazz).invoke().constructor().withoutArgs();
You can also pass a java.lang.reflect.Constructor:
Constructor<T> aConstructor;
Class<T> clazz;
T t = new Mirror().on(clazz).invoke().constructor(aConstructor).withArgs(value1, value2);
Bypassing Constructors
With Mirror, you can completely bypass constructors to create instances.
Class<T> clazz;
T t = new Mirror().on(clazz).invoke().constructor().bypasser();
Please note that this is only supported on Objenesis supported VMs. Take a look at http://code.google.com/p/objenesis/wiki/ListOfCurrentlySupportedVMs .