test/tools/javac/Diagnostics/7116676/T7116676.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 3938
93012e2a5d1d
parent 0
959103a6100f
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe

     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 7116676
    27  * @summary RichDiagnosticFormatter throws NPE when formatMessage is called directly
    28  */
    30 import com.sun.source.util.JavacTask;
    31 import com.sun.tools.javac.api.ClientCodeWrapper.Trusted;
    32 import com.sun.tools.javac.api.DiagnosticFormatter;
    33 import com.sun.tools.javac.api.JavacTaskImpl;
    34 import com.sun.tools.javac.util.Assert;
    35 import com.sun.tools.javac.util.JCDiagnostic;
    36 import com.sun.tools.javac.util.Log;
    37 import java.io.IOException;
    38 import java.net.URI;
    39 import java.util.ArrayList;
    40 import java.util.Arrays;
    41 import java.util.List;
    42 import java.util.Locale;
    43 import javax.tools.Diagnostic;
    44 import javax.tools.DiagnosticListener;
    45 import javax.tools.JavaCompiler;
    46 import javax.tools.JavaFileObject;
    47 import javax.tools.SimpleJavaFileObject;
    48 import javax.tools.ToolProvider;
    50 public class T7116676 {
    52     public static void main(String[] args) throws Exception {
    53         T7116676 test = new T7116676();
    54         test.testThroughFormatterFormat();
    55     }
    57     static class JavaSource extends SimpleJavaFileObject {
    58         private String text = "package test;\n" +
    59                               "public class Test {\n" +
    60                               "   private void t(java.util.List<? extends String> l) {\n" +
    61                               "      t(java.util.Collections.singleton(l));\n" +
    62                               "}  }";
    64         public JavaSource() {
    65             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
    66         }
    67         @Override
    68         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
    69             return text;
    70         }
    71     }
    73     void assertEquals(String req, String found) {
    74         if (!found.equals(req)) {
    75             throw new AssertionError(String.format("Error. Found: \n\n%s ; Expected: \n\n%s", found, req));
    76         }
    77     }
    79     public void testThroughFormatterFormat() throws IOException {
    80         final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    81         DiagnosticChecker dc = new DiagnosticChecker("compiler.err.prob.found.req");
    82         JavacTask ct = (JavacTask)tool.getTask(null, null, dc, null, null, Arrays.asList(new JavaSource()));
    83         ct.analyze();
    84         DiagnosticFormatter<JCDiagnostic> formatter =
    85                 Log.instance(((JavacTaskImpl) ct).getContext()).getDiagnosticFormatter();
    86         String msg = formatter.formatMessage(dc.diag, Locale.getDefault());
    87         //no redundant package qualifiers
    88         Assert.check(msg.indexOf("java.") == -1, msg);
    89     }
    91     @Trusted
    92     private static final class DiagnosticChecker implements DiagnosticListener<JavaFileObject> {
    94         String expectedKey;
    95         JCDiagnostic diag;
    97         DiagnosticChecker(String expectedKey) {
    98             this.expectedKey = expectedKey;
    99         }
   101         @Override
   102         public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   103             JCDiagnostic diag = (JCDiagnostic)diagnostic;
   104             if (diagnostic.getCode().equals(expectedKey)) {
   105                 this.diag = diag;
   106             }
   107         }
   108     }
   109 }

mercurial