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

Implement rules 3,4,7,12,13 and 15 (Boulouane Imane - Chahid Anas) #9

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
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
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@
<version>5.8.2</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-csv -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.9.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220924</version>
</dependency>
</dependencies>


Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/ensao/gi5/lint/constantes/Constantes.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
public class Constantes {
public static final String LINT_REG_000 = "LINT_REG_000";
public static final String LINT_REG_001 = "LINT_REG_001";

public static final String LINT_REG_003 = "LINT_REG_003";
public static final String LINT_REG_004 = "LINT_REG_004";
public static final String LINT_REG_007 = "LINT_REG_007";
public static final String LINT_REG_012 = "LINT_REG_012";
public static final String LINT_REG_013 = "LINT_REG_013";
public static final String LINT_REG_015 = "LINT_REG_015";
private Constantes() {
throw new IllegalStateException("not to be instantiated");
}
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/ensao/gi5/lint/printer/CSVFileWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.ensao.gi5.lint.printer;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;

import com.ensao.gi5.lint.rules.violations.Violation;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;

public class CSVFileWriter implements Printer {
private static final String NEW_LINE_SEPARATOR = "\n";

@Override
public void printViolations(Collection<Violation> violations) {
try (
BufferedWriter writer = Files.newBufferedWriter(Paths.get("violations.csv"));
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withFirstRecordAsHeader());
) {
csvPrinter.printRecord("LEVEL", "RULE", "DESCRIPTION");

for (Violation violation : violations) {
csvPrinter.printRecord(violation.getLevel(), violation.getRuleId(), violation.getDescription());
}

csvPrinter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/ensao/gi5/lint/printer/HtmlPrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.ensao.gi5.lint.printer;

import com.ensao.gi5.lint.rules.violations.Violation;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;

public class HtmlPrinter implements Printer {
private static final String NEW_LINE_SEPARATOR = "\n";

@Override
public void printViolations(Collection<Violation> violations) {
try (
BufferedWriter writer = Files.newBufferedWriter(Paths.get("violations.html"));
) {
writer.write("<html><head><title>Violations</title></head><body><table>");
writer.write("<tr><th>LEVEL</th><th>RULE</th><th>DESCRIPTION</th></tr>");

for (Violation violation : violations) {
writer.write("<tr><td>" + violation.getLevel() + "</td><td>" + violation.getRuleId() + "</td><td>" + violation.getDescription() + "</td></tr>");
}

writer.write("</table></body></html>");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/ensao/gi5/lint/printer/JsonPrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.ensao.gi5.lint.printer;

import com.ensao.gi5.lint.rules.violations.Violation;

import java.io.File;
import java.io.FileWriter;
import java.util.Collection;
import java.io.IOException;
import org.json.JSONArray;
import org.json.JSONObject;


public class JsonPrinter implements Printer{
@Override
public void printViolations(Collection<Violation> violations) {
JSONArray jsonArray = new JSONArray();
for (Violation violation : violations) {
JSONObject json = new JSONObject();
json.put("level", violation.getLevel());
json.put("ruleId", violation.getRuleId());
json.put("description", violation.getDescription());
jsonArray.put(json);
}
writeToFile(jsonArray);
}

private void writeToFile(JSONArray json) {
try {
FileWriter fileWriter = new FileWriter(new File("violations.json"));
fileWriter.write(json.toString());
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/ensao/gi5/lint/printer/MarkdownPrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.ensao.gi5.lint.printer;

import com.ensao.gi5.lint.rules.violations.Violation;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;

public class MarkdownPrinter implements Printer{

private static final String NEW_LINE_SEPARATOR = "\n";

@Override
public void printViolations(Collection<Violation> violations) {
try (
BufferedWriter writer = Files.newBufferedWriter(Paths.get("violations.md"));
) {
writer.write("| LEVEL | RULE | DESCRIPTION |");
writer.write(NEW_LINE_SEPARATOR);
writer.write("| ----- | ---- | ----------- |");
writer.write(NEW_LINE_SEPARATOR);

for (Violation violation : violations) {
writer.write("| " + violation.getLevel() + " | " + violation.getRuleId() + " | " + violation.getDescription() + " |");
writer.write(NEW_LINE_SEPARATOR);
}

writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}

}

41 changes: 41 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/CatchLogExceptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.ensao.gi5.lint.rules;

import com.ensao.gi5.lint.constantes.Constantes;
import com.ensao.gi5.lint.rules.violations.Violation;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.stmt.CatchClause;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;

public class CatchLogExceptions extends Rule{
public CatchLogExceptions() {
super(Constantes.LINT_REG_015, Level.LOW);
}

@Override
public void apply(CompilationUnitWrapper compilationUnit) {
compilationUnit.accept(new VoidVisitorAdapter<Void>() {
@Override
public void visit(CatchClause n, Void arg) {
if (!n.getBody().getStatements().stream()
.anyMatch(s -> s instanceof ExpressionStmt && ((ExpressionStmt) s)
.getExpression() instanceof MethodCallExpr && ((MethodCallExpr)((ExpressionStmt) s)
.getExpression()).getName().asString().equals("log"))) {
final Violation violation = new Violation();
violation.setDescription("Catch block does not contain log statement");
violation.setFileName(compilationUnit.getFileName());
violation.setLine(n.getBegin().get().line);
addViolation(violation);
}
super.visit(n, arg);
}
}, null);
}


@Override
public boolean isActive() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.ensao.gi5.lint.rules;

import com.ensao.gi5.lint.constantes.Constantes;
import com.ensao.gi5.lint.rules.violations.Violation;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;

public class ClassAttributeVisibility extends Rule{
public ClassAttributeVisibility() {
super(Constantes.LINT_REG_013, Level.HIGHEST);
}

@Override
public void apply(CompilationUnitWrapper compilationUnit) {
compilationUnit.accept(new VoidVisitorAdapter<Void>() {
@Override
public void visit(FieldDeclaration n, Void arg) {
if (n.getModifiers().isEmpty()) {
final Violation violation = new Violation();
violation.setDescription("Class attribute must have visibility declared");
violation.setFileName(compilationUnit.getFileName());
violation.setLine(n.getBegin().get().line);
addViolation(violation);
}
super.visit(n, arg);
}
}, null);
}


@Override
public boolean isActive() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.ensao.gi5.lint.rules;

import com.ensao.gi5.lint.constantes.Constantes;
import com.ensao.gi5.lint.rules.violations.Violation;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;

import java.util.List;

public class ClassAttributesStartWithLowerCase extends Rule{
public ClassAttributesStartWithLowerCase() {
super(Constantes.LINT_REG_004, Level.HIGH);
}

@Override
public void apply(CompilationUnitWrapper compilationUnit) {

// get all field declarations
List<FieldDeclaration> fieldDeclarations = compilationUnit.getFields();

// iterate through each field
for (FieldDeclaration fieldDeclaration : fieldDeclarations) {

List<VariableDeclarator> variableDeclarators = fieldDeclaration.getVariables();

// iterate through each variable declarator
for (VariableDeclarator variableDeclarator : variableDeclarators) {

// check if the first character of the attribute is not lowercase
if (!Character.isLowerCase(variableDeclarator.getNameAsString().charAt(0)) && !fieldDeclaration.isFinal()) {

Violation violation = new Violation();

// set filename, line number, and description
violation.setFileName(compilationUnit.getFileName());
violation.setLine(variableDeclarator.getBegin().get().line);
violation.setDescription(variableDeclarator.getNameAsString() + " must start with lower case");

// add the violation to the list of violations
addViolation(violation);
}
}
}

}

@Override
public boolean isActive() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.ensao.gi5.lint.rules;

import com.ensao.gi5.lint.constantes.Constantes;
import com.ensao.gi5.lint.rules.violations.Violation;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.github.javaparser.ast.body.EnumDeclaration;

import java.util.List;

public class EnumsShouldBeAllUpperCaseAndUnderscoreSeparator extends Rule {

public EnumsShouldBeAllUpperCaseAndUnderscoreSeparator() {
super(Constantes.LINT_REG_007, Level.LOW);
}

@Override
public void apply(CompilationUnitWrapper compilationUnit) {

// get all enums declarations
List<EnumDeclaration> enumDeclarations = compilationUnit.getEnums();

// iterate through each enum
for (EnumDeclaration enumDeclaration : enumDeclarations) {

// check if enum characters are all lower case and use underscore as separator
if (!enumDeclaration.getNameAsString().matches("^[A-Z_]+$")) {

Violation violation = new Violation();

// set filename, line number, and description
violation.setFileName(compilationUnit.getFileName());
violation.setLine(enumDeclaration.getBegin().get().line);
violation.setDescription(enumDeclaration.getNameAsString() + " must have all characters to uppercase while using underscore separator");

// add the violation to the list of violations
addViolation(violation);
}

}

}

@Override
public boolean isActive() {
return true;
}
}
Loading