From f9d7c552da023b37bef1cf82c034fd187fea01e0 Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 1 Mar 2024 19:29:38 +0100 Subject: [PATCH 1/6] ADDED case MEMBER_SELECT for EC522 check --- .../sobriety/BrightnessOverrideRule.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java b/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java index 098eb7fd..751cbcbb 100644 --- a/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java +++ b/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java @@ -49,6 +49,20 @@ public void visitNode(Tree tree) { if (isBrightnessAssignment(assignmentTree)) { Tree.Kind assignmentTreeKind = assignmentTree.expression().kind(); switch (assignmentTreeKind) { + case MEMBER_SELECT: + MemberSelectExpressionTree mset = (MemberSelectExpressionTree) assignmentTree.expression(); + Optional identifierTreeConstantMset = mset.identifier().asConstant(); + Tree initialTreeToFlag = mset.identifier(); + + mset = findMemberSelect(mset); + + if(identifierTreeConstantMset.isPresent()){ + checkBrightnessAssignmentExpressionValue(initialTreeToFlag, + mset.identifier().symbolType().fullyQualifiedName(), + (Number) identifierTreeConstantMset.get()); + } + + break; case FLOAT_LITERAL: LiteralTree floatLiteralTree = (LiteralTree) assignmentTree.expression(); checkBrightnessAssignmentExpressionValue(floatLiteralTree, @@ -87,6 +101,14 @@ public void visitNode(Tree tree) { } } + private MemberSelectExpressionTree findMemberSelect(MemberSelectExpressionTree mset) { + if(mset.expression().kind() == Tree.Kind.MEMBER_SELECT){ + mset = (MemberSelectExpressionTree) mset.expression(); + mset = findMemberSelect(mset); + } + return mset; + } + /** * Check that the assignment is an assignment of android.view.WindowManager$LayoutParams screenBrightness element * From 7bafc8fea2698ea11b3ae6420cd98ae13665ff82 Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 1 Mar 2024 19:41:47 +0100 Subject: [PATCH 2/6] UPDATED Case MEMBER_SELECT for EC22 check --- .../sobriety/BrightnessOverrideRule.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java b/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java index 751cbcbb..7d657f77 100644 --- a/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java +++ b/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java @@ -56,10 +56,16 @@ public void visitNode(Tree tree) { mset = findMemberSelect(mset); - if(identifierTreeConstantMset.isPresent()){ - checkBrightnessAssignmentExpressionValue(initialTreeToFlag, - mset.identifier().symbolType().fullyQualifiedName(), - (Number) identifierTreeConstantMset.get()); + if (identifierTreeConstantMset.isPresent()) { + if (((Number) identifierTreeConstantMset.get()).floatValue() == BRIGHTNESS_FULL_VALUE) { + reportIssue(initialTreeToFlag, ERROR_MESSAGE); + } else if (((Number) identifierTreeConstantMset.get()).intValue() == BRIGHTNESS_FULL_VALUE) { + reportIssue(initialTreeToFlag, ERROR_MESSAGE); + } else { + checkBrightnessAssignmentExpressionValue(initialTreeToFlag, + mset.identifier().symbolType().fullyQualifiedName(), + (Number) identifierTreeConstantMset.get()); + } } break; @@ -102,7 +108,7 @@ public void visitNode(Tree tree) { } private MemberSelectExpressionTree findMemberSelect(MemberSelectExpressionTree mset) { - if(mset.expression().kind() == Tree.Kind.MEMBER_SELECT){ + if (mset.expression().kind() == Tree.Kind.MEMBER_SELECT) { mset = (MemberSelectExpressionTree) mset.expression(); mset = findMemberSelect(mset); } From d44939057fc1b64712fe7b788d8c871cb7bdf906 Mon Sep 17 00:00:00 2001 From: Justin Date: Mon, 4 Mar 2024 17:23:24 +0100 Subject: [PATCH 3/6] UPDATED: BrightnessOverrideCHeck add test member select --- .../sobriety/BrightnessOverrideCheck.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/android-plugin/src/test/files/environment/sobriety/BrightnessOverrideCheck.java b/android-plugin/src/test/files/environment/sobriety/BrightnessOverrideCheck.java index 454020d9..3bee5c64 100644 --- a/android-plugin/src/test/files/environment/sobriety/BrightnessOverrideCheck.java +++ b/android-plugin/src/test/files/environment/sobriety/BrightnessOverrideCheck.java @@ -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() { @@ -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.}} } } } From dd439e001fe087c26a93194ba26040971de05df6 Mon Sep 17 00:00:00 2001 From: Justin Date: Mon, 4 Mar 2024 19:57:40 +0100 Subject: [PATCH 4/6] UPDATED: EC522 Add Comments --- .../environment/sobriety/BrightnessOverrideRule.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java b/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java index 7d657f77..9c462958 100644 --- a/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java +++ b/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java @@ -50,6 +50,8 @@ public void visitNode(Tree tree) { Tree.Kind assignmentTreeKind = assignmentTree.expression().kind(); switch (assignmentTreeKind) { case MEMBER_SELECT: + //that case check members attributes, + // if we find an expression get the last expression of the memberSelectExpression MemberSelectExpressionTree mset = (MemberSelectExpressionTree) assignmentTree.expression(); Optional identifierTreeConstantMset = mset.identifier().asConstant(); Tree initialTreeToFlag = mset.identifier(); @@ -107,6 +109,13 @@ public void visitNode(Tree tree) { } } + /** + * Check if parameter's Expression is also a member select, + * reassign parent until expression is not a Member select + * + * @param mset the Member Select Expression to check + * @return the last expression + */ private MemberSelectExpressionTree findMemberSelect(MemberSelectExpressionTree mset) { if (mset.expression().kind() == Tree.Kind.MEMBER_SELECT) { mset = (MemberSelectExpressionTree) mset.expression(); From 4276a8e4512117451b4111cae2fcedab6a750eb6 Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 5 Mar 2024 21:22:44 +0100 Subject: [PATCH 5/6] UPDATED BrightnessOverrideRule remove useless checks --- .../sobriety/BrightnessOverrideRule.java | 31 +++++-------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java b/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java index 9c462958..e7daf542 100644 --- a/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java +++ b/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java @@ -50,37 +50,24 @@ public void visitNode(Tree tree) { Tree.Kind assignmentTreeKind = assignmentTree.expression().kind(); switch (assignmentTreeKind) { case MEMBER_SELECT: - //that case check members attributes, - // if we find an expression get the last expression of the memberSelectExpression + //that case check members attributes, so we need to check tree identifier's value MemberSelectExpressionTree mset = (MemberSelectExpressionTree) assignmentTree.expression(); Optional identifierTreeConstantMset = mset.identifier().asConstant(); Tree initialTreeToFlag = mset.identifier(); - - mset = findMemberSelect(mset); - if (identifierTreeConstantMset.isPresent()) { - if (((Number) identifierTreeConstantMset.get()).floatValue() == BRIGHTNESS_FULL_VALUE) { - reportIssue(initialTreeToFlag, ERROR_MESSAGE); - } else if (((Number) identifierTreeConstantMset.get()).intValue() == BRIGHTNESS_FULL_VALUE) { - reportIssue(initialTreeToFlag, ERROR_MESSAGE); - } else { - checkBrightnessAssignmentExpressionValue(initialTreeToFlag, - mset.identifier().symbolType().fullyQualifiedName(), - (Number) identifierTreeConstantMset.get()); - } + 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: @@ -88,7 +75,6 @@ public void visitNode(Tree tree) { Optional variableTreeConstant = variableTree.initializer().asConstant(); if (variableTreeConstant.isPresent()) { checkBrightnessAssignmentExpressionValue(variableTree, - variableTree.initializer().symbolType().fullyQualifiedName(), (Number) variableTreeConstant.get()); } break; @@ -98,20 +84,20 @@ public void visitNode(Tree tree) { if (identifierTreeConstant.isPresent()) { // Constant identifier only, other cases are ignored checkBrightnessAssignmentExpressionValue(identifierTree, - identifierTree.symbolType().fullyQualifiedName(), (Number) identifierTreeConstant.get()); } break; default: // For other tree types we cannot do anything, juste ignore them + System.out.println(assignmentTreeKind); break; } } } /** - * Check if parameter's Expression is also a member select, - * reassign parent until expression is not a Member select + * Check if parameter's Expression is also a member select, + * reassign parent until expression is not a Member select * * @param mset the Member Select Expression to check * @return the last expression @@ -140,11 +126,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); } } From 07f17d1f208a500974bced59a9e72431fdaa7085 Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 5 Mar 2024 22:47:24 +0100 Subject: [PATCH 6/6] UPDATED: BrightnessOverrideRule remove unused method --- .../sobriety/BrightnessOverrideRule.java | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java b/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java index e7daf542..376292fd 100644 --- a/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java +++ b/android-plugin/src/main/java/io/ecocode/java/checks/environment/sobriety/BrightnessOverrideRule.java @@ -58,7 +58,6 @@ public void visitNode(Tree tree) { checkBrightnessAssignmentExpressionValue(initialTreeToFlag, (Number)identifierTreeConstantMset.get()); } - break; case FLOAT_LITERAL: LiteralTree floatLiteralTree = (LiteralTree) assignmentTree.expression(); @@ -89,27 +88,11 @@ public void visitNode(Tree tree) { break; default: // For other tree types we cannot do anything, juste ignore them - System.out.println(assignmentTreeKind); break; } } } - /** - * Check if parameter's Expression is also a member select, - * reassign parent until expression is not a Member select - * - * @param mset the Member Select Expression to check - * @return the last expression - */ - private MemberSelectExpressionTree findMemberSelect(MemberSelectExpressionTree mset) { - if (mset.expression().kind() == Tree.Kind.MEMBER_SELECT) { - mset = (MemberSelectExpressionTree) mset.expression(); - mset = findMemberSelect(mset); - } - return mset; - } - /** * Check that the assignment is an assignment of android.view.WindowManager$LayoutParams screenBrightness element *