test/langtools/tools/javac/lambda/LambdaParserTest.java
changeset 48935 03ae177c26b0
parent 48926 02404e27d356
child 52016 9ea22a0f9540
equal deleted inserted replaced
48934:3390b463d7a2 48935:03ae177c26b0
    38 
    38 
    39  * @run main LambdaParserTest
    39  * @run main LambdaParserTest
    40  */
    40  */
    41 
    41 
    42 import java.io.IOException;
    42 import java.io.IOException;
       
    43 import java.util.Arrays;
    43 
    44 
    44 import combo.ComboInstance;
    45 import combo.ComboInstance;
    45 import combo.ComboParameter;
    46 import combo.ComboParameter;
    46 import combo.ComboTask.Result;
    47 import combo.ComboTask.Result;
    47 import combo.ComboTestHelper;
    48 import combo.ComboTestHelper;
   103         public String expand(String optParameter) {
   104         public String expand(String optParameter) {
   104             return nameStr;
   105             return nameStr;
   105         }
   106         }
   106     }
   107     }
   107 
   108 
       
   109     enum SourceKind {
       
   110         SOURCE_9("9"),
       
   111         SOURCE_10("10");
       
   112 
       
   113         String sourceNumber;
       
   114 
       
   115         SourceKind(String sourceNumber) {
       
   116             this.sourceNumber = sourceNumber;
       
   117         }
       
   118     }
       
   119 
   108     enum LambdaParameterKind implements ComboParameter {
   120     enum LambdaParameterKind implements ComboParameter {
   109         IMPLICIT(""),
   121 
   110         EXPLIICT_SIMPLE("A"),
   122         IMPLICIT_1("", ExplicitKind.IMPLICIT),
   111         EXPLIICT_SIMPLE_ARR1("A[]"),
   123         IMPLICIT_2("var", ExplicitKind.IMPLICIT_VAR),
   112         EXPLIICT_SIMPLE_ARR2("A[][]"),
   124         EXPLIICT_SIMPLE("A", ExplicitKind.EXPLICIT),
   113         EXPLICIT_VARARGS("A..."),
   125         EXPLIICT_SIMPLE_ARR1("A[]", ExplicitKind.EXPLICIT),
   114         EXPLICIT_GENERIC1("A<X>"),
   126         EXPLIICT_SIMPLE_ARR2("A[][]", ExplicitKind.EXPLICIT),
   115         EXPLICIT_GENERIC2("A<? extends X, ? super Y>"),
   127         EXPLICIT_VARARGS("A...", ExplicitKind.EXPLICIT),
   116         EXPLICIT_GENERIC2_VARARGS("A<? extends X, ? super Y>..."),
   128         EXPLICIT_GENERIC1("A<X>", ExplicitKind.EXPLICIT),
   117         EXPLICIT_GENERIC2_ARR1("A<? extends X, ? super Y>[]"),
   129         EXPLICIT_GENERIC2("A<? extends X, ? super Y>", ExplicitKind.EXPLICIT),
   118         EXPLICIT_GENERIC2_ARR2("A<? extends X, ? super Y>[][]");
   130         EXPLICIT_GENERIC2_VARARGS("A<? extends X, ? super Y>...", ExplicitKind.EXPLICIT),
       
   131         EXPLICIT_GENERIC2_ARR1("A<? extends X, ? super Y>[]", ExplicitKind.EXPLICIT),
       
   132         EXPLICIT_GENERIC2_ARR2("A<? extends X, ? super Y>[][]", ExplicitKind.EXPLICIT);
       
   133 
       
   134         enum ExplicitKind {
       
   135             IMPLICIT,
       
   136             IMPLICIT_VAR,
       
   137             EXPLICIT;
       
   138         }
   119 
   139 
   120         String parameterType;
   140         String parameterType;
   121 
   141         ExplicitKind explicitKind;
   122         LambdaParameterKind(String parameterType) {
   142 
       
   143 
       
   144         LambdaParameterKind(String parameterType, ExplicitKind ekind) {
   123             this.parameterType = parameterType;
   145             this.parameterType = parameterType;
   124         }
   146             this.explicitKind = ekind;
   125 
       
   126         boolean explicit() {
       
   127             return this != IMPLICIT;
       
   128         }
   147         }
   129 
   148 
   130         boolean isVarargs() {
   149         boolean isVarargs() {
   131             return this == EXPLICIT_VARARGS ||
   150             return this == EXPLICIT_VARARGS ||
   132                     this == EXPLICIT_GENERIC2_VARARGS;
   151                     this == EXPLICIT_GENERIC2_VARARGS;
   134 
   153 
   135         @Override
   154         @Override
   136         public String expand(String optParameter) {
   155         public String expand(String optParameter) {
   137             return parameterType;
   156             return parameterType;
   138         }
   157         }
       
   158 
       
   159         ExplicitKind explicitKind(SourceKind sk) {
       
   160             switch (explicitKind) {
       
   161                 case IMPLICIT_VAR:
       
   162                     return (sk == SourceKind.SOURCE_9) ?
       
   163                             ExplicitKind.EXPLICIT : ExplicitKind.IMPLICIT_VAR;
       
   164                 default:
       
   165                     return explicitKind;
       
   166             }
       
   167         }
   139     }
   168     }
   140 
   169 
   141     enum ModifierKind implements ComboParameter {
   170     enum ModifierKind implements ComboParameter {
   142         NONE(""),
   171         NONE(""),
   143         FINAL("final"),
   172         FINAL("final"),
   144         PUBLIC("public");
   173         PUBLIC("public"),
       
   174         ANNO("@A");
   145 
   175 
   146         String modifier;
   176         String modifier;
   147 
   177 
   148         ModifierKind(String modifier) {
   178         ModifierKind(String modifier) {
   149             this.modifier = modifier;
   179             this.modifier = modifier;
   150         }
   180         }
   151 
   181 
   152         boolean compatibleWith(LambdaParameterKind pk) {
   182         boolean compatibleWith(LambdaParameterKind pk) {
   153             switch (this) {
   183             switch (this) {
   154                 case PUBLIC: return false;
   184                 case PUBLIC: return false;
   155                 case FINAL: return pk != LambdaParameterKind.IMPLICIT;
   185                 case ANNO:
       
   186                 case FINAL: return pk != LambdaParameterKind.IMPLICIT_1;
   156                 case NONE: return true;
   187                 case NONE: return true;
   157                 default: throw new AssertionError("Invalid modifier kind " + this);
   188                 default: throw new AssertionError("Invalid modifier kind " + this);
   158             }
   189             }
   159         }
   190         }
   160 
   191 
   206 
   237 
   207     public static void main(String... args) throws Exception {
   238     public static void main(String... args) throws Exception {
   208         new ComboTestHelper<LambdaParserTest>()
   239         new ComboTestHelper<LambdaParserTest>()
   209                 .withFilter(LambdaParserTest::redundantTestFilter)
   240                 .withFilter(LambdaParserTest::redundantTestFilter)
   210                 .withFilter(LambdaParserTest::badImplicitFilter)
   241                 .withFilter(LambdaParserTest::badImplicitFilter)
       
   242                 .withDimension("SOURCE", (x, sk) -> x.sk = sk, SourceKind.values())
   211                 .withDimension("LAMBDA", (x, lk) -> x.lk = lk, LambdaKind.values())
   243                 .withDimension("LAMBDA", (x, lk) -> x.lk = lk, LambdaKind.values())
   212                 .withDimension("NAME", (x, name) -> x.pn = name, LambdaParameterName.values())
   244                 .withDimension("NAME", (x, name) -> x.pn = name, LambdaParameterName.values())
   213                 .withArrayDimension("TYPE", (x, type, idx) -> x.pks[idx] = type, 2, LambdaParameterKind.values())
   245                 .withArrayDimension("TYPE", (x, type, idx) -> x.pks[idx] = type, 2, LambdaParameterKind.values())
   214                 .withArrayDimension("MOD", (x, mod, idx) -> x.mks[idx] = mod, 2, ModifierKind.values())
   246                 .withArrayDimension("MOD", (x, mod, idx) -> x.mks[idx] = mod, 2, ModifierKind.values())
   215                 .withDimension("EXPR", ExprKind.values())
   247                 .withDimension("EXPR", ExprKind.values())
   219 
   251 
   220     LambdaParameterKind[] pks = new LambdaParameterKind[2];
   252     LambdaParameterKind[] pks = new LambdaParameterKind[2];
   221     ModifierKind[] mks = new ModifierKind[2];
   253     ModifierKind[] mks = new ModifierKind[2];
   222     LambdaKind lk;
   254     LambdaKind lk;
   223     LambdaParameterName pn;
   255     LambdaParameterName pn;
       
   256     SourceKind sk;
   224 
   257 
   225     boolean badImplicitFilter() {
   258     boolean badImplicitFilter() {
   226         return !(mks[0] != ModifierKind.NONE && lk.isShort());
   259         return !(mks[0] != ModifierKind.NONE && lk.isShort());
   227     }
   260     }
   228 
   261 
   238             }
   271             }
   239         }
   272         }
   240         return true;
   273         return true;
   241     }
   274     }
   242 
   275 
   243     String template = "class Test {\n" +
   276     String template = "@interface A { }\n" +
   244                       "   SAM s = #{EXPR};\n" +
   277             "class Test {\n" +
   245                       "}";
   278             "   SAM s = #{EXPR};\n" +
       
   279             "}";
   246 
   280 
   247     @Override
   281     @Override
   248     public void doWork() throws IOException {
   282     public void doWork() throws IOException {
   249         newCompilationTask()
   283         newCompilationTask()
       
   284                 .withOptions(Arrays.asList("-source", sk.sourceNumber))
   250                 .withSourceFromTemplate(template)
   285                 .withSourceFromTemplate(template)
   251                 .parse(this::check);
   286                 .parse(this::check);
   252     }
   287     }
   253 
   288 
   254     void check(Result<?> res) {
   289     void check(Result<?> res) {
   255         boolean errorExpected = (lk.arity() > 0 && !mks[0].compatibleWith(pks[0])) ||
   290         boolean errorExpected = (lk.arity() > 0 && !mks[0].compatibleWith(pks[0])) ||
   256                 (lk.arity() > 1 && !mks[1].compatibleWith(pks[1]));
   291                 (lk.arity() > 1 && !mks[1].compatibleWith(pks[1]));
   257 
   292 
   258         if (lk.arity() == 2 &&
   293         if (lk.arity() == 2 &&
   259                 (pks[0].explicit() != pks[1].explicit() ||
   294                 (pks[0].explicitKind(sk) != pks[1].explicitKind(sk) ||
   260                 pks[0].isVarargs())) {
   295                 pks[0].isVarargs())) {
   261             errorExpected = true;
   296             errorExpected = true;
   262         }
   297         }
   263 
   298 
   264         errorExpected |= pn == LambdaParameterName.UNDERSCORE &&
   299         errorExpected |= pn == LambdaParameterName.UNDERSCORE &&