Different behaviors of Ref_InvokeSpecial

Zeker Zhayard zekerzhayard at gmail.com
Mon Oct 4 09:50:30 UTC 2021


Hi,
JEP 371 describes that to invoke private nestmate instance methods from
code in a hidden class, use invokevirtual or invokeinterface instead of
invokespecial and generated bytecode that uses invokespecial to invoke a
private nestmate instance method will fail verification. invokespecial
should only be used to invoke private nestmate constructors.
However, consider the following use case:

import java.util.function.Supplier;

public class ExampleClass {
    public static void main(String[] args) {
        System.out.println(new SubClass().test());
    }

    public String test() {
        Supplier<String> supplier = this::testPrivate;
        return supplier.get();
    }

    private String testPrivate() {
        return "123";
    }

    public static class SubClass extends ExampleClass {
        public String testPrivate() {
            return "456";
        }
    }
}

1. Compile it with Java 8
2. Modify the access of ExampleClass#testPrivate to protected in bytecode
3. Running in Java 15+ will get "456" rather than "123" in Java 8~14



More information about the valhalla-dev mailing list