test/tools/javac/lambda/abort/CompletionFailure.java

changeset 1613
d2a98dde7ecc
parent 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/lambda/abort/CompletionFailure.java	Tue Mar 05 14:12:07 2013 +0000
     1.3 @@ -0,0 +1,137 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, 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 8009227
    1.30 + * @summary Certain diagnostics should not be deferred
    1.31 + */
    1.32 +
    1.33 +import com.sun.source.util.JavacTask;
    1.34 +import java.io.BufferedWriter;
    1.35 +import java.io.File;
    1.36 +import java.io.FileWriter;
    1.37 +import java.io.IOException;
    1.38 +import java.util.Arrays;
    1.39 +import javax.tools.Diagnostic;
    1.40 +import javax.tools.JavaCompiler;
    1.41 +import javax.tools.JavaFileObject;
    1.42 +import javax.tools.SimpleJavaFileObject;
    1.43 +import javax.tools.ToolProvider;
    1.44 +
    1.45 +public class CompletionFailure {
    1.46 +
    1.47 +    public static void main(String... args) throws Exception {
    1.48 +
    1.49 +        String SCRATCH_DIR = System.getProperty("user.dir");
    1.50 +        JavaCompiler javacTool = ToolProvider.getSystemJavaCompiler();
    1.51 +        File scratchDir = new File(SCRATCH_DIR);
    1.52 +
    1.53 +        sourceA.dumpTo(scratchDir);
    1.54 +        sourceB.dumpTo(scratchDir);
    1.55 +
    1.56 +        JavacTask ct = (JavacTask)javacTool.getTask(null, null, null,
    1.57 +                null, null, Arrays.asList(sourceA.asJFO(scratchDir), sourceB.asJFO(scratchDir)));
    1.58 +
    1.59 +        ct.generate();
    1.60 +
    1.61 +        remove(scratchDir, "A.java");
    1.62 +        remove(scratchDir, "B.java");
    1.63 +        remove(scratchDir, "A.class");
    1.64 +
    1.65 +        sourceC.dumpTo(scratchDir);
    1.66 +        sourceD.dumpTo(scratchDir);
    1.67 +
    1.68 +        DiagnosticChecker diagChecker = new DiagnosticChecker();
    1.69 +        ct = (JavacTask)javacTool.getTask(null, null, diagChecker,
    1.70 +                Arrays.asList("-XDrawDiagnostics", "-cp", scratchDir.getAbsolutePath()),
    1.71 +                null, Arrays.asList(sourceC.asJFO(scratchDir), sourceD.asJFO(scratchDir)));
    1.72 +        try {
    1.73 +            ct.analyze();
    1.74 +        } catch (Throwable ex) {
    1.75 +            //ignore abort exception thrown by javac
    1.76 +        }
    1.77 +
    1.78 +        if (!diagChecker.errorFound) {
    1.79 +            throw new AssertionError("Missing diagnostic");
    1.80 +        }
    1.81 +    }
    1.82 +
    1.83 +    static void remove(File dir, String fileName) {
    1.84 +        File fileToRemove = new File(dir, fileName);
    1.85 +        fileToRemove.delete();
    1.86 +    }
    1.87 +
    1.88 +    static class JavaSource {
    1.89 +        String contents;
    1.90 +        String filename;
    1.91 +
    1.92 +        public JavaSource(String filename, String contents) {
    1.93 +            this.filename =  filename;
    1.94 +            this.contents = contents;
    1.95 +        }
    1.96 +
    1.97 +        void dumpTo(java.io.File loc) throws Exception {
    1.98 +            File file = new File(loc, filename);
    1.99 +            BufferedWriter bw = new java.io.BufferedWriter(new FileWriter(file));
   1.100 +            bw.append(contents);
   1.101 +            bw.close();
   1.102 +        }
   1.103 +
   1.104 +        SimpleJavaFileObject asJFO(java.io.File dir) {
   1.105 +            return new SimpleJavaFileObject(new File(dir, filename).toURI(), JavaFileObject.Kind.SOURCE) {
   1.106 +                @Override
   1.107 +                public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.108 +                    return contents;
   1.109 +                }
   1.110 +            };
   1.111 +        }
   1.112 +    }
   1.113 +
   1.114 +    static JavaSource sourceA = new JavaSource("A.java", "public interface A { }\n");
   1.115 +
   1.116 +    static JavaSource sourceB = new JavaSource("B.java", "public class B implements A {\n" +
   1.117 +                                               "   public static Object n() { return null; }\n" +
   1.118 +                                               "}\n");
   1.119 +
   1.120 +    static JavaSource sourceC = new JavaSource("C.java", "public class C {\n" +
   1.121 +                                               "   void test(B b) {\n" +
   1.122 +                                               "      D.m(B.n());\n" +
   1.123 +                                               "}  }\n");
   1.124 +
   1.125 +    static JavaSource sourceD = new JavaSource("D.java", "public class D {\n" +
   1.126 +                                               "   static void m(Object o) { }\n" +
   1.127 +                                               "}\n");
   1.128 +
   1.129 +    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.130 +
   1.131 +        boolean errorFound;
   1.132 +
   1.133 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.134 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR &&
   1.135 +                    diagnostic.getCode().contains("compiler.err.cant.access")) {
   1.136 +                errorFound = true;
   1.137 +            }
   1.138 +        }
   1.139 +    }
   1.140 +}

mercurial