Tue, 04 Jun 2013 11:30:51 +0100
7116676: RichDiagnosticFormatter throws NPE when formatMessage is called directly
Summary: Fix NPE in RichDiagnosticFormatter.formatMessage
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/util/RichDiagnosticFormatter.java Mon Jun 03 17:24:47 2013 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/util/RichDiagnosticFormatter.java Tue Jun 04 11:30:51 2013 +0100 1.3 @@ -125,6 +125,13 @@ 1.4 return sb.toString(); 1.5 } 1.6 1.7 + @Override 1.8 + public String formatMessage(JCDiagnostic diag, Locale l) { 1.9 + nameSimplifier = new ClassNameSimplifier(); 1.10 + preprocessDiagnostic(diag); 1.11 + return super.formatMessage(diag, l); 1.12 + } 1.13 + 1.14 /** 1.15 * Sets the type/symbol printer used by this formatter. 1.16 * @param printer the rich printer to be set
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/Diagnostics/7116676/T7116676.java Tue Jun 04 11:30:51 2013 +0100 2.3 @@ -0,0 +1,109 @@ 2.4 +/* 2.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 7116676 2.30 + * @summary RichDiagnosticFormatter throws NPE when formatMessage is called directly 2.31 + */ 2.32 + 2.33 +import com.sun.source.util.JavacTask; 2.34 +import com.sun.tools.javac.api.ClientCodeWrapper.Trusted; 2.35 +import com.sun.tools.javac.api.DiagnosticFormatter; 2.36 +import com.sun.tools.javac.api.JavacTaskImpl; 2.37 +import com.sun.tools.javac.util.Assert; 2.38 +import com.sun.tools.javac.util.JCDiagnostic; 2.39 +import com.sun.tools.javac.util.Log; 2.40 +import java.io.IOException; 2.41 +import java.net.URI; 2.42 +import java.util.ArrayList; 2.43 +import java.util.Arrays; 2.44 +import java.util.List; 2.45 +import java.util.Locale; 2.46 +import javax.tools.Diagnostic; 2.47 +import javax.tools.DiagnosticListener; 2.48 +import javax.tools.JavaCompiler; 2.49 +import javax.tools.JavaFileObject; 2.50 +import javax.tools.SimpleJavaFileObject; 2.51 +import javax.tools.ToolProvider; 2.52 + 2.53 +public class T7116676 { 2.54 + 2.55 + public static void main(String[] args) throws Exception { 2.56 + T7116676 test = new T7116676(); 2.57 + test.testThroughFormatterFormat(); 2.58 + } 2.59 + 2.60 + static class JavaSource extends SimpleJavaFileObject { 2.61 + private String text = "package test;\n" + 2.62 + "public class Test {\n" + 2.63 + " private void t(java.util.List<? extends String> l) {\n" + 2.64 + " t(java.util.Collections.singleton(l));\n" + 2.65 + "} }"; 2.66 + 2.67 + public JavaSource() { 2.68 + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 2.69 + } 2.70 + @Override 2.71 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 2.72 + return text; 2.73 + } 2.74 + } 2.75 + 2.76 + void assertEquals(String req, String found) { 2.77 + if (!found.equals(req)) { 2.78 + throw new AssertionError(String.format("Error. Found: \n\n%s ; Expected: \n\n%s", found, req)); 2.79 + } 2.80 + } 2.81 + 2.82 + public void testThroughFormatterFormat() throws IOException { 2.83 + final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 2.84 + DiagnosticChecker dc = new DiagnosticChecker("compiler.err.prob.found.req"); 2.85 + JavacTask ct = (JavacTask)tool.getTask(null, null, dc, null, null, Arrays.asList(new JavaSource())); 2.86 + ct.analyze(); 2.87 + DiagnosticFormatter<JCDiagnostic> formatter = 2.88 + Log.instance(((JavacTaskImpl) ct).getContext()).getDiagnosticFormatter(); 2.89 + String msg = formatter.formatMessage(dc.diag, Locale.getDefault()); 2.90 + //no redundant package qualifiers 2.91 + Assert.check(msg.indexOf("java.") == -1, msg); 2.92 + } 2.93 + 2.94 + @Trusted 2.95 + private static final class DiagnosticChecker implements DiagnosticListener<JavaFileObject> { 2.96 + 2.97 + String expectedKey; 2.98 + JCDiagnostic diag; 2.99 + 2.100 + DiagnosticChecker(String expectedKey) { 2.101 + this.expectedKey = expectedKey; 2.102 + } 2.103 + 2.104 + @Override 2.105 + public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 2.106 + JCDiagnostic diag = (JCDiagnostic)diagnostic; 2.107 + if (diagnostic.getCode().equals(expectedKey)) { 2.108 + this.diag = diag; 2.109 + } 2.110 + } 2.111 + } 2.112 +}