diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/initialization/OverridableMethodCallDuringObjectConstructionInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/initialization/OverridableMethodCallDuringObjectConstructionInspectionBase.java index e36fe008ab21..d9604e1961f0 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/initialization/OverridableMethodCallDuringObjectConstructionInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/initialization/OverridableMethodCallDuringObjectConstructionInspectionBase.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -62,7 +62,7 @@ public class OverridableMethodCallDuringObjectConstructionInspectionBase extends return; } final PsiMethod calledMethod = expression.resolveMethod(); - if (calledMethod == null || !PsiUtil.canBeOverriden(calledMethod)) { + if (calledMethod == null || !PsiUtil.canBeOverriden(calledMethod) || calledMethod.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) { return; } final PsiClass calledMethodClass = calledMethod.getContainingClass(); diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/OverridableMethodCallDuringObjectConstruction.html b/plugins/InspectionGadgets/src/inspectionDescriptions/OverridableMethodCallDuringObjectConstruction.html index 30964f84d53f..6fa7018266b0 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/OverridableMethodCallDuringObjectConstruction.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/OverridableMethodCallDuringObjectConstruction.html @@ -4,6 +4,7 @@ Reports any calls to overridable methods of the current class during object cons A call is during object construction if it is made inside a constructor, in an non-static instance initializer, in a non-static field initializer or inside a clone(), readObject() or readObjectNoData() method. Methods are overridable if they are not declared final, static or private. +Package local methods are considered safe, even though they are overridable. Such calls may result in subtle bugs, as the object is not guaranteed to be initialized before the method call occurs. diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/initialization/overridable_method_call_during_object_construction/OverridableMethodCallDuringObjectConstruction.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/initialization/overridable_method_call_during_object_construction/OverridableMethodCallDuringObjectConstruction.java new file mode 100644 index 000000000000..ecb1481f82ce --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/initialization/overridable_method_call_during_object_construction/OverridableMethodCallDuringObjectConstruction.java @@ -0,0 +1,13 @@ +class OverridableMethodCallDuringObjectConstruction { + { + a(); + b(); + c(); + d(); + } + + void a() {} + public void b() {} + private void c() {} + public final void d() {} +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/initialization/OverridableMethodCallDuringObjectConstructionInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/initialization/OverridableMethodCallDuringObjectConstructionInspectionTest.java new file mode 100644 index 000000000000..fba47ad13054 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/initialization/OverridableMethodCallDuringObjectConstructionInspectionTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2000-2015 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. + */ +package com.siyeh.ig.initialization; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import junit.framework.TestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class OverridableMethodCallDuringObjectConstructionInspectionTest extends LightInspectionTestCase { + + public void testOverridableMethodCallDuringObjectConstruction() { + doTest(); + } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new OverridableMethodCallDuringObjectConstructionInspection(); + } +} \ No newline at end of file