7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes

CompoundScope.getElements() is not filtering elements according to the ScopeFilter argument

Reviewed-by: jjg
This commit is contained in:
Maurizio Cimadamore 2011-04-19 13:57:25 +01:00
parent fbb8954b52
commit 95e14306e7
2 changed files with 39 additions and 12 deletions

View file

@ -649,7 +649,7 @@ public class Scope {
public Iterator<Symbol> iterator() { public Iterator<Symbol> iterator() {
return new CompoundScopeIterator(subScopes) { return new CompoundScopeIterator(subScopes) {
Iterator<Symbol> nextIterator(Scope s) { Iterator<Symbol> nextIterator(Scope s) {
return s.getElements().iterator(); return s.getElements(sf).iterator();
} }
}; };
} }

View file

@ -23,7 +23,7 @@
/* /*
* @test * @test
* @bug 7017664 * @bug 7017664 7036906
* @summary Basher for CompoundScopes * @summary Basher for CompoundScopes
*/ */
@ -127,8 +127,17 @@ public class CompoundScopeTest {
} }
} }
log("testing scope: " + root); log("testing scope: " + root);
checkElems(root); checkElems(root, null);
checkShadowed(root); checkElems(root, new OddFilter());
checkShadowed(root, null);
checkShadowed(root, new OddFilter());
}
}
class OddFilter implements Filter<Symbol> {
public boolean accepts(Symbol s) {
Name numPart = s.name.subName(1, s.name.length());
return Integer.parseInt(numPart.toString()) % 2 != 0;
} }
} }
@ -165,15 +174,20 @@ public class CompoundScopeTest {
* Check that CompoundScope.getElements() correctly visits all symbols * Check that CompoundScope.getElements() correctly visits all symbols
* in all subscopes (in the correct order) * in all subscopes (in the correct order)
*/ */
void checkElems(CompoundScope cs) { void checkElems(CompoundScope cs, Filter<Symbol> sf) {
List<Symbol> allSymbols = elems;
int count = 0; int count = 0;
for (Symbol s : cs.getElements()) { ListBuffer<Symbol> found = ListBuffer.lb();
List<Symbol> allSymbols = sf == null ?
elems :
filter(elems, sf);
int expectedCount = allSymbols.length();
for (Symbol s : sf == null ? cs.getElements() : cs.getElements(sf)) {
checkSameSymbols(s, allSymbols.head); checkSameSymbols(s, allSymbols.head);
allSymbols = allSymbols.tail; allSymbols = allSymbols.tail;
found.append(s);
count++; count++;
} }
if (count != elems.size()) { if (count != expectedCount) {
error("CompoundScope.getElements() did not returned enough symbols"); error("CompoundScope.getElements() did not returned enough symbols");
} }
} }
@ -182,22 +196,35 @@ public class CompoundScopeTest {
* Check that CompoundScope.getElements() correctly visits all symbols * Check that CompoundScope.getElements() correctly visits all symbols
* with a given name in all subscopes (in the correct order) * with a given name in all subscopes (in the correct order)
*/ */
void checkShadowed(CompoundScope cs) { void checkShadowed(CompoundScope cs, Filter<Symbol> sf) {
for (Map.Entry<Name, List<Symbol>> shadowedEntry : shadowedMap.entrySet()) { for (Map.Entry<Name, List<Symbol>> shadowedEntry : shadowedMap.entrySet()) {
int count = 0; int count = 0;
List<Symbol> shadowed = shadowedEntry.getValue(); List<Symbol> shadowed = sf == null ?
shadowedEntry.getValue() :
filter(shadowedEntry.getValue(), sf);
int expectedCount = shadowed.length();
Name name = shadowedEntry.getKey(); Name name = shadowedEntry.getKey();
for (Symbol s : cs.getElementsByName(name)) { for (Symbol s : sf == null ? cs.getElementsByName(name) : cs.getElementsByName(name, sf)) {
checkSameSymbols(s, shadowed.head); checkSameSymbols(s, shadowed.head);
shadowed = shadowed.tail; shadowed = shadowed.tail;
count++; count++;
} }
if (count != shadowedEntry.getValue().size()) { if (count != expectedCount) {
error("CompoundScope.lookup() did not returned enough symbols for name " + name); error("CompoundScope.lookup() did not returned enough symbols for name " + name);
} }
} }
} }
List<Symbol> filter(List<Symbol> elems, Filter<Symbol> sf) {
ListBuffer<Symbol> res = ListBuffer.lb();
for (Symbol s : elems) {
if (sf.accepts(s)) {
res.append(s);
}
}
return res.toList();
}
void checkSameSymbols(Symbol found, Symbol req) { void checkSameSymbols(Symbol found, Symbol req) {
if (found != req) { if (found != req) {
error("Symbol mismatch - found : " + found + ":" + found.hashCode() + "\n" + error("Symbol mismatch - found : " + found + ":" + found.hashCode() + "\n" +