Simple reflection utility for string->value.
See the code example for a ready-to-run sample.
In this scenario, you have an initialized object, we'll call it the context.
In the context's class, Human, you have a method,
public String getName(), which returns the value of the field you initialized with the object.
Getting the value from
getName() using the utility would be:
Human human = new Human("Bob"); String name = (String) new ReflectionProcessor("$.getName()", human).process(); // Returns "Bob"
$ represents the context. You can also use
this to access it.
In another scenario, you want to execute a static method. No context is needed here.
com.example.foo.MyClass class, we have a static method,
public static void doSomething().
Invoking the method using the utility would be:
If you want to use multiple variables in your string, this utility supports multi-contextuality.
For example, if you have two Strings:
String a = "Hello"; String b = "World";
And you have a static method in
public static void hello(String a, String b);
You can pass the parameters as such:
new ReflectionProcessor("com.example.foo.MyClass.hello($1, $2)", a, b).process();
$1 references the first context, and
$2 the second. Using
$ alone will reference the first context.
- Object-oriented calls
- Static calls
- Fields, methods (with parameters), classes (with package), literals (String, Boolean, Integer, Long)
This is for demonstration purposes, some features you could expect might or might not be present.
Features not included:
- Decimal literals (Double, Float)
- Short, Byte literals