[java, inspection] make global unused symbol inspection not report main methods for unnamed classes IDEA-333152

GitOrigin-RevId: 597ef591bebf0ee6968e08a05dd989aadc8bdd53
This commit is contained in:
Roman Ivanov
2023-10-04 22:33:23 +00:00
committed by intellij-monorepo-bot
parent b56e8cf4d9
commit eba6e0d551
4 changed files with 12 additions and 6 deletions
@@ -97,9 +97,7 @@ public class RefMethodImpl extends RefJavaElementImpl implements RefMethod {
setAbstract(javaPsi.hasModifierProperty(PsiModifier.ABSTRACT));
setLibraryOverride(javaPsi.hasModifierProperty(PsiModifier.NATIVE));
if (PsiModifier.PUBLIC.equals(getAccessModifier())) {
setAppMain(isAppMain(javaPsi, this));
}
setAppMain(isAppMain(javaPsi, this));
if (!PsiModifier.PRIVATE.equals(getAccessModifier()) && !isStatic()) {
initializeSuperMethods(javaPsi);
}
@@ -125,12 +123,11 @@ public class RefMethodImpl extends RefJavaElementImpl implements RefMethod {
}
private static boolean isAppMain(PsiMethod psiMethod, RefMethod refMethod) {
if (PsiMethodUtil.isMainMethod(psiMethod)) return true;
if (!refMethod.isStatic()) return false;
if (!PsiTypes.voidType().equals(psiMethod.getReturnType())) return false;
PsiMethod appMainPattern = ((RefMethodImpl)refMethod).getRefJavaManager().getAppMainPattern();
if (MethodSignatureUtil.areSignaturesEqual(psiMethod, appMainPattern)) return true;
if ("main".equals(psiMethod.getName()) && psiMethod.getParameterList().isEmpty() &&
psiMethod.getLanguage().isKindOf("kotlin")) return true;
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -0,0 +1 @@
void main() {}
@@ -277,6 +277,12 @@ public class UnusedDeclarationInspectionTest extends AbstractUnusedDeclarationTe
public void testJunitMethodSource() {doTest();}
public void testUnnamedClass() {
IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_21_PREVIEW, () -> {
doTest();
});
}
private void doTest5() {
IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_1_5, () -> doTest());
}