From 7c5a344c83def5ff343963b5b174f7475dc5ad60 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Fri, 15 Jun 2018 16:08:07 +0700 Subject: [PATCH] NullableNotNullManagerImpl: disable setting JetBrains NotNull as Nullable annotation and vice versa Fixes EA-122395 - NA: AnnotationUtil.isAnnotated SOE was reported involving AnnotationUtil#ignoreInference(NotNull) -> isAnnotated(getNullables() which contains NotNull) -> ignoreInference(NotNull) --- .../NullableNotNullManagerImpl.java | 4 ++ .../NullableNotNullManagerTest.java | 61 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/NullableNotNullManagerTest.java diff --git a/java/java-impl/src/com/intellij/codeInsight/NullableNotNullManagerImpl.java b/java/java-impl/src/com/intellij/codeInsight/NullableNotNullManagerImpl.java index f4e0ef153446..1bb5a8ee5423 100644 --- a/java/java-impl/src/com/intellij/codeInsight/NullableNotNullManagerImpl.java +++ b/java/java-impl/src/com/intellij/codeInsight/NullableNotNullManagerImpl.java @@ -50,6 +50,7 @@ public class NullableNotNullManagerImpl extends NullableNotNullManager implement public void setNotNulls(@NotNull String... annotations) { LinkedHashSet set = ContainerUtil.newLinkedHashSet(annotations); Collections.addAll(set, DEFAULT_NOT_NULLS); + set.remove(NULLABLE); myNotNulls = new ArrayList<>(set); } @@ -57,6 +58,7 @@ public class NullableNotNullManagerImpl extends NullableNotNullManager implement public void setNullables(@NotNull String... annotations) { LinkedHashSet set = ContainerUtil.newLinkedHashSet(annotations); Collections.addAll(set, DEFAULT_NULLABLES); + set.remove(NOT_NULL); myNullables = new ArrayList<>(set); } @@ -133,6 +135,8 @@ public class NullableNotNullManagerImpl extends NullableNotNullManager implement readJdomList(state.myNullables, myNullables, DEFAULT_NULLABLES); readJdomList(state.myNotNulls, myNotNulls, DEFAULT_NOT_NULLS); + myNullables.remove(NOT_NULL); + myNotNulls.remove(NULLABLE); } private static void readJdomList(@Nullable Element src, @NotNull List to, @NotNull String[] defaults) { diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/NullableNotNullManagerTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/NullableNotNullManagerTest.java new file mode 100644 index 000000000000..b068511fb592 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/NullableNotNullManagerTest.java @@ -0,0 +1,61 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.java.codeInsight; + +import com.intellij.codeInsight.AnnotationUtil; +import com.intellij.codeInsight.NullableNotNullManager; +import com.intellij.codeInsight.NullableNotNullManagerImpl; +import com.intellij.testFramework.LightPlatformTestCase; +import org.jdom.Element; +import org.junit.Assume; + +import java.util.List; + +public class NullableNotNullManagerTest extends LightPlatformTestCase { + private NullableNotNullManagerImpl myManager; + + @Override + protected void setUp() throws Exception { + super.setUp(); + NullableNotNullManager manager = NullableNotNullManager.getInstance(getProject()); + Assume.assumeTrue(manager instanceof NullableNotNullManagerImpl); + myManager = (NullableNotNullManagerImpl)manager; + } + + @Override + protected void tearDown() throws Exception { + try { + myManager.loadState(new NullableNotNullManagerImpl.StateBean()); + } + finally { + super.tearDown(); + } + } + + public void testCannotAddNotNullToNullable() { + assertNotNull(myManager); + checkAnnotations(); + myManager.setNotNulls(AnnotationUtil.NULLABLE); + myManager.setNullables(AnnotationUtil.NOT_NULL); + checkAnnotations(); + } + + public void testCannotDeserializeNotNullToNullable() { + NullableNotNullManagerImpl.StateBean state = myManager.getState(); + Element tmp = state.myNotNulls; + state.myNotNulls = state.myNullables; + state.myNullables = tmp; + + myManager.loadState(state); + assertFalse(myManager.getNotNulls().contains(AnnotationUtil.NULLABLE)); + assertFalse(myManager.getNullables().contains(AnnotationUtil.NOT_NULL)); + } + + private void checkAnnotations() { + List notNulls = myManager.getNotNulls(); + assertTrue(notNulls.contains(AnnotationUtil.NOT_NULL)); + assertFalse(notNulls.contains(AnnotationUtil.NULLABLE)); + List nullables = myManager.getNullables(); + assertTrue(nullables.contains(AnnotationUtil.NULLABLE)); + assertFalse(nullables.contains(AnnotationUtil.NOT_NULL)); + } +}