Skip to content

Commit 4a61b04

Browse files
committed
Add new rule EC31: Prefer lighter formats for image files
1 parent d5bf295 commit 4a61b04

6 files changed

Lines changed: 156 additions & 18 deletions

File tree

src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,7 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222

23-
import fr.greencodeinitiative.java.checks.ArrayCopyCheck;
24-
import fr.greencodeinitiative.java.checks.AvoidFullSQLRequest;
25-
import fr.greencodeinitiative.java.checks.AvoidGettingSizeCollectionInLoop;
26-
import fr.greencodeinitiative.java.checks.AvoidMultipleIfElseStatement;
27-
import fr.greencodeinitiative.java.checks.AvoidRegexPatternNotStatic;
28-
import fr.greencodeinitiative.java.checks.AvoidSQLRequestInLoop;
29-
import fr.greencodeinitiative.java.checks.AvoidSetConstantInBatchUpdate;
30-
import fr.greencodeinitiative.java.checks.AvoidSpringRepositoryCallInLoopOrStreamCheck;
31-
import fr.greencodeinitiative.java.checks.AvoidStatementForDMLQueries;
32-
import fr.greencodeinitiative.java.checks.AvoidUsageOfStaticCollections;
33-
import fr.greencodeinitiative.java.checks.FreeResourcesOfAutoCloseableInterface;
34-
import fr.greencodeinitiative.java.checks.IncrementCheck;
35-
import fr.greencodeinitiative.java.checks.InitializeBufferWithAppropriateSize;
36-
import fr.greencodeinitiative.java.checks.NoFunctionCallWhenDeclaringForLoop;
37-
import fr.greencodeinitiative.java.checks.OptimizeReadFileExceptions;
23+
import fr.greencodeinitiative.java.checks.*;
3824
import org.sonar.plugins.java.api.CheckRegistrar;
3925
import org.sonar.plugins.java.api.JavaCheck;
4026
import org.sonarsource.api.sonarlint.SonarLintSide;
@@ -62,7 +48,8 @@ public class JavaCheckRegistrar implements CheckRegistrar {
6248
InitializeBufferWithAppropriateSize.class,
6349
AvoidSetConstantInBatchUpdate.class,
6450
FreeResourcesOfAutoCloseableInterface.class,
65-
AvoidMultipleIfElseStatement.class
51+
AvoidMultipleIfElseStatement.class,
52+
PreferLighterImageFormats.class
6653
);
6754

6855
/**
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
3+
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package fr.greencodeinitiative.java.checks;
19+
20+
import java.util.Arrays;
21+
import java.util.List;
22+
import org.sonar.check.Rule;
23+
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
24+
import org.sonar.plugins.java.api.tree.Tree;
25+
import org.sonar.plugins.java.api.tree.LiteralTree;
26+
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
27+
import org.sonar.api.utils.log.Logger;
28+
import org.sonar.api.utils.log.Loggers;
29+
30+
@Rule(key = "EC31")
31+
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "EC31")
32+
public class PreferLighterImageFormats extends IssuableSubscriptionVisitor {
33+
34+
private static final Logger LOGGER = Loggers.get(PreferLighterImageFormats.class);
35+
private static final String MESSAGE = "Consider using lighter image formats like .webp or .avif instead of .jpg, .jpeg or .png";
36+
37+
private static final List<String> HEAVY_FORMATS = Arrays.asList(".jpg", ".jpeg", ".png");
38+
39+
@Override
40+
public List<Tree.Kind> nodesToVisit() {
41+
return Arrays.asList(Tree.Kind.STRING_LITERAL);
42+
}
43+
44+
public void visitNode(Tree tree) {
45+
if (tree.is(Tree.Kind.STRING_LITERAL)) {
46+
LiteralTree literalTree = (LiteralTree) tree;
47+
String value = literalTree.value();
48+
49+
// Remove the quotes around the literal value
50+
if (value.length() > 2 && value.startsWith("\"") && value.endsWith("\"")) {
51+
value = value.substring(1, value.length() - 1);
52+
}
53+
54+
for (String format : HEAVY_FORMATS) {
55+
if (value.endsWith(format)) {
56+
reportIssue(literalTree, MESSAGE);
57+
break;
58+
}
59+
}
60+
}
61+
}
62+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
3+
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package fr.greencodeinitiative.java.checks;
19+
20+
import java.util.logging.Logger;
21+
22+
class PreferLighterImageFormatsCheck {
23+
24+
private static final Logger LOGGER = Logger.getLogger(PreferLighterImageFormatsCheck.class.getName());
25+
26+
public PreferLighterImageFormatsCheck(PreferLighterImageFormatsCheck checker) {
27+
}
28+
29+
30+
public void testDirectAssignments() {
31+
String imagePath1 = "images/photo.jpg"; // Noncompliant
32+
String imagePath2 = "images/graphic.png"; // Noncompliant
33+
String imagePath3 = "images/photo.webp"; // Compliant
34+
String imagePath4 = "images/graphic.avif"; // Compliant
35+
}
36+
37+
public void testPathsInArray() {
38+
String[] imagePaths = {
39+
"assets/image1.jpg", // Noncompliant
40+
"assets/image2.png", // Noncompliant
41+
"assets/image3.webp", // Compliant
42+
"assets/image4.avif" // Compliant
43+
};
44+
45+
for (String imagePath : imagePaths) {
46+
LOGGER.info("Image path: " + imagePath);
47+
}
48+
}
49+
50+
public void testPathsInMethods() {
51+
logImagePath("icons/icon1.jpg"); // Noncompliant
52+
logImagePath("icons/icon2.png"); // Noncompliant
53+
logImagePath("icons/icon3.webp"); // Compliant
54+
logImagePath("icons/icon4.avif"); // Compliant
55+
}
56+
}

src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void checkNumberRules() {
3131
final JavaCheckRegistrar registrar = new JavaCheckRegistrar();
3232
registrar.register(context);
3333

34-
assertThat(context.checkClasses()).hasSize(15);
34+
assertThat(context.checkClasses()).hasSize(16);
3535
assertThat(context.testCheckClasses()).isEmpty();
3636

3737
}

src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void init() {
4646
RulesDefinition.Context context = new RulesDefinition.Context();
4747
rulesDefinition.define(context);
4848
repository = context.repository(rulesDefinition.repositoryKey());
49-
rulesSize = 15;
49+
rulesSize = 16;
5050
}
5151

5252
@Test
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
3+
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package fr.greencodeinitiative.java.checks;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.sonar.java.checks.verifier.CheckVerifier;
22+
23+
class PreferLighterImageFormatsCheckTest {
24+
25+
@Test
26+
void test() {
27+
CheckVerifier.newVerifier()
28+
.onFile("src/test/files/PreferLighterImageFormatsCheck.java")
29+
.withCheck(new PreferLighterImageFormats())
30+
.verifyIssues();
31+
}
32+
}
33+

0 commit comments

Comments
 (0)