test/tools/javac/processing/messager/MessagerDiags.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/processing/messager/MessagerDiags.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,131 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +/*
    1.30 + * @test
    1.31 + * @bug 7166010
    1.32 + * @summary  warnings printed by annotation processors uses incorrect source
    1.33 + */
    1.34 +import com.sun.source.util.JavacTask;
    1.35 +import java.io.IOException;
    1.36 +import java.net.URI;
    1.37 +import java.util.ArrayList;
    1.38 +import java.util.Arrays;
    1.39 +import java.util.LinkedList;
    1.40 +import java.util.List;
    1.41 +import java.util.Set;
    1.42 +import javax.annotation.processing.AbstractProcessor;
    1.43 +import javax.annotation.processing.Messager;
    1.44 +import javax.annotation.processing.RoundEnvironment;
    1.45 +import javax.annotation.processing.SupportedAnnotationTypes;
    1.46 +import javax.annotation.processing.SupportedSourceVersion;
    1.47 +import javax.lang.model.SourceVersion;
    1.48 +import javax.lang.model.element.Element;
    1.49 +import javax.lang.model.element.TypeElement;
    1.50 +import javax.tools.Diagnostic;
    1.51 +import javax.tools.DiagnosticCollector;
    1.52 +import javax.tools.JavaCompiler;
    1.53 +import javax.tools.JavaFileObject;
    1.54 +import javax.tools.SimpleJavaFileObject;
    1.55 +import javax.tools.ToolProvider;
    1.56 +
    1.57 +import static javax.tools.Diagnostic.Kind.*;
    1.58 +import static javax.tools.JavaFileObject.Kind.*;
    1.59 +
    1.60 +@SupportedSourceVersion(SourceVersion.RELEASE_6)
    1.61 +@SupportedAnnotationTypes("*")
    1.62 +public class MessagerDiags extends AbstractProcessor {
    1.63 +    static final String CNAME = "Test";
    1.64 +    static final String TEST_JAVA = CNAME + ".java";
    1.65 +    static final String TEST_JAVA_URI_NAME = "myfo:/" + TEST_JAVA;
    1.66 +    static final String WRN_NO_SOURCE   = "warning without source";
    1.67 +    static final String WRN_WITH_SOURCE = "warning with source";
    1.68 +    static final String NONE = "<none>";
    1.69 +    static final String[] EXPECTED = { NONE + ":-1--1:" + WRN_NO_SOURCE,
    1.70 +                                       TEST_JAVA + ":0-13:" + WRN_WITH_SOURCE,
    1.71 +                                       NONE + ":-1--1:" + WRN_NO_SOURCE
    1.72 +    };
    1.73 +
    1.74 +    @Override
    1.75 +    public boolean process(Set<? extends TypeElement> annotations,
    1.76 +                           RoundEnvironment roundEnv) {
    1.77 +        Messager messager = processingEnv.getMessager();
    1.78 +        for (Element e : roundEnv.getRootElements()) {
    1.79 +            messager.printMessage(WARNING, WRN_NO_SOURCE);
    1.80 +            messager.printMessage(WARNING, WRN_WITH_SOURCE, e);
    1.81 +            messager.printMessage(WARNING, WRN_NO_SOURCE);
    1.82 +        }
    1.83 +        return false;
    1.84 +    }
    1.85 +
    1.86 +    public static void main(String... args) throws IOException {
    1.87 +        final String bootPath = System.getProperty("sun.boot.class.path");
    1.88 +        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    1.89 +        assert tool != null;
    1.90 +
    1.91 +        DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<>();
    1.92 +        List<String> options = new LinkedList<>();
    1.93 +        options.addAll(Arrays.asList("-bootclasspath",  bootPath,
    1.94 +                        "-source", "1.6", "-classpath",
    1.95 +                        System.getProperty("java.class.path")));
    1.96 +        options.addAll(Arrays.asList("-processor",
    1.97 +                       MessagerDiags.class.getName()));
    1.98 +        JavacTask ct = (JavacTask)tool.getTask(null, null, dc, options, null,
    1.99 +                        Arrays.asList(new MyFileObject("class " + CNAME + " {}")));
   1.100 +        ct.analyze();
   1.101 +
   1.102 +        List<String> obtainedErrors = new ArrayList<>();
   1.103 +
   1.104 +        for (Diagnostic<? extends JavaFileObject> d : dc.getDiagnostics()) {
   1.105 +            String dSource;
   1.106 +            if (d.getSource() != null) {
   1.107 +                dSource = d.getSource().toUri().getPath();
   1.108 +                dSource = dSource.substring(dSource.lastIndexOf('/') + 1);
   1.109 +            } else {
   1.110 +                dSource = NONE;
   1.111 +            }
   1.112 +            obtainedErrors.add(dSource + ":" + d.getStartPosition() + "-" +
   1.113 +                    d.getEndPosition() + ":" + d.getMessage(null));
   1.114 +        }
   1.115 +        List<String> expectedErrors = Arrays.asList(EXPECTED);
   1.116 +        if (!expectedErrors.equals(obtainedErrors)) {
   1.117 +            System.err.println("Expected: " + expectedErrors);
   1.118 +            System.err.println("Obtained: " + obtainedErrors);
   1.119 +            throw new AssertionError("Messages don't match");
   1.120 +        }
   1.121 +    }
   1.122 +
   1.123 +    static class MyFileObject extends SimpleJavaFileObject {
   1.124 +        private String text;
   1.125 +        public MyFileObject(String text) {
   1.126 +            super(URI.create(TEST_JAVA_URI_NAME), SOURCE);
   1.127 +            this.text = text;
   1.128 +        }
   1.129 +        @Override
   1.130 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.131 +            return text;
   1.132 +        }
   1.133 +    }
   1.134 +}

mercurial