Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Update ecocode-rules-specifications to 1.4.6


## [unreleased]

### Changed

- #183 GCI94 - reduce false positives by not flagging orElse with already-defined values (literals, constants, identifiers)

## [2.1.2] - 2026-05-20

[unreleased](https://github.com/green-code-initiative/creedengo-java/compare/2.1.2...HEAD)
[2.1.2](https://github.com/green-code-initiative/creedengo-java/compare/2.1.1...2.1.2)
[2.1.1](https://github.com/green-code-initiative/creedengo-java/compare/2.1.0...2.1.1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,54 @@ private static String getUnpredictedMethod() {
return "unpredicted";
}

private static final String DEFAULT_VALUE = "default";

void badMethodCall(String value) {
Optional.ofNullable(value).orElse(getDefaultValue()); // Noncompliant
}

void badNewObject(String value) {
Optional.ofNullable(value).orElse(new String("default")); // Noncompliant
}

void goodBooleanConstant(Boolean value) {
Optional.ofNullable(value).orElse(Boolean.FALSE);
}

void goodStringLiteral(String value) {
Optional.ofNullable(value).orElse("default");
}

void goodNull(String value) {
Optional.ofNullable(value).orElse(null);
}

void goodIdentifier(String value) {
Optional.ofNullable(value).orElse(DEFAULT_VALUE);
}

void goodAlreadyOrElseGet(String value) {
Optional.ofNullable(value).orElseGet(this::getDefaultValue);
}

private String getDefaultValue() {
return "default";
}

void badConcatenation(String value, String suffix) {
Optional.ofNullable(value).orElse("default" + suffix); // Noncompliant
}

void badTernary(String value, boolean condition) {
Optional.ofNullable(value).orElse(condition ? "default" : "fallback"); // Noncompliant
}

void badCast(String value, Object defaultValue) {
Optional.ofNullable(value).orElse((String) defaultValue); // Noncompliant
}

void badArrayAccess(String value, String[] values) {
Optional.ofNullable(value).orElse(values[0]); // Noncompliant
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@

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 org.sonar.plugins.java.api.tree.*;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import org.sonar.plugins.java.api.tree.*;
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
import org.sonar.plugins.java.api.tree.ExpressionTree;
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;
Expand Down Expand Up @@ -51,9 +49,32 @@ public void visitMethodInvocation(MethodInvocationTree tree) {
Objects.requireNonNull(tree.methodSelect().firstToken()).text().equals("Optional")) {
MemberSelectExpressionTree memberSelect = (MemberSelectExpressionTree) tree.methodSelect();
if (memberSelect.identifier().name().equals("orElse")) {
if (tree.arguments().isEmpty()) {
return;
}
ExpressionTree argument = tree.arguments().get(0);

if (isAlreadyDefinedValue(argument)) {
return;
}

reportIssue(memberSelect, MESSAGE_RULE);
}
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

private boolean isAlreadyDefinedValue(ExpressionTree argument) {
return argument.is(
Tree.Kind.STRING_LITERAL,
Tree.Kind.INT_LITERAL,
Tree.Kind.LONG_LITERAL,
Tree.Kind.FLOAT_LITERAL,
Tree.Kind.DOUBLE_LITERAL,
Tree.Kind.CHAR_LITERAL,
Tree.Kind.BOOLEAN_LITERAL,
Tree.Kind.NULL_LITERAL,
Tree.Kind.IDENTIFIER,
Tree.Kind.MEMBER_SELECT
);
}
}
}