Groovy如何能够访问Java类的私有方法? [英] How is Groovy able to access private methods of a Java class?

查看:345
本文介绍了Groovy如何能够访问Java类的私有方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Groovy可以访问Java类的私有方法和变量。 Groovy如何在现场做这件事?是因为使用 MethodHandle 类使用 invokedynamic 字节码指令吗?我认为Java使用 invokespecial 指令来调用 private 方法和 invokevirtual for public 权限访问修饰符?

解决方案

在Java中,所以它希望不直接依赖字节码,它不是使用反射API



有关更多详细信息,请查看 java.lang.reflect ,那么您将看到它在场景后面如何使用Reflection API。


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?

解决方案

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.

这篇关于Groovy如何能够访问Java类的私有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆