MessagerDiags.java revision 3294:9adfb22ff08f
1/* 2 * Copyright (c) 2012, 2015, 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 */ 23 24/* 25 * @test 26 * @bug 7166010 27 * @summary warnings printed by annotation processors uses incorrect source 28 * @modules jdk.compiler 29 */ 30import com.sun.source.util.JavacTask; 31import java.io.IOException; 32import java.net.URI; 33import java.util.ArrayList; 34import java.util.Arrays; 35import java.util.LinkedList; 36import java.util.List; 37import java.util.Set; 38import javax.annotation.processing.AbstractProcessor; 39import javax.annotation.processing.Messager; 40import javax.annotation.processing.RoundEnvironment; 41import javax.annotation.processing.SupportedAnnotationTypes; 42import javax.annotation.processing.SupportedSourceVersion; 43import javax.lang.model.SourceVersion; 44import javax.lang.model.element.Element; 45import javax.lang.model.element.TypeElement; 46import javax.tools.Diagnostic; 47import javax.tools.DiagnosticCollector; 48import javax.tools.JavaCompiler; 49import javax.tools.JavaFileObject; 50import javax.tools.SimpleJavaFileObject; 51import javax.tools.ToolProvider; 52 53import static javax.tools.Diagnostic.Kind.*; 54import static javax.tools.JavaFileObject.Kind.*; 55 56@SupportedSourceVersion(SourceVersion.RELEASE_8) 57@SupportedAnnotationTypes("*") 58public class MessagerDiags extends AbstractProcessor { 59 static final String CNAME = "Test"; 60 static final String TEST_JAVA = CNAME + ".java"; 61 static final String TEST_JAVA_URI_NAME = "myfo:/" + TEST_JAVA; 62 static final String WRN_NO_SOURCE = "warning without source"; 63 static final String WRN_WITH_SOURCE = "warning with source"; 64 static final String NONE = "<none>"; 65 static final String[] EXPECTED = { NONE + ":-1--1:" + WRN_NO_SOURCE, 66 TEST_JAVA + ":0-13:" + WRN_WITH_SOURCE, 67 NONE + ":-1--1:" + WRN_NO_SOURCE 68 }; 69 70 @Override 71 public boolean process(Set<? extends TypeElement> annotations, 72 RoundEnvironment roundEnv) { 73 Messager messager = processingEnv.getMessager(); 74 for (Element e : roundEnv.getRootElements()) { 75 messager.printMessage(WARNING, WRN_NO_SOURCE); 76 messager.printMessage(WARNING, WRN_WITH_SOURCE, e); 77 messager.printMessage(WARNING, WRN_NO_SOURCE); 78 } 79 return false; 80 } 81 82 public static void main(String... args) throws IOException { 83 final String bootPath = System.getProperty("sun.boot.class.path"); 84 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 85 assert tool != null; 86 87 DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<>(); 88 List<String> options = Arrays.asList( 89 "-source", "1.8", 90 "-Xlint:-options", 91 "-classpath", System.getProperty("java.class.path"), 92 "-processor", MessagerDiags.class.getName()); 93 JavacTask ct = (JavacTask)tool.getTask(null, null, dc, options, null, 94 Arrays.asList(new MyFileObject("class " + CNAME + " {}"))); 95 ct.analyze(); 96 97 List<String> obtainedErrors = new ArrayList<>(); 98 99 for (Diagnostic<? extends JavaFileObject> d : dc.getDiagnostics()) { 100 String dSource; 101 if (d.getSource() != null) { 102 dSource = d.getSource().toUri().getPath(); 103 dSource = dSource.substring(dSource.lastIndexOf('/') + 1); 104 } else { 105 dSource = NONE; 106 } 107 obtainedErrors.add(dSource + ":" + d.getStartPosition() + "-" + 108 d.getEndPosition() + ":" + d.getMessage(null)); 109 } 110 List<String> expectedErrors = Arrays.asList(EXPECTED); 111 if (!expectedErrors.equals(obtainedErrors)) { 112 System.err.println("Expected: " + expectedErrors); 113 System.err.println("Obtained: " + obtainedErrors); 114 throw new AssertionError("Messages don't match"); 115 } 116 } 117 118 static class MyFileObject extends SimpleJavaFileObject { 119 private String text; 120 public MyFileObject(String text) { 121 super(URI.create(TEST_JAVA_URI_NAME), SOURCE); 122 this.text = text; 123 } 124 @Override 125 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 126 return text; 127 } 128 } 129} 130