Deprecation warning not highlighted when superclass default constructor is deprecated (IDEA-68898)

This commit is contained in:
anna
2011-06-07 21:34:58 +04:00
parent 7c68816272
commit 4f40958aa0
6 changed files with 88 additions and 0 deletions
@@ -19,12 +19,15 @@ import com.intellij.codeInsight.daemon.JavaErrorMessages;
import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightMessageUtil;
import com.intellij.codeInspection.BaseJavaLocalInspectionTool;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.infos.MethodCandidateInfo;
import com.intellij.psi.util.MethodSignatureBackedByPsiMethod;
import com.intellij.refactoring.util.RefactoringUIUtil;
import com.intellij.refactoring.util.RefactoringUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -119,8 +122,52 @@ public class DeprecationInspection extends BaseJavaLocalInspectionTool {
if (!method.isConstructor()) {
List<MethodSignatureBackedByPsiMethod> superMethodSignatures = method.findSuperMethodSignaturesIncludingStatic(true);
checkMethodOverridesDeprecated(methodSignature, superMethodSignatures, myHolder);
} else {
checkImplicitCallToSuper(method);
}
}
private void checkImplicitCallToSuper(PsiMethod method) {
final PsiClass containingClass = method.getContainingClass();
assert containingClass != null;
final PsiClass superClass = containingClass.getSuperClass();
if (hasDefaultDeprecatedConstructor(superClass)) {
final PsiCodeBlock body = method.getBody();
if (body != null) {
final PsiStatement[] statements = body.getStatements();
if (statements.length == 0 || !RefactoringUtil.isSuperOrThisCall(statements[0], true, true)) {
registerDefaultConstructorProblem(superClass, method.getNameIdentifier());
}
}
}
}
private void registerDefaultConstructorProblem(PsiClass superClass, PsiIdentifier nameIdentifier) {
myHolder.registerProblem(nameIdentifier, "Default constructor in " + superClass.getQualifiedName() + " is deprecated", ProblemHighlightType.LIKE_DEPRECATED);
}
@Override
public void visitClass(PsiClass aClass) {
final PsiMethod[] currentConstructors = aClass.getConstructors();
if (currentConstructors.length == 0) {
final PsiClass superClass = aClass.getSuperClass();
if (hasDefaultDeprecatedConstructor(superClass)) {
registerDefaultConstructorProblem(superClass, aClass.getNameIdentifier());
}
}
}
}
private static boolean hasDefaultDeprecatedConstructor(PsiClass superClass) {
if (superClass != null) {
final PsiMethod[] constructors = superClass.getConstructors();
for (PsiMethod constructor : constructors) {
if (constructor.getParameterList().getParametersCount() == 0 && constructor.isDeprecated()) {
return true;
}
}
}
return false;
}
//@top
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>5</line>
<description>Default constructor in C is deprecated</description>
</problem>
</problems>
@@ -0,0 +1,6 @@
class C {
@Deprecated C() { }
}
class D extends C {
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>6</line>
<description>Default constructor in C is deprecated</description>
</problem>
</problems>
@@ -0,0 +1,8 @@
class C {
@Deprecated C() { }
}
class D extends C {
D() {
}
}
@@ -32,4 +32,13 @@ public class DeprecationInspectionTest extends InspectionTestCase {
public void testDeprecatedField() throws Exception{
doTest();
}
public void testDeprecatedDefaultConstructorInSuper() throws Exception {
doTest();
}
public void testDeprecatedDefaultConstructorInSuperNotCalled() throws Exception {
doTest();
}
}