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)
This commit is contained in:
Tagir Valeev
2018-06-15 16:16:19 +07:00
parent 4019043385
commit 7c5a344c83
2 changed files with 65 additions and 0 deletions
@@ -50,6 +50,7 @@ public class NullableNotNullManagerImpl extends NullableNotNullManager implement
public void setNotNulls(@NotNull String... annotations) {
LinkedHashSet<String> 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<String> 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<String> to, @NotNull String[] defaults) {
@@ -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<String> notNulls = myManager.getNotNulls();
assertTrue(notNulls.contains(AnnotationUtil.NOT_NULL));
assertFalse(notNulls.contains(AnnotationUtil.NULLABLE));
List<String> nullables = myManager.getNullables();
assertTrue(nullables.contains(AnnotationUtil.NULLABLE));
assertFalse(nullables.contains(AnnotationUtil.NOT_NULL));
}
}