| 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.tools.javac.model; |
| 27 | |
| 28 | import com.sun.tools.javac.util.*; |
| 29 | import java.util.AbstractList; |
| 30 | import java.util.Iterator; |
| 31 | import java.util.NoSuchElementException; |
| 32 | import com.sun.tools.javac.code.Scope; |
| 33 | import com.sun.tools.javac.code.Symbol; |
| 34 | |
| 35 | import static com.sun.tools.javac.code.Flags.*; |
| 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 API supported by Sun Microsystems. If |
| 43 | * 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 | */ |
| 47 | public 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 (Scope.Entry e = scope.elems; e != null; e = e.sibling) { |
| 58 | if (!unwanted(e.sym)) |
| 59 | cnt++; |
| 60 | } |
| 61 | return cnt; |
| 62 | } |
| 63 | |
| 64 | public Symbol get(int index) { |
| 65 | for (Scope.Entry e = scope.elems; e != null; e = e.sibling) { |
| 66 | if (!unwanted(e.sym) && (index-- == 0)) |
| 67 | return e.sym; |
| 68 | } |
| 69 | throw new IndexOutOfBoundsException(); |
| 70 | } |
| 71 | |
| 72 | // A more efficient implementation than AbstractList's. |
| 73 | public Iterator<Symbol> iterator() { |
| 74 | return new Iterator<Symbol>() { |
| 75 | |
| 76 | /** The next entry to examine, or null if none. */ |
| 77 | private Scope.Entry nextEntry = scope.elems; |
| 78 | |
| 79 | private boolean hasNextForSure = false; |
| 80 | |
| 81 | public boolean hasNext() { |
| 82 | if (hasNextForSure) { |
| 83 | return true; |
| 84 | } |
| 85 | while (nextEntry != null && unwanted(nextEntry.sym)) { |
| 86 | nextEntry = nextEntry.sibling; |
| 87 | } |
| 88 | hasNextForSure = (nextEntry != null); |
| 89 | return hasNextForSure; |
| 90 | } |
| 91 | |
| 92 | public Symbol next() { |
| 93 | if (hasNext()) { |
| 94 | Symbol result = nextEntry.sym; |
| 95 | nextEntry = nextEntry.sibling; |
| 96 | hasNextForSure = false; |
| 97 | return result; |
| 98 | } else { |
| 99 | throw new NoSuchElementException(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public void remove() { |
| 104 | throw new UnsupportedOperationException(); |
| 105 | } |
| 106 | }; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Tests whether this is a symbol that should never be seen by |
| 111 | * clients, such as a synthetic class. Returns true for null. |
| 112 | */ |
| 113 | private static boolean unwanted(Symbol s) { |
| 114 | return s == null || (s.flags() & SYNTHETIC) != 0; |
| 115 | } |
| 116 | } |