[java-highlighting] IDEA-365996 Constructor in an implicitly declared class is not highlighted as error

(cherry picked from commit 2de45c452608cea2ab314511060ac733259a8c18)

GitOrigin-RevId: daed31a96a3fb96a6cbed183443ed90205b679f3
This commit is contained in:
Mikhail Pyltsin
2025-01-21 19:27:21 +01:00
committed by intellij-monorepo-bot
parent 35c774bf7d
commit b3f8bbb608
6 changed files with 41 additions and 1 deletions

View File

@@ -2341,6 +2341,22 @@ public final class HighlightMethodUtil {
return MethodSignatureUtil.areSignaturesErasureEqual(valueOfMethod, methodSignature);
}
static HighlightInfo.@Nullable Builder checkConstructorInImplicitClass(@NotNull PsiMethod method) {
if (!method.isConstructor()) {
return null;
}
if (!(method.getContainingClass() instanceof PsiImplicitClass)) {
return null;
}
String description = JavaErrorBundle.message("implicit.class.with.explicit.constructor");
TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method);
HighlightInfo.Builder builder =
HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description);
IntentionAction action = QuickFixFactory.getInstance().createDeleteFix(method);
builder.registerFix(action, null, null, null, null);
return builder;
}
private static final class ReturnModel {
final PsiReturnStatement myStatement;
final PsiType myType;

View File

@@ -934,6 +934,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
@Override
public void visitMethod(@NotNull PsiMethod method) {
super.visitMethod(method);
if (!hasErrorResults()) add(HighlightMethodUtil.checkConstructorInImplicitClass(method));
if (!hasErrorResults()) add(HighlightControlFlowUtil.checkUnreachableStatement(method.getBody()));
if (!hasErrorResults()) add(HighlightMethodUtil.checkConstructorHandleSuperClassExceptions(method));
if (!hasErrorResults()) add(GenericsHighlightUtil.checkSafeVarargsAnnotation(method, myLanguageLevel));

View File

@@ -601,4 +601,5 @@ error.implicit.class.has.invalid.file.name=Implicitly declared class's file name
error.package.statement.not.allowed.for.implicit.class=Package statement is not allowed for implicitly declared class
error.initializers.are.not.allowed.in.implicit.classes=Initializers are not allowed in implicitly declared classes
remove.unused.imports.quickfix.text=Remove unused imports
incomplete.project.state.pending.reference=Not resolved until the project is fully loaded
incomplete.project.state.pending.reference=Not resolved until the project is fully loaded
implicit.class.with.explicit.constructor=Explicit constructor in implicitly declared class is not allowed

View File

@@ -0,0 +1,7 @@
void main(String[] args) {
System.out.println("AAAAA");
}
<error descr="Explicit constructor in implicitly declared class is not allowed">public ImplicitClassConstructorNoParam()</error> {
System.out.println("BBBBB");
}

View File

@@ -0,0 +1,7 @@
void main(String[] args) {
System.out.println("AAAAA");
}
<error descr="Explicit constructor in implicitly declared class is not allowed">public ImplicitClassConstructorParam(String a)</error> {
System.out.println("BBBBB");
}

View File

@@ -164,6 +164,14 @@ class ImplicitClassHighlightingTest : LightJavaCodeInsightFixtureTestCase() {
})
}
fun testImplicitClassConstructorNoParam() {
doTest()
}
fun testImplicitClassConstructorParam() {
doTest()
}
private fun doTest() {
myFixture.configureByFile(getTestName(false) + ".java")
myFixture.checkHighlighting()