Skip to content

Commit e43b26d

Browse files
[69] NullPointer exception in eco code java Sonar plugin
1 parent 55781ac commit e43b26d

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
- [#49](https://github.com/green-code-initiative/ecoCode-java/pull/49) Add test to ensure all Rules are registered
1717
- [#336](https://github.com/green-code-initiative/ecoCode/issues/336) [Adds Maven Wrapper](https://github.com/green-code-initiative/ecoCode-java/pull/67)
18+
- [#69](https://github.com/green-code-initiative/ecoCode-java/pull/69) correction of NullPointer in EC79 rule
1819

1920
### Deleted
2021

src/main/java/fr/greencodeinitiative/java/checks/FreeResourcesOfAutoCloseableInterface.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public void visitNode(Tree tree) {
7373
@Override
7474
public void leaveNode(Tree tree) {
7575
if (tree.is(Tree.Kind.TRY_STATEMENT)) {
76+
if(!withinTry.isEmpty()) {
77+
withinTry.pop();
78+
}
7679
List<Tree> secondaryTrees = toReport.pop();
7780
if (!secondaryTrees.isEmpty()) {
7881
reportIssue(tree, MESSAGE_RULE);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package files;
2+
3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
6+
/*
7+
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
8+
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License as published by
12+
* the Free Software Foundation, either version 3 of the License, or
13+
* (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*/
23+
public class FreeResourcesOfAutoCloseableInterface2 {
24+
25+
/**
26+
* The first methods adds a "try" in the stack used to follow if the code is in a try
27+
*/
28+
public void callingMethodWithTheTry() throws IOException {
29+
try {
30+
calledMethodWithoutTry();
31+
} finally {
32+
// Empty block of code
33+
}
34+
}
35+
36+
/**
37+
* The "try" should have been poped from the stack before entering here
38+
*/
39+
private void calledMethodWithoutTry() throws IOException {
40+
FileWriter myWriter = new FileWriter("somefilepath");
41+
myWriter.write("something");
42+
myWriter.flush();
43+
myWriter.close();
44+
}
45+
}

src/test/java/fr/greencodeinitiative/java/checks/FreeResourcesOfAutoCloseableInterfaceTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,11 @@ void test_no_java_version() {
3838
.withCheck(new FreeResourcesOfAutoCloseableInterface())
3939
.verifyIssues();
4040
}
41+
@Test
42+
void test_when_try_before_auto_closeable_but_different_hierarchy_of_code() {
43+
CheckVerifier.newVerifier()
44+
.onFile("src/test/files/FreeResourcesOfAutoCloseableInterface2.java")
45+
.withCheck(new FreeResourcesOfAutoCloseableInterface())
46+
.verifyNoIssues();
47+
}
4148
}

0 commit comments

Comments
 (0)