8057543: Replace javac's Filter with Predicate (and lambdas)

Reviewed-by: mcimadamore
This commit is contained in:
Guoxiong Li 2021-04-22 16:44:29 +00:00 committed by Maurizio Cimadamore
parent 8758b554a0
commit 657f103937
19 changed files with 138 additions and 160 deletions

View File

@ -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
@ -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;
@ -38,7 +38,6 @@ import com.sun.tools.javac.comp.Env;
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.
@ -52,7 +51,7 @@ import com.sun.tools.javac.util.Filter;
*/
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;
};

View File

@ -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
@ -29,6 +29,7 @@ import com.sun.tools.javac.code.Kinds.Kind;
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;
@ -70,7 +71,7 @@ public abstract class Scope {
/**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);
}
@ -84,7 +85,7 @@ public abstract class Scope {
/**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.
*/
@ -95,7 +96,7 @@ public abstract class Scope {
/**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);
}
@ -109,7 +110,7 @@ public abstract class Scope {
/**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.
@ -122,7 +123,7 @@ public abstract class Scope {
/** 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;
}
@ -130,7 +131,7 @@ public abstract class Scope {
/** 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();
}
@ -160,7 +161,7 @@ public abstract class Scope {
*/
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.
*/
@ -514,16 +515,16 @@ public abstract class Scope {
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;
}
@ -563,11 +564,11 @@ public abstract class Scope {
}
}
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;
@ -616,7 +617,7 @@ public abstract class Scope {
}
void skipToNextMatchingEntry() {
while (currEntry != null && sf != null && !sf.accepts(currEntry.sym)) {
while (currEntry != null && sf != null && !sf.test(currEntry.sym)) {
currEntry = currEntry.nextSibling;
}
}
@ -624,7 +625,7 @@ public abstract class Scope {
}
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);
@ -729,8 +730,8 @@ public abstract class Scope {
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);
}
@ -815,7 +816,7 @@ public abstract class Scope {
}
@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();
@ -848,16 +849,16 @@ public abstract class Scope {
}
@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
@ -928,7 +929,7 @@ public abstract class Scope {
}
@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 {
@ -951,7 +952,7 @@ public abstract class Scope {
@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();
@ -1075,7 +1076,7 @@ public abstract class Scope {
}
@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,
@ -1085,7 +1086,7 @@ public abstract class Scope {
@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,

View File

@ -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
@ -34,6 +34,7 @@ import java.util.Map;
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;
@ -2161,10 +2162,10 @@ public abstract class Symbol extends AnnoConstruct implements PoolConstant, Elem
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;

View File

@ -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
@ -30,6 +30,7 @@ import java.util.ArrayDeque;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;
import java.util.function.Predicate;
import javax.lang.model.type.*;
@ -644,10 +645,10 @@ public abstract class Type extends AnnoConstruct implements TypeMirror, PoolCons
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);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 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
@ -35,6 +35,7 @@ import java.util.Set;
import java.util.WeakHashMap;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collector;
import javax.tools.JavaFileObject;
@ -944,14 +945,17 @@ public class Types {
return overridden.toList();
}
//where
private Filter<Symbol> bridgeFilter = new Filter<Symbol>() {
public boolean accepts(Symbol t) {
// Use anonymous class instead of lambda expression intentionally,
// because the variable `names` has modifier: final.
private Predicate<Symbol> bridgeFilter = new Predicate<Symbol>() {
public boolean test(Symbol t) {
return t.kind == MTH &&
t.name != names.init &&
t.name != names.clinit &&
(t.flags() & SYNTHETIC) == 0;
}
};
private boolean pendingBridges(ClassSymbol origin, TypeSymbol s) {
//a symbol will be completed from a classfile if (a) symbol has
//an associated file object with CLASS kind and (b) the symbol has
@ -977,7 +981,7 @@ public class Types {
* Scope filter used to skip methods that should be ignored (such as methods
* overridden by j.l.Object) during function interface conversion interface check
*/
class DescriptorFilter implements Filter<Symbol> {
class DescriptorFilter implements Predicate<Symbol> {
TypeSymbol origin;
@ -986,7 +990,7 @@ public class Types {
}
@Override
public boolean accepts(Symbol sym) {
public boolean test(Symbol sym) {
return sym.kind == MTH &&
(sym.flags() & (ABSTRACT | DEFAULT)) == ABSTRACT &&
!overridesObjectMethod(origin, sym) &&
@ -2930,12 +2934,12 @@ public class Types {
class Entry {
final MethodSymbol cachedImpl;
final Filter<Symbol> implFilter;
final Predicate<Symbol> implFilter;
final boolean checkResult;
final int prevMark;
public Entry(MethodSymbol cachedImpl,
Filter<Symbol> scopeFilter,
Predicate<Symbol> scopeFilter,
boolean checkResult,
int prevMark) {
this.cachedImpl = cachedImpl;
@ -2944,14 +2948,14 @@ public class Types {
this.prevMark = prevMark;
}
boolean matches(Filter<Symbol> scopeFilter, boolean checkResult, int mark) {
boolean matches(Predicate<Symbol> scopeFilter, boolean checkResult, int mark) {
return this.implFilter == scopeFilter &&
this.checkResult == checkResult &&
this.prevMark == mark;
}
}
MethodSymbol get(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) {
MethodSymbol get(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Predicate<Symbol> implFilter) {
SoftReference<Map<TypeSymbol, Entry>> ref_cache = _map.get(ms);
Map<TypeSymbol, Entry> cache = ref_cache != null ? ref_cache.get() : null;
if (cache == null) {
@ -2971,7 +2975,7 @@ public class Types {
}
}
private MethodSymbol implementationInternal(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) {
private MethodSymbol implementationInternal(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Predicate<Symbol> implFilter) {
for (Type t = origin.type; t.hasTag(CLASS) || t.hasTag(TYPEVAR); t = supertype(t)) {
t = skipTypeVars(t, false);
TypeSymbol c = t.tsym;
@ -2996,7 +3000,7 @@ public class Types {
private ImplementationCache implCache = new ImplementationCache();
public MethodSymbol implementation(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) {
public MethodSymbol implementation(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Predicate<Symbol> implFilter) {
return implCache.get(ms, origin, checkResult, implFilter);
}
// </editor-fold>
@ -3017,17 +3021,17 @@ public class Types {
this.scope = scope;
}
Filter<Symbol> combine(Filter<Symbol> sf) {
return s -> !s.owner.isInterface() && (sf == null || sf.accepts(s));
Predicate<Symbol> combine(Predicate<Symbol> sf) {
return s -> !s.owner.isInterface() && (sf == null || sf.test(s));
}
@Override
public Iterable<Symbol> getSymbols(Filter<Symbol> sf, LookupKind lookupKind) {
public Iterable<Symbol> getSymbols(Predicate<Symbol> sf, LookupKind lookupKind) {
return scope.getSymbols(combine(sf), lookupKind);
}
@Override
public Iterable<Symbol> getSymbolsByName(Name name, Filter<Symbol> sf, LookupKind lookupKind) {
public Iterable<Symbol> getSymbolsByName(Name name, Predicate<Symbol> sf, LookupKind lookupKind) {
return scope.getSymbolsByName(name, combine(sf), lookupKind);
}
@ -3187,7 +3191,7 @@ public class Types {
CandidatesCache.Entry e = candidatesCache.new Entry(site, ms);
List<MethodSymbol> candidates = candidatesCache.get(e);
if (candidates == null) {
Filter<Symbol> filter = new MethodFilter(ms, site);
Predicate<Symbol> filter = new MethodFilter(ms, site);
List<MethodSymbol> candidates2 = List.nil();
for (Symbol s : membersClosure(site, false).getSymbols(filter)) {
if (!site.tsym.isInterface() && !s.owner.isInterface()) {
@ -3220,7 +3224,7 @@ public class Types {
return methodsMin.toList();
}
// where
private class MethodFilter implements Filter<Symbol> {
private class MethodFilter implements Predicate<Symbol> {
Symbol msym;
Type site;
@ -3230,7 +3234,8 @@ public class Types {
this.site = site;
}
public boolean accepts(Symbol s) {
@Override
public boolean test(Symbol s) {
return s.kind == MTH &&
s.name == msym.name &&
(s.flags() & SYNTHETIC) == 0 &&

View File

@ -26,6 +26,7 @@
package com.sun.tools.javac.comp;
import java.util.*;
import java.util.function.Predicate;
import java.util.function.Supplier;
import javax.lang.model.element.ElementKind;
@ -2137,7 +2138,7 @@ public class Check {
}
}
private Filter<Symbol> equalsHasCodeFilter = s -> MethodSymbol.implementation_filter.accepts(s) &&
private Predicate<Symbol> equalsHasCodeFilter = s -> MethodSymbol.implementation_filter.test(s) &&
(s.flags() & BAD_OVERRIDE) == 0;
public void checkClassOverrideEqualsAndHashIfNeeded(DiagnosticPosition pos,
@ -2216,8 +2217,8 @@ public class Check {
private boolean checkNameClash(ClassSymbol origin, Symbol s1, Symbol s2) {
ClashFilter cf = new ClashFilter(origin.type);
return (cf.accepts(s1) &&
cf.accepts(s2) &&
return (cf.test(s1) &&
cf.test(s2) &&
types.hasSameArgs(s1.erasure(types), s2.erasure(types)));
}
@ -2583,7 +2584,7 @@ public class Check {
}
//where
private class ClashFilter implements Filter<Symbol> {
private class ClashFilter implements Predicate<Symbol> {
Type site;
@ -2596,7 +2597,8 @@ public class Check {
s.owner == site.tsym;
}
public boolean accepts(Symbol s) {
@Override
public boolean test(Symbol s) {
return s.kind == MTH &&
(s.flags() & SYNTHETIC) == 0 &&
!shouldSkip(s) &&
@ -2647,7 +2649,7 @@ public class Check {
}
//where
private class DefaultMethodClashFilter implements Filter<Symbol> {
private class DefaultMethodClashFilter implements Predicate<Symbol> {
Type site;
@ -2655,7 +2657,8 @@ public class Check {
this.site = site;
}
public boolean accepts(Symbol s) {
@Override
public boolean test(Symbol s) {
return s.kind == MTH &&
(s.flags() & DEFAULT) != 0 &&
s.isInheritedIn(site.tsym, types) &&
@ -3802,7 +3805,7 @@ public class Check {
private boolean checkUniqueImport(DiagnosticPosition pos, Scope ordinallyImportedSoFar,
Scope staticallyImportedSoFar, Scope topLevelScope,
Symbol sym, boolean staticImport) {
Filter<Symbol> duplicates = candidate -> candidate != sym && !candidate.type.isErroneous();
Predicate<Symbol> duplicates = candidate -> candidate != sym && !candidate.type.isErroneous();
Symbol ordinaryClashing = ordinallyImportedSoFar.findFirst(sym.name, duplicates);
Symbol staticClashing = null;
if (ordinaryClashing == null && !staticImport) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 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
@ -58,6 +58,7 @@ import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.function.Predicate;
import java.util.function.Supplier;
import com.sun.source.tree.MemberReferenceTree;
@ -1120,7 +1121,7 @@ public class DeferredAttr extends JCTree.Visitor {
*/
abstract static class FilterScanner extends com.sun.tools.javac.tree.TreeScanner {
final Filter<JCTree> treeFilter;
final Predicate<JCTree> treeFilter;
FilterScanner(final Set<JCTree.Tag> validTags) {
this.treeFilter = t -> validTags.contains(t.getTag());
@ -1129,7 +1130,7 @@ public class DeferredAttr extends JCTree.Visitor {
@Override
public void scan(JCTree tree) {
if (tree != null) {
if (treeFilter.accepts(tree)) {
if (treeFilter.test(tree)) {
super.scan(tree);
} else {
skip(tree);

View File

@ -30,6 +30,7 @@ package com.sun.tools.javac.comp;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;
import com.sun.source.tree.LambdaExpressionTree.BodyKind;
import com.sun.tools.javac.code.*;
@ -700,7 +701,7 @@ public class Flow {
TypeSymbol selectorSym = tree.selector.type.tsym;
if ((selectorSym.flags() & ENUM) != 0) {
constants = new HashSet<>();
Filter<Symbol> enumConstantFilter =
Predicate<Symbol> enumConstantFilter =
s -> (s.flags() & ENUM) != 0 && s.kind == Kind.VAR;
for (Symbol s : selectorSym.members().getSymbols(enumConstantFilter)) {
constants.add(s.name);

View File

@ -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
@ -68,6 +68,7 @@ import java.util.Properties;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import static com.sun.tools.javac.code.TypeTag.*;
@ -1229,7 +1230,7 @@ public class Infer {
/** an incorporation cache keeps track of all executed incorporation-related operations */
Map<IncorporationBinaryOp, Boolean> incorporationCache = new HashMap<>();
protected static class BoundFilter implements Filter<Type> {
protected static class BoundFilter implements Predicate<Type> {
InferenceContext inferenceContext;
@ -1238,7 +1239,7 @@ public class Infer {
}
@Override
public boolean accepts(Type t) {
public boolean test(Type t) {
return !t.isErroneous() && !inferenceContext.free(t) &&
!t.hasTag(BOT);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 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
@ -33,6 +33,7 @@ import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Type.ArrayType;
@ -50,7 +51,6 @@ import com.sun.tools.javac.comp.Infer.InferenceException;
import com.sun.tools.javac.comp.Infer.InferenceStep;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.Assert;
import com.sun.tools.javac.util.Filter;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Warner;
@ -147,11 +147,11 @@ public class InferenceContext {
/* Returns the corresponding inference variables.
*/
private List<Type> filterVars(Filter<UndetVar> fu) {
private List<Type> filterVars(Predicate<UndetVar> fu) {
ListBuffer<Type> res = new ListBuffer<>();
for (Type t : undetvars) {
UndetVar uv = (UndetVar)t;
if (fu.accepts(uv)) {
if (fu.test(uv)) {
res.append(uv.qtype);
}
}

View File

@ -1798,7 +1798,7 @@ public class Resolve {
return bestSoFar;
}
//where
class LookupFilter implements Filter<Symbol> {
class LookupFilter implements Predicate<Symbol> {
boolean abstractOk;
@ -1806,7 +1806,8 @@ public class Resolve {
this.abstractOk = abstractOk;
}
public boolean accepts(Symbol s) {
@Override
public boolean test(Symbol s) {
long flags = s.flags();
return s.kind == MTH &&
(flags & SYNTHETIC) == 0 &&

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 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
@ -30,7 +30,6 @@ import java.util.Iterator;
import com.sun.tools.javac.code.Scope;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.util.Filter;
import static com.sun.tools.javac.code.Flags.*;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;

View File

@ -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
@ -27,6 +27,7 @@ package com.sun.tools.javac.parser;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import com.sun.source.tree.CaseTree;
@ -275,42 +276,42 @@ public class JavacParser implements Parser {
token = S.token();
}
protected boolean peekToken(Filter<TokenKind> tk) {
protected boolean peekToken(Predicate<TokenKind> tk) {
return peekToken(0, tk);
}
protected boolean peekToken(int lookahead, Filter<TokenKind> tk) {
return tk.accepts(S.token(lookahead + 1).kind);
protected boolean peekToken(int lookahead, Predicate<TokenKind> tk) {
return tk.test(S.token(lookahead + 1).kind);
}
protected boolean peekToken(Filter<TokenKind> tk1, Filter<TokenKind> tk2) {
protected boolean peekToken(Predicate<TokenKind> tk1, Predicate<TokenKind> tk2) {
return peekToken(0, tk1, tk2);
}
protected boolean peekToken(int lookahead, Filter<TokenKind> tk1, Filter<TokenKind> tk2) {
return tk1.accepts(S.token(lookahead + 1).kind) &&
tk2.accepts(S.token(lookahead + 2).kind);
protected boolean peekToken(int lookahead, Predicate<TokenKind> tk1, Predicate<TokenKind> tk2) {
return tk1.test(S.token(lookahead + 1).kind) &&
tk2.test(S.token(lookahead + 2).kind);
}
protected boolean peekToken(Filter<TokenKind> tk1, Filter<TokenKind> tk2, Filter<TokenKind> tk3) {
protected boolean peekToken(Predicate<TokenKind> tk1, Predicate<TokenKind> tk2, Predicate<TokenKind> tk3) {
return peekToken(0, tk1, tk2, tk3);
}
protected boolean peekToken(int lookahead, Filter<TokenKind> tk1, Filter<TokenKind> tk2, Filter<TokenKind> tk3) {
return tk1.accepts(S.token(lookahead + 1).kind) &&
tk2.accepts(S.token(lookahead + 2).kind) &&
tk3.accepts(S.token(lookahead + 3).kind);
protected boolean peekToken(int lookahead, Predicate<TokenKind> tk1, Predicate<TokenKind> tk2, Predicate<TokenKind> tk3) {
return tk1.test(S.token(lookahead + 1).kind) &&
tk2.test(S.token(lookahead + 2).kind) &&
tk3.test(S.token(lookahead + 3).kind);
}
@SuppressWarnings("unchecked")
protected boolean peekToken(Filter<TokenKind>... kinds) {
protected boolean peekToken(Predicate<TokenKind>... kinds) {
return peekToken(0, kinds);
}
@SuppressWarnings("unchecked")
protected boolean peekToken(int lookahead, Filter<TokenKind>... kinds) {
protected boolean peekToken(int lookahead, Predicate<TokenKind>... kinds) {
for (; lookahead < kinds.length ; lookahead++) {
if (!kinds[lookahead].accepts(S.token(lookahead + 1).kind)) {
if (!kinds[lookahead].test(S.token(lookahead + 1).kind)) {
return false;
}
}
@ -1824,7 +1825,7 @@ public class JavacParser implements Parser {
}
/** Accepts all identifier-like tokens */
protected Filter<TokenKind> LAX_IDENTIFIER = t -> t == IDENTIFIER || t == UNDERSCORE || t == ASSERT || t == ENUM;
protected Predicate<TokenKind> LAX_IDENTIFIER = t -> t == IDENTIFIER || t == UNDERSCORE || t == ASSERT || t == ENUM;
enum ParensResult {
CAST,
@ -2118,7 +2119,7 @@ public class JavacParser implements Parser {
nextToken();
JCExpression bound = parseType();
result = F.at(pos).Wildcard(t, bound);
} else if (LAX_IDENTIFIER.accepts(token.kind)) {
} else if (LAX_IDENTIFIER.test(token.kind)) {
//error recovery
TypeBoundKind t = F.at(Position.NOPOS).TypeBoundKind(BoundKind.UNBOUND);
JCExpression wc = toP(F.at(pos).Wildcard(t, null));
@ -2201,7 +2202,7 @@ public class JavacParser implements Parser {
if (token.pos == endPosTable.errorEndPos) {
// error recovery
Name name;
if (LAX_IDENTIFIER.accepts(token.kind)) {
if (LAX_IDENTIFIER.test(token.kind)) {
name = token.name();
nextToken();
} else {
@ -2694,7 +2695,7 @@ public class JavacParser implements Parser {
nextToken();
JCStatement stat = parseStatementAsBlock();
return List.of(F.at(pos).Labelled(prevToken.name(), stat));
} else if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) {
} else if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.test(token.kind)) {
pos = token.pos;
JCModifiers mods = F.at(Position.NOPOS).Modifiers(0);
F.at(pos);
@ -2847,14 +2848,14 @@ public class JavacParser implements Parser {
}
case BREAK: {
nextToken();
Name label = LAX_IDENTIFIER.accepts(token.kind) ? ident() : null;
Name label = LAX_IDENTIFIER.test(token.kind) ? ident() : null;
accept(SEMI);
JCBreak t = toP(F.at(pos).Break(label));
return t;
}
case CONTINUE: {
nextToken();
Name label = LAX_IDENTIFIER.accepts(token.kind) ? ident() : null;
Name label = LAX_IDENTIFIER.test(token.kind) ? ident() : null;
accept(SEMI);
JCContinue t = toP(F.at(pos).Continue(label));
return t;
@ -3046,7 +3047,7 @@ public class JavacParser implements Parser {
return variableDeclarators(optFinal(0), parseType(true), stats, true).toList();
} else {
JCExpression t = term(EXPR | TYPE);
if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) {
if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.test(token.kind)) {
return variableDeclarators(modifiersOpt(), t, stats, true).toList();
} else if ((lastmode & TYPE) != 0 && token.kind == COLON) {
log.error(DiagnosticFlag.SYNTAX, pos, Errors.BadInitializer("for-loop"));
@ -3228,7 +3229,7 @@ public class JavacParser implements Parser {
* | Identifier "=" AnnotationValue
*/
JCExpression annotationFieldValue() {
if (LAX_IDENTIFIER.accepts(token.kind)) {
if (LAX_IDENTIFIER.test(token.kind)) {
selectExprMode();
JCExpression t1 = term1();
if (t1.hasTag(IDENT) && token.kind == EQ) {
@ -3429,7 +3430,7 @@ public class JavacParser implements Parser {
} else {
if (allowThisIdent ||
!lambdaParameter ||
LAX_IDENTIFIER.accepts(token.kind) ||
LAX_IDENTIFIER.test(token.kind) ||
mods.flags != Flags.PARAMETER ||
mods.annotations.nonEmpty()) {
JCExpression pn = qualident(false);
@ -3502,7 +3503,7 @@ public class JavacParser implements Parser {
return variableDeclaratorRest(token.pos, mods, t, ident(), true, null, true, false);
}
JCExpression t = term(EXPR | TYPE);
if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) {
if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.test(token.kind)) {
JCModifiers mods = toP(F.at(startPos).Modifiers(Flags.FINAL));
return variableDeclaratorRest(token.pos, mods, t, ident(), true, null, true, false);
} else {
@ -3764,7 +3765,7 @@ public class JavacParser implements Parser {
JCErroneous erroneousTree = syntaxError(token.pos, List.of(mods), Errors.RecordHeaderExpected);
return toP(F.Exec(erroneousTree));
} else {
if (LAX_IDENTIFIER.accepts(token.kind)) {
if (LAX_IDENTIFIER.test(token.kind)) {
errs = List.of(mods, toP(F.at(pos).Ident(ident())));
setErrorEndPos(token.pos);
} else {

View File

@ -27,6 +27,7 @@ package com.sun.tools.javac.parser;
import java.util.HashMap;
import java.util.Locale;
import java.util.function.Predicate;
import java.util.Map;
import com.sun.tools.javac.api.Formattable;
@ -90,7 +91,7 @@ public class Tokens {
* This enum defines all tokens used by the javac scanner. A token is
* optionally associated with a name.
*/
public enum TokenKind implements Formattable, Filter<TokenKind> {
public enum TokenKind implements Formattable, Predicate<TokenKind> {
EOF(),
ERROR(),
IDENTIFIER(Tag.NAMED),
@ -263,7 +264,7 @@ public class Tokens {
}
@Override
public boolean accepts(TokenKind that) {
public boolean test(TokenKind that) {
return this == that;
}
}

View File

@ -1,39 +0,0 @@
/*
* Copyright (c) 2010, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.javac.util;
/**
* Simple filter acting as a boolean predicate. Method accepts return true if
* the supplied element matches against the filter.
*/
public interface Filter<T> {
/**
* Does this element match against the filter?
* @param t element to be checked
* @return true if the element satisfy constraints imposed by filter
*/
boolean accepts(T t);
}

View File

@ -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
@ -132,13 +132,13 @@ public class Log extends AbstractLog {
*/
public static class DeferredDiagnosticHandler extends DiagnosticHandler {
private Queue<JCDiagnostic> deferred = new ListBuffer<>();
private final Filter<JCDiagnostic> filter;
private final Predicate<JCDiagnostic> filter;
public DeferredDiagnosticHandler(Log log) {
this(log, null);
}
public DeferredDiagnosticHandler(Log log, Filter<JCDiagnostic> filter) {
public DeferredDiagnosticHandler(Log log, Predicate<JCDiagnostic> filter) {
this.filter = filter;
install(log);
}
@ -146,7 +146,7 @@ public class Log extends AbstractLog {
@Override
public void report(JCDiagnostic diag) {
if (!diag.isFlagSet(JCDiagnostic.DiagnosticFlag.NON_DEFERRABLE) &&
(filter == null || filter.accepts(diag))) {
(filter == null || filter.test(diag))) {
deferred.add(diag);
} else {
prev.report(diag);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 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
@ -221,7 +221,7 @@ class ReplParser extends JavacParser {
nextToken();
JCStatement stat = parseStatement();
return List.<JCTree>of(F.at(pos).Labelled(prevToken.name(), stat));
} else if ((isVoid || (lastmode & TYPE) != 0) && LAX_IDENTIFIER.accepts(token.kind)) {
} else if ((isVoid || (lastmode & TYPE) != 0) && LAX_IDENTIFIER.test(token.kind)) {
// we have "Type Ident", so we can assume it is variable or method declaration
pos = token.pos;
Name name = ident();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 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
@ -211,7 +211,7 @@ class TrialParser extends JavacParser {
nextToken();
JCStatement stat = parseStatement();
return List.<JCTree>of(F.at(pos).Labelled(prevToken.name(), stat));
} else if ((isVoid || (lastmode & TYPE) != 0) && LAX_IDENTIFIER.accepts(token.kind)) {
} else if ((isVoid || (lastmode & TYPE) != 0) && LAX_IDENTIFIER.test(token.kind)) {
// we have "Type Ident", so we can assume it is variable or method declaration
pos = token.pos;
Name name = ident();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 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
@ -33,6 +33,7 @@
import java.util.Random;
import java.util.Map;
import java.util.HashMap;
import java.util.function.Predicate;
import com.sun.tools.javac.util.*;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Scope.*;
@ -137,8 +138,9 @@ public class CompoundScopeTest {
}
}
class OddFilter implements Filter<Symbol> {
public boolean accepts(Symbol s) {
class OddFilter implements Predicate<Symbol> {
@Override
public boolean test(Symbol s) {
Name numPart = s.name.subName(1, s.name.length());
return Integer.parseInt(numPart.toString()) % 2 != 0;
}
@ -177,7 +179,7 @@ public class CompoundScopeTest {
* Check that CompoundScope.getElements() correctly visits all symbols
* in all subscopes (in the correct order)
*/
void checkElems(CompoundScope cs, Filter<Symbol> sf) {
void checkElems(CompoundScope cs, Predicate<Symbol> sf) {
int count = 0;
ListBuffer<Symbol> found = new ListBuffer<>();
List<Symbol> allSymbols = sf == null ?
@ -199,7 +201,7 @@ public class CompoundScopeTest {
* Check that CompoundScope.getElements() correctly visits all symbols
* with a given name in all subscopes (in the correct order)
*/
void checkShadowed(CompoundScope cs, Filter<Symbol> sf) {
void checkShadowed(CompoundScope cs, Predicate<Symbol> sf) {
for (Map.Entry<Name, List<Symbol>> shadowedEntry : shadowedMap.entrySet()) {
int count = 0;
List<Symbol> shadowed = sf == null ?
@ -218,10 +220,10 @@ public class CompoundScopeTest {
}
}
List<Symbol> filter(List<Symbol> elems, Filter<Symbol> sf) {
List<Symbol> filter(List<Symbol> elems, Predicate<Symbol> sf) {
ListBuffer<Symbol> res = new ListBuffer<>();
for (Symbol s : elems) {
if (sf.accepts(s)) {
if (sf.test(s)) {
res.append(s);
}
}