Improve PsiMethod.hasSignature; fix #294

This commit is contained in:
Karlatemp 2021-02-16 22:44:01 +08:00
parent 06564e9142
commit 5bf1a9755f
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
3 changed files with 35 additions and 10 deletions

View File

@ -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();

View File

@ -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;
@ -28,3 +29,16 @@ class TestCommand extends JSimpleCommand {
}
}
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() {
}
}

View File

@ -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