duke@1: /* ohair@554: * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.model; duke@1: duke@1: import java.util.AbstractList; duke@1: import java.util.Iterator; duke@1: import java.util.NoSuchElementException; duke@1: import com.sun.tools.javac.code.Scope; duke@1: import com.sun.tools.javac.code.Symbol; duke@1: duke@1: import static com.sun.tools.javac.code.Flags.*; duke@1: duke@1: /** duke@1: * Utility to construct a view of a symbol's members, duke@1: * filtering out unwanted elements such as synthetic ones. duke@1: * This view is most efficiently accessed through its iterator() method. duke@1: * jjg@581: *

This is NOT part of any supported API. jjg@581: * If you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ duke@1: public class FilteredMemberList extends AbstractList { duke@1: duke@1: private final Scope scope; duke@1: duke@1: public FilteredMemberList(Scope scope) { duke@1: this.scope = scope; duke@1: } duke@1: duke@1: public int size() { duke@1: int cnt = 0; duke@1: for (Scope.Entry e = scope.elems; e != null; e = e.sibling) { duke@1: if (!unwanted(e.sym)) duke@1: cnt++; duke@1: } duke@1: return cnt; duke@1: } duke@1: duke@1: public Symbol get(int index) { duke@1: for (Scope.Entry e = scope.elems; e != null; e = e.sibling) { duke@1: if (!unwanted(e.sym) && (index-- == 0)) duke@1: return e.sym; duke@1: } duke@1: throw new IndexOutOfBoundsException(); duke@1: } duke@1: duke@1: // A more efficient implementation than AbstractList's. duke@1: public Iterator iterator() { duke@1: return new Iterator() { duke@1: duke@1: /** The next entry to examine, or null if none. */ duke@1: private Scope.Entry nextEntry = scope.elems; duke@1: duke@1: private boolean hasNextForSure = false; duke@1: duke@1: public boolean hasNext() { duke@1: if (hasNextForSure) { duke@1: return true; duke@1: } duke@1: while (nextEntry != null && unwanted(nextEntry.sym)) { duke@1: nextEntry = nextEntry.sibling; duke@1: } duke@1: hasNextForSure = (nextEntry != null); duke@1: return hasNextForSure; duke@1: } duke@1: duke@1: public Symbol next() { duke@1: if (hasNext()) { duke@1: Symbol result = nextEntry.sym; duke@1: nextEntry = nextEntry.sibling; duke@1: hasNextForSure = false; duke@1: return result; duke@1: } else { duke@1: throw new NoSuchElementException(); duke@1: } duke@1: } duke@1: duke@1: public void remove() { duke@1: throw new UnsupportedOperationException(); duke@1: } duke@1: }; duke@1: } duke@1: duke@1: /** duke@1: * Tests whether this is a symbol that should never be seen by duke@1: * clients, such as a synthetic class. Returns true for null. duke@1: */ duke@1: private static boolean unwanted(Symbol s) { duke@1: return s == null || (s.flags() & SYNTHETIC) != 0; duke@1: } duke@1: }