test/tools/javac/util/T6597678.java

Mon, 26 Oct 2015 13:23:30 -0700

author
asaha
date
Mon, 26 Oct 2015 13:23:30 -0700
changeset 2999
683b3e7e05a7
parent 1466
b52a38d4536c
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u76-b00 for changeset 10ffafaf5340

jjg@944 1 /*
jjg@944 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
jjg@944 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@944 4 *
jjg@944 5 * This code is free software; you can redistribute it and/or modify it
jjg@944 6 * under the terms of the GNU General Public License version 2 only, as
jjg@944 7 * published by the Free Software Foundation.
jjg@944 8 *
jjg@944 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@944 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@944 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@944 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@944 13 * accompanied this code).
jjg@944 14 *
jjg@944 15 * You should have received a copy of the GNU General Public License version
jjg@944 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@944 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@944 18 *
jjg@944 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@944 20 * or visit www.oracle.com if you need additional information or have any
jjg@944 21 * questions.
jjg@944 22 */
jjg@944 23
jjg@944 24 /**
jjg@944 25 * @test
darcy@1044 26 * @bug 6597678 6449184
jjg@944 27 * @summary Ensure Messages propogated between rounds
darcy@1466 28 * @library /tools/javac/lib
jjg@944 29 * @build JavacTestingAbstractProcessor T6597678
jjg@944 30 * @run main T6597678
jjg@944 31 */
jjg@944 32
jjg@944 33 import java.io.*;
jjg@944 34 import java.util.*;
jjg@944 35 import javax.annotation.processing.RoundEnvironment;
jjg@944 36 import javax.annotation.processing.SupportedOptions;
jjg@944 37 import javax.lang.model.element.TypeElement;
jjg@944 38 import javax.tools.Diagnostic;
jjg@944 39
jjg@944 40
jjg@944 41 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
jjg@944 42 import com.sun.tools.javac.util.Context;
jjg@944 43 import com.sun.tools.javac.util.JavacMessages;
jjg@1159 44 import com.sun.tools.javac.util.Log;
jjg@944 45
darcy@1044 46 @SupportedOptions("WriterString")
jjg@944 47 public class T6597678 extends JavacTestingAbstractProcessor {
jjg@944 48 public static void main(String... args) throws Exception {
jjg@944 49 new T6597678().run();
jjg@944 50 }
jjg@944 51
jjg@944 52 void run() throws Exception {
jjg@944 53 String myName = T6597678.class.getSimpleName();
jjg@944 54 File testSrc = new File(System.getProperty("test.src"));
jjg@944 55 File file = new File(testSrc, myName + ".java");
jjg@944 56
darcy@1044 57 StringWriter sw = new StringWriter();
darcy@1044 58 PrintWriter pw = new PrintWriter(sw);
darcy@1044 59
darcy@1044 60 compile(sw, pw,
jjg@944 61 "-proc:only",
jjg@944 62 "-processor", myName,
darcy@1044 63 "-AWriterString=" + pw.toString(),
jjg@944 64 file.getPath());
jjg@944 65 }
jjg@944 66
darcy@1044 67 void compile(StringWriter sw, PrintWriter pw, String... args) throws Exception {
jjg@944 68 int rc = com.sun.tools.javac.Main.compile(args, pw);
jjg@944 69 pw.close();
jjg@944 70 String out = sw.toString();
jjg@944 71 if (!out.isEmpty())
jjg@944 72 System.err.println(out);
jjg@944 73 if (rc != 0)
jjg@944 74 throw new Exception("compilation failed unexpectedly: rc=" + rc);
jjg@944 75 }
jjg@944 76
jjg@944 77 //---------------
jjg@944 78
jjg@944 79 @Override
jjg@944 80 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
jjg@944 81 Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
jjg@1159 82 Log log = Log.instance(context);
jjg@1159 83 PrintWriter noteOut = log.getWriter(Log.WriterKind.NOTICE);
jjg@1159 84 PrintWriter warnOut = log.getWriter(Log.WriterKind.WARNING);
jjg@1159 85 PrintWriter errOut = log.getWriter(Log.WriterKind.ERROR);
jjg@944 86 Locale locale = context.get(Locale.class);
jjg@944 87 JavacMessages messages = context.get(JavacMessages.messagesKey);
jjg@944 88
jjg@944 89 round++;
jjg@944 90 if (round == 1) {
jjg@944 91 initialLocale = locale;
jjg@944 92 initialMessages = messages;
jjg@1159 93 initialNoteWriter = noteOut;
jjg@1159 94 initialWarnWriter = warnOut;
jjg@1159 95 initialErrWriter = errOut;
darcy@1044 96
jjg@1159 97 String writerStringOpt = options.get("WriterString").intern();
jjg@1159 98 checkEqual("noteWriterString", noteOut.toString().intern(), writerStringOpt);
jjg@1159 99 checkEqual("warnWriterString", warnOut.toString().intern(), writerStringOpt);
jjg@1159 100 checkEqual("errWriterString", errOut.toString().intern(), writerStringOpt);
jjg@944 101 } else {
jjg@944 102 checkEqual("locale", locale, initialLocale);
jjg@944 103 checkEqual("messages", messages, initialMessages);
jjg@1159 104 checkEqual("noteWriter", noteOut, initialNoteWriter);
jjg@1159 105 checkEqual("warnWriter", warnOut, initialWarnWriter);
jjg@1159 106 checkEqual("errWriter", errOut, initialErrWriter);
jjg@944 107 }
jjg@944 108
jjg@944 109 return true;
jjg@944 110 }
jjg@944 111
jjg@944 112 <T> void checkEqual(String label, T actual, T expected) {
jjg@944 113 if (actual != expected)
jjg@944 114 messager.printMessage(Diagnostic.Kind.ERROR,
jjg@944 115 "Unexpected value for " + label
jjg@944 116 + "; expected: " + expected
jjg@944 117 + "; found: " + actual);
jjg@944 118 }
jjg@944 119
jjg@944 120 int round = 0;
jjg@944 121 Locale initialLocale;
jjg@944 122 JavacMessages initialMessages;
jjg@1159 123 PrintWriter initialNoteWriter;
jjg@1159 124 PrintWriter initialWarnWriter;
jjg@1159 125 PrintWriter initialErrWriter;
jjg@944 126 }

mercurial