Skip to content

Commit

Permalink
add rule GCI94
Browse files Browse the repository at this point in the history
  • Loading branch information
dedece35 committed Jan 2, 2025
1 parent 3e0aae2 commit 0a3ce07
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 38 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,34 @@
import java.util.Optional;

/*
* 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/>.
*/
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
@@ -1,6 +1,6 @@
/*
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
* 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
Expand All @@ -15,26 +15,12 @@
* 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 fr.greencodeinitiative.java;
package org.greencodeinitiative.creedengo.java;

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 All @@ -47,7 +33,7 @@
*/
@SonarLintSide
public class JavaCheckRegistrar implements CheckRegistrar {
private static final List<Class<? extends JavaCheck>> ANNOTATED_RULE_CLASSES = List.of(
static final List<Class<? extends JavaCheck>> ANNOTATED_RULE_CLASSES = List.of(
ArrayCopyCheck.class,
IncrementCheck.class,
AvoidUsageOfStaticCollections.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
* 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
Expand All @@ -15,7 +15,7 @@
* 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 fr.greencodeinitiative.java.checks;
package org.greencodeinitiative.creedengo.java.checks;

import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
Expand All @@ -28,7 +28,7 @@
import java.util.List;
import java.util.Objects;

@Rule(key = "EC1369")
@Rule(key = "GCI94")
public class UseOptionalOrElseGetVsOrElse extends IssuableSubscriptionVisitor {

private static final String MESSAGE_RULE = "Use optional orElseGet instead of orElse.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"GCI76",
"GCI77",
"GCI78",
"GCI79"
"GCI79",
"GCI94"
]
}
19 changes: 14 additions & 5 deletions src/test/files/UseOptionalOrElseGetVsOrElse.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import java.util.Optional;

/*
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
* 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
Expand All @@ -17,9 +19,16 @@
*/
class UseOptionalOrElseGetVsOrElse {

public static final String name = Optional.of("ecoCode").orElse(getUnpredictedMethod()); // Noncompliant {{Use optional orElseGet instead of orElse.}}
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

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

public static final String name = randomClass.orElse(getUnpredictedMethod()); // Compliant
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
* 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
Expand All @@ -15,9 +15,13 @@
* 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 fr.greencodeinitiative.java;
package org.greencodeinitiative.creedengo.java;

import java.util.Set;

import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.CheckRegistrar;

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

final JavaCheckRegistrar registrar = new JavaCheckRegistrar();
registrar.register(context);
(??) assertThat(context.checkClasses())
(??) .describedAs("All implemented rules must be registered into " + JavaCheckRegistrar.class)
(??) .containsExactlyInAnyOrder(getDefinedRules().toArray(new Class[0]));
assertThat(context.checkClasses())
.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
@@ -1,6 +1,6 @@
/*
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
* 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
Expand All @@ -15,7 +15,7 @@
* 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 fr.greencodeinitiative.java.checks;
package org.greencodeinitiative.creedengo.java.checks;

import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
Expand Down

0 comments on commit 0a3ce07

Please sign in to comment.