diff options
Diffstat (limited to 'java/java-tests/testSrc/com/intellij/codeInspection')
5 files changed, 103 insertions, 26 deletions
diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/ContractInferenceFromSourceTest.groovy b/java/java-tests/testSrc/com/intellij/codeInspection/ContractInferenceFromSourceTest.groovy index 89762efe5dd2..c3bb014d8342 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/ContractInferenceFromSourceTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInspection/ContractInferenceFromSourceTest.groovy @@ -27,7 +27,7 @@ class ContractInferenceFromSourceTest extends LightCodeInsightFixtureTestCase { def c = inferContract(""" String smth(String s) { if (s == null) return null; - return s.substring(1); + return smth(); } """) assert c == 'null -> null' @@ -310,6 +310,55 @@ class ContractInferenceFromSourceTest extends LightCodeInsightFixtureTestCase { assert c == [] } + public void "test skip empty declarations"() { + def c = inferContracts(""" + final Object foo(Object bar) { + Object o = 2; + if (bar == null) return null; + return new String("abc"); + } + """) + assert c == ['null -> null', '!null -> !null'] + } + + public void "test go inside do-while"() { + def c = inferContracts(""" + final Object foo(Object bar) { + do { + if (bar == null) return null; + bar = smth(bar); + } while (smthElse()); + return new String("abc"); + } + """) + assert c == ['null -> null'] + } + + public void "test go inside try"() { + def c = inferContracts(""" + final Object foo(Object bar) { + try { + if (bar == null) return null; + bar = smth(bar); + } finally {} + return new String("abc"); + } + """) + assert c == ['null -> null'] + } + + public void "test use invoked method notnull"() { + def c = inferContracts(""" + final Object foo(Object bar) { + if (bar == null) return null; + return doo(); + } + + @org.jetbrains.annotations.NotNull Object doo() {} + """) + assert c == ['null -> null', '!null -> !null'] + } + private String inferContract(String method) { return assertOneElement(inferContracts(method)) } diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java index 33eb32eb0ec6..a10d02d31fca 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java @@ -109,6 +109,7 @@ public class DataFlowInspectionTest extends LightCodeInsightFixtureTestCase { public void testAssigningClassLiteralToNullable() throws Throwable { doTest(); } public void testSynchronizingOnNullable() throws Throwable { doTest(); } + public void testSwitchOnNullable() { doTest(); } public void testReturningNullFromVoidMethod() throws Throwable { doTest(); } public void testCatchRuntimeException() throws Throwable { doTest(); } @@ -214,6 +215,7 @@ public class DataFlowInspectionTest extends LightCodeInsightFixtureTestCase { public void testNoConfusionWithAnonymousConstantInitializer() { doTest(); } public void testForeachOverWildcards() { doTest(); } public void testFinalGetter() { doTest(); } + public void testGetterResultsNotSame() { doTest(); } public void testByteBufferGetter() { myFixture.addClass("package java.nio; public class MappedByteBuffer { public int getInt() {} }"); diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/UnusedLibraryInspectionTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/UnusedLibraryInspectionTest.java index bb6195a94aa7..938a86da7587 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/UnusedLibraryInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/UnusedLibraryInspectionTest.java @@ -25,6 +25,7 @@ package com.intellij.codeInspection; import com.intellij.JavaTestUtil; +import com.intellij.analysis.AnalysisScope; import com.intellij.codeInspection.ex.LocalInspectionToolWrapper; import com.intellij.codeInspection.magicConstant.MagicConstantInspection; import com.intellij.codeInspection.unusedLibraries.UnusedLibrariesInspection; @@ -37,6 +38,7 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.testFramework.InspectionTestCase; import com.intellij.testFramework.PlatformTestUtil; import com.intellij.testFramework.PsiTestUtil; +import org.jetbrains.annotations.NotNull; public class UnusedLibraryInspectionTest extends InspectionTestCase { @Override @@ -54,5 +56,11 @@ public class UnusedLibraryInspectionTest extends InspectionTestCase { doTest("/" + getTestName(true), new UnusedLibrariesInspection()); } + @NotNull + @Override + protected AnalysisScope createAnalysisScope(VirtualFile sourceDir) { + return new AnalysisScope(getProject()); + } + public void testSimple() throws Exception { doTest(); } } diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisIntegrationTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisIntegrationTest.java index 516b140cc0b1..ab6d92263162 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisIntegrationTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisIntegrationTest.java @@ -38,7 +38,7 @@ import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase; import com.intellij.util.AsynchConsumer; import org.jetbrains.annotations.Contract; -import java.io.IOException; +import java.security.MessageDigest; import java.util.ArrayList; import java.util.List; @@ -50,7 +50,7 @@ public class BytecodeAnalysisIntegrationTest extends JavaCodeInsightFixtureTestC private InferredAnnotationsManager myInferredAnnotationsManager; private ExternalAnnotationsManager myExternalAnnotationsManager; - + private MessageDigest myMessageDigest; private List<String> diffs = new ArrayList<String>(); @Override @@ -62,6 +62,7 @@ public class BytecodeAnalysisIntegrationTest extends JavaCodeInsightFixtureTestC myInferredAnnotationsManager = InferredAnnotationsManager.getInstance(myModule.getProject()); myExternalAnnotationsManager = ExternalAnnotationsManager.getInstance(myModule.getProject()); + myMessageDigest = BytecodeAnalysisConverter.getMessageDigest(); } private void setUpLibraries() { @@ -127,13 +128,9 @@ public class BytecodeAnalysisIntegrationTest extends JavaCodeInsightFixtureTestC } private void checkMethodAnnotations(PsiMethod method) { - try { - if (ProjectBytecodeAnalysis.getKey(method) == -1) { - return; - } - } - catch (IOException e) { - fail(); + + if (ProjectBytecodeAnalysis.getKey(method, myMessageDigest) == null) { + return; } // not null-result @@ -149,12 +146,27 @@ public class BytecodeAnalysisIntegrationTest extends JavaCodeInsightFixtureTestC for (PsiParameter parameter : method.getParameterList().getParameters()) { String parameterKey = PsiFormatUtil.getExternalName(parameter, false, Integer.MAX_VALUE); - String externalParameterAnnotation = - myExternalAnnotationsManager.findExternalAnnotation(parameter, AnnotationUtil.NOT_NULL) == null ? "null" : "@NotNull"; - String inferredParameterAnnotation = - myInferredAnnotationsManager.findInferredAnnotation(parameter, AnnotationUtil.NOT_NULL) == null ? "null" : "@NotNull"; - if (!externalParameterAnnotation.equals(inferredParameterAnnotation)) { - diffs.add(parameterKey + ": " + externalParameterAnnotation + " != " + inferredParameterAnnotation); + + { + // @NotNull + String externalNotNull = + myExternalAnnotationsManager.findExternalAnnotation(parameter, AnnotationUtil.NOT_NULL) == null ? "null" : "@NotNull"; + String inferredNotNull = + myInferredAnnotationsManager.findInferredAnnotation(parameter, AnnotationUtil.NOT_NULL) == null ? "null" : "@NotNull"; + if (!externalNotNull.equals(inferredNotNull)) { + diffs.add(parameterKey + ": " + externalNotNull + " != " + inferredNotNull); + } + } + + { + // @Nullable + String externalNullable = + myExternalAnnotationsManager.findExternalAnnotation(parameter, AnnotationUtil.NULLABLE) == null ? "null" : "@Nullable"; + String inferredNullable = + myInferredAnnotationsManager.findInferredAnnotation(parameter, AnnotationUtil.NULLABLE) == null ? "null" : "@Nullable"; + if (!externalNullable.equals(inferredNullable)) { + diffs.add(parameterKey + ": " + externalNullable + " != " + inferredNullable); + } } } diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisTest.java index 9d26fd804f68..2cbf3fbdfd6c 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisTest.java @@ -17,6 +17,7 @@ package com.intellij.codeInspection.bytecodeAnalysis; import com.intellij.codeInsight.AnnotationUtil; import com.intellij.codeInsight.InferredAnnotationsManager; +import com.intellij.codeInspection.bytecodeAnalysis.asm.LeakingParameters; import com.intellij.codeInspection.bytecodeAnalysis.data.*; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.LocalFileSystem; @@ -37,6 +38,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; +import java.security.MessageDigest; import java.util.HashMap; /** @@ -47,15 +49,15 @@ public class BytecodeAnalysisTest extends JavaCodeInsightFixtureTestCase { private final String myClassesProjectRelativePath = "/classes/" + Test01.class.getPackage().getName().replace('.', '/'); private JavaPsiFacade myJavaPsiFacade; private InferredAnnotationsManager myInferredAnnotationsManager; - private BytecodeAnalysisConverter myBytecodeAnalysisConverter; + private MessageDigest myMessageDigest; + @Override protected void setUp() throws Exception { super.setUp(); myJavaPsiFacade = JavaPsiFacade.getInstance(myModule.getProject()); myInferredAnnotationsManager = InferredAnnotationsManager.getInstance(myModule.getProject()); - myBytecodeAnalysisConverter = BytecodeAnalysisConverter.getInstance(); - + myMessageDigest = MessageDigest.getInstance("MD5"); setUpDataClasses(); } @@ -93,7 +95,7 @@ public class BytecodeAnalysisTest extends JavaCodeInsightFixtureTestCase { public void visitEnd() { super.visitEnd(); try { - map.put(method, cfg.leakingParameters(classReader.getClassName(), node)); + map.put(method, LeakingParameters.build(classReader.getClassName(), node, false).parameters); } catch (AnalyzerException ignore) {} } @@ -183,17 +185,21 @@ public class BytecodeAnalysisTest extends JavaCodeInsightFixtureTestCase { private void checkCompoundId(Method method, PsiMethod psiMethod, boolean noKey) throws IOException { Direction direction = new Out(); - long psiKey = myBytecodeAnalysisConverter.mkPsiKey(psiMethod, direction); + System.out.println(); + System.out.println(method.internalClassName); + System.out.println(method.methodName); + System.out.println(method.methodDesc); + + + HKey psiKey = BytecodeAnalysisConverter.psiKey(psiMethod, direction, myMessageDigest); if (noKey) { - assertTrue(-1 == psiKey); + assertTrue(null == psiKey); return; } else { - assertFalse(-1 == psiKey); + assertFalse(null == psiKey); } - - long asmKey = myBytecodeAnalysisConverter.mkAsmKey(new Key(method, direction, true)); - + HKey asmKey = BytecodeAnalysisConverter.asmKey(new Key(method, direction, true), myMessageDigest); Assert.assertEquals(asmKey, psiKey); } |