Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Many methods in the
org.springframework.util.ReflectionUtils
package have been deprecated, but there is no hint which methods should be used as a replacement. How are the following methods to be replaced properly?
org.springframework.util.ReflectionUtils.makeAccessible();
org.springframework.util.ReflectionUtils.isAccessible();
My goal is to get all field names and save them into a Map
. So far the following code worked, but how can I replace the deprecated methods?
ReflectionUtils.doWithFields(object.getClass(), field -> {
if (!field.isAccessible()) ReflectionUtils.makeAccessible(field);
map.put(field.getName(), ReflectionUtils.getField(field, object));
–
Because of module system accessible checks and changes uses now new methods, to make something accessible you should look at trySetAccessible
:
https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/AccessibleObject.html#trySetAccessible()
it returns true/false value instead of exception.
And to check for access use canAccess(object)
:
object - an instance object of the declaring class of this reflected object if it is an instance method or field
https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/AccessibleObject.html#canAccess(java.lang.Object)
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.