Skip to content

Commit 0a3ce07

Browse files
committed
add rule GCI94
1 parent 3e0aae2 commit 0a3ce07

File tree

8 files changed

+79
-38
lines changed

8 files changed

+79
-38
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- [#88](https://github.com/green-code-initiative/creedengo-java/pull/88) Add new Java rule GCI94 (Use orElseGet instead of orElse)
13+
1214
### Changed
1315

1416
- upgrade some libraries versions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.Optional;
2+
3+
/*
4+
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs
5+
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/)
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
class UseOptionalOrElseGetVsOrElse {
21+
22+
private static Optional<String> variable = Optional.empty();
23+
24+
public static final String NAME = Optional.of("creedengo").orElse(getUnpredictedMethod()); // Noncompliant {{Use optional orElseGet instead of orElse.}}
25+
26+
public static final String NAME2 = Optional.of("creedengo").orElseGet(() -> getUnpredictedMethod()); // Compliant
27+
28+
public static final String NAME3 = variable.orElse(getUnpredictedMethod()); // Compliant
29+
30+
private static String getUnpredictedMethod() {
31+
return "unpredicted";
32+
}
33+
34+
}

src/main/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrar.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
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)
2+
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs
3+
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/)
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -15,26 +15,12 @@
1515
* You should have received a copy of the GNU General Public License
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
18-
package fr.greencodeinitiative.java;
18+
package org.greencodeinitiative.creedengo.java;
1919

2020
import java.util.Collections;
2121
import java.util.List;
2222

23-
(??)import org.greencodeinitiative.creedengo.java.checks.ArrayCopyCheck;
24-
(??)import org.greencodeinitiative.creedengo.java.checks.AvoidFullSQLRequest;
25-
(??)import org.greencodeinitiative.creedengo.java.checks.AvoidGettingSizeCollectionInLoop;
26-
(??)import org.greencodeinitiative.creedengo.java.checks.AvoidMultipleIfElseStatement;
27-
(??)import org.greencodeinitiative.creedengo.java.checks.AvoidRegexPatternNotStatic;
28-
(??)import org.greencodeinitiative.creedengo.java.checks.AvoidSQLRequestInLoop;
29-
(??)import org.greencodeinitiative.creedengo.java.checks.AvoidSetConstantInBatchUpdate;
30-
(??)import org.greencodeinitiative.creedengo.java.checks.AvoidSpringRepositoryCallInLoopOrStreamCheck;
31-
(??)import org.greencodeinitiative.creedengo.java.checks.AvoidStatementForDMLQueries;
32-
(??)import org.greencodeinitiative.creedengo.java.checks.AvoidUsageOfStaticCollections;
33-
(??)import org.greencodeinitiative.creedengo.java.checks.FreeResourcesOfAutoCloseableInterface;
34-
(??)import org.greencodeinitiative.creedengo.java.checks.IncrementCheck;
35-
(??)import org.greencodeinitiative.creedengo.java.checks.InitializeBufferWithAppropriateSize;
36-
(??)import org.greencodeinitiative.creedengo.java.checks.NoFunctionCallWhenDeclaringForLoop;
37-
(??)import org.greencodeinitiative.creedengo.java.checks.OptimizeReadFileExceptions;
23+
import org.greencodeinitiative.creedengo.java.checks.*;
3824
import org.sonar.plugins.java.api.CheckRegistrar;
3925
import org.sonar.plugins.java.api.JavaCheck;
4026
import org.sonarsource.api.sonarlint.SonarLintSide;
@@ -47,7 +33,7 @@
4733
*/
4834
@SonarLintSide
4935
public class JavaCheckRegistrar implements CheckRegistrar {
50-
private static final List<Class<? extends JavaCheck>> ANNOTATED_RULE_CLASSES = List.of(
36+
static final List<Class<? extends JavaCheck>> ANNOTATED_RULE_CLASSES = List.of(
5137
ArrayCopyCheck.class,
5238
IncrementCheck.class,
5339
AvoidUsageOfStaticCollections.class,

src/main/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElse.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
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)
2+
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs
3+
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/)
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -15,7 +15,7 @@
1515
* You should have received a copy of the GNU General Public License
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
18-
package fr.greencodeinitiative.java.checks;
18+
package org.greencodeinitiative.creedengo.java.checks;
1919

2020
import org.sonar.check.Rule;
2121
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
@@ -28,7 +28,7 @@
2828
import java.util.List;
2929
import java.util.Objects;
3030

31-
@Rule(key = "EC1369")
31+
@Rule(key = "GCI94")
3232
public class UseOptionalOrElseGetVsOrElse extends IssuableSubscriptionVisitor {
3333

3434
private static final String MESSAGE_RULE = "Use optional orElseGet instead of orElse.";

src/main/resources/org/greencodeinitiative/creedengo/java/creedengo_way_profile.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"GCI76",
1717
"GCI77",
1818
"GCI78",
19-
"GCI79"
19+
"GCI79",
20+
"GCI94"
2021
]
2122
}

src/test/files/UseOptionalOrElseGetVsOrElse.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import java.util.Optional;
2+
13
/*
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+
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs
5+
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/)
46
*
57
* This program is free software: you can redistribute it and/or modify
68
* it under the terms of the GNU General Public License as published by
@@ -17,9 +19,16 @@
1719
*/
1820
class UseOptionalOrElseGetVsOrElse {
1921

20-
public static final String name = Optional.of("ecoCode").orElse(getUnpredictedMethod()); // Noncompliant {{Use optional orElseGet instead of orElse.}}
22+
private static Optional<String> variable = Optional.empty();
23+
24+
public static final String NAME = Optional.of("creedengo").orElse(getUnpredictedMethod()); // Noncompliant {{Use optional orElseGet instead of orElse.}}
25+
26+
public static final String NAME2 = Optional.of("creedengo").orElseGet(() -> getUnpredictedMethod()); // Compliant
27+
28+
public static final String NAME3 = variable.orElse(getUnpredictedMethod()); // Compliant
2129

22-
public static final String name = Optional.of("ecoCode").orElseGet(() -> getUnpredictedMethod()); // Compliant
30+
private static String getUnpredictedMethod() {
31+
return "unpredicted";
32+
}
2333

24-
public static final String name = randomClass.orElse(getUnpredictedMethod()); // Compliant
2534
}

src/test/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrarTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
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)
2+
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs
3+
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/)
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -15,9 +15,13 @@
1515
* You should have received a copy of the GNU General Public License
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
18-
package fr.greencodeinitiative.java;
18+
package org.greencodeinitiative.creedengo.java;
19+
20+
import java.util.Set;
1921

2022
import org.junit.jupiter.api.Test;
23+
import org.reflections.Reflections;
24+
import org.sonar.check.Rule;
2125
import org.sonar.plugins.java.api.CheckRegistrar;
2226

2327
import static org.assertj.core.api.Assertions.assertThat;
@@ -30,11 +34,16 @@ void checkNumberRules() {
3034

3135
final JavaCheckRegistrar registrar = new JavaCheckRegistrar();
3236
registrar.register(context);
33-
(??) assertThat(context.checkClasses())
34-
(??) .describedAs("All implemented rules must be registered into " + JavaCheckRegistrar.class)
35-
(??) .containsExactlyInAnyOrder(getDefinedRules().toArray(new Class[0]));
37+
assertThat(context.checkClasses())
38+
.describedAs("All implemented rules must be registered into " + JavaCheckRegistrar.class)
39+
.containsExactlyInAnyOrder(getDefinedRules().toArray(new Class[0]));
3640
assertThat(context.testCheckClasses()).isEmpty();
3741

3842
}
3943

44+
static Set<Class<?>> getDefinedRules() {
45+
Reflections r = new Reflections(JavaCheckRegistrar.class.getPackageName() + ".checks");
46+
return r.getTypesAnnotatedWith(Rule.class);
47+
}
48+
4049
}

src/test/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElseTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
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)
2+
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs
3+
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/)
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -15,7 +15,7 @@
1515
* You should have received a copy of the GNU General Public License
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
18-
package fr.greencodeinitiative.java.checks;
18+
package org.greencodeinitiative.creedengo.java.checks;
1919

2020
import org.junit.jupiter.api.Test;
2121
import org.sonar.java.checks.verifier.CheckVerifier;

0 commit comments

Comments
 (0)