|
1 /* |
|
2 * Copyright (c) 2005, 2008, 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 |
|
26 package com.sun.tools.javac.model; |
|
27 |
|
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; |
|
33 |
|
34 import static com.sun.tools.javac.code.Flags.*; |
|
35 |
|
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 supported API. |
|
42 * If 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> { |
|
47 |
|
48 private final Scope scope; |
|
49 |
|
50 public FilteredMemberList(Scope scope) { |
|
51 this.scope = scope; |
|
52 } |
|
53 |
|
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 } |
|
62 |
|
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 } |
|
70 |
|
71 // A more efficient implementation than AbstractList's. |
|
72 public Iterator<Symbol> iterator() { |
|
73 return new Iterator<Symbol>() { |
|
74 |
|
75 /** The next entry to examine, or null if none. */ |
|
76 private Scope.Entry nextEntry = scope.elems; |
|
77 |
|
78 private boolean hasNextForSure = false; |
|
79 |
|
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 } |
|
90 |
|
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 } |
|
101 |
|
102 public void remove() { |
|
103 throw new UnsupportedOperationException(); |
|
104 } |
|
105 }; |
|
106 } |
|
107 |
|
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 } |