Allow me to introduce my latest project, Mirror. Mirror is a reflection library for Java designed to take advantage of the streams, lambdas, and optionals introduced in Java 8.
The source code is publicly available on GitHub under the MIT license and the JavaDocs are viewable here.
// get the fieldOptional<MirrorField>optional=Mirror.of(Test.class).field("name");// unwrap the optionalMirrorFieldfield=optional.get();// get the value of the field// we pass null as the instance because the field is staticfield.get(null);// "Mirror"
Setting Fields
1
2
3
4
5
6
7
// get the fieldOptional<MirrorField>optional=Mirror.of(Test.class).field("author");// unwrap the optionalMirrorFieldfield=optional.get();// set the value of the field// we once again pass null as the instance because the field is staticfield.set(null,"Shadowfacts");
Invoking Methods
1
2
3
4
5
6
// get the method using the name and the types of the arguments it acceptsOptional<MirrorMethod>optional=Mirror.of(Test.class).method("reverse",String.class);// unwrap the optionalMirrorMethodmethod=optional.get();// invoke the methodmethod.invoke(null,"Mirror");// "rorriM";
Class Streams
1
2
3
Mirror.ofAllUnwrapped(Test.class,Test2.class)// create the stream of classes.unwrap()// map the MirrorClasses to their Java versions.toArray();// [Test.class, Test2.class]
Field Streams
1
2
3
4
Mirror.ofAllUnwrapped(Test.class,Test2.class)// create the stream of classes.flatMapToFields()// flat map the classes to their fields.get(null)// get the value of the fields on null.toArray();// ["Mirror", "Shadowfacts", "Tesst 2"]
Method Streams
1
2
3
4
5
Mirror.ofAllUnwrapped(Test.class,Test2.class)// create the stream of classes.flatMapToMethods()// flat map the classes to their methods.filter(m->Arrays.equals(m.parameterTypes(),newMirrorClass<?>[]{Mirror.of(String.class)}))// filter the methods by which accept only a String.invoke(null,"Shadowfacts")// invoke them all on nothing, passing in "Shadowfacts".toArray();// ["stcafwodahS"]