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