diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspectionBase.java b/java/java-analysis-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspectionBase.java index 54e4401fad7e..db8e62a1ea52 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspectionBase.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspectionBase.java @@ -25,6 +25,7 @@ import com.intellij.codeInspection.*; import com.intellij.codeInspection.dataFlow.DfaPsiUtil; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.WriteExternalException; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; @@ -77,7 +78,8 @@ public class NullableStuffInspectionBase extends BaseJavaBatchLocalInspectionToo @Override @NotNull public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) { - if (!PsiUtil.isLanguageLevel5OrHigher(holder.getFile())) { + final PsiFile file = holder.getFile(); + if (!PsiUtil.isLanguageLevel5OrHigher(file) || nullabilityAnnotationsNotAvailable(file)) { return new PsiElementVisitor() { }; } return new JavaElementVisitor() { @@ -146,6 +148,18 @@ public class NullableStuffInspectionBase extends BaseJavaBatchLocalInspectionToo }; } + private static boolean nullabilityAnnotationsNotAvailable(final PsiFile file) { + final Project project = file.getProject(); + final GlobalSearchScope scope = GlobalSearchScope.allScope(project); + final JavaPsiFacade facade = JavaPsiFacade.getInstance(project); + return ContainerUtil.find(NullableNotNullManager.getInstance(project).getNullables(), new Condition() { + @Override + public boolean value(String s) { + return facade.findClass(s, scope) != null; + } + }) == null; + } + private static boolean checkNonStandardAnnotations(PsiField field, Annotated annotated, NullableNotNullManager manager, String anno, @NotNull ProblemsHolder holder) { diff --git a/java/java-tests/testData/inspection/nullableProblems/jdkAnnotationsWithoutJetBrainsAnnotations/expected.xml b/java/java-tests/testData/inspection/nullableProblems/jdkAnnotationsWithoutJetBrainsAnnotations/expected.xml new file mode 100644 index 000000000000..d704d58ed391 --- /dev/null +++ b/java/java-tests/testData/inspection/nullableProblems/jdkAnnotationsWithoutJetBrainsAnnotations/expected.xml @@ -0,0 +1,4 @@ + + + + diff --git a/java/java-tests/testData/inspection/nullableProblems/jdkAnnotationsWithoutJetBrainsAnnotations/src/Test.java b/java/java-tests/testData/inspection/nullableProblems/jdkAnnotationsWithoutJetBrainsAnnotations/src/Test.java new file mode 100644 index 000000000000..8856dc92fe80 --- /dev/null +++ b/java/java-tests/testData/inspection/nullableProblems/jdkAnnotationsWithoutJetBrainsAnnotations/src/Test.java @@ -0,0 +1,7 @@ +class B extends Comparable { + @Override + public int compareTo(B o) { + return 0; + } +} + diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTestSuite.java b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTestSuite.java index 0178b38bd58a..89cbbc45395a 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTestSuite.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTestSuite.java @@ -44,7 +44,7 @@ public class DataFlowInspectionTestSuite { suite.addTestSuite(NormalCompletionDfaTest.class); suite.addTestSuite(NullableStuffInspectionTest.class); - suite.addTestSuite(NullableStuffInspection14Test.class); + suite.addTestSuite(NullableStuffInspectionAncientTest.class); suite.addTestSuite(AddAssertStatementFixTest.class); suite.addTestSuite(SurroundWithIfFixTest.class); diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/NullableStuffInspection14Test.java b/java/java-tests/testSrc/com/intellij/codeInspection/NullableStuffInspectionAncientTest.java similarity index 50% rename from java/java-tests/testSrc/com/intellij/codeInspection/NullableStuffInspection14Test.java rename to java/java-tests/testSrc/com/intellij/codeInspection/NullableStuffInspectionAncientTest.java index 7e57a07daf80..e1e7a3b13bc5 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/NullableStuffInspection14Test.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/NullableStuffInspectionAncientTest.java @@ -16,8 +16,9 @@ import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.roots.OrderRootType; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.testFramework.InspectionTestCase; +import com.intellij.testFramework.PsiTestUtil; -public class NullableStuffInspection14Test extends InspectionTestCase { +public class NullableStuffInspectionAncientTest extends InspectionTestCase { private final NullableStuffInspection myInspection = new NullableStuffInspection(); { myInspection.REPORT_ANNOTATION_NOT_PROPAGATED_TO_OVERRIDERS = false; @@ -28,40 +29,34 @@ public class NullableStuffInspection14Test extends InspectionTestCase { return JavaTestUtil.getJavaTestDataPath() + "/inspection"; } - private void doTest14() throws Exception { - myExcludeAnnotations = true; - try { - doTest("nullableProblems/" + getTestName(true), new LocalInspectionToolWrapper(myInspection),"java 1.4"); - } - finally { - myExcludeAnnotations = false; - } + public void testJdk14() throws Exception{ + doTest("nullableProblems/" + getTestName(true), new LocalInspectionToolWrapper(myInspection), "java 1.4"); } - public void testJdk14() throws Exception{ doTest14(); } - - private boolean myExcludeAnnotations = false; + public void testJdkAnnotationsWithoutJetBrainsAnnotations() throws Exception{ + doTest("nullableProblems/" + getTestName(true), new LocalInspectionToolWrapper(myInspection), "java 1.5"); + } @Override protected void setupRootModel(String testDir, VirtualFile[] sourceDir, String sdkName) { super.setupRootModel(testDir, sourceDir, sdkName); - - if (myExcludeAnnotations) { - final Sdk sdk = ModuleRootManager.getInstance(myModule).getSdk(); - assert sdk != null; - ApplicationManager.getApplication().runWriteAction(new Runnable() { - @Override - public void run() { - final SdkModificator sdkMod = sdk.getSdkModificator(); - for (VirtualFile file : sdkMod.getRoots(OrderRootType.CLASSES)) { - if ("annotations.jar".equals(file.getName())) { - sdkMod.removeRoot(file, OrderRootType.CLASSES); - break; - } - } - sdkMod.commitChanges(); - } - }); + Sdk sdk = ModuleRootManager.getInstance(myModule).getSdk(); + removeAnnotationsJar(sdk); + if ("testJdkAnnotationsWithoutJetBrainsAnnotations".equals(getName())) { + PsiTestUtil.addJdkAnnotations(sdk); } } + + private static void removeAnnotationsJar(final Sdk sdk) { + ApplicationManager.getApplication().runWriteAction(() -> { + final SdkModificator sdkMod = sdk.getSdkModificator(); + for (VirtualFile file : sdkMod.getRoots(OrderRootType.CLASSES)) { + if ("annotations.jar".equals(file.getName())) { + sdkMod.removeRoot(file, OrderRootType.CLASSES); + break; + } + } + sdkMod.commitChanges(); + }); + } } \ No newline at end of file