test/tools/javac/lambda/BadLambdaExpr.java

Tue, 12 Mar 2013 16:02:43 +0000

author
mcimadamore
date
Tue, 12 Mar 2013 16:02:43 +0000
changeset 1628
5ddecb91d843
parent 0
959103a6100f
permissions
-rw-r--r--

8009545: Graph inference: dependencies between inference variables should be set during incorporation
Summary: Move all transitivity checks into the incorporation round
Reviewed-by: jjg

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 8003280
aoqi@0 27 * @summary Add lambda tests
aoqi@0 28 * compile crashes on partial lambda expressions
aoqi@0 29 */
aoqi@0 30
aoqi@0 31 import com.sun.source.util.JavacTask;
aoqi@0 32 import java.net.URI;
aoqi@0 33 import java.util.Arrays;
aoqi@0 34 import javax.tools.Diagnostic;
aoqi@0 35 import javax.tools.JavaCompiler;
aoqi@0 36 import javax.tools.JavaFileObject;
aoqi@0 37 import javax.tools.SimpleJavaFileObject;
aoqi@0 38 import javax.tools.StandardJavaFileManager;
aoqi@0 39 import javax.tools.ToolProvider;
aoqi@0 40
aoqi@0 41
aoqi@0 42 public class BadLambdaExpr {
aoqi@0 43
aoqi@0 44 static int checkCount = 0;
aoqi@0 45
aoqi@0 46 enum ParameterListKind {
aoqi@0 47 ZERO_ARY("()"),
aoqi@0 48 UNARY("(#P)"),
aoqi@0 49 TWO_ARY("(#P, #P)"),
aoqi@0 50 THREE_ARY("(#P, #P, #P)");
aoqi@0 51
aoqi@0 52 String parametersTemplateStr;
aoqi@0 53
aoqi@0 54 ParameterListKind(String parametersTemplateStr) {
aoqi@0 55 this.parametersTemplateStr = parametersTemplateStr;
aoqi@0 56 }
aoqi@0 57
aoqi@0 58 String getParameterString(ParameterKind pk) {
aoqi@0 59 return parametersTemplateStr.replaceAll("#P", pk.parameterStr);
aoqi@0 60 }
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 enum ParameterKind {
aoqi@0 64 IMPLICIT("a"),
aoqi@0 65 EXPLIICT("A a");
aoqi@0 66
aoqi@0 67 String parameterStr;
aoqi@0 68
aoqi@0 69 ParameterKind(String parameterStr) {
aoqi@0 70 this.parameterStr = parameterStr;
aoqi@0 71 }
aoqi@0 72 }
aoqi@0 73
aoqi@0 74 enum ArrowKind {
aoqi@0 75 NONE(""),
aoqi@0 76 SEMI("-"),
aoqi@0 77 FULL("->");
aoqi@0 78
aoqi@0 79 String arrowStr;
aoqi@0 80
aoqi@0 81 ArrowKind(String arrowStr) {
aoqi@0 82 this.arrowStr = arrowStr;
aoqi@0 83 }
aoqi@0 84 }
aoqi@0 85
aoqi@0 86 enum ExprKind {
aoqi@0 87 NONE("#P#A"),
aoqi@0 88 METHOD_CALL("m(#P#A)"),
aoqi@0 89 CONSTR_CALL("new Foo(#P#A)");
aoqi@0 90
aoqi@0 91 String expressionTemplate;
aoqi@0 92
aoqi@0 93 ExprKind(String expressionTemplate) {
aoqi@0 94 this.expressionTemplate = expressionTemplate;
aoqi@0 95 }
aoqi@0 96
aoqi@0 97 String expressionString(ParameterListKind plk, ParameterKind pk,
aoqi@0 98 ArrowKind ak) {
aoqi@0 99 return expressionTemplate.replaceAll("#P", plk.getParameterString(pk))
aoqi@0 100 .replaceAll("#A", ak.arrowStr);
aoqi@0 101 }
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 public static void main(String... args) throws Exception {
aoqi@0 105
aoqi@0 106 //create default shared JavaCompiler - reused across multiple compilations
aoqi@0 107 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
aoqi@0 108 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
aoqi@0 109
aoqi@0 110 for (ParameterListKind plk : ParameterListKind.values()) {
aoqi@0 111 for (ParameterKind pk : ParameterKind.values()) {
aoqi@0 112 for (ArrowKind ak : ArrowKind.values()) {
aoqi@0 113 for (ExprKind ek : ExprKind.values()) {
aoqi@0 114 new BadLambdaExpr(plk, pk, ak, ek).run(comp, fm);
aoqi@0 115 }
aoqi@0 116 }
aoqi@0 117 }
aoqi@0 118 }
aoqi@0 119 System.out.println("Total check executed: " + checkCount);
aoqi@0 120 }
aoqi@0 121
aoqi@0 122 ParameterListKind plk;
aoqi@0 123 ParameterKind pk;
aoqi@0 124 ArrowKind ak;
aoqi@0 125 ExprKind ek;
aoqi@0 126 JavaSource source;
aoqi@0 127 DiagnosticChecker diagChecker;
aoqi@0 128
aoqi@0 129 BadLambdaExpr(ParameterListKind plk, ParameterKind pk, ArrowKind ak, ExprKind ek) {
aoqi@0 130 this.plk = plk;
aoqi@0 131 this.pk = pk;
aoqi@0 132 this.ak = ak;
aoqi@0 133 this.ek = ek;
aoqi@0 134 this.source = new JavaSource();
aoqi@0 135 this.diagChecker = new DiagnosticChecker();
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 class JavaSource extends SimpleJavaFileObject {
aoqi@0 139
aoqi@0 140 String template = "class Test {\n" +
aoqi@0 141 " SAM s = #E;\n" +
aoqi@0 142 "}";
aoqi@0 143
aoqi@0 144 String source;
aoqi@0 145
aoqi@0 146 public JavaSource() {
aoqi@0 147 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
aoqi@0 148 source = template.replaceAll("#E", ek.expressionString(plk, pk, ak));
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 @Override
aoqi@0 152 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aoqi@0 153 return source;
aoqi@0 154 }
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
aoqi@0 158 JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
aoqi@0 159 null, null, Arrays.asList(source));
aoqi@0 160 try {
aoqi@0 161 ct.parse();
aoqi@0 162 } catch (Throwable ex) {
aoqi@0 163 throw new AssertionError("Error thron when parsing the following source:\n" + source.getCharContent(true));
aoqi@0 164 }
aoqi@0 165 check();
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 void check() {
aoqi@0 169 boolean errorExpected =
aoqi@0 170 ak != ArrowKind.NONE ||
aoqi@0 171 plk != ParameterListKind.UNARY ||
aoqi@0 172 pk != ParameterKind.IMPLICIT;
aoqi@0 173 if (errorExpected != diagChecker.errorFound) {
aoqi@0 174 throw new Error("bad diag for source:\n" +
aoqi@0 175 source.getCharContent(true));
aoqi@0 176 }
aoqi@0 177 checkCount++;
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
aoqi@0 181
aoqi@0 182 boolean errorFound;
aoqi@0 183
aoqi@0 184 @Override
aoqi@0 185 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
aoqi@0 186 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
aoqi@0 187 errorFound = true;
aoqi@0 188 }
aoqi@0 189 }
aoqi@0 190 }
aoqi@0 191 }

mercurial