XRayInterface is a convenient interface to Java reflection
- not depending on strings
- but on interface conventions
Example
XRayInterface is easy to use:
- determine which private fields or methods needed
- define the public interface you are missing:
-
setX()for a propertyxto change -
getX()for a propertyxto read -
methodX()for a methodmethodXto invoke - bind this interface with XRayInterface to the object you want to access
Setting a private field x of your instance foo
- Define the interface
- take an x-ray snapshot
- and set the property via interface
interface OpenFoo {
void setX(String value);
}OpenFoo openFoo = XRayInterface.xray(foo).to(OpenFoo.class);openFoo.setX("bar");Getting the value of some private field y of your instance foo
- Define the interface
- take an x-ray snapshot
- and get the property via interface
interface OpenFoo {
String getX();
}OpenFoo openFoo = XRayInterface.xray(foo).to(OpenFoo.class);String bar = openFoo.getX();Making a private method boolean m(int value) of foo accessible
- Define the interface
- take an x-ray snapshot
- and get the property via interface
interface OpenFoo {
boolean m(int value);
}OpenFoo openFoo = XRayInterface.xray(foo).to(OpenFoo.class);int bar = openFoo.m("bar");More about XRayInterface
Note that XRayInterface is based on JavaReflection. So there is no reason to blame java reflection.
Yet XRayInterface provides a convenient interface for the most common cases. There are several advantages of using XRayInterface. You can read more here.
Maybe you are interested in some advanced examples: