Skip to content

Commit

Permalink
Merge pull request #88 from green-code-initiative/PR43
Browse files Browse the repository at this point in the history
PR-43 - Use orElseGet instead of orElse
  • Loading branch information
dedece35 authored Jan 2, 2025
2 parents 2bd9ff6 + a168de0 commit b84f138
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

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

### Changed

- upgrade some libraries versions
Expand Down
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";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,7 @@
import java.util.Collections;
import java.util.List;

import org.greencodeinitiative.creedengo.java.checks.ArrayCopyCheck;
import org.greencodeinitiative.creedengo.java.checks.AvoidFullSQLRequest;
import org.greencodeinitiative.creedengo.java.checks.AvoidGettingSizeCollectionInLoop;
import org.greencodeinitiative.creedengo.java.checks.AvoidMultipleIfElseStatement;
import org.greencodeinitiative.creedengo.java.checks.AvoidRegexPatternNotStatic;
import org.greencodeinitiative.creedengo.java.checks.AvoidSQLRequestInLoop;
import org.greencodeinitiative.creedengo.java.checks.AvoidSetConstantInBatchUpdate;
import org.greencodeinitiative.creedengo.java.checks.AvoidSpringRepositoryCallInLoopOrStreamCheck;
import org.greencodeinitiative.creedengo.java.checks.AvoidStatementForDMLQueries;
import org.greencodeinitiative.creedengo.java.checks.AvoidUsageOfStaticCollections;
import org.greencodeinitiative.creedengo.java.checks.FreeResourcesOfAutoCloseableInterface;
import org.greencodeinitiative.creedengo.java.checks.IncrementCheck;
import org.greencodeinitiative.creedengo.java.checks.InitializeBufferWithAppropriateSize;
import org.greencodeinitiative.creedengo.java.checks.NoFunctionCallWhenDeclaringForLoop;
import org.greencodeinitiative.creedengo.java.checks.OptimizeReadFileExceptions;
import org.greencodeinitiative.creedengo.java.checks.*;
import org.sonar.plugins.java.api.CheckRegistrar;
import org.sonar.plugins.java.api.JavaCheck;
import org.sonarsource.api.sonarlint.SonarLintSide;
Expand Down Expand Up @@ -62,7 +48,8 @@ public class JavaCheckRegistrar implements CheckRegistrar {
InitializeBufferWithAppropriateSize.class,
AvoidSetConstantInBatchUpdate.class,
FreeResourcesOfAutoCloseableInterface.class,
AvoidMultipleIfElseStatement.class
AvoidMultipleIfElseStatement.class,
UseOptionalOrElseGetVsOrElse.class
);

/**
Expand Down
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);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"GCI76",
"GCI77",
"GCI78",
"GCI79"
"GCI79",
"GCI94"
]
}
35 changes: 35 additions & 0 deletions src/test/files/UseOptionalOrElseGetVsOrElse.java
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";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ void checkNumberRules() {
.describedAs("All implemented rules must be registered into " + JavaCheckRegistrar.class)
.containsExactlyInAnyOrder(getDefinedRules().toArray(new Class[0]));
assertThat(context.testCheckClasses()).isEmpty();

}

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

}
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();
}
}

0 comments on commit b84f138

Please sign in to comment.