Skip to content

Commit b62da08

Browse files
authored
Merge pull request #17 from green-code-initiative/ISSUE_15
[ISSUE 15] correction NullPointer in EC2
2 parents 0caee62 + 8e3bbb2 commit b62da08

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,9 @@ private Integer internalGetVariableUsageOfNearestParent(Map<Integer, Map<String,
305305
Integer nbParentUsed = null;
306306
for (int i = pLevel; i >= 0 && nbParentUsed == null; i--) {
307307
Map<String, Integer> variablesParentLevelMap = pDataMap.get(i);
308-
nbParentUsed = variablesParentLevelMap.get(variableName);
308+
if (variablesParentLevelMap != null) {
309+
nbParentUsed = variablesParentLevelMap.get(variableName);
310+
}
309311
}
310312

311313
return nbParentUsed;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
3+
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package fr.greencodeinitiative.java.checks;
19+
20+
class AvoidMultipleIfElseStatementCompareMethod {
21+
22+
public int compare(FieldVo o1, FieldVo o2) {
23+
24+
if (o1.getIdBlock().equals(o2.getIdBlock())) {
25+
if (o1.getIdField().equals(o2.getIdField())) {
26+
return 0;
27+
}
28+
// First original
29+
if (o1.isOriginal() && !o2.isOriginal()) {
30+
return -1;
31+
} else if (!o1.isOriginal() && o2.isOriginal()) {
32+
return 1;
33+
}
34+
// First min posgafld
35+
Long result = o1.getColumnPos() - o2.getColumnPos();
36+
if (result != 0) {
37+
return result.intValue();
38+
}
39+
40+
// First min ordgaflc
41+
result = o1.getIndex() - o2.getIndex();
42+
return result.intValue();
43+
}
44+
// First BQRY block
45+
if (o1.getIdBlock().startsWith("BQRY") && !o2.getIdBlock().startsWith("BQRY")) {
46+
return -1;
47+
} else if (!o1.getIdBlock().startsWith("BQRY") && o2.getIdBlock().startsWith("BQRY")) {
48+
return 1;
49+
}
50+
// If both block don't start with BQRY, sort alpha with String.compareTo method
51+
return o1.getIdBlock().compareTo(o2.getIdBlock());
52+
}
53+
54+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,13 @@ void testNotBlockStatement() {
4949
.verifyNoIssues();
5050
}
5151

52+
@Test
53+
void testCompareMethod() {
54+
CheckVerifier.newVerifier()
55+
.onFile("src/test/files/AvoidMultipleIfElseStatementCompareMethod.java")
56+
.withCheck(new AvoidMultipleIfElseStatement())
57+
.verifyNoIssues();
58+
}
59+
5260

5361
}

0 commit comments

Comments
 (0)