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

Tue, 05 Mar 2013 14:12:07 +0000

author
mcimadamore
date
Tue, 05 Mar 2013 14:12:07 +0000
changeset 1613
d2a98dde7ecc
parent 0
959103a6100f
permissions
-rw-r--r--

8009227: Certain diagnostics should not be deferred
Summary: Add new diagnostic flag to mark non deferrable diagnostics
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     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
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 8009227
    27  * @summary Certain diagnostics should not be deferred
    28  */
    30 import com.sun.source.util.JavacTask;
    31 import java.io.BufferedWriter;
    32 import java.io.File;
    33 import java.io.FileWriter;
    34 import java.io.IOException;
    35 import java.util.Arrays;
    36 import javax.tools.Diagnostic;
    37 import javax.tools.JavaCompiler;
    38 import javax.tools.JavaFileObject;
    39 import javax.tools.SimpleJavaFileObject;
    40 import javax.tools.ToolProvider;
    42 public class CompletionFailure {
    44     public static void main(String... args) throws Exception {
    46         String SCRATCH_DIR = System.getProperty("user.dir");
    47         JavaCompiler javacTool = ToolProvider.getSystemJavaCompiler();
    48         File scratchDir = new File(SCRATCH_DIR);
    50         sourceA.dumpTo(scratchDir);
    51         sourceB.dumpTo(scratchDir);
    53         JavacTask ct = (JavacTask)javacTool.getTask(null, null, null,
    54                 null, null, Arrays.asList(sourceA.asJFO(scratchDir), sourceB.asJFO(scratchDir)));
    56         ct.generate();
    58         remove(scratchDir, "A.java");
    59         remove(scratchDir, "B.java");
    60         remove(scratchDir, "A.class");
    62         sourceC.dumpTo(scratchDir);
    63         sourceD.dumpTo(scratchDir);
    65         DiagnosticChecker diagChecker = new DiagnosticChecker();
    66         ct = (JavacTask)javacTool.getTask(null, null, diagChecker,
    67                 Arrays.asList("-XDrawDiagnostics", "-cp", scratchDir.getAbsolutePath()),
    68                 null, Arrays.asList(sourceC.asJFO(scratchDir), sourceD.asJFO(scratchDir)));
    69         try {
    70             ct.analyze();
    71         } catch (Throwable ex) {
    72             //ignore abort exception thrown by javac
    73         }
    75         if (!diagChecker.errorFound) {
    76             throw new AssertionError("Missing diagnostic");
    77         }
    78     }
    80     static void remove(File dir, String fileName) {
    81         File fileToRemove = new File(dir, fileName);
    82         fileToRemove.delete();
    83     }
    85     static class JavaSource {
    86         String contents;
    87         String filename;
    89         public JavaSource(String filename, String contents) {
    90             this.filename =  filename;
    91             this.contents = contents;
    92         }
    94         void dumpTo(java.io.File loc) throws Exception {
    95             File file = new File(loc, filename);
    96             BufferedWriter bw = new java.io.BufferedWriter(new FileWriter(file));
    97             bw.append(contents);
    98             bw.close();
    99         }
   101         SimpleJavaFileObject asJFO(java.io.File dir) {
   102             return new SimpleJavaFileObject(new File(dir, filename).toURI(), JavaFileObject.Kind.SOURCE) {
   103                 @Override
   104                 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   105                     return contents;
   106                 }
   107             };
   108         }
   109     }
   111     static JavaSource sourceA = new JavaSource("A.java", "public interface A { }\n");
   113     static JavaSource sourceB = new JavaSource("B.java", "public class B implements A {\n" +
   114                                                "   public static Object n() { return null; }\n" +
   115                                                "}\n");
   117     static JavaSource sourceC = new JavaSource("C.java", "public class C {\n" +
   118                                                "   void test(B b) {\n" +
   119                                                "      D.m(B.n());\n" +
   120                                                "}  }\n");
   122     static JavaSource sourceD = new JavaSource("D.java", "public class D {\n" +
   123                                                "   static void m(Object o) { }\n" +
   124                                                "}\n");
   126     static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   128         boolean errorFound;
   130         public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   131             if (diagnostic.getKind() == Diagnostic.Kind.ERROR &&
   132                     diagnostic.getCode().contains("compiler.err.cant.access")) {
   133                 errorFound = true;
   134             }
   135         }
   136     }
   137 }

mercurial