-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from green-code-initiative/PR43
PR-43 - Use orElseGet instead of orElse
- Loading branch information
Showing
8 changed files
with
169 additions
and
17 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
35 changes: 35 additions & 0 deletions
35
...main/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElse.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,35 @@ | ||
/* | ||
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs | ||
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import java.util.Optional; | ||
|
||
class UseOptionalOrElseGetVsOrElse { | ||
|
||
private static Optional<String> variable = Optional.empty(); | ||
|
||
public static final String NAME = Optional.of("creedengo").orElse(getUnpredictedMethod()); // Noncompliant {{Use optional orElseGet instead of orElse.}} | ||
|
||
public static final String NAME2 = Optional.of("creedengo").orElseGet(() -> getUnpredictedMethod()); // Compliant | ||
|
||
public static final String NAME3 = variable.orElse(getUnpredictedMethod()); // Compliant | ||
|
||
private static String getUnpredictedMethod() { | ||
return "unpredicted"; | ||
} | ||
|
||
} |
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
59 changes: 59 additions & 0 deletions
59
...main/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElse.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,59 @@ | ||
/* | ||
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs | ||
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.greencodeinitiative.creedengo.java.checks; | ||
|
||
import org.sonar.check.Rule; | ||
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||
import org.sonar.plugins.java.api.tree.BaseTreeVisitor; | ||
import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree; | ||
import org.sonar.plugins.java.api.tree.MethodInvocationTree; | ||
import org.sonar.plugins.java.api.tree.Tree; | ||
import javax.annotation.Nonnull; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
@Rule(key = "GCI94") | ||
public class UseOptionalOrElseGetVsOrElse extends IssuableSubscriptionVisitor { | ||
|
||
private static final String MESSAGE_RULE = "Use optional orElseGet instead of orElse."; | ||
private final UseOptionalOrElseGetVsOrElseVisitor visitorInFile = new UseOptionalOrElseGetVsOrElseVisitor(); | ||
|
||
@Override | ||
public List<Tree.Kind> nodesToVisit() { | ||
return Collections.singletonList(Tree.Kind.METHOD_INVOCATION); | ||
} | ||
|
||
@Override | ||
public void visitNode(@Nonnull Tree tree) { | ||
tree.accept(visitorInFile); | ||
} | ||
|
||
private class UseOptionalOrElseGetVsOrElseVisitor extends BaseTreeVisitor { | ||
@Override | ||
public void visitMethodInvocation(MethodInvocationTree tree) { | ||
if (tree.methodSelect().is(Tree.Kind.MEMBER_SELECT) && | ||
Objects.requireNonNull(tree.methodSelect().firstToken()).text().equals("Optional")) { | ||
MemberSelectExpressionTree memberSelect = (MemberSelectExpressionTree) tree.methodSelect(); | ||
if (memberSelect.identifier().name().equals("orElse")) { | ||
reportIssue(memberSelect, MESSAGE_RULE); | ||
} | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
"GCI76", | ||
"GCI77", | ||
"GCI78", | ||
"GCI79" | ||
"GCI79", | ||
"GCI94" | ||
] | ||
} |
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,35 @@ | ||
/* | ||
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs | ||
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import java.util.Optional; | ||
|
||
class UseOptionalOrElseGetVsOrElse { | ||
|
||
private static Optional<String> variable = Optional.empty(); | ||
|
||
public static final String NAME = Optional.of("creedengo").orElse(getUnpredictedMethod()); // Noncompliant {{Use optional orElseGet instead of orElse.}} | ||
|
||
public static final String NAME2 = Optional.of("creedengo").orElseGet(() -> getUnpredictedMethod()); // Compliant | ||
|
||
public static final String NAME3 = variable.orElse(getUnpredictedMethod()); // Compliant | ||
|
||
private static String getUnpredictedMethod() { | ||
return "unpredicted"; | ||
} | ||
|
||
} |
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
31 changes: 31 additions & 0 deletions
31
.../java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElseTest.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,31 @@ | ||
/* | ||
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs | ||
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.greencodeinitiative.creedengo.java.checks; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.sonar.java.checks.verifier.CheckVerifier; | ||
|
||
class UseOptionalOrElseGetVsOrElseTest { | ||
@Test | ||
void test() { | ||
CheckVerifier.newVerifier() | ||
.onFile("src/test/files/UseOptionalOrElseGetVsOrElse.java") | ||
.withCheck(new UseOptionalOrElseGetVsOrElse()) | ||
.verifyIssues(); | ||
} | ||
} |