SuppressWarnings on preview features

Alex Buckley alex.buckley at oracle.com
Thu Jan 21 21:47:51 UTC 2021


It further occurs to me that you're using Class::isSealed, which is a 
_reflective_ preview API rather than a _normal_ preview API. JEP 12 has 
a section "Special rules for use of reflective preview APIs" which 
explains, among other things, that use of such APIs is allowed whether 
preview features are enabled or disabled.

Due to the timing of when JEP 12 introduced preview APIs, JDK 15 didn't 
quite support them as first-class preview features. That's why your 
error message says "an API that is part of a preview feature", meaning 
part of the preview language feature called Sealed Classes. JDK 16 does 
a much better job of supporting preview APIs.

Alex

On 1/21/2021 1:29 PM, Alex Buckley wrote:
> Hi Nikita,
> 
> The text you quoted -- "Whether preview features are enabled or 
> disabled, ..." -- refers to use of a preview language feature. You're 
> using Class::isSealed, which is a preview API, not a preview language 
> feature. Use of preview APIs is discussed earlier in JEP 12, in the 
> section "Use of Preview Features":
> 
>> When compiling with preview features enabled, any source code
>> reference to (i) a class or interface declared using a preview
>> language feature, or (ii) a normal preview API, causes a preview
>> warning. This warning is mandated by the JLS so it occurs on all Java
>> compilers; it is not specific to javac. It can be suppressed with
>> @SuppressWarnings("preview").
> Alex
> 
> On 1/21/2021 11:52 AM, Nikita Eshkeev wrote:
>> The JEP 12 https://openjdk.java.net/jeps/12 
>> <https://openjdk.java.net/jeps/12> states that:
>>  > /Whether preview features are enabled or disabled/, |javac| in JDK 
>> $N prints a message if it detects the use of a preview language 
>> feature of Java SE $N in source code. THIS MESSAGE CANNOT BE TURNED 
>> OFF BY USING |@SuppressWarnings| in source code, because developers 
>> must be unfailingly aware of their reliance on the Java SE $N version 
>> of a preview language feature;
>> But consider the following code :
>> // @SuppressWarnings("preview")
>> class Main {
>>   {
>>     String.class.isSealed();
>>   }
>> }
>> The Class#isSealed method is annotated (here is the link 
>> https://github.com/openjdk/jdk/blob/jdk-15%2B36/src/java.base/share/classes/java/lang/Class.java#L4446 
>> <https://github.com/openjdk/jdk/blob/jdk-15%2B36/src/java.base/share/classes/java/lang/Class.java#L4446>) 
>> with @PreviewFeature(feature=PreviewFeature.Feature.SEALED_CLASSES ... 
>> and SEALED_CLASSES is a preview feature in JDK15.
>> When I compile the code with OpenJDK 15 I get the following output:
>>
>> Main.java:4: warning: [preview] isSealed() is an API that is part of a 
>> preview feature
>>                    Main.class.isSealed();
>>                                     ^
>> 1 warning
>> When I comment out the SuppressWarnings annotation and compile the 
>> code again, there is no warnings in the output. Is this a bug?
>> Sincerely,
>> Nikita Eshkeev


More information about the compiler-dev mailing list