How can I access an inherited protected field from an object by reflection?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
If you are just getting the protected field
If you are using Eclipse Ctrl + Space will bring up a list of methods when you type a "." after the object
Do you perhaps mean from a different object an untrusted context with a
SecurityManager
set? That would break the type system, so you can't. From a trusted context, you can callsetAccessible
to defeat the type system. Ideally, don't use reflection.Use
FieldUtils.writeField(object, "fieldname", value, true)
orreadField(object, "fieldname", true)
from Apache Commons lang3.I didn't want to drag in more libraries so I made a pure one that worked for me. It is an extension of one of the methods from jweyrich:
You could do something like...
You might also need to change the accessibility, as noted in Maurice's answer.