Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new rules for EC203 #55

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
19 changes: 3 additions & 16 deletions src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,7 @@
import java.util.Collections;
import java.util.List;

import fr.greencodeinitiative.java.checks.ArrayCopyCheck;
import fr.greencodeinitiative.java.checks.AvoidFullSQLRequest;
import fr.greencodeinitiative.java.checks.AvoidGettingSizeCollectionInLoop;
import fr.greencodeinitiative.java.checks.AvoidMultipleIfElseStatement;
import fr.greencodeinitiative.java.checks.AvoidRegexPatternNotStatic;
import fr.greencodeinitiative.java.checks.AvoidSQLRequestInLoop;
import fr.greencodeinitiative.java.checks.AvoidSetConstantInBatchUpdate;
import fr.greencodeinitiative.java.checks.AvoidSpringRepositoryCallInLoopOrStreamCheck;
import fr.greencodeinitiative.java.checks.AvoidStatementForDMLQueries;
import fr.greencodeinitiative.java.checks.AvoidUsageOfStaticCollections;
import fr.greencodeinitiative.java.checks.FreeResourcesOfAutoCloseableInterface;
import fr.greencodeinitiative.java.checks.IncrementCheck;
import fr.greencodeinitiative.java.checks.InitializeBufferWithAppropriateSize;
import fr.greencodeinitiative.java.checks.NoFunctionCallWhenDeclaringForLoop;
import fr.greencodeinitiative.java.checks.OptimizeReadFileExceptions;
import fr.greencodeinitiative.java.checks.*;
import org.sonar.plugins.java.api.CheckRegistrar;
import org.sonar.plugins.java.api.JavaCheck;
import org.sonarsource.api.sonarlint.SonarLintSide;
Expand Down Expand Up @@ -62,7 +48,8 @@ public class JavaCheckRegistrar implements CheckRegistrar {
InitializeBufferWithAppropriateSize.class,
AvoidSetConstantInBatchUpdate.class,
FreeResourcesOfAutoCloseableInterface.class,
AvoidMultipleIfElseStatement.class
AvoidMultipleIfElseStatement.class,
SVGImageCheck.class
);

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package fr.greencodeinitiative.java.checks;

import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.ExpressionTree;
import org.sonar.plugins.java.api.tree.LiteralTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

import java.util.Arrays;
import java.util.List;

@Rule(key = "EC501")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S501")
public class SVGImageCheck extends IssuableSubscriptionVisitor {

private static final String MESSAGE_RULES = "Consider using SVG format over other image formats for small images.";
private static final List<String> IMAGE_FORMATS = Arrays.asList("jpeg", "jpg", "png");
private static final int MAX_ACCEPTABLE_SIZE = 100000;

@Override
public List<Tree.Kind> nodesToVisit() {
return List.of(Tree.Kind.STRING_LITERAL);
}

@Override
public void visitNode(Tree tree) {
String imagePath = ((LiteralTree) tree).value().replaceAll("\"", "");
if (isImage(imagePath)) {
String fileExtension = getFileExtension(imagePath);
if (!fileExtension.equalsIgnoreCase("svg")) {
File imageFile = new File(imagePath);
if (imageFile.exists()) {
int imageSize = getImageSize(imageFile);
if (imageSize > 0 && imageSize < MAX_ACCEPTABLE_SIZE) {
reportIssue(tree, MESSAGE_RULES);
}
}
}
}
}

private boolean isImage(String filename) {
String fileExtension = getFileExtension(filename);
return IMAGE_FORMATS.contains(fileExtension.toLowerCase());
}

private String getFileExtension(String filename) {
int lastDotIndex = filename.lastIndexOf(".");
if (lastDotIndex != -1) {
return filename.substring(lastDotIndex + 1);
}
return "";
}

private int getImageSize(File imageFile) {
try {
BufferedImage image = ImageIO.read(imageFile);
if (image != null) {
return image.getWidth() * image.getHeight();
}
} catch (Exception e) {
System.err.println("Image is not provided");
}
return 0;
}

}
Binary file added src/test/assets/Image_chien.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/test/assets/fond_ecran.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
179 changes: 179 additions & 0 deletions src/test/assets/svg.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions src/test/files/CheckSvg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.greencodeinitiative.java.checks;


public class CheckSvg {

CheckSvg(){}

public void loadImage() {

String imagePathj = "src/test/assets/Image_chien.jpg"; // Noncompliant {{Consider using SVG format over other image formats for small images.}}

try {
File image = new File(imagePathj);
} catch (Exeception e){
System.err.out(e);
}
}
}
35 changes: 35 additions & 0 deletions src/test/files/CheckSvg2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.greencodeinitiative.java.checks;


public class CheckSvg2 {

CheckSvg2(){}

public void loadImage() {

String imagePathj = "src/test/assets/fond_ecran.jpeg"; // Noncompliant {{Consider using SVG format over other image formats for small images.}}

try {
File image = new File(imagePathj);
} catch (Exeception e){
System.err.out(e);
}
}
}
Loading
Loading