T6597678.java revision 1465:b52a38d4536c
1/* 2 * Copyright (c) 2011, 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 6597678 6449184 27 * @summary Ensure Messages propogated between rounds 28 * @library /tools/javac/lib 29 * @build JavacTestingAbstractProcessor T6597678 30 * @run main T6597678 31 */ 32 33import java.io.*; 34import java.util.*; 35import javax.annotation.processing.RoundEnvironment; 36import javax.annotation.processing.SupportedOptions; 37import javax.lang.model.element.TypeElement; 38import javax.tools.Diagnostic; 39 40 41import com.sun.tools.javac.processing.JavacProcessingEnvironment; 42import com.sun.tools.javac.util.Context; 43import com.sun.tools.javac.util.JavacMessages; 44import com.sun.tools.javac.util.Log; 45 46@SupportedOptions("WriterString") 47public class T6597678 extends JavacTestingAbstractProcessor { 48 public static void main(String... args) throws Exception { 49 new T6597678().run(); 50 } 51 52 void run() throws Exception { 53 String myName = T6597678.class.getSimpleName(); 54 File testSrc = new File(System.getProperty("test.src")); 55 File file = new File(testSrc, myName + ".java"); 56 57 StringWriter sw = new StringWriter(); 58 PrintWriter pw = new PrintWriter(sw); 59 60 compile(sw, pw, 61 "-proc:only", 62 "-processor", myName, 63 "-AWriterString=" + pw.toString(), 64 file.getPath()); 65 } 66 67 void compile(StringWriter sw, PrintWriter pw, String... args) throws Exception { 68 int rc = com.sun.tools.javac.Main.compile(args, pw); 69 pw.close(); 70 String out = sw.toString(); 71 if (!out.isEmpty()) 72 System.err.println(out); 73 if (rc != 0) 74 throw new Exception("compilation failed unexpectedly: rc=" + rc); 75 } 76 77 //--------------- 78 79 @Override 80 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 81 Context context = ((JavacProcessingEnvironment) processingEnv).getContext(); 82 Log log = Log.instance(context); 83 PrintWriter noteOut = log.getWriter(Log.WriterKind.NOTICE); 84 PrintWriter warnOut = log.getWriter(Log.WriterKind.WARNING); 85 PrintWriter errOut = log.getWriter(Log.WriterKind.ERROR); 86 Locale locale = context.get(Locale.class); 87 JavacMessages messages = context.get(JavacMessages.messagesKey); 88 89 round++; 90 if (round == 1) { 91 initialLocale = locale; 92 initialMessages = messages; 93 initialNoteWriter = noteOut; 94 initialWarnWriter = warnOut; 95 initialErrWriter = errOut; 96 97 String writerStringOpt = options.get("WriterString").intern(); 98 checkEqual("noteWriterString", noteOut.toString().intern(), writerStringOpt); 99 checkEqual("warnWriterString", warnOut.toString().intern(), writerStringOpt); 100 checkEqual("errWriterString", errOut.toString().intern(), writerStringOpt); 101 } else { 102 checkEqual("locale", locale, initialLocale); 103 checkEqual("messages", messages, initialMessages); 104 checkEqual("noteWriter", noteOut, initialNoteWriter); 105 checkEqual("warnWriter", warnOut, initialWarnWriter); 106 checkEqual("errWriter", errOut, initialErrWriter); 107 } 108 109 return true; 110 } 111 112 <T> void checkEqual(String label, T actual, T expected) { 113 if (actual != expected) 114 messager.printMessage(Diagnostic.Kind.ERROR, 115 "Unexpected value for " + label 116 + "; expected: " + expected 117 + "; found: " + actual); 118 } 119 120 int round = 0; 121 Locale initialLocale; 122 JavacMessages initialMessages; 123 PrintWriter initialNoteWriter; 124 PrintWriter initialWarnWriter; 125 PrintWriter initialErrWriter; 126} 127