test/langtools/tools/javac/processing/model/TestSourceVersion.java
changeset 59068 dc45ed0ab083
parent 48694 b0a54e2ba484
equal deleted inserted replaced
59067:f080b08daace 59068:dc45ed0ab083
     1 /*
     1 /*
     2  * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 7025809 8028543 6415644 8028544 8029942 8187951 8193291 8196551
    26  * @bug 7025809 8028543 6415644 8028544 8029942 8187951 8193291 8196551 8233096
    27  * @summary Test latest, latestSupported, underscore as keyword, etc.
    27  * @summary Test latest, latestSupported, underscore as keyword, etc.
    28  * @author  Joseph D. Darcy
    28  * @author  Joseph D. Darcy
    29  * @modules java.compiler
    29  * @modules java.compiler
    30  *          jdk.compiler
    30  *          jdk.compiler
    31  */
    31  */
    32 
    32 
    33 import java.util.*;
    33 import java.util.*;
       
    34 import java.util.function.Predicate;
    34 import javax.lang.model.SourceVersion;
    35 import javax.lang.model.SourceVersion;
    35 import static javax.lang.model.SourceVersion.*;
    36 import static javax.lang.model.SourceVersion.*;
    36 
    37 
    37 /**
    38 /**
    38  * Verify latest[Supported] behavior.
    39  * Verify behavior of latest[Supported] and other methods.
    39  */
    40  */
    40 public class TestSourceVersion {
    41 public class TestSourceVersion {
    41     public static void main(String... args) {
    42     public static void main(String... args) {
    42         testLatestSupported();
    43         testLatestSupported();
    43         testVersionVaryingKeywords();
    44         testVersionVaryingKeywords();
    44         testRestrictedKeywords();
    45         testRestrictedKeywords();
    45         testVar();
    46         testVar();
       
    47         testYield();
    46     }
    48     }
    47 
    49 
    48     private static void testLatestSupported() {
    50     private static void testLatestSupported() {
    49         SourceVersion[] values = SourceVersion.values();
    51         SourceVersion[] values = SourceVersion.values();
    50         SourceVersion last = values[values.length - 1];
    52         SourceVersion last = values[values.length - 1];
    51         SourceVersion latest = SourceVersion.latest();
    53         SourceVersion latest = SourceVersion.latest();
    52         SourceVersion latestSupported = SourceVersion.latestSupported();
    54         SourceVersion latestSupported = SourceVersion.latestSupported();
    53 
    55 
    54         if (latest == last &&
    56         if (latest == last &&
    55             latestSupported == SourceVersion.valueOf("RELEASE_" + Runtime.version().feature()) &&
    57             latestSupported == SourceVersion.valueOf("RELEASE_" +
    56             (latest == latestSupported || (latest.ordinal() - latestSupported.ordinal() == 1)) )
    58                                                      Runtime.version().feature()) &&
       
    59             (latest == latestSupported ||
       
    60              (latest.ordinal() - latestSupported.ordinal() == 1)) )
    57             return;
    61             return;
    58         else {
    62         else {
    59             throw new RuntimeException("Unexpected release value(s) found:\n" +
    63             throw new RuntimeException("Unexpected release value(s) found:\n" +
    60                                        "latest:\t" + latest + "\n" +
    64                                        "latest:\t" + latest + "\n" +
    61                                        "latestSupported:\t" + latestSupported);
    65                                        "latestSupported:\t" + latestSupported);
    71 
    75 
    72         for (Map.Entry<String, SourceVersion> entry : keyWordStart.entrySet()) {
    76         for (Map.Entry<String, SourceVersion> entry : keyWordStart.entrySet()) {
    73             String key = entry.getKey();
    77             String key = entry.getKey();
    74             SourceVersion value = entry.getValue();
    78             SourceVersion value = entry.getValue();
    75 
    79 
    76             check(true, isKeyword(key), "keyword", latest());
    80             check(true,  key, (String s) -> isKeyword(s), "keyword", latest());
    77             check(false, isName(key),   "name",    latest());
    81             check(false, key, (String s) -> isName(s),    "name",    latest());
    78 
    82 
    79             for(SourceVersion version : SourceVersion.values()) {
    83             for(SourceVersion version : SourceVersion.values()) {
    80                 boolean isKeyword = version.compareTo(value) >= 0;
    84                 boolean isKeyword = version.compareTo(value) >= 0;
    81 
    85 
    82                 check(isKeyword,  isKeyword(key, version), "keyword", version);
    86                 check(isKeyword,  key, (String s) -> isKeyword(s, version), "keyword", version);
    83                 check(!isKeyword, isName(key, version),    "name",    version);
    87                 check(!isKeyword, key, (String s) -> isName(s, version),    "name",    version);
    84             }
    88             }
    85         }
    89         }
    86     }
    90     }
    87 
    91 
    88     private static void testRestrictedKeywords() {
    92     private static void testRestrictedKeywords() {
    96          */
   100          */
    97         Set<String> restrictedKeywords =
   101         Set<String> restrictedKeywords =
    98             Set.of("open", "module", "requires", "transitive", "exports",
   102             Set.of("open", "module", "requires", "transitive", "exports",
    99                    "opens", "to", "uses", "provides", "with");
   103                    "opens", "to", "uses", "provides", "with");
   100 
   104 
   101         for(String key : restrictedKeywords) {
   105         for (String key : restrictedKeywords) {
   102             for(SourceVersion version : SourceVersion.values()) {
   106             for (SourceVersion version : SourceVersion.values()) {
   103                 check(false, isKeyword(key, version), "keyword", version);
   107                 check(false, key, (String s) -> isKeyword(s, version), "keyword", version);
   104                 check(true,  isName(key, version),    "name",    version);
   108                 check(true,  key, (String s) -> isName(s, version),    "name",    version);
   105             }
   109             }
   106         }
   110         }
   107     }
   111     }
   108 
   112 
   109     private static void testVar() {
   113     private static void testVar() {
       
   114         for (SourceVersion version : SourceVersion.values()) {
       
   115             Predicate<String> isKeywordVersion = (String s) -> isKeyword(s, version);
       
   116             Predicate<String> isNameVersion = (String s) -> isName(s, version);
   110 
   117 
   111         for(SourceVersion version : SourceVersion.values()) {
   118             for (String name : List.of("var", "foo.var", "var.foo")) {
   112             check(false, isKeyword("var",     version), "keyword", version);
   119                 check(false, name, isKeywordVersion, "keyword", version);
   113             check(false, isKeyword("foo.var", version), "keyword", version);
   120                 check(true, name,  isNameVersion, "name", version);
   114             check(false, isKeyword("var.foo", version), "keyword", version);
   121             }
   115 
       
   116             check(true, isName("var", version),     "name", version);
       
   117             check(true, isName("foo.var", version), "name", version);
       
   118             check(true, isName("var.foo", version), "name", version);
       
   119         }
   122         }
   120     }
   123     }
   121 
   124 
   122     private static void check(boolean result, boolean expected,
   125     private static void testYield() {
   123                               String message, SourceVersion version) {
   126         for (SourceVersion version : SourceVersion.values()) {
       
   127             Predicate<String> isKeywordVersion = (String s) -> isKeyword(s, version);
       
   128             Predicate<String> isNameVersion = (String s) -> isName(s, version);
       
   129 
       
   130             for  (String name : List.of("yield", "foo.yield", "yield.foo")) {
       
   131                 check(false, name, isKeywordVersion, "keyword", version);
       
   132                 check(true, name,  isNameVersion, "name", version);
       
   133             }
       
   134         }
       
   135     }
       
   136 
       
   137     private static void check(boolean expected,
       
   138                               String input,
       
   139                               Predicate<String> predicate,
       
   140                               String message,
       
   141                               SourceVersion version) {
       
   142         boolean result  = predicate.test(input);
   124         if (result != expected) {
   143         if (result != expected) {
   125             throw new RuntimeException("Unexpected " + message +  "-ness of _ on " + version);
   144             throw new RuntimeException("Unexpected " + message +  "-ness of " + input +
       
   145                                        " on " + version);
   126         }
   146         }
   127     }
   147     }
   128 }
   148 }