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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- [#121](https://github.com/green-code-initiative/creedengo-java/issues/121) Correction of a False detection for rule GCI82 : "The variable is never reassigned and can be 'final'"
- [#69](https://github.com/green-code-initiative/creedengo-java/issues/69) correction of NullPointer in GCI79 rule + technical refactoring of GCI79
- update integration tests system to use the new component "creedengo-integration-test"
- compatibility updates for SonarQube up to 26.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,27 @@ class notReassignedInConstructorNotFinal{
notReassignedInConstructorNotFinal(String notReassignedInConstructorNotFinal) { // Noncompliant {{The variable is never reassigned and can be 'final'}}
System.out.println(notReassignedInConstructorNotFinal);
}
}
}


@FunctionalInterface
interface EventListenerSample<T> {
/**
* Callback method when an event occurs
* @param value Event data
* @return False if this event must stop at this treatment.
*/
boolean onEvent(T value); // Compliant
}

interface InterfaceWithMultipleMethods {
void abstractMethod(String param); // Compliant

default void defaultMethod(String param) { // Noncompliant {{The variable is never reassigned and can be 'final'}}
System.out.println(param);
}

static void staticMethod(String param) { // Noncompliant {{The variable is never reassigned and can be 'final'}}
System.out.println(param);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.greencodeinitiative.creedengo.java.checks;

import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.*;
Expand All @@ -15,8 +13,6 @@ public class MakeNonReassignedVariablesConstants extends IssuableSubscriptionVis

protected static final String MESSAGE_RULE = "The variable is never reassigned and can be 'final'";

private static final Logger LOGGER = Loggers.get(MakeNonReassignedVariablesConstants.class);

@Override
public List<Kind> nodesToVisit() {
return List.of(Kind.VARIABLE);
Expand All @@ -25,20 +21,20 @@ public List<Kind> nodesToVisit() {
@Override
public void visitNode(@Nonnull Tree tree) {
VariableTree variableTree = (VariableTree) tree;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Variable > {}", getVariableNameForLogger(variableTree));
LOGGER.debug(" => isNotFinalAndNotStatic(variableTree) = {}", isNotFinalAndNotStatic(variableTree));
LOGGER.debug(" => usages = {}", variableTree.symbol().usages().size());
LOGGER.debug(" => isNotReassigned = {}", isNotReassigned(variableTree));
LOGGER.debug(" => isPassedAsNonFinalParameter = {}", isPassedAsNonFinalParameter(variableTree));

if (isParameterOfAbstractMethod(variableTree)) {
return;
}
if (isNotFinalAndNotStatic(variableTree) && isNotReassigned(variableTree)) {
reportIssue(tree, MESSAGE_RULE);
} else {
super.visitNode(tree);
}
}

private static boolean isParameterOfAbstractMethod(VariableTree variableTree) {
Tree parent = variableTree.parent();
return parent != null && parent.is(Kind.METHOD) && ((MethodTree) parent).block() == null;
}

private static boolean isNotReassigned(VariableTree variableTree) {
return variableTree.symbol()
.usages()
Expand Down Expand Up @@ -138,25 +134,4 @@ public static boolean hasModifier(ModifiersTree modifiersTree, Modifier expected
return false;
}

private String getVariableNameForLogger(VariableTree variableTree) {
String name = variableTree.simpleName().name();

if (variableTree.parent() != null) return name;

if (variableTree.parent().is(Kind.CLASS)) {
ClassTree cTree = (ClassTree) variableTree.parent();
name += " --- from CLASS '" + cTree.simpleName() + "'";
}
if (variableTree.parent().is(Kind.BLOCK)) {
BlockTree bTree = (BlockTree) variableTree.parent();
if (bTree.parent() != null && bTree.parent().is(Kind.METHOD)) {
MethodTree mTree = (MethodTree) bTree.parent();
name += " --- from METHOD '" + mTree.simpleName() + "'";
}
}

return name;

}

}
Loading