是在Java方法引用反射的一部分API
Is in Java method reference a part of the reflection API
在 Kotlin 中,方法引用的结果放在包 kotlin.reflect 中。 Java中的method-reference是否也是反射的一部分-api?
有意未指定实现方法引用功能接口的实际类型。重要的是,它是由当前的 JRE 提供的。
实际上,在 HotSpot/OpenJDK 的情况下,它是一个运行时生成的 class 驻留在同一个包和 class 加载上下文作为 class 包含方法参考。它不继承自特殊的 class 并且不实现除其目标类型定义的接口之外的其他接口。因此,虽然这个 class 有一些特殊的属性,即它不能通过名称查找,并且无论其定义的 class 加载器的可达性如何,它都可能被垃圾收集,但它不是 Reflection API.
为方法引用创建的实例的属性由the Java Language Specification指定如下:
The value of a method reference expression is a reference to an instance of a class with the following properties:
The class implements the targeted functional interface type and, if the target type is an intersection type, every other interface type mentioned in the intersection.
Where the method reference expression has type U
, for each non-static
member method m
of U
:
If the function type of U
has a subsignature of the signature of m
, then the class declares an invocation method that overrides m
. The invocation method's body invokes the referenced method, creates a class instance, or creates an array, as described below. If the invocation method's result is not void
, then the body returns the result of the method invocation or object creation, after any necessary assignment conversions (§5.2).
If the erasure of the type of a method being overridden differs in its signature from the erasure of the function type of U
, then before the method invocation or object creation, an invocation method's body checks that each argument value is an instance of a subclass or subinterface of the erasure of the corresponding parameter type in the function type of U
; if not, a ClassCastException
is thrown.
The class overrides no other methods of the functional interface type or other interface types mentioned above, although it may override methods of the Object
class.
在 Kotlin 中,方法引用的结果放在包 kotlin.reflect 中。 Java中的method-reference是否也是反射的一部分-api?
有意未指定实现方法引用功能接口的实际类型。重要的是,它是由当前的 JRE 提供的。
实际上,在 HotSpot/OpenJDK 的情况下,它是一个运行时生成的 class 驻留在同一个包和 class 加载上下文作为 class 包含方法参考。它不继承自特殊的 class 并且不实现除其目标类型定义的接口之外的其他接口。因此,虽然这个 class 有一些特殊的属性,即它不能通过名称查找,并且无论其定义的 class 加载器的可达性如何,它都可能被垃圾收集,但它不是 Reflection API.
为方法引用创建的实例的属性由the Java Language Specification指定如下:
The value of a method reference expression is a reference to an instance of a class with the following properties:
The class implements the targeted functional interface type and, if the target type is an intersection type, every other interface type mentioned in the intersection.
Where the method reference expression has type
U
, for each non-static
member methodm
ofU
:If the function type of
U
has a subsignature of the signature ofm
, then the class declares an invocation method that overridesm
. The invocation method's body invokes the referenced method, creates a class instance, or creates an array, as described below. If the invocation method's result is notvoid
, then the body returns the result of the method invocation or object creation, after any necessary assignment conversions (§5.2).If the erasure of the type of a method being overridden differs in its signature from the erasure of the function type of
U
, then before the method invocation or object creation, an invocation method's body checks that each argument value is an instance of a subclass or subinterface of the erasure of the corresponding parameter type in the function type ofU
; if not, aClassCastException
is thrown.The class overrides no other methods of the functional interface type or other interface types mentioned above, although it may override methods of the
Object
class.