aoqi@0: /* aoqi@0: * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package javax.lang.model.util; aoqi@0: aoqi@0: import java.util.Collections; aoqi@0: import java.util.List; aoqi@0: import java.util.Set; aoqi@0: import java.util.EnumSet; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.LinkedHashSet; aoqi@0: aoqi@0: import javax.lang.model.element.*; aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Filters for selecting just the elements of interest from a aoqi@0: * collection of elements. The returned sets and lists are new aoqi@0: * collections and do use the argument as a backing store. The aoqi@0: * methods in this class do not make any attempts to guard against aoqi@0: * concurrent modifications of the arguments. The returned sets and aoqi@0: * lists are mutable but unsafe for concurrent access. A returned set aoqi@0: * has the same iteration order as the argument set to a method. aoqi@0: * aoqi@0: *

If iterables and sets containing {@code null} are passed as aoqi@0: * arguments to methods in this class, a {@code NullPointerException} aoqi@0: * will be thrown. aoqi@0: * aoqi@0: *

Note that a static import statement can make the text of aoqi@0: * calls to the methods in this class more concise; for example: aoqi@0: * aoqi@0: *

aoqi@0:  *     import static javax.lang.model.util.ElementFilter.*;
aoqi@0:  *     ...
aoqi@0:  *         {@code List} fs = fieldsIn(someClass.getEnclosedElements());
aoqi@0:  * 
aoqi@0: * aoqi@0: * @author Joseph D. Darcy aoqi@0: * @author Scott Seligman aoqi@0: * @author Peter von der Ahé aoqi@0: * @author Martin Buchholz aoqi@0: * @since 1.6 aoqi@0: */ aoqi@0: public class ElementFilter { aoqi@0: private ElementFilter() {} // Do not instantiate. aoqi@0: aoqi@0: private static final Set CONSTRUCTOR_KIND = aoqi@0: Collections.unmodifiableSet(EnumSet.of(ElementKind.CONSTRUCTOR)); aoqi@0: aoqi@0: private static final Set FIELD_KINDS = aoqi@0: Collections.unmodifiableSet(EnumSet.of(ElementKind.FIELD, aoqi@0: ElementKind.ENUM_CONSTANT)); aoqi@0: private static final Set METHOD_KIND = aoqi@0: Collections.unmodifiableSet(EnumSet.of(ElementKind.METHOD)); aoqi@0: aoqi@0: private static final Set PACKAGE_KIND = aoqi@0: Collections.unmodifiableSet(EnumSet.of(ElementKind.PACKAGE)); aoqi@0: aoqi@0: private static final Set TYPE_KINDS = aoqi@0: Collections.unmodifiableSet(EnumSet.of(ElementKind.CLASS, aoqi@0: ElementKind.ENUM, aoqi@0: ElementKind.INTERFACE, aoqi@0: ElementKind.ANNOTATION_TYPE)); aoqi@0: /** aoqi@0: * Returns a list of fields in {@code elements}. aoqi@0: * @return a list of fields in {@code elements} aoqi@0: * @param elements the elements to filter aoqi@0: */ aoqi@0: public static List aoqi@0: fieldsIn(Iterable elements) { aoqi@0: return listFilter(elements, FIELD_KINDS, VariableElement.class); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a set of fields in {@code elements}. aoqi@0: * @return a set of fields in {@code elements} aoqi@0: * @param elements the elements to filter aoqi@0: */ aoqi@0: public static Set aoqi@0: fieldsIn(Set elements) { aoqi@0: return setFilter(elements, FIELD_KINDS, VariableElement.class); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a list of constructors in {@code elements}. aoqi@0: * @return a list of constructors in {@code elements} aoqi@0: * @param elements the elements to filter aoqi@0: */ aoqi@0: public static List aoqi@0: constructorsIn(Iterable elements) { aoqi@0: return listFilter(elements, CONSTRUCTOR_KIND, ExecutableElement.class); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a set of constructors in {@code elements}. aoqi@0: * @return a set of constructors in {@code elements} aoqi@0: * @param elements the elements to filter aoqi@0: */ aoqi@0: public static Set aoqi@0: constructorsIn(Set elements) { aoqi@0: return setFilter(elements, CONSTRUCTOR_KIND, ExecutableElement.class); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a list of methods in {@code elements}. aoqi@0: * @return a list of methods in {@code elements} aoqi@0: * @param elements the elements to filter aoqi@0: */ aoqi@0: public static List aoqi@0: methodsIn(Iterable elements) { aoqi@0: return listFilter(elements, METHOD_KIND, ExecutableElement.class); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a set of methods in {@code elements}. aoqi@0: * @return a set of methods in {@code elements} aoqi@0: * @param elements the elements to filter aoqi@0: */ aoqi@0: public static Set aoqi@0: methodsIn(Set elements) { aoqi@0: return setFilter(elements, METHOD_KIND, ExecutableElement.class); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a list of types in {@code elements}. aoqi@0: * @return a list of types in {@code elements} aoqi@0: * @param elements the elements to filter aoqi@0: */ aoqi@0: public static List aoqi@0: typesIn(Iterable elements) { aoqi@0: return listFilter(elements, TYPE_KINDS, TypeElement.class); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a set of types in {@code elements}. aoqi@0: * @return a set of types in {@code elements} aoqi@0: * @param elements the elements to filter aoqi@0: */ aoqi@0: public static Set aoqi@0: typesIn(Set elements) { aoqi@0: return setFilter(elements, TYPE_KINDS, TypeElement.class); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a list of packages in {@code elements}. aoqi@0: * @return a list of packages in {@code elements} aoqi@0: * @param elements the elements to filter aoqi@0: */ aoqi@0: public static List aoqi@0: packagesIn(Iterable elements) { aoqi@0: return listFilter(elements, PACKAGE_KIND, PackageElement.class); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a set of packages in {@code elements}. aoqi@0: * @return a set of packages in {@code elements} aoqi@0: * @param elements the elements to filter aoqi@0: */ aoqi@0: public static Set aoqi@0: packagesIn(Set elements) { aoqi@0: return setFilter(elements, PACKAGE_KIND, PackageElement.class); aoqi@0: } aoqi@0: aoqi@0: // Assumes targetKinds and E are sensible. aoqi@0: private static List listFilter(Iterable elements, aoqi@0: Set targetKinds, aoqi@0: Class clazz) { aoqi@0: List list = new ArrayList(); aoqi@0: for (Element e : elements) { aoqi@0: if (targetKinds.contains(e.getKind())) aoqi@0: list.add(clazz.cast(e)); aoqi@0: } aoqi@0: return list; aoqi@0: } aoqi@0: aoqi@0: // Assumes targetKinds and E are sensible. aoqi@0: private static Set setFilter(Set elements, aoqi@0: Set targetKinds, aoqi@0: Class clazz) { aoqi@0: // Return set preserving iteration order of input set. aoqi@0: Set set = new LinkedHashSet(); aoqi@0: for (Element e : elements) { aoqi@0: if (targetKinds.contains(e.getKind())) aoqi@0: set.add(clazz.cast(e)); aoqi@0: } aoqi@0: return set; aoqi@0: } aoqi@0: }