mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-05 01:50:56 +07:00
[jvm] Remove UAST call matcher API
Replaces usages with CallMatcher API. GitOrigin-RevId: 26ea3df0834cf0c208288fd54a2c49abb8e87563
This commit is contained in:
committed by
intellij-monorepo-bot
parent
ca89c5b24d
commit
6f567952cd
@@ -1,259 +0,0 @@
|
||||
// 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.openapi.util.NullUtils;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.uast.UCallExpression;
|
||||
import org.jetbrains.uast.UCallableReferenceExpression;
|
||||
import org.jetbrains.uast.UExpression;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
|
||||
@ApiStatus.Internal
|
||||
public interface UastCallMatcher {
|
||||
|
||||
@Contract("null -> false")
|
||||
boolean testCallExpression(@Nullable UCallExpression expression);
|
||||
|
||||
@Contract("null -> false")
|
||||
boolean testCallableReferenceExpression(@Nullable UCallableReferenceExpression expression);
|
||||
|
||||
|
||||
@NotNull
|
||||
static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static UastCallMatcher anyOf(UastCallMatcher @NotNull ... matchers) {
|
||||
return new UastCallMatcher() {
|
||||
@Override
|
||||
public boolean testCallExpression(@Nullable UCallExpression expression) {
|
||||
return Arrays.stream(matchers).anyMatch(matcher -> matcher.testCallExpression(expression));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean testCallableReferenceExpression(@Nullable UCallableReferenceExpression expression) {
|
||||
return Arrays.stream(matchers).anyMatch(matcher -> matcher.testCallableReferenceExpression(expression));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
//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
|
||||
|
||||
private final String myMethodName;
|
||||
/**
|
||||
* array length is arguments count; each element is argument type FQN
|
||||
*/
|
||||
private final String[] myArguments;
|
||||
private final boolean myMatchArgumentTypeInheritors;
|
||||
private final String myReturnTypeClassFqn;
|
||||
/**
|
||||
* FQN of receiver type class (for method calls) or class/object type class (for method references).
|
||||
*/
|
||||
private final String myClassFqn;
|
||||
|
||||
|
||||
public SimpleUastCallMatcher(@Nullable String methodName,
|
||||
String @Nullable [] arguments,
|
||||
boolean matchArgumentTypeInheritors,
|
||||
@Nullable String classFqn,
|
||||
@Nullable String returnTypeClassFqn) {
|
||||
if (methodName == null &&
|
||||
arguments == null &&
|
||||
classFqn == null &&
|
||||
returnTypeClassFqn == null) {
|
||||
throw new IllegalArgumentException("At least one qualifier must be specified");
|
||||
}
|
||||
myMethodName = methodName;
|
||||
myArguments = arguments;
|
||||
myMatchArgumentTypeInheritors = matchArgumentTypeInheritors;
|
||||
myClassFqn = classFqn;
|
||||
myReturnTypeClassFqn = returnTypeClassFqn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean testCallExpression(@Nullable UCallExpression expression) {
|
||||
if (expression == null || expression.getMethodName() == null) return false; // null method name for constructor calls
|
||||
return methodNameMatches(expression) &&
|
||||
classMatches(expression) &&
|
||||
returnTypeMatches(expression) &&
|
||||
argumentsMatch(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean testCallableReferenceExpression(@Nullable UCallableReferenceExpression expression) {
|
||||
if (expression == null) return false;
|
||||
return methodNameMatches(expression) &&
|
||||
classMatches(expression) &&
|
||||
returnTypeMatches(expression) &&
|
||||
argumentsMatch(expression);
|
||||
}
|
||||
|
||||
|
||||
private boolean methodNameMatches(@NotNull UCallExpression expression) {
|
||||
return myMethodName == null ||
|
||||
myMethodName.equals(expression.getMethodName());
|
||||
}
|
||||
|
||||
private boolean methodNameMatches(@NotNull UCallableReferenceExpression expression) {
|
||||
return myMethodName == null ||
|
||||
myMethodName.equals(expression.getCallableName());
|
||||
}
|
||||
|
||||
private boolean classMatches(@NotNull UCallExpression expression) {
|
||||
return myClassFqn == null ||
|
||||
myClassFqn.equals(AnalysisUastUtil.getExpressionReceiverTypeClassFqn(expression));
|
||||
}
|
||||
|
||||
private boolean classMatches(@NotNull UCallableReferenceExpression expression) {
|
||||
return myClassFqn == null ||
|
||||
myClassFqn.equals(AnalysisUastUtil.getCallableReferenceClassFqn(expression));
|
||||
}
|
||||
|
||||
private boolean returnTypeMatches(@NotNull UCallExpression expression) {
|
||||
return myReturnTypeClassFqn == null ||
|
||||
myReturnTypeClassFqn.equals(AnalysisUastUtil.getExpressionReturnTypePsiClassFqn(expression));
|
||||
}
|
||||
|
||||
private boolean returnTypeMatches(@NotNull UCallableReferenceExpression expression) {
|
||||
if (myReturnTypeClassFqn == null) return true;
|
||||
PsiElement resolved = expression.resolve();
|
||||
if (!(resolved instanceof PsiMethod)) return false;
|
||||
//TODO doesn't work for generics
|
||||
return myReturnTypeClassFqn.equals(AnalysisUastUtil.getTypeClassFqn(((PsiMethod)resolved).getReturnType()));
|
||||
}
|
||||
|
||||
private boolean argumentsMatch(@NotNull UCallExpression expression) {
|
||||
if (myArguments == null) return true;
|
||||
if (myArguments.length != expression.getValueArgumentCount()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<UExpression> argumentExpressions = null;
|
||||
for (int i = 0; i < myArguments.length; i++) {
|
||||
String requiredArgumentTypeClassFqn = myArguments[i];
|
||||
if (requiredArgumentTypeClassFqn == null) continue;
|
||||
if (argumentExpressions == null) {
|
||||
argumentExpressions = expression.getValueArguments();
|
||||
}
|
||||
|
||||
UExpression argumentExpression = argumentExpressions.get(i);
|
||||
PsiType argumentExpressionType = argumentExpression.getExpressionType();
|
||||
if (requiredArgumentTypeClassFqn.equals(AnalysisUastUtil.getTypeClassFqn(argumentExpressionType))) {
|
||||
continue;
|
||||
}
|
||||
if (!myMatchArgumentTypeInheritors) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PsiClass argumentExpressionTypeClass = AnalysisUastUtil.getTypePsiClass(argumentExpressionType);
|
||||
if (argumentExpressionTypeClass == null) return false;
|
||||
|
||||
//TODO probably this can be optimized using BFS
|
||||
LinkedHashSet<PsiClass> expressionTypeSupers = InheritanceUtil.getSuperClasses(argumentExpressionTypeClass);
|
||||
boolean argumentMatches = false;
|
||||
for (PsiClass expressionTypeSuper : expressionTypeSupers) {
|
||||
if (requiredArgumentTypeClassFqn.equals(expressionTypeSuper.getQualifiedName())) {
|
||||
argumentMatches = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!argumentMatches) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean argumentsMatch(@NotNull UCallableReferenceExpression expression) {
|
||||
if (myArguments == null) return true;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Builder for {@link SimpleUastCallMatcher}. At least one qualifier must be specified.
|
||||
*
|
||||
* Please note that {@link #withArgumentsCount(int)} and {@link #withArgumentTypes(String...)} cannot be used
|
||||
* at the same time (only the last call will have an effect).
|
||||
*/
|
||||
class Builder {
|
||||
private String myMethodName;
|
||||
private String[] myArguments;
|
||||
private boolean myMatchArgumentTypeInheritors = true;
|
||||
private String myClassFqn;
|
||||
private String myReturnTypeClassFqn;
|
||||
|
||||
@NotNull
|
||||
public Builder withMethodName(@NotNull String methodName) {
|
||||
myMethodName = methodName;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Builder withClassFqn(@NotNull String classFqn) {
|
||||
myClassFqn = classFqn;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Builder withReturnType(@NotNull String returnTypeClassFqn) {
|
||||
myReturnTypeClassFqn = returnTypeClassFqn;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Builder withArgumentsCount(int argumentsCount) {
|
||||
myArguments = new String[argumentsCount];
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Builder withArgumentTypes(String @NotNull ... arguments) {
|
||||
myArguments = arguments;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Builder withMatchArgumentTypeInheritors(boolean matchArgumentTypeInheritors) {
|
||||
myMatchArgumentTypeInheritors = matchArgumentTypeInheritors;
|
||||
return this;
|
||||
}
|
||||
|
||||
@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,
|
||||
myClassFqn,
|
||||
myReturnTypeClassFqn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// 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.
|
||||
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.codeInspection;
|
||||
|
||||
import com.intellij.analysis.JvmAnalysisBundle;
|
||||
@@ -9,6 +9,7 @@ import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiElementVisitor;
|
||||
import com.intellij.psi.PsiIdentifier;
|
||||
import com.siyeh.HardcodedMethodConstants;
|
||||
import com.siyeh.ig.callMatcher.CallMatcher;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.uast.UCallExpression;
|
||||
import org.jetbrains.uast.UCallableReferenceExpression;
|
||||
@@ -17,17 +18,9 @@ import org.jetbrains.uast.UastContextKt;
|
||||
|
||||
|
||||
public class StringToUpperWithoutLocale2Inspection extends AbstractBaseUastLocalInspectionTool {
|
||||
|
||||
private static final UastCallMatcher MATCHER = UastCallMatcher.anyOf(
|
||||
UastCallMatcher.builder()
|
||||
.withMethodName(HardcodedMethodConstants.TO_UPPER_CASE)
|
||||
.withClassFqn(CommonClassNames.JAVA_LANG_STRING)
|
||||
.withArgumentsCount(0).build(),
|
||||
UastCallMatcher.builder()
|
||||
.withMethodName(HardcodedMethodConstants.TO_LOWER_CASE)
|
||||
.withClassFqn(CommonClassNames.JAVA_LANG_STRING)
|
||||
.withArgumentsCount(0).build()
|
||||
);
|
||||
private static final CallMatcher MATCHER = CallMatcher.instanceCall(
|
||||
CommonClassNames.JAVA_LANG_STRING, HardcodedMethodConstants.TO_UPPER_CASE, HardcodedMethodConstants.TO_LOWER_CASE
|
||||
).parameterCount(0);
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
@@ -52,7 +45,7 @@ public class StringToUpperWithoutLocale2Inspection extends AbstractBaseUastLocal
|
||||
}
|
||||
|
||||
private static void handleCallExpression(@NotNull UCallExpression callExpression, @NotNull ProblemsHolder holder) {
|
||||
if (!MATCHER.testCallExpression(callExpression)) return;
|
||||
if (!MATCHER.uCallMatches(callExpression)) return;
|
||||
if (NonNlsUastUtil.isCallExpressionWithNonNlsReceiver(callExpression)) return;
|
||||
|
||||
PsiElement methodIdentifierPsi = AnalysisUastUtil.getMethodIdentifierSourcePsi(callExpression);
|
||||
@@ -66,7 +59,7 @@ public class StringToUpperWithoutLocale2Inspection extends AbstractBaseUastLocal
|
||||
private static void handleCallableReferenceExpression(@NotNull UCallableReferenceExpression expression,
|
||||
@NotNull PsiElement identifier,
|
||||
@NotNull ProblemsHolder holder) {
|
||||
if (!MATCHER.testCallableReferenceExpression(expression)) return;
|
||||
if (!MATCHER.uCallableReferenceMatches(expression)) return;
|
||||
if (NonNlsUastUtil.isCallableReferenceExpressionWithNonNlsQualifier(expression)) return;
|
||||
|
||||
holder.registerProblem(identifier, getErrorDescription(expression.getCallableName()));
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class MethodReferences {
|
||||
public void foo(List<String> list) {
|
||||
list.stream().map(String::toUpperCase);
|
||||
list.stream().map(String::chars);
|
||||
list.stream().map(java.util.Objects::hashCode); // static method
|
||||
|
||||
list.stream().map(this::bar);
|
||||
Function<String, String> f = null;
|
||||
list.stream().map(f::apply);
|
||||
}
|
||||
|
||||
private String bar(String s) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
public class MyClass {
|
||||
public void foo() {
|
||||
String s = "123";
|
||||
s.toUpperCase();
|
||||
s.toUpperCase(java.util.Locale.ENGLISH);
|
||||
s.concat("1");
|
||||
s.chars();
|
||||
|
||||
java.util.ArrayList list = new java.util.ArrayList();
|
||||
list.addAll(new java.util.ArrayList());
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.intellij.codeInspection.tests.java;
|
||||
|
||||
import com.intellij.codeInspection.tests.UastCallMatcherTestBase;
|
||||
import com.intellij.jvm.analysis.JavaJvmAnalysisTestUtil;
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
|
||||
@TestDataPath("$CONTENT_ROOT/testData/codeInspection/uastCallMatcher")
|
||||
public class UastCallMatcherTest extends UastCallMatcherTestBase {
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return JavaJvmAnalysisTestUtil.TEST_DATA_PROJECT_RELATIVE_BASE_PATH + "/codeInspection/uastCallMatcher";
|
||||
}
|
||||
|
||||
public void testCallExpressions() {
|
||||
doTestCallExpressions("MyClass.java");
|
||||
}
|
||||
|
||||
public void testCallableReferences() {
|
||||
doTestCallableReferences("MethodReferences.java");
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
import java.util.*
|
||||
import java.util.function.Function
|
||||
import java.util.stream.IntStream
|
||||
|
||||
class MethodReferences {
|
||||
fun foo(list: List<String>) {
|
||||
list.stream().map(java.lang.String::toUpperCase)
|
||||
list.stream().map<IntStream>(String::chars)
|
||||
list.stream().map(Objects::hashCode) // static method
|
||||
|
||||
list.stream().map(this::bar)
|
||||
val f: Function<String, String>? = null
|
||||
list.stream().map(f!!::apply)
|
||||
}
|
||||
|
||||
private fun bar(s: String): String? {
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
class MyClass {
|
||||
fun foo() {
|
||||
val s = "123"
|
||||
s.toUpperCase()
|
||||
s.toUpperCase(java.util.Locale.ENGLISH)
|
||||
s.plus("1")
|
||||
s.chars()
|
||||
|
||||
val list = java.util.ArrayList<Any>()
|
||||
list.addAll(java.util.ArrayList())
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.intellij.codeInspection.tests.kotlin;
|
||||
|
||||
import com.intellij.codeInspection.tests.UastCallMatcherTestBase;
|
||||
import com.intellij.jvm.analysis.KotlinJvmAnalysisTestUtil;
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
import com.intellij.testFramework.builders.JavaModuleFixtureBuilder;
|
||||
import kotlin.KotlinVersion;
|
||||
import org.jetbrains.kotlin.idea.artifacts.KotlinArtifacts;
|
||||
import org.junit.Assume;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@TestDataPath("$CONTENT_ROOT/testData/codeInspection/uastCallMatcher")
|
||||
public class KtUastCallMatcherTest extends UastCallMatcherTestBase {
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
Assume.assumeTrue(KotlinVersion.CURRENT.isAtLeast(1, 2, 60));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return KotlinJvmAnalysisTestUtil.TEST_DATA_PROJECT_RELATIVE_BASE_PATH + "/codeInspection/uastCallMatcher";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tuneFixture(JavaModuleFixtureBuilder moduleBuilder) {
|
||||
super.tuneFixture(moduleBuilder);
|
||||
File kotlinStdlib = KotlinArtifacts.getInstance().getKotlinStdlib();
|
||||
moduleBuilder.addLibraryJars("kotlin-stdlib", kotlinStdlib.getParent(), kotlinStdlib.getName());
|
||||
}
|
||||
|
||||
|
||||
public void testCallExpressions() {
|
||||
doTestCallExpressions("MyClass.kt");
|
||||
}
|
||||
|
||||
public void testCallableReferences() {
|
||||
doTestCallableReferences("MethodReferences.kt");
|
||||
}
|
||||
}
|
||||
@@ -1,168 +0,0 @@
|
||||
package com.intellij.codeInspection.tests;
|
||||
|
||||
import com.intellij.codeInspection.UastCallMatcher;
|
||||
import com.intellij.openapi.application.PathManager;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.testFramework.IdeaTestUtil;
|
||||
import com.intellij.testFramework.TestDataFile;
|
||||
import com.intellij.testFramework.builders.JavaModuleFixtureBuilder;
|
||||
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.uast.UCallExpression;
|
||||
import org.jetbrains.uast.UCallableReferenceExpression;
|
||||
import org.jetbrains.uast.UastCallKind;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Set;
|
||||
|
||||
import static com.intellij.codeInspection.UastCallMatcher.builder;
|
||||
import static com.intellij.codeInspection.tests.JvmAnalysisTestsUastUtil.getUElementsOfTypeFromFile;
|
||||
|
||||
public abstract class UastCallMatcherTestBase extends JavaCodeInsightFixtureTestCase {
|
||||
@Override
|
||||
protected void tuneFixture(JavaModuleFixtureBuilder moduleBuilder) {
|
||||
moduleBuilder.addJdk(IdeaTestUtil.getMockJdk18Path().getPath());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return PathManager.getCommunityHomePath().replace(File.separatorChar, '/') + getBasePath();
|
||||
}
|
||||
|
||||
protected static int matchCallExpression(UastCallMatcher matcher, Set<? extends UCallExpression> expressions) {
|
||||
return (int)expressions.stream().filter(matcher::testCallExpression).count();
|
||||
}
|
||||
|
||||
protected static int matchCallableReferenceExpression(UastCallMatcher matcher, Set<? extends UCallableReferenceExpression> expressions) {
|
||||
return (int)expressions.stream().filter(matcher::testCallableReferenceExpression).count();
|
||||
}
|
||||
|
||||
|
||||
protected void doTestCallExpressions(@TestDataFile @NotNull String file) {
|
||||
PsiFile psiFile = myFixture.configureByFile(file);
|
||||
Set<UCallExpression> expressions = getUElementsOfTypeFromFile(psiFile, UCallExpression.class,
|
||||
e -> e.getKind() == UastCallKind.METHOD_CALL);
|
||||
assertSize(5, expressions);
|
||||
|
||||
assertEquals(1, matchCallExpression(
|
||||
builder().withClassFqn("java.util.ArrayList").build(),
|
||||
expressions)
|
||||
);
|
||||
assertEquals(0, matchCallExpression(
|
||||
builder().withClassFqn("java.util.ArrayList").withMethodName("size").build(),
|
||||
expressions)
|
||||
);
|
||||
assertEquals(0, matchCallExpression(
|
||||
builder().withMethodName("size").build(),
|
||||
expressions)
|
||||
);
|
||||
assertEquals(1, matchCallExpression(
|
||||
builder().withClassFqn("java.util.ArrayList").withMethodName("addAll").withArgumentsCount(1).build(),
|
||||
expressions)
|
||||
);
|
||||
assertEquals(1, matchCallExpression(
|
||||
builder().withClassFqn("java.util.ArrayList").withMethodName("addAll").withArgumentTypes("java.util.Collection").build(),
|
||||
expressions)
|
||||
);
|
||||
|
||||
assertEquals(4, matchCallExpression(
|
||||
builder().withClassFqn("java.lang.String").build(),
|
||||
expressions
|
||||
));
|
||||
assertEquals(2, matchCallExpression(
|
||||
builder().withMethodName("toUpperCase").build(),
|
||||
expressions
|
||||
));
|
||||
assertEquals(2, matchCallExpression(
|
||||
builder().withClassFqn("java.lang.String").withMethodName("toUpperCase").build(),
|
||||
expressions
|
||||
));
|
||||
|
||||
assertEquals(3, matchCallExpression(
|
||||
builder().withReturnType("java.lang.String").build(),
|
||||
expressions
|
||||
));
|
||||
assertEquals(2, matchCallExpression(
|
||||
builder().withReturnType("java.lang.String").withMethodName("toUpperCase").build(),
|
||||
expressions
|
||||
));
|
||||
assertEquals(1, matchCallExpression(
|
||||
builder().withReturnType("java.lang.String").withMethodName("toUpperCase").withArgumentsCount(1).build(),
|
||||
expressions
|
||||
));
|
||||
assertEquals(1, matchCallExpression(
|
||||
builder().withReturnType("java.lang.String").withMethodName("toUpperCase").withArgumentTypes("java.util.Locale").build(),
|
||||
expressions
|
||||
));
|
||||
|
||||
assertEquals(2, matchCallExpression(
|
||||
builder().withArgumentsCount(0).build(),
|
||||
expressions
|
||||
));
|
||||
assertEquals(3, matchCallExpression(
|
||||
builder().withArgumentsCount(1).build(),
|
||||
expressions
|
||||
));
|
||||
|
||||
assertEquals(1, matchCallExpression(
|
||||
builder().withArgumentTypes("java.util.Locale").build(),
|
||||
expressions
|
||||
));
|
||||
assertEquals(1, matchCallExpression(
|
||||
builder().withArgumentTypes("java.util.Collection").withMatchArgumentTypeInheritors(true).build(),
|
||||
expressions
|
||||
));
|
||||
}
|
||||
|
||||
protected void doTestCallableReferences(@TestDataFile @NotNull String file) {
|
||||
PsiFile psiFile = myFixture.configureByFile(file);
|
||||
Set<UCallableReferenceExpression> expressions = getUElementsOfTypeFromFile(psiFile, UCallableReferenceExpression.class);
|
||||
assertSize(5, expressions);
|
||||
|
||||
assertEquals(2, matchCallableReferenceExpression(
|
||||
builder().withClassFqn("java.lang.String").build(),
|
||||
expressions
|
||||
));
|
||||
assertEquals(1, matchCallableReferenceExpression(
|
||||
builder().withClassFqn("java.util.Objects").build(),
|
||||
expressions
|
||||
));
|
||||
assertEquals(1, matchCallableReferenceExpression(
|
||||
builder().withClassFqn("java.util.function.Function").build(),
|
||||
expressions
|
||||
));
|
||||
assertEquals(1, matchCallableReferenceExpression(
|
||||
builder().withClassFqn("MethodReferences").build(),
|
||||
expressions
|
||||
));
|
||||
|
||||
assertEquals(1, matchCallableReferenceExpression(
|
||||
builder().withClassFqn("java.lang.String").withMethodName("toUpperCase").build(),
|
||||
expressions
|
||||
));
|
||||
assertEquals(1, matchCallableReferenceExpression(
|
||||
builder().withClassFqn("java.util.Objects").withMethodName("hashCode").build(),
|
||||
expressions
|
||||
));
|
||||
assertEquals(1, matchCallableReferenceExpression(
|
||||
builder().withClassFqn("java.util.function.Function").withMethodName("apply").build(),
|
||||
expressions
|
||||
));
|
||||
assertEquals(1, matchCallableReferenceExpression(
|
||||
builder().withClassFqn("MethodReferences").withMethodName("bar").build(),
|
||||
expressions
|
||||
));
|
||||
|
||||
assertEquals(3, matchCallableReferenceExpression(
|
||||
builder().withArgumentsCount(1).build(),
|
||||
expressions
|
||||
));
|
||||
|
||||
//TODO uncomment after fix resolving: generics (Java and Kotlin) and String (Kotlin, https://youtrack.jetbrains.com/issue/KT-25024)
|
||||
// one is for Function<String, String>#apply
|
||||
//assertEquals(3, matchCallableReferenceExpression(
|
||||
// builder().withReturnType("java.lang.String").build(),
|
||||
// expressions
|
||||
//));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user