From 5bf1a9755fb3bf440a5427cbf2bf8ae05b1beec6 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Tue, 16 Feb 2021 22:44:01 +0800 Subject: [PATCH] Improve PsiMethod.hasSignature; fix #294 --- .../ResourceNotClosedInspectionTestJava.java | 12 ++++++++++++ .../src/main/java/test/TestJavaPlugin.java | 16 +++++++++++++++- .../src/resolve/FunctionSignature.kt | 17 ++++++++--------- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/tools/intellij-plugin/run/projects/test-project/src/main/java/test/ResourceNotClosedInspectionTestJava.java b/tools/intellij-plugin/run/projects/test-project/src/main/java/test/ResourceNotClosedInspectionTestJava.java index 3034e5657..e216b120a 100644 --- a/tools/intellij-plugin/run/projects/test-project/src/main/java/test/ResourceNotClosedInspectionTestJava.java +++ b/tools/intellij-plugin/run/projects/test-project/src/main/java/test/ResourceNotClosedInspectionTestJava.java @@ -11,7 +11,19 @@ import java.io.IOException; import static org.example.myplugin.ResourceNotClosedInspectionTestKt.magic; public class ResourceNotClosedInspectionTestJava { + + public static Object funA() { + return new Object(); + } + + public static void funB(Object obj) { + System.out.println(obj); + } + public static void main(String[] args) { + // https://github.com/mamoe/mirai-console/issues/294 + funB(funA()); + File file = magic(); Contact contact = magic(); diff --git a/tools/intellij-plugin/run/projects/test-project/src/main/java/test/TestJavaPlugin.java b/tools/intellij-plugin/run/projects/test-project/src/main/java/test/TestJavaPlugin.java index 2af0e12f8..28f9c3396 100644 --- a/tools/intellij-plugin/run/projects/test-project/src/main/java/test/TestJavaPlugin.java +++ b/tools/intellij-plugin/run/projects/test-project/src/main/java/test/TestJavaPlugin.java @@ -3,6 +3,7 @@ package test; import net.mamoe.mirai.console.command.Command; import net.mamoe.mirai.console.command.CommandOwner; import net.mamoe.mirai.console.command.descriptor.CommandSignatureFromKFunction; +import net.mamoe.mirai.console.command.java.JCompositeCommand; import net.mamoe.mirai.console.command.java.JSimpleCommand; import net.mamoe.mirai.console.permission.Permission; import net.mamoe.mirai.console.plugin.jvm.JavaPlugin; @@ -27,4 +28,17 @@ class TestCommand extends JSimpleCommand { public void test(String s) { } -} \ No newline at end of file +} + +class TestCommand2 extends JCompositeCommand { + public TestCommand2(@NotNull CommandOwner owner, @NotNull String primaryName, @NotNull String[] secondaryNames, @NotNull Permission parentPermission) { + super(owner, primaryName, secondaryNames, parentPermission); + } + + @SubCommand("test") + public void test() {} + + @SubCommand({}) + public void subCmd() { + } +} diff --git a/tools/intellij-plugin/src/resolve/FunctionSignature.kt b/tools/intellij-plugin/src/resolve/FunctionSignature.kt index 50b244aea..eb185fd79 100644 --- a/tools/intellij-plugin/src/resolve/FunctionSignature.kt +++ b/tools/intellij-plugin/src/resolve/FunctionSignature.kt @@ -96,16 +96,21 @@ fun KtFunction.hasSignature(functionSignature: FunctionSignature): Boolean { return true } -fun KtLightMethod.hasSignature(functionSignature: FunctionSignature): Boolean { + +fun PsiMethod.hasSignature(functionSignature: FunctionSignature): Boolean { if (functionSignature.name != null) { if (this.name != functionSignature.name) return false } val parameters = parameterList.parameters.toMutableList() if (functionSignature.dispatchReceiver != null) { - val kotlinContainingClassFqn = + val containingClass = this.containingClass ?: return false + + val kotlinContainingClassFqn = if (this is KtLightMethod) { if (this.modifierList.hasExplicitModifier(PsiModifier.STATIC)) { this.containingClass.kotlinOrigin?.companionObjects?.firstOrNull()?.fqName - } else this.containingClass.getKotlinFqName() + } else containingClass.getKotlinFqName() + } else containingClass.getKotlinFqName() + if (kotlinContainingClassFqn != functionSignature.dispatchReceiver) return false } if (functionSignature.extensionReceiver != null) { @@ -121,12 +126,6 @@ fun KtLightMethod.hasSignature(functionSignature: FunctionSignature): Boolean { return true } -fun PsiMethod.hasSignature(functionSignature: FunctionSignature): Boolean { - if (this is KtLightMethod) { - return this.hasSignature(functionSignature) - } - return true -} fun KtExpression.isCalling(functionSignature: FunctionSignature): Boolean { val descriptor = resolveToCall(BodyResolveMode.PARTIAL)?.resultingDescriptor ?: return false