本文介绍了Java 8访问lambda的私有成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出了私有成员访问的答案,而,它是调用网站构建器。如何自定义构建器以控制其访问检查?

points to revealDirect which is part of metafactory call site builder. How can I customize the builder to control its access checks?

更新:每:

因此, lambda表达式只能访问包含lambda表达式的类可访问的方法,因为提供的JVM Lookup 对象将完全反映这些访问权限。

As a consequence, lambda expression can only access methods accessible by the class containing the lambda expression as the JVM provided Lookup object will reflect exactly these access permissions.

这也适用于Java Beans方法,因为它们按照惯例是 public

This also works for Java Beans methods as these are public by convention.

因此,如果您想调用私有方法,您有三种选择:

So if you want to invoke private methods you have three options:


  • 从有权访问它的 private 方法的声明类中生成lambda实例。当这个类调用它将获得适当的查找实例

  • Generate the lambda instance from within the declaring class of the private method which has access to it. When this class calls MethodHandles.lookup() it will get an appropriate Lookup instance

类也可以创建具有所需功能的 Lookup 实例,并将其移交给另一个(可信)类,该类可以使用它来执行此类反射操作。这正是在执行 invokedynamic 指令时隐式发生的情况。包含指向 LambdaMetaFactory 中的引导方法的 invokedynamic 指令的类意味着这种信任。

A class may also create such a Lookup instance with the desired capabilities and hand it over to another (trusted) class which may use it to perform such reflective operations. This is exactly what happens implicitly when an invokedynamic instruction is executed. A class containing invokedynamic instructions pointing to a bootstrap method within LambdaMetaFactory implies such trust.

因此,使用所有普通操作,它始终是具有访问权限的类,必须启用对另一个类的访问

So using all-ordinary operations, its always the class having the access permissions which has to enable the access for another class

这篇关于Java 8访问lambda的私有成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 21:14
查看更多