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