| 1 | /* | 
| 2 |  * Copyright 2005-2006 Sun Microsystems, Inc.  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.  Sun designates this | 
| 8 |  * particular file as subject to the "Classpath" exception as provided | 
| 9 |  * by Sun in the LICENSE file that accompanied this code. | 
| 10 |  * | 
| 11 |  * This code is distributed in the hope that it will be useful, but WITHOUT | 
| 12 |  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | 
| 13 |  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License | 
| 14 |  * version 2 for more details (a copy is included in the LICENSE file that | 
| 15 |  * accompanied this code). | 
| 16 |  * | 
| 17 |  * You should have received a copy of the GNU General Public License version | 
| 18 |  * 2 along with this work; if not, write to the Free Software Foundation, | 
| 19 |  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | 
| 20 |  * | 
| 21 |  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | 
| 22 |  * CA 95054 USA or visit www.sun.com if you need additional information or | 
| 23 |  * have any questions. | 
| 24 |  */ | 
| 25 |   | 
| 26 | package javax.lang.model.util; | 
| 27 |   | 
| 28 | import java.lang.Iterable; | 
| 29 | import java.util.Collections; | 
| 30 | import java.util.Iterator; | 
| 31 | import java.util.List; | 
| 32 | import java.util.Set; | 
| 33 | import java.util.EnumSet; | 
| 34 | import java.util.ArrayList; | 
| 35 | import java.util.LinkedHashSet; | 
| 36 | import java.util.NoSuchElementException; | 
| 37 |   | 
| 38 | import javax.lang.model.element.*; | 
| 39 | import javax.lang.model.type.*; | 
| 40 |   | 
| 41 |   | 
| 42 | /** | 
| 43 |  * Filters for selecting just the elements of interest from a | 
| 44 |  * collection of elements.  The returned sets and lists are new | 
| 45 |  * collections and do use the argument as a backing store.  The | 
| 46 |  * methods in this class do not make any attempts to guard against | 
| 47 |  * concurrent modifications of the arguments.  The returned sets and | 
| 48 |  * lists are mutable but unsafe for concurrent access.  A returned set | 
| 49 |  * has the same iteration order as the argument set to a method. | 
| 50 |  * | 
| 51 |  * <p>If iterables and sets containing {@code null} are passed as | 
| 52 |  * arguments to methods in this class, a {@code NullPointerException} | 
| 53 |  * will be thrown. | 
| 54 |  * | 
| 55 |  * <p>Note that a <i>static import</i> statement can make the text of | 
| 56 |  * calls to the methods in this class more concise; for example: | 
| 57 |  * | 
| 58 |  * <blockquote><pre> | 
| 59 |  *     import static javax.lang.model.util.ElementFilter.*; | 
| 60 |  *     ... | 
| 61 |  *         {@code List<VariableElement>} fs = fieldsIn(someClass.getEnclosedElements()); | 
| 62 |  * </pre></blockquote> | 
| 63 |  * | 
| 64 |  * @author Joseph D. Darcy | 
| 65 |  * @author Scott Seligman | 
| 66 |  * @author Peter von der Ahé | 
| 67 |  * @author Martin Buchholz | 
| 68 |  * @since 1.6 | 
| 69 |  */ | 
| 70 | public class ElementFilter { | 
| 71 |     private ElementFilter() {} // Do not instantiate. | 
| 72 |   | 
| 73 |     private static Set<ElementKind> CONSTRUCTOR_KIND = | 
| 74 |         Collections.unmodifiableSet(EnumSet.of(ElementKind.CONSTRUCTOR)); | 
| 75 |   | 
| 76 |     private static Set<ElementKind> FIELD_KINDS = | 
| 77 |         Collections.unmodifiableSet(EnumSet.of(ElementKind.FIELD, | 
| 78 |                                                ElementKind.ENUM_CONSTANT)); | 
| 79 |     private static Set<ElementKind> METHOD_KIND = | 
| 80 |         Collections.unmodifiableSet(EnumSet.of(ElementKind.METHOD)); | 
| 81 |   | 
| 82 |     private static Set<ElementKind> PACKAGE_KIND = | 
| 83 |         Collections.unmodifiableSet(EnumSet.of(ElementKind.PACKAGE)); | 
| 84 |   | 
| 85 |     private static Set<ElementKind> TYPE_KINDS = | 
| 86 |         Collections.unmodifiableSet(EnumSet.of(ElementKind.CLASS, | 
| 87 |                                                ElementKind.ENUM, | 
| 88 |                                                ElementKind.INTERFACE, | 
| 89 |                                                ElementKind.ANNOTATION_TYPE)); | 
| 90 |     /** | 
| 91 |      * Returns a list of fields in {@code elements}. | 
| 92 |      * @return a list of fields in {@code elements} | 
| 93 |      * @param elements the elements to filter | 
| 94 |      */ | 
| 95 |     public static List<VariableElement> | 
| 96 |             fieldsIn(Iterable<? extends Element> elements) { | 
| 97 |         return listFilter(elements, FIELD_KINDS, VariableElement.class); | 
| 98 |     } | 
| 99 |   | 
| 100 |     /** | 
| 101 |      * Returns a set of fields in {@code elements}. | 
| 102 |      * @return a set of fields in {@code elements} | 
| 103 |      * @param elements the elements to filter | 
| 104 |      */ | 
| 105 |     public static Set<VariableElement> | 
| 106 |             fieldsIn(Set<? extends Element> elements) { | 
| 107 |         return setFilter(elements, FIELD_KINDS, VariableElement.class); | 
| 108 |     } | 
| 109 |   | 
| 110 |     /** | 
| 111 |      * Returns a list of constructors in {@code elements}. | 
| 112 |      * @return a list of constructors in {@code elements} | 
| 113 |      * @param elements the elements to filter | 
| 114 |      */ | 
| 115 |     public static List<ExecutableElement> | 
| 116 |             constructorsIn(Iterable<? extends Element> elements) { | 
| 117 |         return listFilter(elements, CONSTRUCTOR_KIND, ExecutableElement.class); | 
| 118 |     } | 
| 119 |   | 
| 120 |     /** | 
| 121 |      * Returns a set of constructors in {@code elements}. | 
| 122 |      * @return a set of constructors in {@code elements} | 
| 123 |      * @param elements the elements to filter | 
| 124 |      */ | 
| 125 |     public static Set<ExecutableElement> | 
| 126 |             constructorsIn(Set<? extends Element> elements) { | 
| 127 |         return setFilter(elements, CONSTRUCTOR_KIND, ExecutableElement.class); | 
| 128 |     } | 
| 129 |   | 
| 130 |     /** | 
| 131 |      * Returns a list of methods in {@code elements}. | 
| 132 |      * @return a list of methods in {@code elements} | 
| 133 |      * @param elements the elements to filter | 
| 134 |      */ | 
| 135 |     public static List<ExecutableElement> | 
| 136 |             methodsIn(Iterable<? extends Element> elements) { | 
| 137 |         return listFilter(elements, METHOD_KIND, ExecutableElement.class); | 
| 138 |     } | 
| 139 |   | 
| 140 |     /** | 
| 141 |      * Returns a set of methods in {@code elements}. | 
| 142 |      * @return a set of methods in {@code elements} | 
| 143 |      * @param elements the elements to filter | 
| 144 |      */ | 
| 145 |     public static Set<ExecutableElement> | 
| 146 |             methodsIn(Set<? extends Element> elements) { | 
| 147 |         return setFilter(elements, METHOD_KIND, ExecutableElement.class); | 
| 148 |     } | 
| 149 |   | 
| 150 |     /** | 
| 151 |      * Returns a list of types in {@code elements}. | 
| 152 |      * @return a list of types in {@code elements} | 
| 153 |      * @param elements the elements to filter | 
| 154 |      */ | 
| 155 |     public static List<TypeElement> | 
| 156 |             typesIn(Iterable<? extends Element> elements) { | 
| 157 |         return listFilter(elements, TYPE_KINDS, TypeElement.class); | 
| 158 |     } | 
| 159 |   | 
| 160 |     /** | 
| 161 |      * Returns a set of types in {@code elements}. | 
| 162 |      * @return a set of types in {@code elements} | 
| 163 |      * @param elements the elements to filter | 
| 164 |      */ | 
| 165 |     public static Set<TypeElement> | 
| 166 |             typesIn(Set<? extends Element> elements) { | 
| 167 |         return setFilter(elements, TYPE_KINDS, TypeElement.class); | 
| 168 |     } | 
| 169 |   | 
| 170 |     /** | 
| 171 |      * Returns a list of packages in {@code elements}. | 
| 172 |      * @return a list of packages in {@code elements} | 
| 173 |      * @param elements the elements to filter | 
| 174 |      */ | 
| 175 |     public static List<PackageElement> | 
| 176 |             packagesIn(Iterable<? extends Element> elements) { | 
| 177 |         return listFilter(elements, PACKAGE_KIND, PackageElement.class); | 
| 178 |     } | 
| 179 |   | 
| 180 |     /** | 
| 181 |      * Returns a set of packages in {@code elements}. | 
| 182 |      * @return a set of packages in {@code elements} | 
| 183 |      * @param elements the elements to filter | 
| 184 |      */ | 
| 185 |     public static Set<PackageElement> | 
| 186 |             packagesIn(Set<? extends Element> elements) { | 
| 187 |         return setFilter(elements, PACKAGE_KIND, PackageElement.class); | 
| 188 |     } | 
| 189 |   | 
| 190 |     // Assumes targetKinds and E are sensible. | 
| 191 |     private static <E extends Element> List<E> listFilter(Iterable<? extends Element> elements, | 
| 192 |                                                           Set<ElementKind> targetKinds, | 
| 193 |                                                           Class<E> clazz) { | 
| 194 |         List<E> list = new ArrayList<E>(); | 
| 195 |         for (Element e : elements) { | 
| 196 |             if (targetKinds.contains(e.getKind())) | 
| 197 |                 list.add(clazz.cast(e)); | 
| 198 |         } | 
| 199 |         return list; | 
| 200 |     } | 
| 201 |   | 
| 202 |     // Assumes targetKinds and E are sensible. | 
| 203 |     private static <E extends Element> Set<E> setFilter(Set<? extends Element> elements, | 
| 204 |                                                         Set<ElementKind> targetKinds, | 
| 205 |                                                         Class<E> clazz) { | 
| 206 |         // Return set preserving iteration order of input set. | 
| 207 |         Set<E> set = new LinkedHashSet<E>(); | 
| 208 |         for (Element e : elements) { | 
| 209 |             if (targetKinds.contains(e.getKind())) | 
| 210 |                 set.add(clazz.cast(e)); | 
| 211 |         } | 
| 212 |         return set; | 
| 213 |     } | 
| 214 | } |