Skip to content

Commit

Permalink
Merge pull request #92 from green-code-initiative/feature/EC522-membe…
Browse files Browse the repository at this point in the history
…r-select

ADDED case MEMBER_SELECT for EC522 check
  • Loading branch information
jhertout authored Mar 13, 2024
2 parents 2746ebc + 07f17d1 commit 3e17b16
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,31 @@ public void visitNode(Tree tree) {
if (isBrightnessAssignment(assignmentTree)) {
Tree.Kind assignmentTreeKind = assignmentTree.expression().kind();
switch (assignmentTreeKind) {
case MEMBER_SELECT:
//that case check members attributes, so we need to check tree identifier's value
MemberSelectExpressionTree mset = (MemberSelectExpressionTree) assignmentTree.expression();
Optional<Object> identifierTreeConstantMset = mset.identifier().asConstant();
Tree initialTreeToFlag = mset.identifier();
if (identifierTreeConstantMset.isPresent()) {
checkBrightnessAssignmentExpressionValue(initialTreeToFlag,
(Number)identifierTreeConstantMset.get());
}
break;
case FLOAT_LITERAL:
LiteralTree floatLiteralTree = (LiteralTree) assignmentTree.expression();
checkBrightnessAssignmentExpressionValue(floatLiteralTree,
floatLiteralTree.symbolType().fullyQualifiedName(),
Float.valueOf(floatLiteralTree.value()));
break;
case INT_LITERAL:
LiteralTree intLiteralTree = (LiteralTree) assignmentTree.expression();
checkBrightnessAssignmentExpressionValue(intLiteralTree,
intLiteralTree.symbolType().fullyQualifiedName(),
Integer.valueOf(intLiteralTree.value()));
break;
case VARIABLE:
VariableTree variableTree = (VariableTree) assignmentTree.expression();
Optional<Object> variableTreeConstant = variableTree.initializer().asConstant();
if (variableTreeConstant.isPresent()) {
checkBrightnessAssignmentExpressionValue(variableTree,
variableTree.initializer().symbolType().fullyQualifiedName(),
(Number) variableTreeConstant.get());
}
break;
Expand All @@ -76,7 +83,6 @@ public void visitNode(Tree tree) {
if (identifierTreeConstant.isPresent()) {
// Constant identifier only, other cases are ignored
checkBrightnessAssignmentExpressionValue(identifierTree,
identifierTree.symbolType().fullyQualifiedName(),
(Number) identifierTreeConstant.get());
}
break;
Expand All @@ -103,11 +109,10 @@ private boolean isBrightnessAssignment(AssignmentExpressionTree assignmentExpres
}
}

private void checkBrightnessAssignmentExpressionValue(Tree tree, String qualifiedType, Number value) {
private void checkBrightnessAssignmentExpressionValue(Tree tree, Number value) {
int intValue = value.intValue();
float floatValue = value.floatValue();
if (qualifiedType.equals("float") && floatValue == BRIGHTNESS_FULL_VALUE
|| qualifiedType.equals("int") && intValue == BRIGHTNESS_FULL_VALUE) {
if (floatValue == BRIGHTNESS_FULL_VALUE || intValue == BRIGHTNESS_FULL_VALUE) {
reportIssue(tree, ERROR_MESSAGE);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
package android.view;

public interface WindowManager extends ViewManager {

public static final float BRIGHTNESS_OVERRIDE_NONE = -1.0f;
public static final float BRIGHTNESS_OVERRIDE_OFF = 0.0f;
public static final float BRIGHTNESS_OVERRIDE_FULL = 1.0f;

public static class LayoutParams {
public static final float BRIGHTNESS_OVERRIDE_NONE = -1.0f;
public static final float BRIGHTNESS_OVERRIDE_OFF = 0.0f;
public static final float BRIGHTNESS_OVERRIDE_FULL = 1.0f;

public float screenBrightness = BRIGHTNESS_OVERRIDE_NONE;

public void InnerStaticClassMethod() {
Expand All @@ -36,6 +35,10 @@ public void InnerStaticClassMethod() {

float blabla = 1f;
params.screenBrightness = blabla; // TODO Noncompliant for VARIABLES

params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL; // Noncompliant {{Forcing brightness to max value may cause useless energy consumption.}}
params.screenBrightness = LayoutParams.BRIGHTNESS_OVERRIDE_FULL; // Noncompliant {{Forcing brightness to max value may cause useless energy consumption.}}
params.screenBrightness = android.view.WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL; // Noncompliant {{Forcing brightness to max value may cause useless energy consumption.}}
}
}
}

0 comments on commit 3e17b16

Please sign in to comment.