How can I make reflection work on JDK 16 and later?

 

Reflection is another form of meta-programming, and just as valid, as the type-based parameters that you see in most languages these days. Reflection is powerful and generic, and reflective programs are a high order of maintainability (when used correctly, of course) and more so than purely object-orientated or procedural programs. 

Reflection is fantastic for building tools for developers.

With the advent of Java 9  the approach  to encapsulate has changed and the scary-sounding message appeared:

WARNING: An illegal reflective access operation has occurred 
WARNING: Illegal reflective access by ReflBytecodeName (file:/Users/ben/projects/books/resources/) to method sun.invoke.util.BytecodeName.parseBytecodeName(java.lang.String)
WARNING: Please consider reporting this to the maintainers of ReflBytecodeName 
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations 
WARNING: All illegal access operations will be denied in a future release


How to avoid this message? The simplest way is to avoid deep reflection is never call setAccessible(true).

That should solve the illegal reflective access. So what should we use instead of deep reflection? The best way is use property descriptor to get public methods to read and write the fields.



I created a project based on Java reflection for procedures without using setAccessible to access field values and modify them.


Go to source code

Comments

Popular posts from this blog

Debug Java Stream in Intellij Idea

Creating Efficient Docker Images with Spring Boot 2.3

CRUD Goes Even Easier With JPABuddy