PreviewFeature on packages and modules

Jan Lahoda jan.lahoda at oracle.com
Wed Feb 3 21:30:09 UTC 2021


On 03. 02. 21 18:54, Alex Buckley wrote:

> It looks like your neshkeev/jdk repo was forked from openjdk/jdk very 
> recently, so your compiler should have the latest support for preview 
> APIs. That support is specified at 
> https://bugs.openjdk.java.net/browse/JDK-8249554 -- when preview 
> features are enabled/disabled, no warning/error is due if "The 
> declaration where the use of a preview API element appears is within 
> the same module as the declaration of the preview API element."  This 
> rule reflects the JEP 12 policy you refer to, where source code in the 
> same module as a preview API is deemed as "participating" and causes 
> no warnings/errors.
>
> I can't tell where your Main.java file lives at compile time, but 
> presumably you are not accidentally placing it in the jdk.neshkeev 
> module. Like you, I expect a warning/error for Main's reference to 
> MyPreviewClass, since Main is not participating in your preview API.


One thing to note here is e.g. @Deprecated on a package does not 
automatically deprecate the classes belonging to the given package. 
Here, we have a package annotated with @PreviewFeature, but not a class 
annotated as a preview feature, and the preview flag is not propagated 
from packages to classes, similarly to the deprecated flag. But we can 
surely make this particular flag to propagate to the classes in the 
package, if desired.

Jan

>
> Alex
>
> On 2/3/2021 8:00 AM, Nikita Eshkeev wrote:
>> The PreviewFeature 
>> (https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/jdk/internal/javac/PreviewFeature.java 
>> <https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/jdk/internal/javac/PreviewFeature.java>) 
>> annotation can be applied to modules and packages. But I don't get 
>> any compile-time errors/warnings when I try to use code that is 
>> inside of either a package or a module that is annotated with 
>> PreviewFeature.
>> What I did
>> 1. I added a new module to openjdk with the "jdk.neshkeev" name
>> 2. I added module-info.java, package-info.java and 
>> MyPreviewClass.java (which is just an empty class)
>> 3. I annotated both module-info.java and package-info.java with 
>> PreviewFeature
>> 4. I exported jdk.internal.javac package to jdk.neshkeev in 
>> module-info.java of java.base
>> 5. `make images` the jdk's source code
>> 6. I created Main.java with the following content
>> class Main {
>>   {
>>     new jdk.neshkeev.MyPreviewClass();
>>   }
>> }
>> 7. I compiled Main.java with my openjdk that was built in the step 5.
>> Here is the link to my openjdk fork with the jdk.neshkeev module: 
>> https://github.com/neshkeev/jdk <https://github.com/neshkeev/jdk>
>> What is expected
>> Since MyPreviewClass is inside of the package that is annotated with 
>> PreviewFeature and it is also inside of the module that is also 
>> annotated with PreviewFeature I expect to get compile time 
>> errors/warnings. I believe that compile-time errors/warnings should 
>> arise since Main.java can't be considered as participating source code
>> What happened
>> No compile-time errors/warnings occurred
>> Is this a bug?


More information about the compiler-dev mailing list