Citymonstret / safety-mirror

Type safe Java Reflection

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

safety-mirror

Type safe Java Reflection

Usage

Simply provide a method reference to one of the overloaded methods in the Types class. This will provide you with a java.lang.reflect.Method.

Method m1 = Types.createMethod(Thread::isAlive)  // Get final method
Method m2 = Types.createMethod(String::isEmpty); // Get method from final class
Method m3 = Types.createMethod(BufferedReader::readLine); // Get method that throws checked exception
Method m4 = Types.<String, Class[]>createMethod(getClass()::getDeclaredMethod); //to get vararg method you must specify parameters in generics
Method m5 = Types.<String>createMethod(Class::forName); // to get overloaded method you must specify parameters in generics
Method m6 = Types.createMethod(this::toString); //Works with inherited methods

Notice that you have to provide the method parameters in generics under certain circumstances (When the method is overloaded, or if the method has a varargs parameter).

Note that the project requires Java 8 or above.

About

Type safe Java Reflection

License:Apache License 2.0


Languages

Language:Java 100.0%