MessagerDiags.java revision 3567:d7aa2b610144
176264Sgreen/*
276264Sgreen * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
3255460Sdes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4255460Sdes *
576264Sgreen * This code is free software; you can redistribute it and/or modify it
6126282Sdes * under the terms of the GNU General Public License version 2 only, as
7158519Sdes * published by the Free Software Foundation.
876264Sgreen *
9255460Sdes * This code is distributed in the hope that it will be useful, but WITHOUT
10255460Sdes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11197679Sdes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12255460Sdes * version 2 for more details (a copy is included in the LICENSE file that
13197679Sdes * accompanied this code).
14255460Sdes *
15255460Sdes * You should have received a copy of the GNU General Public License version
16255386Sdes * 2 along with this work; if not, write to the Free Software Foundation,
1776264Sgreen * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18255460Sdes *
19255460Sdes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20255460Sdes * or visit www.oracle.com if you need additional information or have any
21255460Sdes * questions.
22255460Sdes */
23255460Sdes
24255460Sdes/*
25255460Sdes * @test
26255460Sdes * @bug 7166010
27255460Sdes * @summary  warnings printed by annotation processors uses incorrect source
2876264Sgreen * @modules jdk.compiler
2976264Sgreen */
3076264Sgreenimport com.sun.source.util.JavacTask;
31158529Sdesimport java.io.IOException;
32158529Sdesimport 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 JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
84        assert tool != null;
85
86        DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<>();
87        List<String> options = Arrays.asList(
88                "-source", "1.8",
89                "-Xlint:-options",
90                "-classpath", System.getProperty("java.class.path"),
91                "-processor", MessagerDiags.class.getName());
92        JavacTask ct = (JavacTask)tool.getTask(null, null, dc, options, null,
93                        Arrays.asList(new MyFileObject("class " + CNAME + " {}")));
94        ct.analyze();
95
96        List<String> obtainedErrors = new ArrayList<>();
97
98        for (Diagnostic<? extends JavaFileObject> d : dc.getDiagnostics()) {
99            String dSource;
100            if (d.getSource() != null) {
101                dSource = d.getSource().toUri().getPath();
102                dSource = dSource.substring(dSource.lastIndexOf('/') + 1);
103            } else {
104                dSource = NONE;
105            }
106            obtainedErrors.add(dSource + ":" + d.getStartPosition() + "-" +
107                    d.getEndPosition() + ":" + d.getMessage(null));
108        }
109        List<String> expectedErrors = Arrays.asList(EXPECTED);
110        if (!expectedErrors.equals(obtainedErrors)) {
111            System.err.println("Expected: " + expectedErrors);
112            System.err.println("Obtained: " + obtainedErrors);
113            throw new AssertionError("Messages don't match");
114        }
115    }
116
117    static class MyFileObject extends SimpleJavaFileObject {
118        private String text;
119        public MyFileObject(String text) {
120            super(URI.create(TEST_JAVA_URI_NAME), SOURCE);
121            this.text = text;
122        }
123        @Override
124        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
125            return text;
126        }
127    }
128}
129