From 4f40958aa0b38a42810ddc77ed26c99d3a0584f1 Mon Sep 17 00:00:00 2001 From: anna Date: Tue, 7 Jun 2011 16:03:12 +0400 Subject: [PATCH] Deprecation warning not highlighted when superclass default constructor is deprecated (IDEA-68898) --- .../deprecation/DeprecationInspection.java | 47 +++++++++++++++++++ .../expected.xml | 9 ++++ .../src/Test.java | 6 +++ .../expected.xml | 9 ++++ .../src/Test.java | 8 ++++ .../DeprecationInspectionTest.java | 9 ++++ 6 files changed, 88 insertions(+) create mode 100644 java/java-tests/testData/inspection/deprecation/deprecatedDefaultConstructorInSuper/expected.xml create mode 100644 java/java-tests/testData/inspection/deprecation/deprecatedDefaultConstructorInSuper/src/Test.java create mode 100644 java/java-tests/testData/inspection/deprecation/deprecatedDefaultConstructorInSuperNotCalled/expected.xml create mode 100644 java/java-tests/testData/inspection/deprecation/deprecatedDefaultConstructorInSuperNotCalled/src/Test.java diff --git a/java/java-impl/src/com/intellij/codeInspection/deprecation/DeprecationInspection.java b/java/java-impl/src/com/intellij/codeInspection/deprecation/DeprecationInspection.java index 54f090eaec8f..8c2009b23ab9 100644 --- a/java/java-impl/src/com/intellij/codeInspection/deprecation/DeprecationInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/deprecation/DeprecationInspection.java @@ -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 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 diff --git a/java/java-tests/testData/inspection/deprecation/deprecatedDefaultConstructorInSuper/expected.xml b/java/java-tests/testData/inspection/deprecation/deprecatedDefaultConstructorInSuper/expected.xml new file mode 100644 index 000000000000..2ea8b4aa2f50 --- /dev/null +++ b/java/java-tests/testData/inspection/deprecation/deprecatedDefaultConstructorInSuper/expected.xml @@ -0,0 +1,9 @@ + + + + Test.java + 5 + Default constructor in C is deprecated + + + diff --git a/java/java-tests/testData/inspection/deprecation/deprecatedDefaultConstructorInSuper/src/Test.java b/java/java-tests/testData/inspection/deprecation/deprecatedDefaultConstructorInSuper/src/Test.java new file mode 100644 index 000000000000..52d011602385 --- /dev/null +++ b/java/java-tests/testData/inspection/deprecation/deprecatedDefaultConstructorInSuper/src/Test.java @@ -0,0 +1,6 @@ +class C { + @Deprecated C() { } +} + +class D extends C { +} diff --git a/java/java-tests/testData/inspection/deprecation/deprecatedDefaultConstructorInSuperNotCalled/expected.xml b/java/java-tests/testData/inspection/deprecation/deprecatedDefaultConstructorInSuperNotCalled/expected.xml new file mode 100644 index 000000000000..fbfc5a0037f4 --- /dev/null +++ b/java/java-tests/testData/inspection/deprecation/deprecatedDefaultConstructorInSuperNotCalled/expected.xml @@ -0,0 +1,9 @@ + + + + Test.java + 6 + Default constructor in C is deprecated + + + diff --git a/java/java-tests/testData/inspection/deprecation/deprecatedDefaultConstructorInSuperNotCalled/src/Test.java b/java/java-tests/testData/inspection/deprecation/deprecatedDefaultConstructorInSuperNotCalled/src/Test.java new file mode 100644 index 000000000000..c40f1d0188ac --- /dev/null +++ b/java/java-tests/testData/inspection/deprecation/deprecatedDefaultConstructorInSuperNotCalled/src/Test.java @@ -0,0 +1,8 @@ +class C { + @Deprecated C() { } +} + +class D extends C { + D() { + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/DeprecationInspectionTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/DeprecationInspectionTest.java index f82546959228..b53c7ed57418 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/DeprecationInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/DeprecationInspectionTest.java @@ -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(); + } + }