src/share/classes/com/sun/tools/javac/model/FilteredMemberList.java

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 104
5e89c4ca637c
child 554
9d9f26857129
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

     1 /*
     2  * Copyright 2005-2008 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  */
    26 package com.sun.tools.javac.model;
    28 import java.util.AbstractList;
    29 import java.util.Iterator;
    30 import java.util.NoSuchElementException;
    31 import com.sun.tools.javac.code.Scope;
    32 import com.sun.tools.javac.code.Symbol;
    34 import static com.sun.tools.javac.code.Flags.*;
    36 /**
    37  * Utility to construct a view of a symbol's members,
    38  * filtering out unwanted elements such as synthetic ones.
    39  * This view is most efficiently accessed through its iterator() method.
    40  *
    41  * <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    42  * you write code that depends on this, you do so at your own risk.
    43  * This code and its internal interfaces are subject to change or
    44  * deletion without notice.</b>
    45  */
    46 public class FilteredMemberList extends AbstractList<Symbol> {
    48     private final Scope scope;
    50     public FilteredMemberList(Scope scope) {
    51         this.scope = scope;
    52     }
    54     public int size() {
    55         int cnt = 0;
    56         for (Scope.Entry e = scope.elems; e != null; e = e.sibling) {
    57             if (!unwanted(e.sym))
    58                 cnt++;
    59         }
    60         return cnt;
    61     }
    63     public Symbol get(int index) {
    64         for (Scope.Entry e = scope.elems; e != null; e = e.sibling) {
    65             if (!unwanted(e.sym) && (index-- == 0))
    66                 return e.sym;
    67         }
    68         throw new IndexOutOfBoundsException();
    69     }
    71     // A more efficient implementation than AbstractList's.
    72     public Iterator<Symbol> iterator() {
    73         return new Iterator<Symbol>() {
    75             /** The next entry to examine, or null if none. */
    76             private Scope.Entry nextEntry = scope.elems;
    78             private boolean hasNextForSure = false;
    80             public boolean hasNext() {
    81                 if (hasNextForSure) {
    82                     return true;
    83                 }
    84                 while (nextEntry != null && unwanted(nextEntry.sym)) {
    85                     nextEntry = nextEntry.sibling;
    86                 }
    87                 hasNextForSure = (nextEntry != null);
    88                 return hasNextForSure;
    89             }
    91             public Symbol next() {
    92                 if (hasNext()) {
    93                     Symbol result = nextEntry.sym;
    94                     nextEntry = nextEntry.sibling;
    95                     hasNextForSure = false;
    96                     return result;
    97                 } else {
    98                     throw new NoSuchElementException();
    99                 }
   100             }
   102             public void remove() {
   103                 throw new UnsupportedOperationException();
   104             }
   105         };
   106     }
   108     /**
   109      * Tests whether this is a symbol that should never be seen by
   110      * clients, such as a synthetic class.  Returns true for null.
   111      */
   112     private static boolean unwanted(Symbol s) {
   113         return s == null  ||  (s.flags() & SYNTHETIC) != 0;
   114     }
   115 }

mercurial