From a585246332a7bf07ee6dd6f85a46013214450b4e Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Thu, 23 May 2019 11:13:06 +0700 Subject: [PATCH] OptionalIsPresentInspection: simplified using CallMatchers GitOrigin-RevId: 059883407fe4ad57a75b3ed4cf750be4d00d9e95 --- .../OptionalIsPresentInspection.java | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java b/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java index e56f20e99496..66fc2cc5d582 100644 --- a/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java @@ -16,6 +16,7 @@ import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiTypesUtil; import com.intellij.psi.util.PsiUtil; import com.intellij.util.ObjectUtils; +import com.siyeh.ig.callMatcher.CallMatcher; import com.siyeh.ig.psiutils.*; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Nls; @@ -29,6 +30,9 @@ import static com.intellij.codeInsight.PsiEquivalenceUtil.areElementsEquivalent; public class OptionalIsPresentInspection extends AbstractBaseJavaLocalInspectionTool { private static final Logger LOG = Logger.getInstance(OptionalIsPresentInspection.class); + private static final CallMatcher OPTIONAL_IS_PRESENT = + CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_OPTIONAL, "isPresent").parameterCount(0); + private static final OptionalIsPresentCase[] CASES = { new ReturnCase(), new AssignmentCase(), @@ -132,14 +136,8 @@ public class OptionalIsPresentInspection extends AbstractBaseJavaLocalInspection @Nullable @Contract("null -> null") static PsiReferenceExpression extractOptionalFromIsPresentCheck(PsiExpression expression) { - if (!(expression instanceof PsiMethodCallExpression)) return null; - PsiMethodCallExpression call = (PsiMethodCallExpression)expression; - if (!call.getArgumentList().isEmpty()) return null; - if (!"isPresent".equals(call.getMethodExpression().getReferenceName())) return null; - PsiMethod method = call.resolveMethod(); - if (method == null) return null; - PsiClass containingClass = method.getContainingClass(); - if (containingClass == null || !CommonClassNames.JAVA_UTIL_OPTIONAL.equals(containingClass.getQualifiedName())) return null; + PsiMethodCallExpression call = ObjectUtils.tryCast(expression, PsiMethodCallExpression.class); + if (!OPTIONAL_IS_PRESENT.matches(call)) return null; PsiReferenceExpression qualifier = ObjectUtils.tryCast(call.getMethodExpression().getQualifierExpression(), PsiReferenceExpression.class); if (qualifier == null) return null; @@ -150,15 +148,10 @@ public class OptionalIsPresentInspection extends AbstractBaseJavaLocalInspection @Contract("null, _ -> false") static boolean isOptionalGetCall(PsiElement element, @NotNull PsiReferenceExpression optionalRef) { - if (!(element instanceof PsiMethodCallExpression)) return false; - PsiMethodCallExpression call = (PsiMethodCallExpression)element; - if (!call.getArgumentList().isEmpty()) return false; - PsiReferenceExpression methodExpression = call.getMethodExpression(); - if ("get".equals(methodExpression.getReferenceName())) { - PsiExpression qualifier = ExpressionUtils.getEffectiveQualifier(methodExpression); - return qualifier != null && areElementsEquivalent(qualifier, optionalRef); - } - return false; + PsiMethodCallExpression call = ObjectUtils.tryCast(element, PsiMethodCallExpression.class); + if (!OptionalUtil.JDK_OPTIONAL_GET.matches(call)) return false; + PsiExpression qualifier = ExpressionUtils.getEffectiveQualifier(call.getMethodExpression()); + return qualifier != null && areElementsEquivalent(qualifier, optionalRef); } @NotNull