test/tools/apt/Options/OptionChecker.java

Thu, 25 Aug 2011 17:18:25 -0700

author
schien
date
Thu, 25 Aug 2011 17:18:25 -0700
changeset 1067
f497fac86cf9
parent 554
9d9f26857129
permissions
-rw-r--r--

Added tag jdk8-b02 for changeset b3c059de2a61

     1 /*
     2  * Copyright (c) 2004, 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  */
    25 import com.sun.mirror.apt.*;
    26 import com.sun.mirror.declaration.*;
    27 import com.sun.mirror.type.*;
    28 import com.sun.mirror.util.*;
    30 import java.util.Collection;
    31 import java.util.Set;
    32 import java.util.Map;
    33 import java.util.Arrays;
    34 import java.util.Collections;
    36 public class OptionChecker implements AnnotationProcessorFactory {
    37     static class OptionCheck implements AnnotationProcessor {
    38         AnnotationProcessorEnvironment ape;
    39         OptionCheck(AnnotationProcessorEnvironment ape) {
    40             this.ape = ape;
    41         }
    43         public void process() {
    44             Map<String, String> options = ape.getOptions();
    45             if (options.containsKey("-Afoo") &&
    46                 options.containsKey("-Abar") &&
    47                 options.containsKey("-classpath") ) {
    48                 System.out.println("Expected options found.");
    49                 return;  // All is well
    50             } else {
    51                 System.err.println("Unexpected options values: " + options);
    52                 throw new RuntimeException();
    53             }
    54         }
    55     }
    57     static class HelloWorld implements AnnotationProcessor {
    58         AnnotationProcessorEnvironment ape;
    59         HelloWorld(AnnotationProcessorEnvironment ape) {
    60             this.ape = ape;
    61         }
    63         public void process() {
    64             java.io.PrintWriter pw;
    65             try {
    66                 pw = ape.getFiler().createSourceFile("HelloWorld");
    67             } catch (Exception e) {
    68                 throw new RuntimeException(e);
    69             }
    71             pw.println("public class HelloWorld {");
    72             pw.println("  public static void main (String argv[]) {");
    73             pw.println("    System.out.println(\"Hello apt world.\");");
    74             pw.println("  }");
    75             pw.println("}");
    76         }
    77     }
    80     static Collection<String> supportedTypes;
    81     static {
    82         String types[] = {"*"};
    83         supportedTypes = Collections.unmodifiableCollection(Arrays.asList(types));
    84     }
    86     static Collection<String> supportedOptions;
    87     static {
    88         String options[] = {"-Afoo", "-Abar"};
    89         supportedOptions = Collections.unmodifiableCollection(Arrays.asList(options));
    90     }
    92     public Collection<String> supportedOptions() {
    93         return supportedOptions;
    94     }
    96     public Collection<String> supportedAnnotationTypes() {
    97         return supportedTypes;
    98     }
   100     /*
   101      * Return the same processor independent of what annotations are
   102      * present, if any.
   103      */
   104     public AnnotationProcessor getProcessorFor(Set<AnnotationTypeDeclaration> atds,
   105                                                AnnotationProcessorEnvironment ape) {
   107         if (atds.contains(ape.getTypeDeclaration("Marker"))) {
   108             System.out.println("Returning composite processor.");
   109             return AnnotationProcessors.getCompositeAnnotationProcessor(new OptionCheck(ape),
   110                                                                         new HelloWorld(ape));
   111         }
   112         else {
   113             System.out.println("Returning single processor.");
   114             return new OptionCheck(ape);
   115         }
   116     }
   117 }

mercurial