mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java, inspection] make unused symbol inspection not report main methods for unnamed classes IDEA-333152
GitOrigin-RevId: 1a1b41037a7bc28fc9a20620a182ca49ef789951
This commit is contained in:
committed by
intellij-monorepo-bot
parent
258f4afc9d
commit
b56e8cf4d9
@@ -313,15 +313,16 @@ public final class PsiClassImplUtil {
|
||||
}
|
||||
|
||||
public static boolean isMainOrPremainMethod(@NotNull PsiMethod method) {
|
||||
if (PsiMethodUtil.isMainMethod(method)) {
|
||||
return true;
|
||||
}
|
||||
String name = method.getName();
|
||||
if (!("main".equals(name) || "premain".equals(name) || "agentmain".equals(name))) return false;
|
||||
if ("premain".equals(name) || "agentmain".equals(name)) return false;
|
||||
if (!PsiTypes.voidType().equals(method.getReturnType())) return false;
|
||||
|
||||
PsiElementFactory factory = JavaPsiFacade.getElementFactory(method.getProject());
|
||||
MethodSignature signature = method.getSignature(PsiSubstitutor.EMPTY);
|
||||
try {
|
||||
MethodSignature main = createSignatureFromText(factory, "void main(String[] args);");
|
||||
if (MethodSignatureUtil.areSignaturesEqual(signature, main)) return true;
|
||||
MethodSignature premain = createSignatureFromText(factory, "void premain(String args, java.lang.instrument.Instrumentation i);");
|
||||
if (MethodSignatureUtil.areSignaturesEqual(signature, premain)) return true;
|
||||
MethodSignature agentmain = createSignatureFromText(factory, "void agentmain(String args, java.lang.instrument.Instrumentation i);");
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
void main() {}
|
||||
+12
@@ -21,7 +21,9 @@ import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection;
|
||||
import com.intellij.lang.annotation.HighlightSeverity;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.testFramework.IdeaTestUtil;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -39,6 +41,16 @@ public class UnusedSymbolLocalTest extends DaemonAnalyzerTestCase {
|
||||
public void testInnerUsesSelf() throws Exception { doTest(); }
|
||||
public void testLocalClass() throws Exception { doTest(); }
|
||||
public void testPrivateConstructor() throws Exception { doTest(); }
|
||||
public void testUnnamedClassInstanceMainWithoutParams() {
|
||||
IdeaTestUtil.withLevel(myModule, LanguageLevel.JDK_21_PREVIEW, () -> {
|
||||
try {
|
||||
doTest();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//@Bombed(day = 5, month = Calendar.SEPTEMBER, user = "anet")
|
||||
//public void testInjectedAnno() throws Exception { doTest(); }
|
||||
|
||||
Reference in New Issue
Block a user