From 972167979b8880968aea5200bb60befeb34f3d20 Mon Sep 17 00:00:00 2001 From: anna Date: Thu, 30 Jun 2011 17:55:52 +0400 Subject: [PATCH] can be final: do not suggest to make final if overriding constructor reassign the field --- .../canBeFinal/CanBeFinalAnnotator.java | 3 +- .../simpleClassInheritanceField/expected.xml | 3 ++ .../simpleClassInheritanceField/src/Foo.java | 31 +++++++++++++++++++ .../codeInspection/CanBeFinalTest.java | 4 +++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/inspection/canBeFinal/simpleClassInheritanceField/expected.xml create mode 100644 java/java-tests/testData/inspection/canBeFinal/simpleClassInheritanceField/src/Foo.java diff --git a/java/java-impl/src/com/intellij/codeInspection/canBeFinal/CanBeFinalAnnotator.java b/java/java-impl/src/com/intellij/codeInspection/canBeFinal/CanBeFinalAnnotator.java index 72a704c0181e..687f0c3060ce 100644 --- a/java/java-impl/src/com/intellij/codeInspection/canBeFinal/CanBeFinalAnnotator.java +++ b/java/java-impl/src/com/intellij/codeInspection/canBeFinal/CanBeFinalAnnotator.java @@ -99,7 +99,8 @@ class CanBeFinalAnnotator extends RefGraphAnnotatorEx { boolean forReading, boolean forWriting) { if (!(refWhat instanceof RefField)) return; - if (!(refFrom instanceof RefMethod) || !((RefMethod)refFrom).isConstructor() || ((PsiField)refWhat.getElement()).hasInitializer()) { + if (!(refFrom instanceof RefMethod) || !((RefMethod)refFrom).isConstructor() || ((PsiField)refWhat.getElement()).hasInitializer() || + ((RefMethod)refFrom).getOwnerClass() != ((RefField)refWhat).getOwnerClass()) { if (!referencedFromClassInitializer && forWriting) { ((RefFieldImpl)refWhat).setFlag(false, CAN_BE_FINAL_MASK); } diff --git a/java/java-tests/testData/inspection/canBeFinal/simpleClassInheritanceField/expected.xml b/java/java-tests/testData/inspection/canBeFinal/simpleClassInheritanceField/expected.xml new file mode 100644 index 000000000000..9ac879d78616 --- /dev/null +++ b/java/java-tests/testData/inspection/canBeFinal/simpleClassInheritanceField/expected.xml @@ -0,0 +1,3 @@ + + + diff --git a/java/java-tests/testData/inspection/canBeFinal/simpleClassInheritanceField/src/Foo.java b/java/java-tests/testData/inspection/canBeFinal/simpleClassInheritanceField/src/Foo.java new file mode 100644 index 000000000000..d5772f53a2d6 --- /dev/null +++ b/java/java-tests/testData/inspection/canBeFinal/simpleClassInheritanceField/src/Foo.java @@ -0,0 +1,31 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +public class A { + protected String myString; + protected String myAnotherString; + + public A(String s) { + myString = s; + } +} + +final class AImpl extends A { + public AImpl(String s) { + super(s); + myString = s + ""; + myAnotherString = s; + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/CanBeFinalTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/CanBeFinalTest.java index bf025d5f5dd6..4f216bfc550b 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/CanBeFinalTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/CanBeFinalTest.java @@ -30,6 +30,10 @@ public class CanBeFinalTest extends InspectionTestCase { doTest("canBeFinal/" + getTestName(false), tool); } + public void testsimpleClassInheritanceField() throws Exception { + doTest(); + } + public void testsimpleClassInheritance() throws Exception { doTest(); }