mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-highlighting] IDEA-374019 Don't show error about 'main' method if there is an error with braces
GitOrigin-RevId: 38e303a7ace428d45c6e869fce3acc655dba1a6d
This commit is contained in:
committed by
intellij-monorepo-bot
parent
fd2efb1197
commit
dc8ca06f05
+18
-1
@@ -15,6 +15,7 @@ import com.intellij.openapi.roots.ModuleFileIndex;
|
||||
import com.intellij.openapi.roots.ModuleRootManager;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.pom.java.JavaFeature;
|
||||
import com.intellij.psi.*;
|
||||
@@ -402,10 +403,24 @@ final class ClassChecker {
|
||||
PsiMethod[] methods = implicitClass.getMethods();
|
||||
boolean hasMainMethod = ContainerUtil.exists(methods, method -> "main".equals(method.getName()) && PsiMethodUtil.isMainMethod(method));
|
||||
if (!hasMainMethod) {
|
||||
//don't show errors if the file contains broken {}
|
||||
if(hasErrorElementWithBraces(file)) return;
|
||||
myVisitor.report(JavaErrorKinds.CLASS_IMPLICIT_NO_MAIN_METHOD.create(file, implicitClass));
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean hasErrorElementWithBraces(@NotNull PsiElement parentElement) {
|
||||
Ref<Boolean> result = new Ref<>(false);
|
||||
PsiWalkingState.processAll(parentElement, el -> {
|
||||
if (el instanceof PsiErrorElement element &&
|
||||
(element.getText().contains("}") || element.getText().contains("{"))) {
|
||||
result.set(true);
|
||||
return false;
|
||||
} else return true;
|
||||
});
|
||||
return result.get();
|
||||
}
|
||||
|
||||
void checkImplicitClassMember(@NotNull PsiMember member) {
|
||||
if (!(member.getContainingClass() instanceof PsiImplicitClass)) return;
|
||||
|
||||
@@ -448,7 +463,9 @@ final class ClassChecker {
|
||||
}
|
||||
|
||||
void checkPackageNotAllowedInImplicitClass(@NotNull PsiPackageStatement statement) {
|
||||
if (myVisitor.isApplicable(JavaFeature.IMPLICIT_CLASSES) && JavaImplicitClassUtil.isFileWithImplicitClass(myVisitor.file())) {
|
||||
if (myVisitor.isApplicable(JavaFeature.IMPLICIT_CLASSES) &&
|
||||
JavaImplicitClassUtil.isFileWithImplicitClass(myVisitor.file()) &&
|
||||
!hasErrorElementWithBraces(myVisitor.file())) {
|
||||
myVisitor.report(JavaErrorKinds.CLASS_IMPLICIT_PACKAGE.create(statement));
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
<error descr="Implicitly declared class contains no 'main' method"><error descr="Package statement is not allowed for implicitly declared class">package p1;</error>
|
||||
package p1;
|
||||
|
||||
class <warning descr="Class 'Demo' is never used">Demo</warning>{
|
||||
public static void <warning descr="Method 't1()' is never used">t1</warning>(){}
|
||||
@@ -9,4 +9,4 @@ class <warning descr="Class 'Demo' is never used">Demo</warning>{
|
||||
}
|
||||
public static void <warning descr="Method 't5()' is never used">t5</warning>(){}
|
||||
public static void <warning descr="Method 't6()' is never used">t6</warning>(){}
|
||||
<error descr="'class' or 'interface' expected">}</error></error>
|
||||
<error descr="'class' or 'interface' expected">}</error>
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.example;
|
||||
|
||||
public class A {
|
||||
}
|
||||
|
||||
<error descr="'class' or 'interface' expected">}</error>
|
||||
|
||||
int a = 1;
|
||||
+5
-1
@@ -13,7 +13,7 @@ import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase
|
||||
import org.jetbrains.plugins.groovy.intentions.style.inference.resolve
|
||||
|
||||
class ImplicitClassHighlightingTest : LightJavaCodeInsightFixtureTestCase() {
|
||||
override fun getProjectDescriptor() = JAVA_21
|
||||
override fun getProjectDescriptor() = JAVA_23
|
||||
override fun getBasePath() = JavaTestUtil.getRelativeJavaTestDataPath() + "/codeInsight/daemonCodeAnalyzer/implicitClass"
|
||||
|
||||
fun testHighlightInsufficientLevel() {
|
||||
@@ -172,6 +172,10 @@ class ImplicitClassHighlightingTest : LightJavaCodeInsightFixtureTestCase() {
|
||||
doTest()
|
||||
}
|
||||
|
||||
fun testBrokenFileNoHighlighting() {
|
||||
doTest()
|
||||
}
|
||||
|
||||
private fun doTest() {
|
||||
myFixture.configureByFile(getTestName(false) + ".java")
|
||||
myFixture.checkHighlighting()
|
||||
|
||||
Reference in New Issue
Block a user