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
  27  * @summary Ensure Messages propogated between rounds
  28  * @library ../lib
  29  * @build JavacTestingAbstractProcessor T6597678
  30  * @run main T6597678
  31  */
  32 
  33 import java.io.*;
  34 import java.util.*;
  35 import javax.annotation.processing.RoundEnvironment;
  36 import javax.annotation.processing.SupportedOptions;
  37 import javax.lang.model.element.TypeElement;
  38 import javax.tools.Diagnostic;
  39 
  40 
  41 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
  42 import com.sun.tools.javac.util.Context;
  43 import com.sun.tools.javac.util.JavacMessages;
  44 
  45 public class T6597678 extends JavacTestingAbstractProcessor {
  46     public static void main(String... args) throws Exception {
  47         new T6597678().run();
  48     }
  49 
  50 
  51     void run() throws Exception {
  52         String myName = T6597678.class.getSimpleName();
  53         File testSrc = new File(System.getProperty("test.src"));
  54         File file = new File(testSrc, myName + ".java");
  55 
  56         compile(
  57             "-proc:only",
  58             "-processor", myName,
  59             file.getPath());
  60     }
  61 
  62     void compile(String... args) throws Exception {
  63         StringWriter sw = new StringWriter();
  64         PrintWriter pw = new PrintWriter(sw);
  65         int rc = com.sun.tools.javac.Main.compile(args, pw);
  66         pw.close();
  67         String out = sw.toString();
  68         if (!out.isEmpty())
  69             System.err.println(out);
  70         if (rc != 0)
  71             throw new Exception("compilation failed unexpectedly: rc=" + rc);
  72     }
  73 
  74     //---------------
  75 
  76     @Override
  77     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  78         Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
  79         Locale locale = context.get(Locale.class);
  80         JavacMessages messages = context.get(JavacMessages.messagesKey);
  81 
  82         round++;
  83         if (round == 1) {
  84             initialLocale = locale;
  85             initialMessages = messages;
  86         } else {
  87             checkEqual("locale", locale, initialLocale);
  88             checkEqual("messages", messages, initialMessages);
  89         }
  90 
  91         return true;
  92     }
  93 
  94     <T> void checkEqual(String label, T actual, T expected) {
  95         if (actual != expected)
  96             messager.printMessage(Diagnostic.Kind.ERROR,
  97                     "Unexpected value for " + label
  98                     + "; expected: " + expected
  99                     + "; found: " + actual);
 100     }
 101 
 102     int round = 0;
 103     Locale initialLocale;
 104     JavacMessages initialMessages;
 105 }