1 /*
   2  * Copyright (c) 2006, 2007, 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 /*
  26  * JSR 269 annotation processor to test combined apt + JSR 269
  27  * annotation processor file generation and option passing.
  28  */
  29 
  30 import javax.annotation.processing.*;
  31 import static  javax.lang.model.SourceVersion.*;
  32 import javax.lang.model.element.*;
  33 import javax.lang.model.type.*;
  34 import javax.lang.model.util.*;
  35 import java.util.*;
  36 import java.io.*;
  37 
  38 @SupportedAnnotationTypes("*")  // Process all annotations
  39 @SupportedSourceVersion(RELEASE_6)
  40 public class PhantomUpdate extends AbstractProcessor {
  41     boolean firstRound = true;
  42 
  43     public boolean process(Set<? extends TypeElement> annotations,
  44                            RoundEnvironment roundEnv) {
  45         if (firstRound) {
  46             verifyOptions();
  47             printGoodbyeWorld();
  48             firstRound = false;
  49         }
  50         return true; // Claim all annotations
  51     }
  52 
  53     /*
  54      * Expected options are "foo" and "bar=baz".
  55      */
  56     private void verifyOptions() {
  57         Map<String, String> actualOptions = processingEnv.getOptions();
  58         Map<String, String> expectedOptions = new LinkedHashMap<String, String>();
  59         expectedOptions.put("foo", null);
  60         expectedOptions.put("bar", "baz");
  61 
  62         if (!actualOptions.equals(expectedOptions) ) {
  63             System.err.println("Expected options " + expectedOptions +
  64                                "\n but got " + actualOptions);
  65             throw new RuntimeException("Options mismatch");
  66         }
  67     }
  68 
  69     private void printGoodbyeWorld() {
  70         try {
  71             // Create new source file
  72             PrintWriter pw = new PrintWriter(processingEnv.getFiler().createSourceFile("GoodbyeWorld").openWriter());
  73             pw.println("public class GoodbyeWorld {");
  74             pw.println("  // PhantomUpdate Goodbye world");
  75             pw.println("  public static void main(String argv[]) {");
  76             pw.println("    System.out.println(\"Goodbye World\");");
  77             pw.println("  }");
  78             pw.println("}");
  79             pw.close();
  80         } catch (Exception e) {
  81             throw new RuntimeException(e);
  82         }
  83     }
  84 }