tiny library for accessing java fields by reflexion on java 7,8,9,10,11,...
Switch branches/tags
Nothing to show
Clone or download
ndeloof adopt simpler mave layout for multi-release jar
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
Latest commit 1090056 Jun 20, 2018

README.md

Fields

Fields is a micro-library intended to help in the transition from java 6,7,8 into java 9,10,11 and later.

About

Many Java frameworks do use and abuse relfexion, including Field#setAccessible to grant access to private fields in java classes. This has been used both to hack JDK APIs and offer some advanced features, or to support field annotations as a popular programming model.

Java 9 deprecates Field#setAccessible and offer as a replacement VarHandle with a better visibility scope regarding SecurityManager. Whenever this was a valuable reason this just breaks most of the java ecosystem :'(

Fields is a tiny library to wrap fields reflective access into a simple static method call. Using a multi-release jar packaging, it will automatically select a java9 compliant implementation when running on a recent JRE.

Usage :

assuming you have class Foo

public class Foo {
    private String bar;
}

and you used to rely on code like this:

        final Foo foo = new Foo();
        Field field = foo.getClass().getDeclaredField("bar");
        field.setAccessible(true);
	field.set(foo, "some value");

just rewrite to adopt :

        final Foo foo = new Foo();
        Fields.write(foo, "bar", "some value");

Same to read field value :

        return Fields.read(foo, "zot");