--- old/src/java.compiler/share/classes/javax/annotation/processing/ProcessingEnvironment.java 2019-04-15 13:33:48.210000000 -0700 +++ new/src/java.compiler/share/classes/javax/annotation/processing/ProcessingEnvironment.java 2019-04-15 13:33:47.838000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -131,4 +131,18 @@ * effect */ Locale getLocale(); + + /** + * Returns {@code true} if preview features are enabled + * and {@code false} otherwise. + * @return whether or not preview features are enabled + * + * @implSpec The default implementation of this method returns + * {@code false}. + * + * @since 13 + */ + default boolean isPreviewEnabled() { + return false; + } } --- old/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java 2019-04-15 13:33:48.866000000 -0700 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java 2019-04-15 13:33:48.530000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -188,6 +188,11 @@ private final Context context; + /** + * Support for preview language features. + */ + private final Preview preview; + /** Get the JavacProcessingEnvironment instance for this context. */ public static JavacProcessingEnvironment instance(Context context) { JavacProcessingEnvironment instance = context.get(JavacProcessingEnvironment.class); @@ -236,6 +241,7 @@ enter = Enter.instance(context); initialCompleter = ClassFinder.instance(context).getCompleter(); chk = Check.instance(context); + preview = Preview.instance(context); initProcessorLoader(); } @@ -1675,6 +1681,11 @@ return messages.getCurrentLocale(); } + @DefinedBy(Api.ANNOTATION_PROCESSING) + public boolean isPreviewEnabled() { + return preview.isEnabled(); + } + public Set getSpecifiedPackages() { return specifiedPackages; } --- /dev/null 2019-04-08 19:34:03.876000000 -0700 +++ new/test/langtools/tools/javac/processing/environment/TestPreviewEnabled.java 2019-04-15 13:33:49.230000000 -0700 @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8222378 + * @summary Test that ProcessingEnvironment.isPreviewEnabled works properly + * @library /tools/javac/lib + * @modules java.compiler + * @build JavacTestingAbstractProcessor + * @compile TestPreviewEnabled.java + * @compile -processor TestPreviewEnabled -proc:only -source ${jdk.version} -AExpectedPreview=false TestSourceVersion.java + * @compile -processor TestPreviewEnabled -proc:only -source ${jdk.version} -AExpectedPreview=true --enable-preview TestSourceVersion.java + */ + +import java.util.Locale; +import java.util.Map; +import java.util.Set; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.TypeElement; +import javax.annotation.processing.*; +import javax.lang.model.util.*; + +/** + * This processor checks that ProcessingEnvironment.isPreviewEnabled + * is consistent with the compiler options. + */ +@SupportedOptions("ExpectedPreview") +public class TestPreviewEnabled extends JavacTestingAbstractProcessor { + public boolean process(Set annotations, + RoundEnvironment roundEnvironment) { + if (!roundEnvironment.processingOver()) { + boolean expectedPreview = + Boolean.valueOf(processingEnv.getOptions().get("ExpectedPreview")); + boolean actualPreview = processingEnv.isPreviewEnabled(); + System.out.println("Expected PreviewEnabled: " + expectedPreview + + "\n actual PreviewEnabled: " + actualPreview); + if (expectedPreview != actualPreview) + throw new RuntimeException(); + + if (expectedPreview) { + // Create a ProcessingEnvironment that uses the + // default implemention of isPreviewEnabled. + ProcessingEnvironment testEnv = new ProcessingEnvironment() { + @Override public Elements getElementUtils() {return null;} + @Override public Filer getFiler() {return null;} + @Override public Locale getLocale() {return null;} + @Override public Messager getMessager() {return null;} + @Override public Map getOptions() {return null;} + @Override public SourceVersion getSourceVersion() {return null;} + @Override public Types getTypeUtils() {return null;} + }; + if (testEnv.isPreviewEnabled()) { + throw new RuntimeException("Bad true return value from default " + + "ProcessingEnvironment.isPreviewEnabled."); + } + } + } + return true; + } +}