test/tools/javac/lambda/BadLambdaExpr.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/lambda/BadLambdaExpr.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,191 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 8003280
    1.30 + * @summary Add lambda tests
    1.31 + *  compile crashes on partial lambda expressions
    1.32 + */
    1.33 +
    1.34 +import com.sun.source.util.JavacTask;
    1.35 +import java.net.URI;
    1.36 +import java.util.Arrays;
    1.37 +import javax.tools.Diagnostic;
    1.38 +import javax.tools.JavaCompiler;
    1.39 +import javax.tools.JavaFileObject;
    1.40 +import javax.tools.SimpleJavaFileObject;
    1.41 +import javax.tools.StandardJavaFileManager;
    1.42 +import javax.tools.ToolProvider;
    1.43 +
    1.44 +
    1.45 +public class BadLambdaExpr {
    1.46 +
    1.47 +    static int checkCount = 0;
    1.48 +
    1.49 +    enum ParameterListKind {
    1.50 +        ZERO_ARY("()"),
    1.51 +        UNARY("(#P)"),
    1.52 +        TWO_ARY("(#P, #P)"),
    1.53 +        THREE_ARY("(#P, #P, #P)");
    1.54 +
    1.55 +        String parametersTemplateStr;
    1.56 +
    1.57 +        ParameterListKind(String parametersTemplateStr) {
    1.58 +            this.parametersTemplateStr = parametersTemplateStr;
    1.59 +        }
    1.60 +
    1.61 +        String getParameterString(ParameterKind pk) {
    1.62 +            return parametersTemplateStr.replaceAll("#P", pk.parameterStr);
    1.63 +        }
    1.64 +    }
    1.65 +
    1.66 +    enum ParameterKind {
    1.67 +        IMPLICIT("a"),
    1.68 +        EXPLIICT("A a");
    1.69 +
    1.70 +        String parameterStr;
    1.71 +
    1.72 +        ParameterKind(String parameterStr) {
    1.73 +            this.parameterStr = parameterStr;
    1.74 +        }
    1.75 +    }
    1.76 +
    1.77 +    enum ArrowKind {
    1.78 +        NONE(""),
    1.79 +        SEMI("-"),
    1.80 +        FULL("->");
    1.81 +
    1.82 +        String arrowStr;
    1.83 +
    1.84 +        ArrowKind(String arrowStr) {
    1.85 +            this.arrowStr = arrowStr;
    1.86 +        }
    1.87 +    }
    1.88 +
    1.89 +    enum ExprKind {
    1.90 +        NONE("#P#A"),
    1.91 +        METHOD_CALL("m(#P#A)"),
    1.92 +        CONSTR_CALL("new Foo(#P#A)");
    1.93 +
    1.94 +        String expressionTemplate;
    1.95 +
    1.96 +        ExprKind(String expressionTemplate) {
    1.97 +            this.expressionTemplate = expressionTemplate;
    1.98 +        }
    1.99 +
   1.100 +        String expressionString(ParameterListKind plk, ParameterKind pk,
   1.101 +                ArrowKind ak) {
   1.102 +            return expressionTemplate.replaceAll("#P", plk.getParameterString(pk))
   1.103 +                    .replaceAll("#A", ak.arrowStr);
   1.104 +        }
   1.105 +    }
   1.106 +
   1.107 +    public static void main(String... args) throws Exception {
   1.108 +
   1.109 +        //create default shared JavaCompiler - reused across multiple compilations
   1.110 +        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   1.111 +        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   1.112 +
   1.113 +        for (ParameterListKind plk : ParameterListKind.values()) {
   1.114 +            for (ParameterKind pk : ParameterKind.values()) {
   1.115 +                for (ArrowKind ak : ArrowKind.values()) {
   1.116 +                    for (ExprKind ek : ExprKind.values()) {
   1.117 +                        new BadLambdaExpr(plk, pk, ak, ek).run(comp, fm);
   1.118 +                    }
   1.119 +                }
   1.120 +            }
   1.121 +        }
   1.122 +        System.out.println("Total check executed: " + checkCount);
   1.123 +    }
   1.124 +
   1.125 +    ParameterListKind plk;
   1.126 +    ParameterKind pk;
   1.127 +    ArrowKind ak;
   1.128 +    ExprKind ek;
   1.129 +    JavaSource source;
   1.130 +    DiagnosticChecker diagChecker;
   1.131 +
   1.132 +    BadLambdaExpr(ParameterListKind plk, ParameterKind pk, ArrowKind ak, ExprKind ek) {
   1.133 +        this.plk = plk;
   1.134 +        this.pk = pk;
   1.135 +        this.ak = ak;
   1.136 +        this.ek = ek;
   1.137 +        this.source = new JavaSource();
   1.138 +        this.diagChecker = new DiagnosticChecker();
   1.139 +    }
   1.140 +
   1.141 +    class JavaSource extends SimpleJavaFileObject {
   1.142 +
   1.143 +        String template = "class Test {\n" +
   1.144 +                          "   SAM s = #E;\n" +
   1.145 +                          "}";
   1.146 +
   1.147 +        String source;
   1.148 +
   1.149 +        public JavaSource() {
   1.150 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   1.151 +            source = template.replaceAll("#E", ek.expressionString(plk, pk, ak));
   1.152 +        }
   1.153 +
   1.154 +        @Override
   1.155 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.156 +            return source;
   1.157 +        }
   1.158 +    }
   1.159 +
   1.160 +    void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
   1.161 +        JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
   1.162 +                null, null, Arrays.asList(source));
   1.163 +        try {
   1.164 +            ct.parse();
   1.165 +        } catch (Throwable ex) {
   1.166 +            throw new AssertionError("Error thron when parsing the following source:\n" + source.getCharContent(true));
   1.167 +        }
   1.168 +        check();
   1.169 +    }
   1.170 +
   1.171 +    void check() {
   1.172 +        boolean errorExpected =
   1.173 +                ak != ArrowKind.NONE ||
   1.174 +                plk != ParameterListKind.UNARY ||
   1.175 +                pk != ParameterKind.IMPLICIT;
   1.176 +        if (errorExpected != diagChecker.errorFound) {
   1.177 +            throw new Error("bad diag for source:\n" +
   1.178 +                source.getCharContent(true));
   1.179 +        }
   1.180 +        checkCount++;
   1.181 +    }
   1.182 +
   1.183 +    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.184 +
   1.185 +        boolean errorFound;
   1.186 +
   1.187 +        @Override
   1.188 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.189 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   1.190 +                errorFound = true;
   1.191 +            }
   1.192 +        }
   1.193 +    }
   1.194 +}

mercurial