FilteredMemberList.java revision 2571:10fc81ac75b4
1/*
2 * Copyright (c) 2005, 2014, 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
26package com.sun.tools.javac.model;
27
28import java.util.AbstractList;
29import java.util.Iterator;
30import com.sun.tools.javac.code.Scope;
31import com.sun.tools.javac.code.Symbol;
32
33import com.sun.tools.javac.util.Filter;
34import static com.sun.tools.javac.code.Flags.*;
35import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
36
37/**
38 * Utility to construct a view of a symbol's members,
39 * filtering out unwanted elements such as synthetic ones.
40 * This view is most efficiently accessed through its iterator() method.
41 *
42 * <p><b>This is NOT part of any supported API.
43 * If you write code that depends on this, you do so at your own risk.
44 * This code and its internal interfaces are subject to change or
45 * deletion without notice.</b>
46 */
47public class FilteredMemberList extends AbstractList<Symbol> {
48
49    private final Scope scope;
50
51    public FilteredMemberList(Scope scope) {
52        this.scope = scope;
53    }
54
55    public int size() {
56        int cnt = 0;
57        for (Symbol sym : scope.getSymbols(NON_RECURSIVE)) {
58            if (!unwanted(sym))
59                cnt++;
60        }
61        return cnt;
62    }
63
64    public Symbol get(int index) {
65        for (Symbol sym : scope.getSymbols(NON_RECURSIVE)) {
66            if (!unwanted(sym) && (index-- == 0))
67                return sym;
68        }
69        throw new IndexOutOfBoundsException();
70    }
71
72    // A more efficient implementation than AbstractList's.
73    public Iterator<Symbol> iterator() {
74        return scope.getSymbols(new Filter<Symbol>() {
75            public boolean accepts(Symbol t) {
76                return !unwanted(t);
77            }
78        }, NON_RECURSIVE).iterator();
79    }
80
81    /**
82     * Tests whether this is a symbol that should never be seen by
83     * clients, such as a synthetic class.  Returns true for null.
84     */
85    private static boolean unwanted(Symbol s) {
86        return s == null  ||  (s.flags() & SYNTHETIC) != 0;
87    }
88}
89