test/tools/apt/Scanners/Counter.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/Counter.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,108 @@
     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.Arrays;
    1.36 +
    1.37 +import static java.util.Collections.*;
    1.38 +import static com.sun.mirror.util.DeclarationVisitors.*;
    1.39 +
    1.40 +/*
    1.41 + * Used to verify counts from different kinds of declaration scanners.
    1.42 + */
    1.43 +public class Counter implements AnnotationProcessorFactory {
    1.44 +    static class CounterProc implements AnnotationProcessor {
    1.45 +        static class CountingVisitor extends SimpleDeclarationVisitor {
    1.46 +            int count;
    1.47 +            int count() {
    1.48 +                return count;
    1.49 +            }
    1.50 +
    1.51 +            CountingVisitor() {
    1.52 +                count = 0;
    1.53 +            }
    1.54 +
    1.55 +            public void visitDeclaration(Declaration d) {
    1.56 +                count++;
    1.57 +                System.out.println(d.getSimpleName());
    1.58 +            }
    1.59 +        }
    1.60 +
    1.61 +        AnnotationProcessorEnvironment env;
    1.62 +        CounterProc(AnnotationProcessorEnvironment env) {
    1.63 +            this.env = env;
    1.64 +        }
    1.65 +
    1.66 +        public void process() {
    1.67 +            for(TypeDeclaration td: env.getSpecifiedTypeDeclarations() ) {
    1.68 +                CountingVisitor sourceOrder = new CountingVisitor();
    1.69 +                CountingVisitor someOrder = new CountingVisitor();
    1.70 +
    1.71 +                System.out.println("Source Order Scanner");
    1.72 +                td.accept(getSourceOrderDeclarationScanner(sourceOrder,
    1.73 +                                                           NO_OP));
    1.74 +
    1.75 +                System.out.println("\nSome Order Scanner");
    1.76 +                td.accept(getDeclarationScanner(someOrder,
    1.77 +                                                NO_OP));
    1.78 +
    1.79 +                if (sourceOrder.count() != someOrder.count() )
    1.80 +                    throw new RuntimeException("Counts from different scanners don't agree");
    1.81 +            }
    1.82 +
    1.83 +        }
    1.84 +    }
    1.85 +
    1.86 +    static Collection<String> supportedTypes;
    1.87 +    static {
    1.88 +        String types[] = {"*"};
    1.89 +        supportedTypes = unmodifiableCollection(Arrays.asList(types));
    1.90 +    }
    1.91 +
    1.92 +    static Collection<String> supportedOptions;
    1.93 +    static {
    1.94 +        String options[] = {""};
    1.95 +        supportedOptions = unmodifiableCollection(Arrays.asList(options));
    1.96 +    }
    1.97 +
    1.98 +    public Collection<String> supportedOptions() {
    1.99 +        return supportedOptions;
   1.100 +    }
   1.101 +
   1.102 +    public Collection<String> supportedAnnotationTypes() {
   1.103 +        return supportedTypes;
   1.104 +    }
   1.105 +
   1.106 +    public AnnotationProcessor getProcessorFor(Set<AnnotationTypeDeclaration> atds,
   1.107 +                                               AnnotationProcessorEnvironment env) {
   1.108 +        return new CounterProc(env);
   1.109 +    }
   1.110 +
   1.111 +}

mercurial