XRayInterface

XrayInterface allows you to adapt foreign classes to your interfaces by automatically mapping property methods to private fields of the adapted instances. XrayInterface also creating Hamcrest-Matchers by deriving them from such interfaces.

Get XRayInterface Github

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 property x to change
    • getX() for a property x to read
    • methodX() for a method methodX to 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
  • interface OpenFoo {
          void setX(String value);
    }
  • take an x-ray snapshot
  • OpenFoo openFoo = XRayInterface.xray(foo).to(OpenFoo.class);
  • and set the property via interface
  • openFoo.setX("bar");

Getting the value of some private field y of your instance foo

  • Define the interface
  • interface OpenFoo {
        String getX();
    }
  • take an x-ray snapshot
  • OpenFoo openFoo = XRayInterface.xray(foo).to(OpenFoo.class);
  • and get the property via interface
  • String bar = openFoo.getX();

Making a private method boolean m(int value) of foo accessible

  • Define the interface
  • interface OpenFoo {
        boolean m(int value);
    }
  • take an x-ray snapshot
  • OpenFoo openFoo = XRayInterface.xray(foo).to(OpenFoo.class);
  • and get the property via interface
  • int bar = openFoo.m("bar");

More about XRayInterface

XRayInterface vs Java Reflection

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.

Authors

Stefan Mandel