langtools/test/tools/apt/Discovery/PhantomUpdate.java
author jeff
Wed, 22 Jun 2011 10:10:54 -0700
changeset 9916 c35ecac744a5
parent 5520 86e4b9a9da40
permissions -rw-r--r--
7057046: Add embedded license to THIRD PARTY README Reviewed-by: lana
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     2
 * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * JSR 269 annotation processor to test combined apt + JSR 269
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * annotation processor file generation and option passing.
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import javax.annotation.processing.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import static  javax.lang.model.SourceVersion.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import javax.lang.model.type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.lang.model.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
@SupportedAnnotationTypes("*")  // Process all annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
@SupportedSourceVersion(RELEASE_6)
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
public class PhantomUpdate extends AbstractProcessor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
    boolean firstRound = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    public boolean process(Set<? extends TypeElement> annotations,
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
                           RoundEnvironment roundEnv) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
        if (firstRound) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
            verifyOptions();
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
            printGoodbyeWorld();
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
            firstRound = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        return true; // Claim all annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
     * Expected options are "foo" and "bar=baz".
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    private void verifyOptions() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        Map<String, String> actualOptions = processingEnv.getOptions();
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        Map<String, String> expectedOptions = new LinkedHashMap<String, String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        expectedOptions.put("foo", null);
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        expectedOptions.put("bar", "baz");
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        if (!actualOptions.equals(expectedOptions) ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
            System.err.println("Expected options " + expectedOptions +
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
                               "\n but got " + actualOptions);
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
            throw new RuntimeException("Options mismatch");
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    private void printGoodbyeWorld() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
            // Create new source file
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
            PrintWriter pw = new PrintWriter(processingEnv.getFiler().createSourceFile("GoodbyeWorld").openWriter());
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
            pw.println("public class GoodbyeWorld {");
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
            pw.println("  // PhantomUpdate Goodbye world");
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
            pw.println("  public static void main(String argv[]) {");
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
            pw.println("    System.out.println(\"Goodbye World\");");
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
            pw.println("  }");
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
            pw.println("}");
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
            pw.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        } catch (Exception e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
            throw new RuntimeException(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
}