API Overview API Index Package Overview Direct link to this page
JDK 1.6
  javax.lang.model.util. ElementFilter View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

/*
 * @(#)ElementFilter.java	1.6 06/08/02
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package javax.lang.model.util;

import java.lang.Iterable;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.EnumSet;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.NoSuchElementException;

import javax.lang.model.element.*;
import javax.lang.model.type.*;


/**
 * Filters for selecting just the elements of interest from a
 * collection of elements.  The returned sets and lists are new
 * collections and do use the argument as a backing store.  The
 * methods in this class do not make any attempts to guard against
 * concurrent modifications of the arguments.  The returned sets and
 * lists are mutable but unsafe for concurrent access.  A returned set
 * has the same iteration order as the argument set to a method.
 *
 * <p>If iterables and sets containing {@code null} are passed as
 * arguments to methods in this class, a {@code NullPointerException}
 * will be thrown.
 *
 * <p>Note that a <i>static import</i> statement can make the text of
 * calls to the methods in this class more concise; for example:
 *
 * <blockquote><pre>
 *     import static javax.lang.model.util.ElementFilter.*;
 *     ...
 *         {@code List<VariableElement>} fs = fieldsIn(someClass.getEnclosedElements());
 * </pre></blockquote>
 *
 * @author Joseph D. Darcy
 * @author Scott Seligman
 * @author Peter von der Ah&eacute;
 * @author Martin Buchholz
 * @version 1.6 06/08/02
 * @since 1.6
 */
public class ElementFilter {
    private ElementFilter() {} // Do not instantiate.

    private static Set<ElementKind> CONSTRUCTOR_KIND = 
	Collections.unmodifiableSet(EnumSet.of(ElementKind.CONSTRUCTOR));

    private static Set<ElementKind> FIELD_KINDS = 
	Collections.unmodifiableSet(EnumSet.of(ElementKind.FIELD,
					       ElementKind.ENUM_CONSTANT));
    private static Set<ElementKind> METHOD_KIND = 
	Collections.unmodifiableSet(EnumSet.of(ElementKind.METHOD));

    private static Set<ElementKind> PACKAGE_KIND = 
	Collections.unmodifiableSet(EnumSet.of(ElementKind.PACKAGE));

    private static Set<ElementKind> TYPE_KINDS = 
	Collections.unmodifiableSet(EnumSet.of(ElementKind.CLASS,
					       ElementKind.ENUM,
					       ElementKind.INTERFACE,
					       ElementKind.ANNOTATION_TYPE));
    /**
     * Returns a list of fields in {@code elements}.
     * @return a list of fields in {@code elements}
     * @param elements the elements to filter
     */
    public static List<VariableElement>
	    fieldsIn(Iterable<? extends Element> elements) {
	return listFilter(elements, FIELD_KINDS, VariableElement.class);
    }

    /**
     * Returns a set of fields in {@code elements}.
     * @return a set of fields in {@code elements}
     * @param elements the elements to filter
     */
    public static Set<VariableElement>
	    fieldsIn(Set<? extends Element> elements) {
	return setFilter(elements, FIELD_KINDS, VariableElement.class);
    }

    /**
     * Returns a list of constructors in {@code elements}.
     * @return a list of constructors in {@code elements}
     * @param elements the elements to filter
     */
    public static List<ExecutableElement>
	    constructorsIn(Iterable<? extends Element> elements) {
	return listFilter(elements, CONSTRUCTOR_KIND, ExecutableElement.class);
    }

    /**
     * Returns a set of constructors in {@code elements}.
     * @return a set of constructors in {@code elements}
     * @param elements the elements to filter
     */
    public static Set<ExecutableElement>
	    constructorsIn(Set<? extends Element> elements) {
	return setFilter(elements, CONSTRUCTOR_KIND, ExecutableElement.class);
    }

    /**
     * Returns a list of methods in {@code elements}.
     * @return a list of methods in {@code elements}
     * @param elements the elements to filter
     */
    public static List<ExecutableElement>
	    methodsIn(Iterable<? extends Element> elements) {
	return listFilter(elements, METHOD_KIND, ExecutableElement.class);
    }

    /**
     * Returns a set of methods in {@code elements}.
     * @return a set of methods in {@code elements}
     * @param elements the elements to filter
     */
    public static Set<ExecutableElement>
	    methodsIn(Set<? extends Element> elements) {
	return setFilter(elements, METHOD_KIND, ExecutableElement.class);
    }

    /**
     * Returns a list of types in {@code elements}.
     * @return a list of types in {@code elements}
     * @param elements the elements to filter
     */
    public static List<TypeElement>
	    typesIn(Iterable<? extends Element> elements) {
	return listFilter(elements, TYPE_KINDS, TypeElement.class);
    }

    /**
     * Returns a set of types in {@code elements}.
     * @return a set of types in {@code elements}
     * @param elements the elements to filter
     */
    public static Set<TypeElement>
	    typesIn(Set<? extends Element> elements) {
	return setFilter(elements, TYPE_KINDS, TypeElement.class);
    }

    /**
     * Returns a list of packages in {@code elements}.
     * @return a list of packages in {@code elements}
     * @param elements the elements to filter
     */
    public static List<PackageElement>
	    packagesIn(Iterable<? extends Element> elements) {
	return listFilter(elements, PACKAGE_KIND, PackageElement.class);
    }

    /**
     * Returns a set of packages in {@code elements}.
     * @return a set of packages in {@code elements}
     * @param elements the elements to filter
     */
    public static Set<PackageElement>
	    packagesIn(Set<? extends Element> elements) {
	return setFilter(elements, PACKAGE_KIND, PackageElement.class);
    }

    // Assumes targetKinds and E are sensible.
    private static <E extends Element> List<E> listFilter(Iterable<? extends Element> elements,
							  Set<ElementKind> targetKinds,
							  Class<E> clazz) {
	List<E> list = new ArrayList<E>();
	for (Element e : elements) {
	    if (targetKinds.contains(e.getKind()))
		list.add(clazz.cast(e));
	}
	return list;
    }

    // Assumes targetKinds and E are sensible.
    private static <E extends Element> Set<E> setFilter(Set<? extends Element> elements,
							Set<ElementKind> targetKinds,
							Class<E> clazz) {
	// Return set preserving iteration order of input set.
	Set<E> set = new LinkedHashSet<E>();
	for (Element e : elements) {
	    if (targetKinds.contains(e.getKind()))
		set.add(clazz.cast(e));
	}
	return set;
    }
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar