Skip to content

Latest commit

 

History

History
23 lines (18 loc) · 1.46 KB

README.md

File metadata and controls

23 lines (18 loc) · 1.46 KB

Java Reflection Compatible Layer

Compatible layer for java.lang.reflect API which could run on Java 6+ (which meant fully support Android) and bypass the strong encapsulation in Java 16+.

Implementation details

Field access

The strong encapsulation has no effect for sun.misc.Unsafe, I use this API to access fields, it works perfectly.

Method invocation

Since Java 7+, java.lang.invoke API has been added to the JRE, which could be a replacement for java.lang.reflect API.
It has an internal field: java.lang.invoke.MethodHandles$Lookup.IMPL_LOOKUP, which marked as "trusted", that could invoke any method without accessibility check.
After obtain this field using sun.misc.Unsafe, I use this field to invoke methods, it also works perfectly.

Comparison

Narcissus is also an open-source, MIT-licensed library to bypass the strong encapsulation, supports Java 7+, depends on JNI.
Compare to Narcissus, this library is pure Java, means you don't need to compile and load the JNI libraries for a new platform, and supports any Java 6+ compatible runtime environment.

Usage

Just copy the source code to your project and use the API.

JavaDoc
Examples

License

MIT