Skip to content

Commit b84f138

Browse files
authored
Merge pull request #88 from green-code-initiative/PR43
PR-43 - Use orElseGet instead of orElse
2 parents 2bd9ff6 + a168de0 commit b84f138

File tree

8 files changed

+169
-17
lines changed

8 files changed

+169
-17
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,35 @@
1+
/*
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/)
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+
19+
import java.util.Optional;
20+
21+
class UseOptionalOrElseGetVsOrElse {
22+
23+
private static Optional<String> variable = Optional.empty();
24+
25+
public static final String NAME = Optional.of("creedengo").orElse(getUnpredictedMethod()); // Noncompliant {{Use optional orElseGet instead of orElse.}}
26+
27+
public static final String NAME2 = Optional.of("creedengo").orElseGet(() -> getUnpredictedMethod()); // Compliant
28+
29+
public static final String NAME3 = variable.orElse(getUnpredictedMethod()); // Compliant
30+
31+
private static String getUnpredictedMethod() {
32+
return "unpredicted";
33+
}
34+
35+
}

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,7 @@
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;
@@ -62,7 +48,8 @@ public class JavaCheckRegistrar implements CheckRegistrar {
6248
InitializeBufferWithAppropriateSize.class,
6349
AvoidSetConstantInBatchUpdate.class,
6450
FreeResourcesOfAutoCloseableInterface.class,
65-
AvoidMultipleIfElseStatement.class
51+
AvoidMultipleIfElseStatement.class,
52+
UseOptionalOrElseGetVsOrElse.class
6653
);
6754

6855
/**
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
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/)
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 org.greencodeinitiative.creedengo.java.checks;
19+
20+
import org.sonar.check.Rule;
21+
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
22+
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
23+
import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree;
24+
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
25+
import org.sonar.plugins.java.api.tree.Tree;
26+
import javax.annotation.Nonnull;
27+
import java.util.Collections;
28+
import java.util.List;
29+
import java.util.Objects;
30+
31+
@Rule(key = "GCI94")
32+
public class UseOptionalOrElseGetVsOrElse extends IssuableSubscriptionVisitor {
33+
34+
private static final String MESSAGE_RULE = "Use optional orElseGet instead of orElse.";
35+
private final UseOptionalOrElseGetVsOrElseVisitor visitorInFile = new UseOptionalOrElseGetVsOrElseVisitor();
36+
37+
@Override
38+
public List<Tree.Kind> nodesToVisit() {
39+
return Collections.singletonList(Tree.Kind.METHOD_INVOCATION);
40+
}
41+
42+
@Override
43+
public void visitNode(@Nonnull Tree tree) {
44+
tree.accept(visitorInFile);
45+
}
46+
47+
private class UseOptionalOrElseGetVsOrElseVisitor extends BaseTreeVisitor {
48+
@Override
49+
public void visitMethodInvocation(MethodInvocationTree tree) {
50+
if (tree.methodSelect().is(Tree.Kind.MEMBER_SELECT) &&
51+
Objects.requireNonNull(tree.methodSelect().firstToken()).text().equals("Optional")) {
52+
MemberSelectExpressionTree memberSelect = (MemberSelectExpressionTree) tree.methodSelect();
53+
if (memberSelect.identifier().name().equals("orElse")) {
54+
reportIssue(memberSelect, MESSAGE_RULE);
55+
}
56+
}
57+
}
58+
}
59+
}

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
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
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/)
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+
19+
import java.util.Optional;
20+
21+
class UseOptionalOrElseGetVsOrElse {
22+
23+
private static Optional<String> variable = Optional.empty();
24+
25+
public static final String NAME = Optional.of("creedengo").orElse(getUnpredictedMethod()); // Noncompliant {{Use optional orElseGet instead of orElse.}}
26+
27+
public static final String NAME2 = Optional.of("creedengo").orElseGet(() -> getUnpredictedMethod()); // Compliant
28+
29+
public static final String NAME3 = variable.orElse(getUnpredictedMethod()); // Compliant
30+
31+
private static String getUnpredictedMethod() {
32+
return "unpredicted";
33+
}
34+
35+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ void checkNumberRules() {
3838
.describedAs("All implemented rules must be registered into " + JavaCheckRegistrar.class)
3939
.containsExactlyInAnyOrder(getDefinedRules().toArray(new Class[0]));
4040
assertThat(context.testCheckClasses()).isEmpty();
41+
4142
}
4243

4344
static Set<Class<?>> getDefinedRules() {
4445
Reflections r = new Reflections(JavaCheckRegistrar.class.getPackageName() + ".checks");
4546
return r.getTypesAnnotatedWith(Rule.class);
4647
}
48+
4749
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
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/)
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 org.greencodeinitiative.creedengo.java.checks;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.sonar.java.checks.verifier.CheckVerifier;
22+
23+
class UseOptionalOrElseGetVsOrElseTest {
24+
@Test
25+
void test() {
26+
CheckVerifier.newVerifier()
27+
.onFile("src/test/files/UseOptionalOrElseGetVsOrElse.java")
28+
.withCheck(new UseOptionalOrElseGetVsOrElse())
29+
.verifyIssues();
30+
}
31+
}

0 commit comments

Comments
 (0)