CallMatcher: add default method for restricting language level

This commit is contained in:
Roman.Ivanov
2019-04-01 16:59:54 +07:00
parent f490d2a3dc
commit 8609e2540a
2 changed files with 34 additions and 5 deletions
@@ -1,6 +1,7 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.siyeh.ig.callMatcher;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiUtil;
@@ -160,6 +161,38 @@ public interface CallMatcher extends Predicate<PsiMethodCallExpression> {
return null;
}
default CallMatcher withLanguageLevelAtLeast(@NotNull LanguageLevel level) {
return new CallMatcher() {
@Override
public Stream<String> names() {
return CallMatcher.this.names();
}
@Override
public boolean methodReferenceMatches(PsiMethodReferenceExpression methodRef) {
return CallMatcher.this.methodReferenceMatches(methodRef);
}
@Override
public boolean test(@Nullable PsiMethodCallExpression call) {
if (call == null || PsiUtil.getLanguageLevel(call).isAtLeast(level)) return false;
return CallMatcher.this.test(call);
}
@Override
public boolean methodMatches(@Nullable PsiMethod method) {
if (method == null || PsiUtil.getLanguageLevel(method).isAtLeast(level)) return false;
return CallMatcher.this.methodMatches(method);
}
@Override
public String toString() {
return CallMatcher.this.toString();
}
};
}
class Simple implements CallMatcher {
static final Simple ENUM_VALUES = new Simple("", Collections.singleton("values"), ArrayUtil.EMPTY_STRING_ARRAY, CallType.ENUM_STATIC);
static final Simple ENUM_VALUE_OF =
@@ -101,10 +101,6 @@ public class BoolUtils {
CallMatcher.exactInstanceCall(OPTIONAL_DOUBLE, "isEmpty").parameterCount(0)
);
private static Predicate<PsiMethodCallExpression> withMinimalLanguageLevel(CallMatcher matcher, LanguageLevel level) {
return matcher.and(expression -> PsiUtil.getLanguageLevel(expression).isAtLeast(level));
}
private static class PredicatedReplacement {
Predicate<PsiMethodCallExpression> predicate;
String name;
@@ -118,7 +114,7 @@ public class BoolUtils {
private static final List<PredicatedReplacement> ourReplacements = new ArrayList<>();
static {
ourReplacements.add(new PredicatedReplacement(OPTIONAL_IS_EMPTY, "isPresent"));
ourReplacements.add(new PredicatedReplacement(withMinimalLanguageLevel(OPTIONAL_IS_PRESENT, LanguageLevel.JDK_11), "isEmpty"));
ourReplacements.add(new PredicatedReplacement(OPTIONAL_IS_PRESENT.withLanguageLevelAtLeast(LanguageLevel.JDK_11), "isEmpty"));
ourReplacements.add(new PredicatedReplacement(STREAM_ANY_MATCH, "noneMatch"));
ourReplacements.add(new PredicatedReplacement(STREAM_NONE_MATCH, "anyMatch"));
}