8222378: Provide mechanism to query preview feature status for annotation processors
authordarcy
Mon, 15 Apr 2019 15:44:42 -0700
changeset 54539 bdbfa0115fc6
parent 54538 ef331769d4ab
child 54540 4de70bc80f24
8222378: Provide mechanism to query preview feature status for annotation processors Reviewed-by: jjg
src/java.compiler/share/classes/javax/annotation/processing/ProcessingEnvironment.java
src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java
test/langtools/tools/javac/processing/environment/TestPreviewEnabled.java
--- a/src/java.compiler/share/classes/javax/annotation/processing/ProcessingEnvironment.java	Mon Apr 15 12:35:29 2019 -0700
+++ b/src/java.compiler/share/classes/javax/annotation/processing/ProcessingEnvironment.java	Mon Apr 15 15:44:42 2019 -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 <em>preview features</em> 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;
+    }
 }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Mon Apr 15 12:35:29 2019 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Mon Apr 15 15:44:42 2019 -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<Symbol.PackageSymbol> getSpecifiedPackages() {
         return specifiedPackages;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/processing/environment/TestPreviewEnabled.java	Mon Apr 15 15:44:42 2019 -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<? extends TypeElement> 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<String,String> 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;
+    }
+}