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