1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/javax/lang/model/util/ElementFilter.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,210 @@ 1.4 +/* 1.5 + * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package javax.lang.model.util; 1.30 + 1.31 +import java.util.Collections; 1.32 +import java.util.List; 1.33 +import java.util.Set; 1.34 +import java.util.EnumSet; 1.35 +import java.util.ArrayList; 1.36 +import java.util.LinkedHashSet; 1.37 + 1.38 +import javax.lang.model.element.*; 1.39 + 1.40 + 1.41 +/** 1.42 + * Filters for selecting just the elements of interest from a 1.43 + * collection of elements. The returned sets and lists are new 1.44 + * collections and do use the argument as a backing store. The 1.45 + * methods in this class do not make any attempts to guard against 1.46 + * concurrent modifications of the arguments. The returned sets and 1.47 + * lists are mutable but unsafe for concurrent access. A returned set 1.48 + * has the same iteration order as the argument set to a method. 1.49 + * 1.50 + * <p>If iterables and sets containing {@code null} are passed as 1.51 + * arguments to methods in this class, a {@code NullPointerException} 1.52 + * will be thrown. 1.53 + * 1.54 + * <p>Note that a <i>static import</i> statement can make the text of 1.55 + * calls to the methods in this class more concise; for example: 1.56 + * 1.57 + * <blockquote><pre> 1.58 + * import static javax.lang.model.util.ElementFilter.*; 1.59 + * ... 1.60 + * {@code List<VariableElement>} fs = fieldsIn(someClass.getEnclosedElements()); 1.61 + * </pre></blockquote> 1.62 + * 1.63 + * @author Joseph D. Darcy 1.64 + * @author Scott Seligman 1.65 + * @author Peter von der Ahé 1.66 + * @author Martin Buchholz 1.67 + * @since 1.6 1.68 + */ 1.69 +public class ElementFilter { 1.70 + private ElementFilter() {} // Do not instantiate. 1.71 + 1.72 + private static final Set<ElementKind> CONSTRUCTOR_KIND = 1.73 + Collections.unmodifiableSet(EnumSet.of(ElementKind.CONSTRUCTOR)); 1.74 + 1.75 + private static final Set<ElementKind> FIELD_KINDS = 1.76 + Collections.unmodifiableSet(EnumSet.of(ElementKind.FIELD, 1.77 + ElementKind.ENUM_CONSTANT)); 1.78 + private static final Set<ElementKind> METHOD_KIND = 1.79 + Collections.unmodifiableSet(EnumSet.of(ElementKind.METHOD)); 1.80 + 1.81 + private static final Set<ElementKind> PACKAGE_KIND = 1.82 + Collections.unmodifiableSet(EnumSet.of(ElementKind.PACKAGE)); 1.83 + 1.84 + private static final Set<ElementKind> TYPE_KINDS = 1.85 + Collections.unmodifiableSet(EnumSet.of(ElementKind.CLASS, 1.86 + ElementKind.ENUM, 1.87 + ElementKind.INTERFACE, 1.88 + ElementKind.ANNOTATION_TYPE)); 1.89 + /** 1.90 + * Returns a list of fields in {@code elements}. 1.91 + * @return a list of fields in {@code elements} 1.92 + * @param elements the elements to filter 1.93 + */ 1.94 + public static List<VariableElement> 1.95 + fieldsIn(Iterable<? extends Element> elements) { 1.96 + return listFilter(elements, FIELD_KINDS, VariableElement.class); 1.97 + } 1.98 + 1.99 + /** 1.100 + * Returns a set of fields in {@code elements}. 1.101 + * @return a set of fields in {@code elements} 1.102 + * @param elements the elements to filter 1.103 + */ 1.104 + public static Set<VariableElement> 1.105 + fieldsIn(Set<? extends Element> elements) { 1.106 + return setFilter(elements, FIELD_KINDS, VariableElement.class); 1.107 + } 1.108 + 1.109 + /** 1.110 + * Returns a list of constructors in {@code elements}. 1.111 + * @return a list of constructors in {@code elements} 1.112 + * @param elements the elements to filter 1.113 + */ 1.114 + public static List<ExecutableElement> 1.115 + constructorsIn(Iterable<? extends Element> elements) { 1.116 + return listFilter(elements, CONSTRUCTOR_KIND, ExecutableElement.class); 1.117 + } 1.118 + 1.119 + /** 1.120 + * Returns a set of constructors in {@code elements}. 1.121 + * @return a set of constructors in {@code elements} 1.122 + * @param elements the elements to filter 1.123 + */ 1.124 + public static Set<ExecutableElement> 1.125 + constructorsIn(Set<? extends Element> elements) { 1.126 + return setFilter(elements, CONSTRUCTOR_KIND, ExecutableElement.class); 1.127 + } 1.128 + 1.129 + /** 1.130 + * Returns a list of methods in {@code elements}. 1.131 + * @return a list of methods in {@code elements} 1.132 + * @param elements the elements to filter 1.133 + */ 1.134 + public static List<ExecutableElement> 1.135 + methodsIn(Iterable<? extends Element> elements) { 1.136 + return listFilter(elements, METHOD_KIND, ExecutableElement.class); 1.137 + } 1.138 + 1.139 + /** 1.140 + * Returns a set of methods in {@code elements}. 1.141 + * @return a set of methods in {@code elements} 1.142 + * @param elements the elements to filter 1.143 + */ 1.144 + public static Set<ExecutableElement> 1.145 + methodsIn(Set<? extends Element> elements) { 1.146 + return setFilter(elements, METHOD_KIND, ExecutableElement.class); 1.147 + } 1.148 + 1.149 + /** 1.150 + * Returns a list of types in {@code elements}. 1.151 + * @return a list of types in {@code elements} 1.152 + * @param elements the elements to filter 1.153 + */ 1.154 + public static List<TypeElement> 1.155 + typesIn(Iterable<? extends Element> elements) { 1.156 + return listFilter(elements, TYPE_KINDS, TypeElement.class); 1.157 + } 1.158 + 1.159 + /** 1.160 + * Returns a set of types in {@code elements}. 1.161 + * @return a set of types in {@code elements} 1.162 + * @param elements the elements to filter 1.163 + */ 1.164 + public static Set<TypeElement> 1.165 + typesIn(Set<? extends Element> elements) { 1.166 + return setFilter(elements, TYPE_KINDS, TypeElement.class); 1.167 + } 1.168 + 1.169 + /** 1.170 + * Returns a list of packages in {@code elements}. 1.171 + * @return a list of packages in {@code elements} 1.172 + * @param elements the elements to filter 1.173 + */ 1.174 + public static List<PackageElement> 1.175 + packagesIn(Iterable<? extends Element> elements) { 1.176 + return listFilter(elements, PACKAGE_KIND, PackageElement.class); 1.177 + } 1.178 + 1.179 + /** 1.180 + * Returns a set of packages in {@code elements}. 1.181 + * @return a set of packages in {@code elements} 1.182 + * @param elements the elements to filter 1.183 + */ 1.184 + public static Set<PackageElement> 1.185 + packagesIn(Set<? extends Element> elements) { 1.186 + return setFilter(elements, PACKAGE_KIND, PackageElement.class); 1.187 + } 1.188 + 1.189 + // Assumes targetKinds and E are sensible. 1.190 + private static <E extends Element> List<E> listFilter(Iterable<? extends Element> elements, 1.191 + Set<ElementKind> targetKinds, 1.192 + Class<E> clazz) { 1.193 + List<E> list = new ArrayList<E>(); 1.194 + for (Element e : elements) { 1.195 + if (targetKinds.contains(e.getKind())) 1.196 + list.add(clazz.cast(e)); 1.197 + } 1.198 + return list; 1.199 + } 1.200 + 1.201 + // Assumes targetKinds and E are sensible. 1.202 + private static <E extends Element> Set<E> setFilter(Set<? extends Element> elements, 1.203 + Set<ElementKind> targetKinds, 1.204 + Class<E> clazz) { 1.205 + // Return set preserving iteration order of input set. 1.206 + Set<E> set = new LinkedHashSet<E>(); 1.207 + for (Element e : elements) { 1.208 + if (targetKinds.contains(e.getKind())) 1.209 + set.add(clazz.cast(e)); 1.210 + } 1.211 + return set; 1.212 + } 1.213 +}