Fri, 04 May 2012 07:55:51 -0700
7166010: (javac) JavacMessager incorrectly restores log source file
Reviewed-by: jjg
Contributed-by: jan.lahoda@oracle.com
1.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacMessager.java Thu Apr 26 14:07:29 2012 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacMessager.java Fri May 04 07:55:51 2012 -0700 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. 1.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 * 1.9 * This code is free software; you can redistribute it and/or modify it 1.10 @@ -99,6 +99,7 @@ 1.11 if (treeTop != null) { 1.12 newSource = treeTop.snd.sourcefile; 1.13 if (newSource != null) { 1.14 + // save the old version and reinstate it later 1.15 oldSource = log.useSource(newSource); 1.16 pos = treeTop.fst.pos(); 1.17 } 1.18 @@ -131,7 +132,8 @@ 1.19 break; 1.20 } 1.21 } finally { 1.22 - if (oldSource != null) 1.23 + // reinstate the saved version, only if it was saved earlier 1.24 + if (newSource != null) 1.25 log.useSource(oldSource); 1.26 } 1.27 }
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/processing/messager/MessagerDiags.java Fri May 04 07:55:51 2012 -0700 2.3 @@ -0,0 +1,131 @@ 2.4 +/* 2.5 + * Copyright (c) 2012, 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. Oracle designates this 2.11 + * particular file as subject to the "Classpath" exception as provided 2.12 + * by Oracle in the LICENSE file that accompanied this code. 2.13 + * 2.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.17 + * version 2 for more details (a copy is included in the LICENSE file that 2.18 + * accompanied this code). 2.19 + * 2.20 + * You should have received a copy of the GNU General Public License version 2.21 + * 2 along with this work; if not, write to the Free Software Foundation, 2.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.23 + * 2.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.25 + * or visit www.oracle.com if you need additional information or have any 2.26 + * questions. 2.27 + */ 2.28 + 2.29 +/* 2.30 + * @test 2.31 + * @bug 7166010 2.32 + * @summary warnings printed by annotation processors uses incorrect source 2.33 + */ 2.34 +import com.sun.source.util.JavacTask; 2.35 +import java.io.IOException; 2.36 +import java.net.URI; 2.37 +import java.util.ArrayList; 2.38 +import java.util.Arrays; 2.39 +import java.util.LinkedList; 2.40 +import java.util.List; 2.41 +import java.util.Set; 2.42 +import javax.annotation.processing.AbstractProcessor; 2.43 +import javax.annotation.processing.Messager; 2.44 +import javax.annotation.processing.RoundEnvironment; 2.45 +import javax.annotation.processing.SupportedAnnotationTypes; 2.46 +import javax.annotation.processing.SupportedSourceVersion; 2.47 +import javax.lang.model.SourceVersion; 2.48 +import javax.lang.model.element.Element; 2.49 +import javax.lang.model.element.TypeElement; 2.50 +import javax.tools.Diagnostic; 2.51 +import javax.tools.DiagnosticCollector; 2.52 +import javax.tools.JavaCompiler; 2.53 +import javax.tools.JavaFileObject; 2.54 +import javax.tools.SimpleJavaFileObject; 2.55 +import javax.tools.ToolProvider; 2.56 + 2.57 +import static javax.tools.Diagnostic.Kind.*; 2.58 +import static javax.tools.JavaFileObject.Kind.*; 2.59 + 2.60 +@SupportedSourceVersion(SourceVersion.RELEASE_6) 2.61 +@SupportedAnnotationTypes("*") 2.62 +public class MessagerDiags extends AbstractProcessor { 2.63 + static final String CNAME = "Test"; 2.64 + static final String TEST_JAVA = CNAME + ".java"; 2.65 + static final String TEST_JAVA_URI_NAME = "myfo:/" + TEST_JAVA; 2.66 + static final String WRN_NO_SOURCE = "warning without source"; 2.67 + static final String WRN_WITH_SOURCE = "warning with source"; 2.68 + static final String NONE = "<none>"; 2.69 + static final String[] EXPECTED = { NONE + ":-1--1:" + WRN_NO_SOURCE, 2.70 + TEST_JAVA + ":0-13:" + WRN_WITH_SOURCE, 2.71 + NONE + ":-1--1:" + WRN_NO_SOURCE 2.72 + }; 2.73 + 2.74 + @Override 2.75 + public boolean process(Set<? extends TypeElement> annotations, 2.76 + RoundEnvironment roundEnv) { 2.77 + Messager messager = processingEnv.getMessager(); 2.78 + for (Element e : roundEnv.getRootElements()) { 2.79 + messager.printMessage(WARNING, WRN_NO_SOURCE); 2.80 + messager.printMessage(WARNING, WRN_WITH_SOURCE, e); 2.81 + messager.printMessage(WARNING, WRN_NO_SOURCE); 2.82 + } 2.83 + return false; 2.84 + } 2.85 + 2.86 + public static void main(String... args) throws IOException { 2.87 + final String bootPath = System.getProperty("sun.boot.class.path"); 2.88 + final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 2.89 + assert tool != null; 2.90 + 2.91 + DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<>(); 2.92 + List<String> options = new LinkedList<>(); 2.93 + options.addAll(Arrays.asList("-bootclasspath", bootPath, 2.94 + "-source", "1.6", "-classpath", 2.95 + System.getProperty("java.class.path"))); 2.96 + options.addAll(Arrays.asList("-processor", 2.97 + MessagerDiags.class.getName())); 2.98 + JavacTask ct = (JavacTask)tool.getTask(null, null, dc, options, null, 2.99 + Arrays.asList(new MyFileObject("class " + CNAME + " {}"))); 2.100 + ct.analyze(); 2.101 + 2.102 + List<String> obtainedErrors = new ArrayList<>(); 2.103 + 2.104 + for (Diagnostic<? extends JavaFileObject> d : dc.getDiagnostics()) { 2.105 + String dSource; 2.106 + if (d.getSource() != null) { 2.107 + dSource = d.getSource().toUri().getPath(); 2.108 + dSource = dSource.substring(dSource.lastIndexOf('/') + 1); 2.109 + } else { 2.110 + dSource = NONE; 2.111 + } 2.112 + obtainedErrors.add(dSource + ":" + d.getStartPosition() + "-" + 2.113 + d.getEndPosition() + ":" + d.getMessage(null)); 2.114 + } 2.115 + List<String> expectedErrors = Arrays.asList(EXPECTED); 2.116 + if (!expectedErrors.equals(obtainedErrors)) { 2.117 + System.err.println("Expected: " + expectedErrors); 2.118 + System.err.println("Obtained: " + obtainedErrors); 2.119 + throw new AssertionError("Messages don't match"); 2.120 + } 2.121 + } 2.122 + 2.123 + static class MyFileObject extends SimpleJavaFileObject { 2.124 + private String text; 2.125 + public MyFileObject(String text) { 2.126 + super(URI.create(TEST_JAVA_URI_NAME), SOURCE); 2.127 + this.text = text; 2.128 + } 2.129 + @Override 2.130 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 2.131 + return text; 2.132 + } 2.133 + } 2.134 +}