test/tools/javac/util/T6597678.java

Print this page




   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 }


   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 ../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 @SupportedOptions("WriterString")
  46 public class T6597678 extends JavacTestingAbstractProcessor {
  47     public static void main(String... args) throws Exception {
  48         new T6597678().run();
  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         StringWriter sw = new StringWriter();
  57         PrintWriter pw = new PrintWriter(sw);
  58 
  59         compile(sw, pw,
  60             "-proc:only",
  61             "-processor", myName,
  62             "-AWriterString=" + pw.toString(),
  63             file.getPath());
  64     }
  65 
  66     void compile(StringWriter sw, PrintWriter pw, String... args) throws Exception {


  67         int rc = com.sun.tools.javac.Main.compile(args, pw);
  68         pw.close();
  69         String out = sw.toString();
  70         if (!out.isEmpty())
  71             System.err.println(out);
  72         if (rc != 0)
  73             throw new Exception("compilation failed unexpectedly: rc=" + rc);
  74     }
  75 
  76     //---------------
  77 
  78     @Override
  79     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  80         Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
  81         PrintWriter out = ((JavacProcessingEnvironment) processingEnv).getWriter();
  82         Locale locale = context.get(Locale.class);
  83         JavacMessages messages = context.get(JavacMessages.messagesKey);
  84 
  85         round++;
  86         if (round == 1) {
  87             initialLocale = locale;
  88             initialMessages = messages;
  89             initialWriter = out;
  90 
  91             checkEqual("writerString", out.toString().intern(), options.get("WriterString").intern());
  92         } else {
  93             checkEqual("locale", locale, initialLocale);
  94             checkEqual("messages", messages, initialMessages);
  95             checkEqual("writer", out, initialWriter);
  96         }
  97 
  98         return true;
  99     }
 100 
 101     <T> void checkEqual(String label, T actual, T expected) {
 102         if (actual != expected)
 103             messager.printMessage(Diagnostic.Kind.ERROR,
 104                     "Unexpected value for " + label
 105                     + "; expected: " + expected
 106                     + "; found: " + actual);
 107     }
 108 
 109     int round = 0;
 110     Locale initialLocale;
 111     JavacMessages initialMessages;
 112     PrintWriter initialWriter;
 113 }