aoqi@0: /* aoqi@0: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 7166010 aoqi@0: * @summary warnings printed by annotation processors uses incorrect source aoqi@0: */ aoqi@0: import com.sun.source.util.JavacTask; aoqi@0: import java.io.IOException; aoqi@0: import java.net.URI; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.Arrays; aoqi@0: import java.util.LinkedList; aoqi@0: import java.util.List; aoqi@0: import java.util.Set; aoqi@0: import javax.annotation.processing.AbstractProcessor; aoqi@0: import javax.annotation.processing.Messager; aoqi@0: import javax.annotation.processing.RoundEnvironment; aoqi@0: import javax.annotation.processing.SupportedAnnotationTypes; aoqi@0: import javax.annotation.processing.SupportedSourceVersion; aoqi@0: import javax.lang.model.SourceVersion; aoqi@0: import javax.lang.model.element.Element; aoqi@0: import javax.lang.model.element.TypeElement; aoqi@0: import javax.tools.Diagnostic; aoqi@0: import javax.tools.DiagnosticCollector; aoqi@0: import javax.tools.JavaCompiler; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.SimpleJavaFileObject; aoqi@0: import javax.tools.ToolProvider; aoqi@0: aoqi@0: import static javax.tools.Diagnostic.Kind.*; aoqi@0: import static javax.tools.JavaFileObject.Kind.*; aoqi@0: aoqi@0: @SupportedSourceVersion(SourceVersion.RELEASE_6) aoqi@0: @SupportedAnnotationTypes("*") aoqi@0: public class MessagerDiags extends AbstractProcessor { aoqi@0: static final String CNAME = "Test"; aoqi@0: static final String TEST_JAVA = CNAME + ".java"; aoqi@0: static final String TEST_JAVA_URI_NAME = "myfo:/" + TEST_JAVA; aoqi@0: static final String WRN_NO_SOURCE = "warning without source"; aoqi@0: static final String WRN_WITH_SOURCE = "warning with source"; aoqi@0: static final String NONE = ""; aoqi@0: static final String[] EXPECTED = { NONE + ":-1--1:" + WRN_NO_SOURCE, aoqi@0: TEST_JAVA + ":0-13:" + WRN_WITH_SOURCE, aoqi@0: NONE + ":-1--1:" + WRN_NO_SOURCE aoqi@0: }; aoqi@0: aoqi@0: @Override aoqi@0: public boolean process(Set annotations, aoqi@0: RoundEnvironment roundEnv) { aoqi@0: Messager messager = processingEnv.getMessager(); aoqi@0: for (Element e : roundEnv.getRootElements()) { aoqi@0: messager.printMessage(WARNING, WRN_NO_SOURCE); aoqi@0: messager.printMessage(WARNING, WRN_WITH_SOURCE, e); aoqi@0: messager.printMessage(WARNING, WRN_NO_SOURCE); aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public static void main(String... args) throws IOException { aoqi@0: final String bootPath = System.getProperty("sun.boot.class.path"); aoqi@0: final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); aoqi@0: assert tool != null; aoqi@0: aoqi@0: DiagnosticCollector dc = new DiagnosticCollector<>(); aoqi@0: List options = new LinkedList<>(); aoqi@0: options.addAll(Arrays.asList("-bootclasspath", bootPath, aoqi@0: "-source", "1.6", "-classpath", aoqi@0: System.getProperty("java.class.path"))); aoqi@0: options.addAll(Arrays.asList("-processor", aoqi@0: MessagerDiags.class.getName())); aoqi@0: JavacTask ct = (JavacTask)tool.getTask(null, null, dc, options, null, aoqi@0: Arrays.asList(new MyFileObject("class " + CNAME + " {}"))); aoqi@0: ct.analyze(); aoqi@0: aoqi@0: List obtainedErrors = new ArrayList<>(); aoqi@0: aoqi@0: for (Diagnostic d : dc.getDiagnostics()) { aoqi@0: String dSource; aoqi@0: if (d.getSource() != null) { aoqi@0: dSource = d.getSource().toUri().getPath(); aoqi@0: dSource = dSource.substring(dSource.lastIndexOf('/') + 1); aoqi@0: } else { aoqi@0: dSource = NONE; aoqi@0: } aoqi@0: obtainedErrors.add(dSource + ":" + d.getStartPosition() + "-" + aoqi@0: d.getEndPosition() + ":" + d.getMessage(null)); aoqi@0: } aoqi@0: List expectedErrors = Arrays.asList(EXPECTED); aoqi@0: if (!expectedErrors.equals(obtainedErrors)) { aoqi@0: System.err.println("Expected: " + expectedErrors); aoqi@0: System.err.println("Obtained: " + obtainedErrors); aoqi@0: throw new AssertionError("Messages don't match"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static class MyFileObject extends SimpleJavaFileObject { aoqi@0: private String text; aoqi@0: public MyFileObject(String text) { aoqi@0: super(URI.create(TEST_JAVA_URI_NAME), SOURCE); aoqi@0: this.text = text; aoqi@0: } aoqi@0: @Override aoqi@0: public CharSequence getCharContent(boolean ignoreEncodingErrors) { aoqi@0: return text; aoqi@0: } aoqi@0: } aoqi@0: }