Skip to content

Commit

Permalink
8057543: Replace javac's Filter with Predicate (and lambdas)
Browse files Browse the repository at this point in the history
Reviewed-by: mcimadamore
  • Loading branch information
lgxbslgx authored and mcimadamore committed Apr 22, 2021
1 parent 8758b55 commit 657f103
Show file tree
Hide file tree
Showing 19 changed files with 138 additions and 160 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,7 +25,7 @@

package com.sun.tools.javac.api;


import java.util.function.Predicate;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
Expand All @@ -38,7 +38,6 @@
import com.sun.tools.javac.util.DefinedBy;
import com.sun.tools.javac.util.DefinedBy.Api;
import com.sun.tools.javac.util.Assert;
import com.sun.tools.javac.util.Filter;

/**
* Provides an implementation of Scope.
Expand All @@ -52,7 +51,7 @@
*/
public class JavacScope implements com.sun.source.tree.Scope {

private static final Filter<Symbol> VALIDATOR = sym -> {
private static final Predicate<Symbol> VALIDATOR = sym -> {
sym.apiComplete();
return sym.kind != Kind.ERR;
};
Expand Down
53 changes: 27 additions & 26 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/code/Scope.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -29,6 +29,7 @@
import java.lang.ref.WeakReference;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Predicate;

import com.sun.tools.javac.code.Symbol.CompletionFailure;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
Expand Down Expand Up @@ -70,7 +71,7 @@ public final Iterable<Symbol> getSymbols() {

/**Returns Symbols that match the given filter. Symbols from outward Scopes are included.
*/
public final Iterable<Symbol> getSymbols(Filter<Symbol> sf) {
public final Iterable<Symbol> getSymbols(Predicate<Symbol> sf) {
return getSymbols(sf, RECURSIVE);
}

Expand All @@ -84,7 +85,7 @@ public final Iterable<Symbol> getSymbols(LookupKind lookupKind) {
/**Returns Symbols that match the given filter. Symbols from outward Scopes are included
* iff lookupKind == RECURSIVE.
*/
public abstract Iterable<Symbol> getSymbols(Filter<Symbol> sf, LookupKind lookupKind);
public abstract Iterable<Symbol> getSymbols(Predicate<Symbol> sf, LookupKind lookupKind);

/**Returns Symbols with the given name. Symbols from outward Scopes are included.
*/
Expand All @@ -95,7 +96,7 @@ public final Iterable<Symbol> getSymbolsByName(Name name) {
/**Returns Symbols with the given name that match the given filter.
* Symbols from outward Scopes are included.
*/
public final Iterable<Symbol> getSymbolsByName(final Name name, final Filter<Symbol> sf) {
public final Iterable<Symbol> getSymbolsByName(final Name name, final Predicate<Symbol> sf) {
return getSymbolsByName(name, sf, RECURSIVE);
}

Expand All @@ -109,7 +110,7 @@ public final Iterable<Symbol> getSymbolsByName(Name name, LookupKind lookupKind)
/**Returns Symbols with the given name that match the given filter.
* Symbols from outward Scopes are included iff lookupKind == RECURSIVE.
*/
public abstract Iterable<Symbol> getSymbolsByName(final Name name, final Filter<Symbol> sf,
public abstract Iterable<Symbol> getSymbolsByName(final Name name, final Predicate<Symbol> sf,
final LookupKind lookupKind);

/** Return the first Symbol from this or outward scopes with the given name.
Expand All @@ -122,15 +123,15 @@ public final Symbol findFirst(Name name) {
/** Return the first Symbol from this or outward scopes with the given name that matches the
* given filter. Returns null if none.
*/
public Symbol findFirst(Name name, Filter<Symbol> sf) {
public Symbol findFirst(Name name, Predicate<Symbol> sf) {
Iterator<Symbol> it = getSymbolsByName(name, sf).iterator();
return it.hasNext() ? it.next() : null;
}

/** Returns true iff there are is at least one Symbol in this scope matching the given filter.
* Does not inspect outward scopes.
*/
public boolean anyMatch(Filter<Symbol> filter) {
public boolean anyMatch(Predicate<Symbol> filter) {
return getSymbols(filter, NON_RECURSIVE).iterator().hasNext();
}

Expand Down Expand Up @@ -160,7 +161,7 @@ public boolean isEmpty() {
*/
public abstract boolean isStaticallyImported(Symbol byName);

private static final Filter<Symbol> noFilter = null;
private static final Predicate<Symbol> noFilter = null;

/** A list of scopes to be notified if items are to be removed from this scope.
*/
Expand Down Expand Up @@ -514,16 +515,16 @@ protected Entry lookup(Name name) {
return lookup(name, noFilter);
}

protected Entry lookup(Name name, Filter<Symbol> sf) {
protected Entry lookup(Name name, Predicate<Symbol> sf) {
Entry e = table[getIndex(name)];
if (e == null || e == sentinel)
return sentinel;
while (e.scope != null && (e.sym.name != name || (sf != null && !sf.accepts(e.sym))))
while (e.scope != null && (e.sym.name != name || (sf != null && !sf.test(e.sym))))
e = e.shadowed;
return e;
}

public Symbol findFirst(Name name, Filter<Symbol> sf) {
public Symbol findFirst(Name name, Predicate<Symbol> sf) {
return lookup(name, sf).sym;
}

Expand Down Expand Up @@ -563,11 +564,11 @@ int getIndex (Name name) {
}
}

public boolean anyMatch(Filter<Symbol> sf) {
public boolean anyMatch(Predicate<Symbol> sf) {
return getSymbols(sf, NON_RECURSIVE).iterator().hasNext();
}

public Iterable<Symbol> getSymbols(final Filter<Symbol> sf,
public Iterable<Symbol> getSymbols(final Predicate<Symbol> sf,
final LookupKind lookupKind) {
return () -> new Iterator<Symbol>() {
private ScopeImpl currScope = ScopeImpl.this;
Expand Down Expand Up @@ -616,15 +617,15 @@ private void update() {
}

void skipToNextMatchingEntry() {
while (currEntry != null && sf != null && !sf.accepts(currEntry.sym)) {
while (currEntry != null && sf != null && !sf.test(currEntry.sym)) {
currEntry = currEntry.nextSibling;
}
}
};
}

public Iterable<Symbol> getSymbolsByName(final Name name,
final Filter<Symbol> sf,
final Predicate<Symbol> sf,
final LookupKind lookupKind) {
return () -> new Iterator<Symbol>() {
Entry currentEntry = lookup(name, sf);
Expand Down Expand Up @@ -729,8 +730,8 @@ public Entry next() {
return shadowed;
}

public Entry next(Filter<Symbol> sf) {
if (shadowed.sym == null || sf == null || sf.accepts(shadowed.sym)) return shadowed;
public Entry next(Predicate<Symbol> sf) {
if (shadowed.sym == null || sf == null || sf.test(shadowed.sym)) return shadowed;
else return shadowed.next(sf);
}

Expand Down Expand Up @@ -815,7 +816,7 @@ private Scope appendScope(Scope newScope, Name name) {
}

@Override
public Iterable<Symbol> getSymbolsByName(Name name, Filter<Symbol> sf, LookupKind lookupKind) {
public Iterable<Symbol> getSymbolsByName(Name name, Predicate<Symbol> sf, LookupKind lookupKind) {
Scope[] scopes = name2Scopes.get(name);
if (scopes == null)
return Collections.emptyList();
Expand Down Expand Up @@ -848,16 +849,16 @@ public SingleEntryScope(Symbol owner, Symbol sym, Scope origin) {
}

@Override
public Iterable<Symbol> getSymbols(Filter<Symbol> sf, LookupKind lookupKind) {
return sf == null || sf.accepts(sym) ? content : Collections.emptyList();
public Iterable<Symbol> getSymbols(Predicate<Symbol> sf, LookupKind lookupKind) {
return sf == null || sf.test(sym) ? content : Collections.emptyList();
}

@Override
public Iterable<Symbol> getSymbolsByName(Name name,
Filter<Symbol> sf,
Predicate<Symbol> sf,
LookupKind lookupKind) {
return sym.name == name &&
(sf == null || sf.accepts(sym)) ? content : Collections.emptyList();
(sf == null || sf.test(sym)) ? content : Collections.emptyList();
}

@Override
Expand Down Expand Up @@ -928,7 +929,7 @@ public FilterImportScope(Types types,
}

@Override
public Iterable<Symbol> getSymbols(final Filter<Symbol> sf, final LookupKind lookupKind) {
public Iterable<Symbol> getSymbols(final Predicate<Symbol> sf, final LookupKind lookupKind) {
if (filterName != null)
return getSymbolsByName(filterName, sf, lookupKind);
try {
Expand All @@ -951,7 +952,7 @@ Iterable<Symbol> doLookup(TypeSymbol tsym) {

@Override
public Iterable<Symbol> getSymbolsByName(final Name name,
final Filter<Symbol> sf,
final Predicate<Symbol> sf,
final LookupKind lookupKind) {
if (filterName != null && filterName != name)
return Collections.emptyList();
Expand Down Expand Up @@ -1075,7 +1076,7 @@ public String toString() {
}

@Override
public Iterable<Symbol> getSymbols(final Filter<Symbol> sf,
public Iterable<Symbol> getSymbols(final Predicate<Symbol> sf,
final LookupKind lookupKind) {
return () -> Iterators.createCompoundIterator(subScopes,
scope -> scope.getSymbols(sf,
Expand All @@ -1085,7 +1086,7 @@ public Iterable<Symbol> getSymbols(final Filter<Symbol> sf,

@Override
public Iterable<Symbol> getSymbolsByName(final Name name,
final Filter<Symbol> sf,
final Predicate<Symbol> sf,
final LookupKind lookupKind) {
return () -> Iterators.createCompoundIterator(subScopes,
scope -> scope.getSymbolsByName(name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -34,6 +34,7 @@
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import java.util.function.Predicate;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
Expand Down Expand Up @@ -2161,10 +2162,10 @@ public MethodSymbol implementation(TypeSymbol origin, Types types, boolean check
return implementation(origin, types, checkResult, implementation_filter);
}
// where
public static final Filter<Symbol> implementation_filter = s ->
public static final Predicate<Symbol> implementation_filter = s ->
s.kind == MTH && (s.flags() & SYNTHETIC) == 0;

public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult, Filter<Symbol> implFilter) {
public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult, Predicate<Symbol> implFilter) {
MethodSymbol res = types.implementation(this, origin, checkResult, implFilter);
if (res != null)
return res;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -30,6 +30,7 @@
import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;
import java.util.function.Predicate;

import javax.lang.model.type.*;

Expand Down Expand Up @@ -644,10 +645,10 @@ public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
return false;
}

public static List<Type> filter(List<Type> ts, Filter<Type> tf) {
public static List<Type> filter(List<Type> ts, Predicate<Type> tf) {
ListBuffer<Type> buf = new ListBuffer<>();
for (Type t : ts) {
if (tf.accepts(t)) {
if (tf.test(t)) {
buf.append(t);
}
}
Expand Down
Loading

1 comment on commit 657f103

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.