Groovy can access private methods and variables of a Java class. How does Groovy do this behind the scene? Is it because of the use of invokedynamic
bytecode instruction which is used by MethodHandle
class? I think Java uses invokespecial
instruction for calling private
methods and invokevirtual
for public
right which respects access modifiers?
相关问题
- 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
You can do this in Java anyway by using reflection, for example, this method will set the value of a private static field...
Notice
field.setAccessible(true)
This can be prevented by having an appropriate Security Manager Policy set up. See How to restrict developers to use reflection to access private methods and constructors in Java?
you can use java.lang.reflect
Groovy is written in Java, so it hopefully doesn't rely on the byte code directly, it doesn't it using the Reflection API.
For more details check for
java.lang.reflect
in the source code, you will then see how it uses the Reflection API behind the scene.