-
Bug
-
Resolution: Fixed
-
P5
-
None
-
b105
-
Not verified
In JDK 9 b83 MethodHandles.Lookup.findSpecial() throws IllegalAccessException if the target method is static.
Example:
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
public class FindSpecialStaticTest {
public static void m(FindSpecialStaticTest arg) {}
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException {
MethodHandles.lookup().findSpecial(FindSpecialStaticTest.class, "m",
MethodType.methodType(void.class, FindSpecialStaticTest.class), FindSpecialStaticTest.class);
}
}
Output:
Exception in thread "main" java.lang.IllegalAccessException: no such method: FindSpecialStaticTest.m(FindSpecialStaticTest)void/invokeSpecial
at java.lang.invoke.MemberName.makeAccessException(MemberName.java:869)
at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:990)
at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1382)
at java.lang.invoke.MethodHandles$Lookup.findSpecial(MethodHandles.java:1000)
at FindSpecialStaticTest.main(FindSpecialStaticTest.java:8)
Caused by: java.lang.IncompatibleClassChangeError: Expecting non-static method FindSpecialStaticTest.m(LFindSpecialStaticTest;)V
at java.lang.invoke.MethodHandleNatives.resolve(Native Method)
at java.lang.invoke.MemberName$Factory.resolve(MemberName.java:962)
at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:987)
... 3 more
However, findSpecial() Javadoc doesn't mention this case: "IllegalAccessException - if access checking fails or if the method's variable arity modifier bit is set and asVarargsCollector fails"
Other lookup methods mention check of static flag, e.g. findVirtual: "IllegalAccessException - if access checking fails, ___or if the method is static___ or if the method's variable arity modifier bit is set and asVarargsCollector fails"
Example:
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
public class FindSpecialStaticTest {
public static void m(FindSpecialStaticTest arg) {}
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException {
MethodHandles.lookup().findSpecial(FindSpecialStaticTest.class, "m",
MethodType.methodType(void.class, FindSpecialStaticTest.class), FindSpecialStaticTest.class);
}
}
Output:
Exception in thread "main" java.lang.IllegalAccessException: no such method: FindSpecialStaticTest.m(FindSpecialStaticTest)void/invokeSpecial
at java.lang.invoke.MemberName.makeAccessException(MemberName.java:869)
at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:990)
at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1382)
at java.lang.invoke.MethodHandles$Lookup.findSpecial(MethodHandles.java:1000)
at FindSpecialStaticTest.main(FindSpecialStaticTest.java:8)
Caused by: java.lang.IncompatibleClassChangeError: Expecting non-static method FindSpecialStaticTest.m(LFindSpecialStaticTest;)V
at java.lang.invoke.MethodHandleNatives.resolve(Native Method)
at java.lang.invoke.MemberName$Factory.resolve(MemberName.java:962)
at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:987)
... 3 more
However, findSpecial() Javadoc doesn't mention this case: "IllegalAccessException - if access checking fails or if the method's variable arity modifier bit is set and asVarargsCollector fails"
Other lookup methods mention check of static flag, e.g. findVirtual: "IllegalAccessException - if access checking fails, ___or if the method is static___ or if the method's variable arity modifier bit is set and asVarargsCollector fails"
- relates to
-
JDK-8138569 5.4.3.5: Clarify that method handle resolution is static-aware
-
- Closed
-