IDEA-124359 Add Inspection for detecting nullable values passed into Optional.of()

test that it correctly works with Guava
This commit is contained in:
peter
2015-04-14 23:12:39 +03:00
parent 1b7a05e26f
commit 6c8750291f
4 changed files with 43 additions and 14 deletions
@@ -0,0 +1,6 @@
// "Replace with '.fromNullable()'" "true"
class A{
void test(){
com.google.common.base.Optional.fromNullable(n<caret>ull);
}
}
@@ -0,0 +1,6 @@
// "Replace with '.fromNullable()'" "true"
class A{
void test(){
com.google.common.base.Optional.of(n<caret>ull);
}
}
@@ -14,19 +14,14 @@
* limitations under the License.
*/
/*
* User: anna
* Date: 21-Mar-2008
*/
package com.intellij.codeInsight.daemon.quickFix
import com.intellij.codeInspection.LocalInspectionTool
import com.intellij.codeInspection.dataFlow.DataFlowInspection
import com.intellij.openapi.Disposable
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.testFramework.IdeaTestUtil
import org.jetbrains.annotations.NotNull
@@ -46,11 +41,7 @@ public class ReplaceFromOfNullableFixTest extends LightQuickFixParameterizedTest
return "/codeInsight/daemonCodeAnalyzer/quickFix/replaceFromOfNullable";
}
static void addGuavaOptional() {
if (JavaPsiFacade.getInstance(project).findClass("com.google.common.base.Optional", GlobalSearchScope.allScope(project))) {
return
}
static void addGuavaOptional(Disposable parent) {
WriteCommandAction.runWriteCommandAction(project) {
VirtualFile optional = getSourceRoot()
.createChildDirectory(this, "com")
@@ -63,22 +54,34 @@ package com.google.common.base;
public abstract class Optional<T> {
public static <T> Optional<T> absent() { }
public static <T> Optional<T> of(T reference) { }
public static <T> Optional<T> of(@org.jetbrains.annotations.NotNull T reference) { }
public static <T> Optional<T> fromNullable(@Nullable T nullableReference) { }
public static <T> Optional<T> fromNullable(T nullableReference) { }
}
""")
}
}
static void cleanupGuava() {
WriteCommandAction.runWriteCommandAction(project) {
getSourceRoot().findChild("com")?.delete(this)
}
}
@Override
protected void beforeActionStarted(String testName, String contents) {
if (testName.contains("Guava")) {
addGuavaOptional()
addGuavaOptional(testRootDisposable)
}
super.beforeActionStarted(testName, contents)
}
@Override
protected void afterActionCompleted(String testName, String contents) {
cleanupGuava()
super.afterActionCompleted(testName, contents)
}
@Override
protected Sdk getProjectJDK() {
return IdeaTestUtil.getMockJdk18();
@@ -42,6 +42,20 @@ public class ReplaceWithOfNullableFixTest extends LightQuickFixParameterizedTest
return "/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithOfNullable";
}
@Override
protected void beforeActionStarted(String testName, String contents) {
if (testName.contains("Guava")) {
ReplaceFromOfNullableFixTest.addGuavaOptional(myTestRootDisposable);
}
super.beforeActionStarted(testName, contents);
}
@Override
protected void afterActionCompleted(String testName, String contents) {
ReplaceFromOfNullableFixTest.cleanupGuava();
super.afterActionCompleted(testName, contents);
}
@Override
protected Sdk getProjectJDK() {
return IdeaTestUtil.getMockJdk18();