Summary
javac will start to produce compilation errors or warnings for two cases of usages of "effectively-preview" methods and fields:
- when invoking a method or using a field declared in a preview class or interface, but on a receiver whose type is not a preview type
- when directly overriding a method declared in a preview class or interface
Problem
Consider a case where a new preview interface is introduced to JDK, and an existing non-preview class is retrofitted to use it. I.e. a case like:
//JDK types:
@PreviewFeature(...)
public interface NewAPI {
public default void test() {}
}
public class ExistingRetrofittedClass implements NewAPI {}
//user type:
class Test {
void t(ExistingRetrofittedClass c) {
c.test();
}
}
There is currently no error or warning about the invocation of the test
method, as the method itself is not marked as preview, and the receiver is not preview either. (Note that the ExistingRetrofittedClass
is "participating in preview", hence the classfile is not tainted with the preview flag.)
Something similar might happen when implementing or overriding such "effectively" preview methods, like:
//user types:
class Test1 extends ExistingRetrofittedClass {
public void test() {}
}
This also does not produce any error or warning.
Note that if the NewApi.test()
method itself would be marked as a preview feature, then there would be an error (or a warning, depending on the nature of the API and the presence or absence of the --enable-preview
option).
Solution
javac will start to produce compilation errors or warnings (as appropriate based on the nature of the API and the --enable-preview
flag) for methods and fields declared inside a preview class or interface, even if the methods and fields are not themselves marked as preview:
- when the receiver is not of a preview type
- when the given method is overridden. In this case, this will produce the same results as if the method itself would be marked as a preview.
Specification
A new error or warning, as appropriate depending on the nature of the API and the presence or absence of the --enable-preview
flag will be produced in the following scenarios:
- when a field, itself not marked as a preview field, but being enclosed by a preview class or interface, is accessed using a class/interface or instance of type that is not preview
- when a method, itself not marked as a preview method, but being enclosed by a preview class or interface, is invoked with an explicit or implicit receiver of type that is not preview
- when a method, itself not marked as a preview method, but being enclosed by a preview class or interface, is overridden in a class or interface not marked as a preview class or interface
- csr of
-
JDK-8343540 Report preview error for inherited effectively-preview methods
- Resolved