test/tools/javac/processing/model/util/filter/TestIterables.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/processing/model/util/filter/TestIterables.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,122 @@
     1.4 +/*
     1.5 + * Copyright (c) 2006, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6406164
    1.30 + * @summary Test that ElementFilter iterable methods behave properly.
    1.31 + * @author  Joseph D. Darcy
    1.32 + * @library /tools/javac/lib
    1.33 + * @build JavacTestingAbstractProcessor
    1.34 + * @compile TestIterables.java
    1.35 + * @compile -processor TestIterables -proc:only Foo1.java
    1.36 + * @compile Foo1.java
    1.37 + * @compile -processor TestIterables Foo1 TestIterables.java
    1.38 + */
    1.39 +
    1.40 +import java.util.Set;
    1.41 +import java.util.HashSet;
    1.42 +import java.util.Arrays;
    1.43 +import java.util.Iterator;
    1.44 +import javax.annotation.processing.*;
    1.45 +import javax.lang.model.SourceVersion;
    1.46 +import static javax.lang.model.SourceVersion.*;
    1.47 +import javax.lang.model.element.*;
    1.48 +import javax.lang.model.util.*;
    1.49 +import static javax.tools.Diagnostic.Kind.*;
    1.50 +
    1.51 +import static javax.lang.model.util.ElementFilter.*;
    1.52 +
    1.53 +/**
    1.54 + * Verify that the {@code ElementFilter} methods taking iterables
    1.55 + * behave correctly by comparing ExpectedElementCounts with actual
    1.56 + * results.
    1.57 + */
    1.58 +@SupportedAnnotationTypes("ExpectedElementCounts")
    1.59 +@ExpectedElementCounts(methods=2)
    1.60 +public class TestIterables extends JavacTestingAbstractProcessor {
    1.61 +    public boolean process(Set<? extends TypeElement> annotations,
    1.62 +                           RoundEnvironment roundEnv) {
    1.63 +        if (!roundEnv.processingOver()) {
    1.64 +            boolean error = false;
    1.65 +            int topLevelCount = 0;
    1.66 +
    1.67 +            for (TypeElement type : typesIn(roundEnv.getRootElements())) {
    1.68 +                topLevelCount++;
    1.69 +                ExpectedElementCounts elementCounts = type.getAnnotation(ExpectedElementCounts.class);
    1.70 +                if (elementCounts == null)
    1.71 +                    throw new RuntimeException("Missing ExpectedElementCounts annotation!");
    1.72 +
    1.73 +                Iterable<? extends Element> irritable = type.getEnclosedElements();
    1.74 +
    1.75 +                int constructorCount = getCount(constructorsIn(irritable));
    1.76 +                int fieldCount       = getCount(fieldsIn(irritable));
    1.77 +                int methodCount      = getCount(methodsIn(irritable));
    1.78 +                int typeCount        = getCount(typesIn(irritable));
    1.79 +
    1.80 +                System.out.println("\nType: " + type.getQualifiedName());
    1.81 +                System.out.format("Element      Actual\tExpected%n");
    1.82 +                System.out.format("constructors %d\t\t%d%n", constructorCount, elementCounts.constructors());
    1.83 +                System.out.format("fields       %d\t\t%d%n", fieldCount,       elementCounts.fields());
    1.84 +                System.out.format("methods      %d\t\t%d%n", methodCount,      elementCounts.methods());
    1.85 +                System.out.format("types        %d\t\t%d%n", typeCount,        elementCounts.types());
    1.86 +
    1.87 +                if ((constructorCount != elementCounts.constructors()) ||
    1.88 +                    (fieldCount       != elementCounts.fields()) ||
    1.89 +                    (methodCount      != elementCounts.methods()) ||
    1.90 +                    (typeCount        != elementCounts.types()) )
    1.91 +                    error = true;
    1.92 +            }
    1.93 +
    1.94 +            if (topLevelCount == 0)
    1.95 +                error = true;
    1.96 +
    1.97 +            if (error)
    1.98 +                throw new RuntimeException("A count mismatch has occured.");
    1.99 +        }
   1.100 +
   1.101 +        return true;
   1.102 +    }
   1.103 +
   1.104 +    private int getCount(Iterable<? extends Element> iter) {
   1.105 +        int count1 = 0;
   1.106 +        int count2 = 0;
   1.107 +
   1.108 +        Iterator<? extends Element> iterator = iter.iterator();
   1.109 +        while (iterator.hasNext() ) {
   1.110 +            count1++;
   1.111 +            iterator.next();
   1.112 +        }
   1.113 +
   1.114 +        iterator = iter.iterator();
   1.115 +        while (iterator.hasNext() ) {
   1.116 +            count2++;
   1.117 +            iterator.next();
   1.118 +        }
   1.119 +
   1.120 +        if (count1 != count2)
   1.121 +            throw new RuntimeException("Inconsistent counts from iterator.");
   1.122 +
   1.123 +        return count1;
   1.124 +    }
   1.125 +}

mercurial