1 /*
   2  * Copyright (c) 2005, 2017, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.lang.model.util;
  27 
  28 import java.util.Collections;
  29 import java.util.List;
  30 import java.util.Set;
  31 import java.util.EnumSet;
  32 import java.util.ArrayList;
  33 import java.util.LinkedHashSet;
  34 
  35 import javax.lang.model.element.*;
  36 import javax.lang.model.element.ModuleElement.Directive;
  37 import javax.lang.model.element.ModuleElement.DirectiveKind;
  38 import javax.lang.model.element.ModuleElement.ExportsDirective;
  39 import javax.lang.model.element.ModuleElement.OpensDirective;
  40 import javax.lang.model.element.ModuleElement.ProvidesDirective;
  41 import javax.lang.model.element.ModuleElement.RequiresDirective;
  42 import javax.lang.model.element.ModuleElement.UsesDirective;
  43 
  44 
  45 /**
  46  * Filters for selecting just the elements of interest from a
  47  * collection of elements.  The returned sets and lists are new
  48  * collections and do use the argument as a backing store.  The
  49  * methods in this class do not make any attempts to guard against
  50  * concurrent modifications of the arguments.  The returned sets and
  51  * lists are mutable but unsafe for concurrent access.  A returned set
  52  * has the same iteration order as the argument set to a method.
  53  *
  54  * <p>If iterables and sets containing {@code null} are passed as
  55  * arguments to methods in this class, a {@code NullPointerException}
  56  * will be thrown.
  57  *
  58  * @author Joseph D. Darcy
  59  * @author Scott Seligman
  60  * @author Peter von der Ah&eacute;
  61  * @author Martin Buchholz
  62  * @since 1.6
  63  */
  64 public class ElementFilter {
  65     private ElementFilter() {} // Do not instantiate.
  66 
  67     private static final Set<ElementKind> CONSTRUCTOR_KIND =
  68         Collections.unmodifiableSet(EnumSet.of(ElementKind.CONSTRUCTOR));
  69 
  70     private static final Set<ElementKind> FIELD_KINDS =
  71         Collections.unmodifiableSet(EnumSet.of(ElementKind.FIELD,
  72                                                ElementKind.ENUM_CONSTANT));
  73     private static final Set<ElementKind> METHOD_KIND =
  74         Collections.unmodifiableSet(EnumSet.of(ElementKind.METHOD));
  75 
  76     private static final Set<ElementKind> PACKAGE_KIND =
  77         Collections.unmodifiableSet(EnumSet.of(ElementKind.PACKAGE));
  78 
  79     private static final Set<ElementKind> MODULE_KIND =
  80         Collections.unmodifiableSet(EnumSet.of(ElementKind.MODULE));
  81 
  82     private static final Set<ElementKind> TYPE_KINDS =
  83         Collections.unmodifiableSet(EnumSet.of(ElementKind.CLASS,
  84                                                ElementKind.ENUM,
  85                                                ElementKind.INTERFACE,
  86                                                ElementKind.RECORD,
  87                                                ElementKind.ANNOTATION_TYPE));
  88 
  89     private static final Set<ElementKind> STATE_COMPONENT_KIND =
  90         Set.of(ElementKind.STATE_COMPONENT);
  91 
  92     /**
  93      * Returns a list of fields in {@code elements}.
  94      * @return a list of fields in {@code elements}
  95      * @param elements the elements to filter
  96      */
  97     public static List<VariableElement>
  98             fieldsIn(Iterable<? extends Element> elements) {
  99         return listFilter(elements, FIELD_KINDS, VariableElement.class);
 100     }
 101 
 102     /**
 103      * Returns a set of fields in {@code elements}.
 104      * @return a set of fields in {@code elements}
 105      * @param elements the elements to filter
 106      */
 107     public static Set<VariableElement>
 108             fieldsIn(Set<? extends Element> elements) {
 109         return setFilter(elements, FIELD_KINDS, VariableElement.class);
 110     }
 111 
 112     // Method below may only be temporary
 113     /**
 114      * Returns a list of state descriptions in {@code elements}.
 115      * @return a list of state descriptions in {@code elements}
 116      * @param elements the elements to filter
 117      */
 118     public static List<VariableElement>
 119         stateComponentsIn(List<? extends Element> elements) {
 120         return listFilter(elements, STATE_COMPONENT_KIND, VariableElement.class);
 121     }
 122 
 123     /**
 124      * Returns a list of constructors in {@code elements}.
 125      * @return a list of constructors in {@code elements}
 126      * @param elements the elements to filter
 127      */
 128     public static List<ExecutableElement>
 129             constructorsIn(Iterable<? extends Element> elements) {
 130         return listFilter(elements, CONSTRUCTOR_KIND, ExecutableElement.class);
 131     }
 132 
 133     /**
 134      * Returns a set of constructors in {@code elements}.
 135      * @return a set of constructors in {@code elements}
 136      * @param elements the elements to filter
 137      */
 138     public static Set<ExecutableElement>
 139             constructorsIn(Set<? extends Element> elements) {
 140         return setFilter(elements, CONSTRUCTOR_KIND, ExecutableElement.class);
 141     }
 142 
 143     /**
 144      * Returns a list of methods in {@code elements}.
 145      * @return a list of methods in {@code elements}
 146      * @param elements the elements to filter
 147      */
 148     public static List<ExecutableElement>
 149             methodsIn(Iterable<? extends Element> elements) {
 150         return listFilter(elements, METHOD_KIND, ExecutableElement.class);
 151     }
 152 
 153     /**
 154      * Returns a set of methods in {@code elements}.
 155      * @return a set of methods in {@code elements}
 156      * @param elements the elements to filter
 157      */
 158     public static Set<ExecutableElement>
 159             methodsIn(Set<? extends Element> elements) {
 160         return setFilter(elements, METHOD_KIND, ExecutableElement.class);
 161     }
 162 
 163     /**
 164      * Returns a list of types in {@code elements}.
 165      * @return a list of types in {@code elements}
 166      * @param elements the elements to filter
 167      */
 168     public static List<TypeElement>
 169             typesIn(Iterable<? extends Element> elements) {
 170         return listFilter(elements, TYPE_KINDS, TypeElement.class);
 171     }
 172 
 173     /**
 174      * Returns a set of types in {@code elements}.
 175      * @return a set of types in {@code elements}
 176      * @param elements the elements to filter
 177      */
 178     public static Set<TypeElement>
 179             typesIn(Set<? extends Element> elements) {
 180         return setFilter(elements, TYPE_KINDS, TypeElement.class);
 181     }
 182 
 183     /**
 184      * Returns a list of packages in {@code elements}.
 185      * @return a list of packages in {@code elements}
 186      * @param elements the elements to filter
 187      */
 188     public static List<PackageElement>
 189             packagesIn(Iterable<? extends Element> elements) {
 190         return listFilter(elements, PACKAGE_KIND, PackageElement.class);
 191     }
 192 
 193     /**
 194      * Returns a set of packages in {@code elements}.
 195      * @return a set of packages in {@code elements}
 196      * @param elements the elements to filter
 197      */
 198     public static Set<PackageElement>
 199             packagesIn(Set<? extends Element> elements) {
 200         return setFilter(elements, PACKAGE_KIND, PackageElement.class);
 201     }
 202 
 203     /**
 204      * Returns a list of modules in {@code elements}.
 205      * @return a list of modules in {@code elements}
 206      * @param elements the elements to filter
 207      * @since 9
 208      * @spec JPMS
 209      */
 210     public static List<ModuleElement>
 211             modulesIn(Iterable<? extends Element> elements) {
 212         return listFilter(elements, MODULE_KIND, ModuleElement.class);
 213     }
 214 
 215     /**
 216      * Returns a set of modules in {@code elements}.
 217      * @return a set of modules in {@code elements}
 218      * @param elements the elements to filter
 219      * @since 9
 220      * @spec JPMS
 221      */
 222     public static Set<ModuleElement>
 223             modulesIn(Set<? extends Element> elements) {
 224         return setFilter(elements, MODULE_KIND, ModuleElement.class);
 225     }
 226 
 227     // Assumes targetKinds and E are sensible.
 228     private static <E extends Element> List<E> listFilter(Iterable<? extends Element> elements,
 229                                                           Set<ElementKind> targetKinds,
 230                                                           Class<E> clazz) {
 231         List<E> list = new ArrayList<>();
 232         for (Element e : elements) {
 233             if (targetKinds.contains(e.getKind()))
 234                 list.add(clazz.cast(e));
 235         }
 236         return list;
 237     }
 238 
 239     // Assumes targetKinds and E are sensible.
 240     private static <E extends Element> Set<E> setFilter(Set<? extends Element> elements,
 241                                                         Set<ElementKind> targetKinds,
 242                                                         Class<E> clazz) {
 243         // Return set preserving iteration order of input set.
 244         Set<E> set = new LinkedHashSet<>();
 245         for (Element e : elements) {
 246             if (targetKinds.contains(e.getKind()))
 247                 set.add(clazz.cast(e));
 248         }
 249         return set;
 250     }
 251 
 252     /**
 253      * Returns a list of {@code exports} directives in {@code directives}.
 254      * @return a list of {@code exports} directives in {@code directives}
 255      * @param directives the directives to filter
 256      * @since 9
 257      * @spec JPMS
 258      */
 259     public static List<ExportsDirective>
 260             exportsIn(Iterable<? extends Directive> directives) {
 261         return listFilter(directives, DirectiveKind.EXPORTS, ExportsDirective.class);
 262     }
 263 
 264     /**
 265      * Returns a list of {@code opens} directives in {@code directives}.
 266      * @return a list of {@code opens} directives in {@code directives}
 267      * @param directives the directives to filter
 268      * @since 9
 269      */
 270     public static List<OpensDirective>
 271             opensIn(Iterable<? extends Directive> directives) {
 272         return listFilter(directives, DirectiveKind.OPENS, OpensDirective.class);
 273     }
 274 
 275     /**
 276      * Returns a list of {@code provides} directives in {@code directives}.
 277      * @return a list of {@code provides} directives in {@code directives}
 278      * @param directives the directives to filter
 279      * @since 9
 280      * @spec JPMS
 281      */
 282     public static List<ProvidesDirective>
 283             providesIn(Iterable<? extends Directive> directives) {
 284         return listFilter(directives, DirectiveKind.PROVIDES, ProvidesDirective.class);
 285     }
 286 
 287     /**
 288      * Returns a list of {@code requires} directives in {@code directives}.
 289      * @return a list of {@code requires} directives in {@code directives}
 290      * @param directives the directives to filter
 291      * @since 9
 292      * @spec JPMS
 293      */
 294     public static List<RequiresDirective>
 295             requiresIn(Iterable<? extends Directive> directives) {
 296         return listFilter(directives, DirectiveKind.REQUIRES, RequiresDirective.class);
 297     }
 298 
 299     /**
 300      * Returns a list of {@code uses} directives in {@code directives}.
 301      * @return a list of {@code uses} directives in {@code directives}
 302      * @param directives the directives to filter
 303      * @since 9
 304      * @spec JPMS
 305      */
 306     public static List<UsesDirective>
 307             usesIn(Iterable<? extends Directive> directives) {
 308         return listFilter(directives, DirectiveKind.USES, UsesDirective.class);
 309     }
 310 
 311     // Assumes directiveKind and D are sensible.
 312     private static <D extends Directive> List<D> listFilter(Iterable<? extends Directive> directives,
 313                                                           DirectiveKind directiveKind,
 314                                                           Class<D> clazz) {
 315         List<D> list = new ArrayList<>();
 316         for (Directive d : directives) {
 317             if (d.getKind() == directiveKind)
 318                 list.add(clazz.cast(d));
 319         }
 320         return list;
 321     }
 322 }