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