IDEA-136891 ('Overridable method called during object construction' should have an option to ignore the calling of package local methods)

This commit is contained in:
Bas Leijdekkers
2015-07-06 16:24:24 +02:00
parent 78bddba268
commit 6a95edea11
4 changed files with 53 additions and 2 deletions
@@ -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();
@@ -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 <b>clone()</b>, <b>readObject()</b> or <b>readObjectNoData()</b> method.
Methods are overridable if they are not declared <b>final</b>, <b>static</b> or <b>private</b>.
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.
<!-- tooltip end -->
@@ -0,0 +1,13 @@
class OverridableMethodCallDuringObjectConstruction {
{
a();
<warning descr="Call to overridable method 'b()' during object construction">b</warning>();
c();
d();
}
void a() {}
public void b() {}
private void c() {}
public final void d() {}
}
@@ -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();
}
}