1 7 8 package javax.lang.model.util; 9 10 import java.lang.Iterable ; 11 import java.util.Collections ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import java.util.Set ; 15 import java.util.EnumSet ; 16 import java.util.ArrayList ; 17 import java.util.LinkedHashSet ; 18 import java.util.NoSuchElementException ; 19 20 import javax.lang.model.element.*; 21 import javax.lang.model.type.*; 22 23 24 53 public class ElementFilter { 54 private ElementFilter() {} 56 private static Set <ElementKind> CONSTRUCTOR_KIND = 57 Collections.unmodifiableSet(EnumSet.of(ElementKind.CONSTRUCTOR)); 58 59 private static Set <ElementKind> FIELD_KINDS = 60 Collections.unmodifiableSet(EnumSet.of(ElementKind.FIELD, 61 ElementKind.ENUM_CONSTANT)); 62 private static Set <ElementKind> METHOD_KIND = 63 Collections.unmodifiableSet(EnumSet.of(ElementKind.METHOD)); 64 65 private static Set <ElementKind> PACKAGE_KIND = 66 Collections.unmodifiableSet(EnumSet.of(ElementKind.PACKAGE)); 67 68 private static Set <ElementKind> TYPE_KINDS = 69 Collections.unmodifiableSet(EnumSet.of(ElementKind.CLASS, 70 ElementKind.ENUM, 71 ElementKind.INTERFACE, 72 ElementKind.ANNOTATION_TYPE)); 73 78 public static List <VariableElement> 79 fieldsIn(Iterable <? extends Element> elements) { 80 return listFilter(elements, FIELD_KINDS, VariableElement.class); 81 } 82 83 88 public static Set <VariableElement> 89 fieldsIn(Set <? extends Element> elements) { 90 return setFilter(elements, FIELD_KINDS, VariableElement.class); 91 } 92 93 98 public static List <ExecutableElement> 99 constructorsIn(Iterable <? extends Element> elements) { 100 return listFilter(elements, CONSTRUCTOR_KIND, ExecutableElement.class); 101 } 102 103 108 public static Set <ExecutableElement> 109 constructorsIn(Set <? extends Element> elements) { 110 return setFilter(elements, CONSTRUCTOR_KIND, ExecutableElement.class); 111 } 112 113 118 public static List <ExecutableElement> 119 methodsIn(Iterable <? extends Element> elements) { 120 return listFilter(elements, METHOD_KIND, ExecutableElement.class); 121 } 122 123 128 public static Set <ExecutableElement> 129 methodsIn(Set <? extends Element> elements) { 130 return setFilter(elements, METHOD_KIND, ExecutableElement.class); 131 } 132 133 138 public static List <TypeElement> 139 typesIn(Iterable <? extends Element> elements) { 140 return listFilter(elements, TYPE_KINDS, TypeElement.class); 141 } 142 143 148 public static Set <TypeElement> 149 typesIn(Set <? extends Element> elements) { 150 return setFilter(elements, TYPE_KINDS, TypeElement.class); 151 } 152 153 158 public static List <PackageElement> 159 packagesIn(Iterable <? extends Element> elements) { 160 return listFilter(elements, PACKAGE_KIND, PackageElement.class); 161 } 162 163 168 public static Set <PackageElement> 169 packagesIn(Set <? extends Element> elements) { 170 return setFilter(elements, PACKAGE_KIND, PackageElement.class); 171 } 172 173 private static <E extends Element> List <E> listFilter(Iterable <? extends Element> elements, 175 Set <ElementKind> targetKinds, 176 Class <E> clazz) { 177 List <E> list = new ArrayList <E>(); 178 for (Element e : elements) { 179 if (targetKinds.contains(e.getKind())) 180 list.add(clazz.cast(e)); 181 } 182 return list; 183 } 184 185 private static <E extends Element> Set <E> setFilter(Set <? extends Element> elements, 187 Set <ElementKind> targetKinds, 188 Class <E> clazz) { 189 Set <E> set = new LinkedHashSet <E>(); 191 for (Element e : elements) { 192 if (targetKinds.contains(e.getKind())) 193 set.add(clazz.cast(e)); 194 } 195 return set; 196 } 197 } 198 | Popular Tags |