mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
can be final: should not trigger static fields with initializer in a constructor (IDEA-69011)
This commit is contained in:
@@ -99,8 +99,11 @@ class CanBeFinalAnnotator extends RefGraphAnnotatorEx {
|
||||
boolean forReading,
|
||||
boolean forWriting) {
|
||||
if (!(refWhat instanceof RefField)) return;
|
||||
if (!(refFrom instanceof RefMethod) || !((RefMethod)refFrom).isConstructor() || ((PsiField)refWhat.getElement()).hasInitializer() ||
|
||||
((RefMethod)refFrom).getOwnerClass() != ((RefField)refWhat).getOwnerClass()) {
|
||||
if (!(refFrom instanceof RefMethod) ||
|
||||
!((RefMethod)refFrom).isConstructor() ||
|
||||
((PsiField)refWhat.getElement()).hasInitializer() ||
|
||||
((RefMethod)refFrom).getOwnerClass() != ((RefField)refWhat).getOwnerClass() ||
|
||||
((RefField)refWhat).isStatic()) {
|
||||
if (!referencedFromClassInitializer && forWriting) {
|
||||
((RefFieldImpl)refWhat).setFlag(false, CAN_BE_FINAL_MASK);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>Foo.java</file>
|
||||
<line>2</line>
|
||||
<description>final</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
public final class Foo {
|
||||
private static Object f1 = new Object(); // Can be final but unused
|
||||
private static Object object; // can't be final
|
||||
|
||||
Foo() {
|
||||
object = new Object();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(Foo.f1);
|
||||
System.out.println(Foo.object);
|
||||
}
|
||||
}
|
||||
@@ -82,6 +82,10 @@ public class CanBeFinalTest extends InspectionTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void teststaticFields() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testSCR7737() throws Exception {
|
||||
CanBeFinalInspection tool = new CanBeFinalInspection();
|
||||
tool.REPORT_CLASSES = false;
|
||||
|
||||
Reference in New Issue
Block a user