forked from spotbugs/spotbugs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spotbugs#600: Fix false positves for RCN_REDUNDANT_NULLCHECK_OF_NONNU…
…LL_VALUE in Java 11 This should fix issue spotbugs#600 and spotbugs#1338. I also fixed the broken tests for spotbugs#259.
- Loading branch information
Showing
15 changed files
with
590 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
spotbugs-tests/src/test/java/edu/umd/cs/findbugs/nullness/Issue1338Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package edu.umd.cs.findbugs.nullness; | ||
|
||
import edu.umd.cs.findbugs.BugCollection; | ||
import edu.umd.cs.findbugs.test.SpotBugsRule; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import java.nio.file.Paths; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.emptyIterable; | ||
|
||
/** | ||
* Unit test to reproduce <a href="https://github.com/spotbugs/spotbugs/issues/1338">#1338</a>. | ||
*/ | ||
public class Issue1338Test { | ||
@Rule | ||
public SpotBugsRule spotbugs = new SpotBugsRule(); | ||
|
||
@Test | ||
public void test() { | ||
BugCollection bugCollection = spotbugs.performAnalysis( | ||
Paths.get("../spotbugsTestCases/build/classes/java/main/ghIssues/Issue1338.class")); | ||
assertThat(bugCollection, emptyIterable()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
spotbugs-tests/src/test/java/edu/umd/cs/findbugs/nullness/Issue600Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package edu.umd.cs.findbugs.nullness; | ||
|
||
import edu.umd.cs.findbugs.BugCollection; | ||
import edu.umd.cs.findbugs.test.SpotBugsRule; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import java.nio.file.Paths; | ||
|
||
import static edu.umd.cs.findbugs.test.CountMatcher.containsExactly; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
/** | ||
* Unit tests to reproduce <a href="https://github.com/spotbugs/spotbugs/issues/600">#600</a>. | ||
*/ | ||
public class Issue600Test { | ||
@Rule | ||
public SpotBugsRule spotbugs = new SpotBugsRule(); | ||
|
||
@Test | ||
public void testTryWithResourcesSimple() { | ||
assertBugCount("TryWithResourcesSimple", 0); | ||
assertBugCount("TryWithResourcesMultiple", 0); | ||
assertBugCount("TryWithResourcesNested", 0); | ||
} | ||
|
||
@Test | ||
public void testExplicitNulls() { | ||
assertBugCount("ExplicitNullCheckSimple", 1); | ||
assertBugCount("ExplicitNullCheckNested", 2); | ||
assertBugCount("ExplicitNullCheckMultiple", 2); | ||
assertBugCount("Mixed1", 1); | ||
assertBugCount("Mixed2", 1); | ||
} | ||
|
||
private void assertBugCount(String clazz, int count) { | ||
BugCollection bugCollection = analyse(clazz); | ||
final BugInstanceMatcher bugTypeMatcher = new BugInstanceMatcherBuilder() | ||
.bugType("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE").build(); | ||
assertThat(bugCollection, containsExactly(count, bugTypeMatcher)); | ||
} | ||
|
||
private BugCollection analyse(String clazz) { | ||
return spotbugs.performAnalysis( | ||
Paths.get("../spotbugsTestCases/build/classes/java/main/ghIssues/issue600/" + clazz + ".class"), | ||
Paths.get("../spotbugsTestCases/build/classes/java/main/ghIssues/issue600/MyAutocloseable.class")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package ghIssues; | ||
|
||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.security.KeyStore; | ||
|
||
public class Issue1338 { | ||
public static KeyStore loadKeyStore(final Path path) throws Exception { | ||
try (InputStream inputStream = Files.newInputStream(path)) { | ||
// final KeyStore keyStore = KeyStore.getInstance("JKS"); | ||
// keyStore.load(inputStream, password.toCharArray()); | ||
// return keyStore; | ||
return KeyStore.getInstance("JKS"); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
spotbugsTestCases/src/java/ghIssues/issue600/ExplicitNullCheckMultiple.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package ghIssues.issue600; | ||
|
||
/** | ||
* This class is the compiled and decompiled version of TryWithResourcesMultiple. | ||
*/ | ||
public class ExplicitNullCheckMultiple { | ||
public void test() throws Exception { | ||
MyAutocloseable closeable1 = MyAutocloseable.create(); | ||
|
||
try { | ||
MyAutocloseable closeable2 = MyAutocloseable.create(); | ||
|
||
try { | ||
closeable1.exampleMethod(); | ||
closeable2.exampleMethod(); | ||
} catch (Throwable var7) { | ||
if (closeable2 != null) { | ||
try { | ||
closeable2.close(); | ||
} catch (Throwable var6) { | ||
var7.addSuppressed(var6); | ||
} | ||
} | ||
|
||
throw var7; | ||
} | ||
|
||
if (closeable2 != null) { | ||
closeable2.close(); | ||
} | ||
} catch (Throwable var8) { | ||
if (closeable1 != null) { | ||
try { | ||
closeable1.close(); | ||
} catch (Throwable var5) { | ||
var8.addSuppressed(var5); | ||
} | ||
} | ||
|
||
throw var8; | ||
} | ||
|
||
if (closeable1 != null) { | ||
closeable1.close(); | ||
} | ||
|
||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
spotbugsTestCases/src/java/ghIssues/issue600/ExplicitNullCheckNested.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package ghIssues.issue600; | ||
|
||
/** | ||
* This class is the compiled and decompiled version of TryWithResourcesNested. | ||
*/ | ||
public class ExplicitNullCheckNested { | ||
public void test() throws Exception { | ||
MyAutocloseable closeable1 = MyAutocloseable.create(); | ||
|
||
try { | ||
MyAutocloseable closeable2 = MyAutocloseable.create(); | ||
|
||
try { | ||
closeable1.exampleMethod(); | ||
closeable2.exampleMethod(); | ||
} catch (Throwable var7) { | ||
if (closeable2 != null) { | ||
try { | ||
closeable2.close(); | ||
} catch (Throwable var6) { | ||
var7.addSuppressed(var6); | ||
} | ||
} | ||
|
||
throw var7; | ||
} | ||
|
||
if (closeable2 != null) { | ||
closeable2.close(); | ||
} | ||
} catch (Throwable var8) { | ||
if (closeable1 != null) { | ||
try { | ||
closeable1.close(); | ||
} catch (Throwable var5) { | ||
var8.addSuppressed(var5); | ||
} | ||
} | ||
|
||
throw var8; | ||
} | ||
|
||
if (closeable1 != null) { | ||
closeable1.close(); | ||
} | ||
|
||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
spotbugsTestCases/src/java/ghIssues/issue600/ExplicitNullCheckSimple.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ghIssues.issue600; | ||
|
||
/** | ||
* This class is the compiled and decompiled version of TryWithResourcesSimple. | ||
*/ | ||
public class ExplicitNullCheckSimple { | ||
public void test() throws Exception { | ||
MyAutocloseable closeable = MyAutocloseable.create(); | ||
|
||
try { | ||
closeable.exampleMethod(); | ||
} catch (Throwable var5) { | ||
if (closeable != null) { | ||
try { | ||
closeable.close(); | ||
} catch (Throwable var4) { | ||
var5.addSuppressed(var4); | ||
} | ||
} | ||
throw var5; | ||
} | ||
|
||
if (closeable != null) { | ||
closeable.close(); | ||
} | ||
} | ||
} |
Oops, something went wrong.