can be final: fix batch inspection false positives (IDEA-225311)

GitOrigin-RevId: 8c10b7b77c4362f106ee4dcb450ea9fce4631e25
This commit is contained in:
Bas Leijdekkers
2021-10-26 14:18:40 +00:00
committed by intellij-monorepo-bot
parent 38d68fb1fc
commit 362a97ea20
6 changed files with 48 additions and 10 deletions
@@ -90,7 +90,6 @@ class CanBeFinalAnnotator extends RefGraphAnnotatorEx {
}
}
@Override
public void onMarkReferenced(RefElement refWhat,
RefElement refFrom,
@@ -98,18 +97,24 @@ class CanBeFinalAnnotator extends RefGraphAnnotatorEx {
boolean forReading,
boolean forWriting,
PsiElement referenceElement) {
if (!forWriting) return;
if (!(refWhat instanceof RefField)) return;
if (!(refFrom instanceof RefMethod) ||
!((RefMethod)refFrom).isConstructor() ||
((RefField)refWhat).getUastElement().getUastInitializer() != null ||
((RefMethod)refFrom).getOwnerClass() != ((RefField)refWhat).getOwnerClass() ||
((RefField)refWhat).isStatic()) {
if (forWriting &&
!(referencedFromClassInitializer && PsiTreeUtil.getParentOfType(referenceElement, PsiLambdaExpression.class, true) == null)) {
final RefField refField = (RefField)refWhat;
if (refFrom instanceof RefClass && refField.getOwnerClass() != refFrom) {
((RefFieldImpl)refWhat).setFlag(false, CAN_BE_FINAL_MASK);
}
else if (refField.getUastElement().getUastInitializer() != null) {
((RefFieldImpl)refWhat).setFlag(false, CAN_BE_FINAL_MASK);
}
else if (!(refFrom instanceof RefMethod) ||
!((RefMethod)refFrom).isConstructor() ||
((RefMethod)refFrom).getOwnerClass() != refField.getOwnerClass() ||
refField.isStatic()) {
if (!referencedFromClassInitializer || PsiTreeUtil.getParentOfType(referenceElement, PsiLambdaExpression.class, true) != null) {
((RefFieldImpl)refWhat).setFlag(false, CAN_BE_FINAL_MASK);
}
}
else if (forWriting && PsiTreeUtil.getParentOfType(referenceElement, PsiLambdaExpression.class, true) != null) {
else if (PsiTreeUtil.getParentOfType(referenceElement, PsiLambdaExpression.class, true) != null) {
((RefFieldImpl)refWhat).setFlag(false, CAN_BE_FINAL_MASK);
}
}
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
</problems>
@@ -0,0 +1,10 @@
public final class X {
private static int blabla;
final class Other {
{
blabla = 2;
}
}
}
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
</problems>
@@ -0,0 +1,7 @@
public final class Test {
protected static String prefix = "";
static {
prefix = "../";
}
}
@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInspection;
@@ -41,6 +41,14 @@ public class CanBeFinalInspectionTest extends JavaInspectionTestCase {
doTest();
}
public void testFieldAssignedInClassInitializer() {
doTest();
}
public void testFieldAssignedFromOtherClass() {
doTest();
}
public void testsimpleClassInheritance() {
doTest();
}