JDK-8282823 : javac should constrain more uses of preview APIs
  • Type: Enhancement
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 18
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2022-03-08
  • Updated: 2022-04-25
  • Resolved: 2022-04-25
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 19
19 masterFixed
Related Reports
CSR :  
Description
Panama will preview an instance method in a well-known abstract class in java.*. The implementation of the preview method just throws, so that clients which already subclass the abstract class are not broken, but new Panama-aware clients will want to override the preview method.

Overriding a preview method is akin to invoking it -- neither should be allowed if preview features are disabled. I have updated JEP 12 to clarify this point: "When compiling with preview features disabled, any source code reference to ... (iii) a normal preview API (whether referenced by name, or invoked, or overridden), causes a compile-time error."

At present, with preview features disabled, javac allows a preview method to be overridden without warning or error. In the code below, javac is silent at 1 and 2, but rightly gives an error at 3. There should be errors at 1, 2, and 3. With preview features enabled, there should be preview warnings at 1, 2, and 3.

class Test {
    public static void main(String[] args) {
        // Assume Iterable declares a preview instance method `mumble`
        class MyIterable implements Iterable {
            public java.util.Iterator iterator() { return null; }
            @Override public void mumble() { }  // 1
        }
        MyIterable it = null;
        it.mumble();  // 2
        Iterable it2 = null;
        it2.mumble();  // 3
    }
}

Comments
Changeset: 4c22a9bc Author: Jan Lahoda <jlahoda@openjdk.org> Date: 2022-04-25 09:10:44 +0000 URL: https://git.openjdk.java.net/jdk/commit/4c22a9bcf9a694f7a08e0e8fa8eb5e915095cec6
25-04-2022

A pull request was submitted for review. URL: https://git.openjdk.java.net/jdk/pull/7774 Date: 2022-03-10 12:28:27 +0000
10-03-2022