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