test/tools/apt/Options/OptionChecker.java

changeset 1
9a66ca7c79fa
child 554
9d9f26857129
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/apt/Options/OptionChecker.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,117 @@
     1.4 +/*
     1.5 + * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + */
    1.26 +
    1.27 +
    1.28 +import com.sun.mirror.apt.*;
    1.29 +import com.sun.mirror.declaration.*;
    1.30 +import com.sun.mirror.type.*;
    1.31 +import com.sun.mirror.util.*;
    1.32 +
    1.33 +import java.util.Collection;
    1.34 +import java.util.Set;
    1.35 +import java.util.Map;
    1.36 +import java.util.Arrays;
    1.37 +import java.util.Collections;
    1.38 +
    1.39 +public class OptionChecker implements AnnotationProcessorFactory {
    1.40 +    static class OptionCheck implements AnnotationProcessor {
    1.41 +        AnnotationProcessorEnvironment ape;
    1.42 +        OptionCheck(AnnotationProcessorEnvironment ape) {
    1.43 +            this.ape = ape;
    1.44 +        }
    1.45 +
    1.46 +        public void process() {
    1.47 +            Map<String, String> options = ape.getOptions();
    1.48 +            if (options.containsKey("-Afoo") &&
    1.49 +                options.containsKey("-Abar") &&
    1.50 +                options.containsKey("-classpath") ) {
    1.51 +                System.out.println("Expected options found.");
    1.52 +                return;  // All is well
    1.53 +            } else {
    1.54 +                System.err.println("Unexpected options values: " + options);
    1.55 +                throw new RuntimeException();
    1.56 +            }
    1.57 +        }
    1.58 +    }
    1.59 +
    1.60 +    static class HelloWorld implements AnnotationProcessor {
    1.61 +        AnnotationProcessorEnvironment ape;
    1.62 +        HelloWorld(AnnotationProcessorEnvironment ape) {
    1.63 +            this.ape = ape;
    1.64 +        }
    1.65 +
    1.66 +        public void process() {
    1.67 +            java.io.PrintWriter pw;
    1.68 +            try {
    1.69 +                pw = ape.getFiler().createSourceFile("HelloWorld");
    1.70 +            } catch (Exception e) {
    1.71 +                throw new RuntimeException(e);
    1.72 +            }
    1.73 +
    1.74 +            pw.println("public class HelloWorld {");
    1.75 +            pw.println("  public static void main (String argv[]) {");
    1.76 +            pw.println("    System.out.println(\"Hello apt world.\");");
    1.77 +            pw.println("  }");
    1.78 +            pw.println("}");
    1.79 +        }
    1.80 +    }
    1.81 +
    1.82 +
    1.83 +    static Collection<String> supportedTypes;
    1.84 +    static {
    1.85 +        String types[] = {"*"};
    1.86 +        supportedTypes = Collections.unmodifiableCollection(Arrays.asList(types));
    1.87 +    }
    1.88 +
    1.89 +    static Collection<String> supportedOptions;
    1.90 +    static {
    1.91 +        String options[] = {"-Afoo", "-Abar"};
    1.92 +        supportedOptions = Collections.unmodifiableCollection(Arrays.asList(options));
    1.93 +    }
    1.94 +
    1.95 +    public Collection<String> supportedOptions() {
    1.96 +        return supportedOptions;
    1.97 +    }
    1.98 +
    1.99 +    public Collection<String> supportedAnnotationTypes() {
   1.100 +        return supportedTypes;
   1.101 +    }
   1.102 +
   1.103 +    /*
   1.104 +     * Return the same processor independent of what annotations are
   1.105 +     * present, if any.
   1.106 +     */
   1.107 +    public AnnotationProcessor getProcessorFor(Set<AnnotationTypeDeclaration> atds,
   1.108 +                                               AnnotationProcessorEnvironment ape) {
   1.109 +
   1.110 +        if (atds.contains(ape.getTypeDeclaration("Marker"))) {
   1.111 +            System.out.println("Returning composite processor.");
   1.112 +            return AnnotationProcessors.getCompositeAnnotationProcessor(new OptionCheck(ape),
   1.113 +                                                                        new HelloWorld(ape));
   1.114 +        }
   1.115 +        else {
   1.116 +            System.out.println("Returning single processor.");
   1.117 +            return new OptionCheck(ape);
   1.118 +        }
   1.119 +    }
   1.120 +}

mercurial