|
1 /* |
|
2 * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 8222378 |
|
27 * @summary Test that ProcessingEnvironment.isPreviewEnabled works properly |
|
28 * @library /tools/javac/lib |
|
29 * @modules java.compiler |
|
30 * @build JavacTestingAbstractProcessor |
|
31 * @compile TestPreviewEnabled.java |
|
32 * @compile -processor TestPreviewEnabled -proc:only -source ${jdk.version} -AExpectedPreview=false TestSourceVersion.java |
|
33 * @compile -processor TestPreviewEnabled -proc:only -source ${jdk.version} -AExpectedPreview=true --enable-preview TestSourceVersion.java |
|
34 */ |
|
35 |
|
36 import java.util.Locale; |
|
37 import java.util.Map; |
|
38 import java.util.Set; |
|
39 import javax.lang.model.SourceVersion; |
|
40 import javax.lang.model.element.TypeElement; |
|
41 import javax.annotation.processing.*; |
|
42 import javax.lang.model.util.*; |
|
43 |
|
44 /** |
|
45 * This processor checks that ProcessingEnvironment.isPreviewEnabled |
|
46 * is consistent with the compiler options. |
|
47 */ |
|
48 @SupportedOptions("ExpectedPreview") |
|
49 public class TestPreviewEnabled extends JavacTestingAbstractProcessor { |
|
50 public boolean process(Set<? extends TypeElement> annotations, |
|
51 RoundEnvironment roundEnvironment) { |
|
52 if (!roundEnvironment.processingOver()) { |
|
53 boolean expectedPreview = |
|
54 Boolean.valueOf(processingEnv.getOptions().get("ExpectedPreview")); |
|
55 boolean actualPreview = processingEnv.isPreviewEnabled(); |
|
56 System.out.println("Expected PreviewEnabled: " + expectedPreview + |
|
57 "\n actual PreviewEnabled: " + actualPreview); |
|
58 if (expectedPreview != actualPreview) |
|
59 throw new RuntimeException(); |
|
60 |
|
61 if (expectedPreview) { |
|
62 // Create a ProcessingEnvironment that uses the |
|
63 // default implemention of isPreviewEnabled. |
|
64 ProcessingEnvironment testEnv = new ProcessingEnvironment() { |
|
65 @Override public Elements getElementUtils() {return null;} |
|
66 @Override public Filer getFiler() {return null;} |
|
67 @Override public Locale getLocale() {return null;} |
|
68 @Override public Messager getMessager() {return null;} |
|
69 @Override public Map<String,String> getOptions() {return null;} |
|
70 @Override public SourceVersion getSourceVersion() {return null;} |
|
71 @Override public Types getTypeUtils() {return null;} |
|
72 }; |
|
73 if (testEnv.isPreviewEnabled()) { |
|
74 throw new RuntimeException("Bad true return value from default " + |
|
75 "ProcessingEnvironment.isPreviewEnabled."); |
|
76 } |
|
77 } |
|
78 } |
|
79 return true; |
|
80 } |
|
81 } |