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

Print this page


   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  */
  23 
  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  */
  34 
  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.*;
  45 
  46 import static javax.lang.model.util.ElementFilter.*;
  47 
  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 {
  56 
  57     public boolean process(Set<? extends TypeElement> annotations,
  58                            RoundEnvironment roundEnv) {
  59         if (!roundEnv.processingOver()) {
  60             boolean error = false;
  61             int topLevelCount = 0;
  62 
  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!");
  68 
  69                 Iterable<? extends Element> irritable = type.getEnclosedElements();
  70 
  71                 int constructorCount = getCount(constructorsIn(irritable));
  72                 int fieldCount       = getCount(fieldsIn(irritable));
  73                 int methodCount      = getCount(methodsIn(irritable));
  74                 int typeCount        = getCount(typesIn(irritable));
  75 
  76                 System.out.println("\nType: " + type.getQualifiedName());


 101         int count1 = 0;
 102         int count2 = 0;
 103 
 104         Iterator<? extends Element> iterator = iter.iterator();
 105         while (iterator.hasNext() ) {
 106             count1++;
 107             iterator.next();
 108         }
 109 
 110         iterator = iter.iterator();
 111         while (iterator.hasNext() ) {
 112             count2++;
 113             iterator.next();
 114         }
 115 
 116         if (count1 != count2)
 117             throw new RuntimeException("Inconsistent counts from iterator.");
 118 
 119         return count1;
 120     }
 121 
 122     @Override
 123     public SourceVersion getSupportedSourceVersion() {
 124         return SourceVersion.latest();
 125     }
 126 
 127 }
   1 /*
   2  * Copyright (c) 2006, 2010, 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  */
  23 
  24 /*
  25  * @test
  26  * @bug 6406164
  27  * @summary Test that ElementFilter iterable methods behave properly.
  28  * @author  Joseph D. Darcy
  29  * @library ../../../../lib
  30  * @build JavacTestingAbstractProcessor
  31  * @compile TestIterables.java
  32  * @compile -processor TestIterables -proc:only Foo1.java
  33  * @compile Foo1.java
  34  * @compile -processor TestIterables Foo1 TestIterables.java
  35  */
  36 
  37 import java.util.Set;
  38 import java.util.HashSet;
  39 import java.util.Arrays;
  40 import java.util.Iterator;
  41 import javax.annotation.processing.*;
  42 import javax.lang.model.SourceVersion;
  43 import static javax.lang.model.SourceVersion.*;
  44 import javax.lang.model.element.*;
  45 import javax.lang.model.util.*;
  46 import static javax.tools.Diagnostic.Kind.*;
  47 
  48 import static javax.lang.model.util.ElementFilter.*;
  49 
  50 /**
  51  * Verify that the {@code ElementFilter} methods taking iterables
  52  * behave correctly by comparing ExpectedElementCounts with actual
  53  * results.
  54  */
  55 @SupportedAnnotationTypes("ExpectedElementCounts")
  56 @ExpectedElementCounts(methods=2)
  57 public class TestIterables extends JavacTestingAbstractProcessor {

  58     public boolean process(Set<? extends TypeElement> annotations,
  59                            RoundEnvironment roundEnv) {
  60         if (!roundEnv.processingOver()) {
  61             boolean error = false;
  62             int topLevelCount = 0;
  63 
  64             for (TypeElement type : typesIn(roundEnv.getRootElements())) {
  65                 topLevelCount++;
  66                 ExpectedElementCounts elementCounts = type.getAnnotation(ExpectedElementCounts.class);
  67                 if (elementCounts == null)
  68                     throw new RuntimeException("Missing ExpectedElementCounts annotation!");
  69 
  70                 Iterable<? extends Element> irritable = type.getEnclosedElements();
  71 
  72                 int constructorCount = getCount(constructorsIn(irritable));
  73                 int fieldCount       = getCount(fieldsIn(irritable));
  74                 int methodCount      = getCount(methodsIn(irritable));
  75                 int typeCount        = getCount(typesIn(irritable));
  76 
  77                 System.out.println("\nType: " + type.getQualifiedName());


 102         int count1 = 0;
 103         int count2 = 0;
 104 
 105         Iterator<? extends Element> iterator = iter.iterator();
 106         while (iterator.hasNext() ) {
 107             count1++;
 108             iterator.next();
 109         }
 110 
 111         iterator = iter.iterator();
 112         while (iterator.hasNext() ) {
 113             count2++;
 114             iterator.next();
 115         }
 116 
 117         if (count1 != count2)
 118             throw new RuntimeException("Inconsistent counts from iterator.");
 119 
 120         return count1;
 121     }






 122 }