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()); 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()); 82 83 if ((constructorCount != elementCounts.constructors()) || 84 (fieldCount != elementCounts.fields()) || 85 (methodCount != elementCounts.methods()) || 86 (typeCount != elementCounts.types()) ) 87 error = true; 88 } 89 90 if (topLevelCount == 0) 91 error = true; 92 93 if (error) 94 throw new RuntimeException("A count mismatch has occured."); 95 } 96 97 return true; 98 } 99 100 private int getCount(Iterable<? extends Element> iter) { 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 }