test/tools/apt/Scanners/Scanner.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/Scanners/Scanner.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,123 @@
     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 +
    1.40 +public class Scanner implements AnnotationProcessorFactory {
    1.41 +    static class ScannerProc implements AnnotationProcessor {
    1.42 +        AnnotationProcessorEnvironment env;
    1.43 +        ScannerProc(AnnotationProcessorEnvironment env) {
    1.44 +            this.env = env;
    1.45 +        }
    1.46 +
    1.47 +        static class CountingVisitor extends SimpleDeclarationVisitor {
    1.48 +            int count;
    1.49 +            CountingVisitor() {
    1.50 +                count = 0;
    1.51 +            }
    1.52 +
    1.53 +            public void visitDeclaration(Declaration d) {
    1.54 +                count++;
    1.55 +
    1.56 +                Collection<AnnotationMirror> ams = d.getAnnotationMirrors();
    1.57 +                if (ams == null)
    1.58 +                    throw new RuntimeException("Declaration " + d +
    1.59 +                                               " not annotated with visit order.");
    1.60 +                else {
    1.61 +                    if (ams.size() != 1)
    1.62 +                        throw new RuntimeException("Declaration " + d +
    1.63 +                                                   " has wrong number of declarations.");
    1.64 +                    else {
    1.65 +                        for(AnnotationMirror am: ams) {
    1.66 +                            Map<AnnotationTypeElementDeclaration,AnnotationValue> elementValues = am.getElementValues();
    1.67 +                            for(AnnotationTypeElementDeclaration atmd: elementValues.keySet()) {
    1.68 +                                if (!atmd.getDeclaringType().toString().equals("VisitOrder"))
    1.69 +                                    throw new RuntimeException("Annotation " + atmd +
    1.70 +                                                               " is the wrong type.");
    1.71 +                                else {
    1.72 +                                    AnnotationValue av =
    1.73 +                                        elementValues.get(atmd);
    1.74 +                                    Integer value = (Integer) av.getValue();
    1.75 +                                    if (value.intValue() != count)
    1.76 +                                        throw new RuntimeException("Expected declaration " + d +
    1.77 +                                                                   " to be in position " + count +
    1.78 +                                                                   " instead of " + value.intValue());
    1.79 +
    1.80 +                                    System.out.println("Declaration " + d +
    1.81 +                                                       ": visit order " + value.intValue());
    1.82 +                                }
    1.83 +                            }
    1.84 +
    1.85 +                        }
    1.86 +                    }
    1.87 +                }
    1.88 +
    1.89 +            }
    1.90 +        }
    1.91 +
    1.92 +        public void process() {
    1.93 +            for(TypeDeclaration td: env.getSpecifiedTypeDeclarations() ) {
    1.94 +                td.accept(DeclarationVisitors.getSourceOrderDeclarationScanner(new CountingVisitor(),
    1.95 +                                                                               DeclarationVisitors.NO_OP));
    1.96 +            }
    1.97 +        }
    1.98 +    }
    1.99 +
   1.100 +
   1.101 +    static Collection<String> supportedTypes;
   1.102 +    static {
   1.103 +        String types[] = {"*"};
   1.104 +        supportedTypes = Collections.unmodifiableCollection(Arrays.asList(types));
   1.105 +    }
   1.106 +
   1.107 +    static Collection<String> supportedOptions;
   1.108 +    static {
   1.109 +        String options[] = {""};
   1.110 +        supportedOptions = Collections.unmodifiableCollection(Arrays.asList(options));
   1.111 +    }
   1.112 +
   1.113 +    public Collection<String> supportedOptions() {
   1.114 +        return supportedOptions;
   1.115 +    }
   1.116 +
   1.117 +    public Collection<String> supportedAnnotationTypes() {
   1.118 +        return supportedTypes;
   1.119 +    }
   1.120 +
   1.121 +    public AnnotationProcessor getProcessorFor(Set<AnnotationTypeDeclaration> atds,
   1.122 +                                        AnnotationProcessorEnvironment env) {
   1.123 +        return new ScannerProc(env);
   1.124 +    }
   1.125 +
   1.126 +}

mercurial