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

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 1
9a66ca7c79fa
child 699
d2aaaec153e8
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 2006, 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  */
    24 /*
    25  * @test
    26  * @bug 6406164
    27  * @summary Test that ElementFilter iterable methods behave properly.
    28  * @author  Joseph D. Darcy
    29  * @compile TestIterables.java
    30  * @compile -processor TestIterables -proc:only Foo1.java
    31  * @compile Foo1.java
    32  * @compile -processor TestIterables Foo1 TestIterables.java
    33  */
    35 import java.util.Set;
    36 import java.util.HashSet;
    37 import java.util.Arrays;
    38 import java.util.Iterator;
    39 import javax.annotation.processing.*;
    40 import javax.lang.model.SourceVersion;
    41 import static javax.lang.model.SourceVersion.*;
    42 import javax.lang.model.element.*;
    43 import javax.lang.model.util.*;
    44 import static javax.tools.Diagnostic.Kind.*;
    46 import static javax.lang.model.util.ElementFilter.*;
    48 /**
    49  * Verify that the {@code ElementFilter} methods taking iterables
    50  * behave correctly by comparing ExpectedElementCounts with actual
    51  * results.
    52  */
    53 @SupportedAnnotationTypes("ExpectedElementCounts")
    54 @ExpectedElementCounts(methods=3)
    55 public class TestIterables extends AbstractProcessor {
    57     public boolean process(Set<? extends TypeElement> annotations,
    58                            RoundEnvironment roundEnv) {
    59         if (!roundEnv.processingOver()) {
    60             boolean error = false;
    61             int topLevelCount = 0;
    63             for (TypeElement type : typesIn(roundEnv.getRootElements())) {
    64                 topLevelCount++;
    65                 ExpectedElementCounts elementCounts = type.getAnnotation(ExpectedElementCounts.class);
    66                 if (elementCounts == null)
    67                     throw new RuntimeException("Missing ExpectedElementCounts annotation!");
    69                 Iterable<? extends Element> irritable = type.getEnclosedElements();
    71                 int constructorCount = getCount(constructorsIn(irritable));
    72                 int fieldCount       = getCount(fieldsIn(irritable));
    73                 int methodCount      = getCount(methodsIn(irritable));
    74                 int typeCount        = getCount(typesIn(irritable));
    76                 System.out.println("\nType: " + type.getQualifiedName());
    77                 System.out.format("Element      Actual\tExpected%n");
    78                 System.out.format("constructors %d\t\t%d%n", constructorCount, elementCounts.constructors());
    79                 System.out.format("fields       %d\t\t%d%n", fieldCount,       elementCounts.fields());
    80                 System.out.format("methods      %d\t\t%d%n", methodCount,      elementCounts.methods());
    81                 System.out.format("types        %d\t\t%d%n", typeCount,        elementCounts.types());
    83                 if ((constructorCount != elementCounts.constructors()) ||
    84                     (fieldCount       != elementCounts.fields()) ||
    85                     (methodCount      != elementCounts.methods()) ||
    86                     (typeCount        != elementCounts.types()) )
    87                     error = true;
    88             }
    90             if (topLevelCount == 0)
    91                 error = true;
    93             if (error)
    94                 throw new RuntimeException("A count mismatch has occured.");
    95         }
    97         return true;
    98     }
   100     private int getCount(Iterable<? extends Element> iter) {
   101         int count1 = 0;
   102         int count2 = 0;
   104         Iterator<? extends Element> iterator = iter.iterator();
   105         while (iterator.hasNext() ) {
   106             count1++;
   107             iterator.next();
   108         }
   110         iterator = iter.iterator();
   111         while (iterator.hasNext() ) {
   112             count2++;
   113             iterator.next();
   114         }
   116         if (count1 != count2)
   117             throw new RuntimeException("Inconsistent counts from iterator.");
   119         return count1;
   120     }
   122     @Override
   123     public SourceVersion getSupportedSourceVersion() {
   124         return SourceVersion.latest();
   125     }
   127 }

mercurial