From e5e395889a50496aea0b2fc605f8930103d4d92b Mon Sep 17 00:00:00 2001 From: Yaroslav Pankratyev Date: Sun, 24 Jun 2018 20:14:45 +0700 Subject: [PATCH] UastCallMatcher to support matching arguments count for callable reference expressions --- .../codeInspection/UastCallMatcher.java | 24 +++++++++++++------ .../codeInspection/UastCallMatcherTest.java | 5 ++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/jvm/jvm-analysis-api/src/com/intellij/codeInspection/UastCallMatcher.java b/jvm/jvm-analysis-api/src/com/intellij/codeInspection/UastCallMatcher.java index 0602701a6a78..b44ea54932d8 100644 --- a/jvm/jvm-analysis-api/src/com/intellij/codeInspection/UastCallMatcher.java +++ b/jvm/jvm-analysis-api/src/com/intellij/codeInspection/UastCallMatcher.java @@ -1,10 +1,8 @@ // 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.intellij.codeInspection; -import com.intellij.psi.PsiClass; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiMethod; -import com.intellij.psi.PsiType; +import com.intellij.openapi.util.NullUtils; +import com.intellij.psi.*; import com.intellij.psi.util.InheritanceUtil; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Contract; @@ -56,11 +54,10 @@ public interface UastCallMatcher { - - //TODO support primitive types for receiver/return types and arguments //TODO support static methods //TODO support inheritors for classFqn + //TODO support generics class SimpleUastCallMatcher implements UastCallMatcher { // for all fields 'null' = doesn't matter @@ -189,7 +186,16 @@ public interface UastCallMatcher { private boolean argumentsMatch(@NotNull UCallableReferenceExpression expression) { if (myArguments == null) return true; - return true; //TODO implement (seems like it only has meaning for static method references) + PsiElement resolved = expression.resolve(); + if (!(resolved instanceof PsiMethod)) return false; + + PsiMethod method = (PsiMethod)resolved; + PsiParameterList parameterList = method.getParameterList(); + if (myArguments.length != parameterList.getParametersCount()) { + return false; + } + //TODO implement argument types matching + return true; } } @@ -245,6 +251,10 @@ public interface UastCallMatcher { @NotNull public UastCallMatcher build() { + if (!NullUtils.hasNotNull(myMethodName, myArguments, myClassFqn, myReturnTypeClassFqn)) { + throw new IllegalStateException("At least one qualifier must be specified"); + } + return new SimpleUastCallMatcher(myMethodName, myArguments, myMatchArgumentTypeInheritors, diff --git a/jvm/jvm-analysis-java-tests/testSrc/com/intellij/codeInspection/UastCallMatcherTest.java b/jvm/jvm-analysis-java-tests/testSrc/com/intellij/codeInspection/UastCallMatcherTest.java index 6eba8b6c2ffb..cc857875ac7b 100644 --- a/jvm/jvm-analysis-java-tests/testSrc/com/intellij/codeInspection/UastCallMatcherTest.java +++ b/jvm/jvm-analysis-java-tests/testSrc/com/intellij/codeInspection/UastCallMatcherTest.java @@ -146,6 +146,11 @@ public class UastCallMatcherTest extends JavaCodeInsightFixtureTestCase { )); assertEquals(3, matchCallableReferenceExpression( + builder().withArgumentsCount(1).build(), + expressions + )); + + assertEquals(3, matchCallableReferenceExpression( // one is for Function#apply builder().withReturnType("java.lang.String").build(), expressions ));