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

MbarkiPr #13

Open
wants to merge 2 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
12 changes: 1 addition & 11 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,9 @@ Implémenter les règles suivantes :
| LINT_REG_003 |HIGH|Les variables locales commencent par une minuscule |
| LINT_REG_004 |HIGH|Les attributs d'une classe commencent par une minuscule |
| LINT_REG_005 |MEDIUM|Les constantes d'une classe sont écrites en majuscule, avec des `_` comme séparateur|
| LINT_REG_006 |HIGHEST|Les expressions booléennes ne doivent avoir plus que `2` opérandes |
| LINT_REG_007 |LOW|Les éléments d'une énumeration sont en majuscule, avec des `_` comme séparateur|
| LINT_REG_008 |HIGHEST|Le corps d'une méthode ne doit pas dépasser `30` lignes|
| LINT_REG_009 |HIGH|Les instanciations anonymes sont à remplacer par des expressions `lambda`|
| LINT_REG_010 |MEDIUM|Les expressions `lambda` intuitives sont à remplacer par `method reference` |
| LINT_REG_011 |HIGHEST|Le nombre de méthodes ne doit pas dépasser `20` méthodes déclarées par classe|
| LINT_REG_012 |HIGHEST|Le nombre de paramètres d'une méthode/constructeur ne doit dépasser `2`|
| LINT_REG_013 |HIGHEST|Les attributs de classe doivent avoir une visibilité déclarée|
| LINT_REG_014 |LOW|Préférer l'utilisation d'une seule instruction de sortie `(return, throw)` dans les méthodes|
| LINT_REG_015|LOW|Ne pas catcher les exceptions sans les logger|
| LINT_REG_016|MEDIUM|Les variables non utilisées sont à supprimer|
| LINT_REG_017|MEDIUM|Les méthodes privées non utilisées sont à supprimer|
| LINT_REG_018 |LOW|les clauses `if` , `else` doivent avoir des accolades |


Si une règle est bloquante, alors remonter l'erreur est mettre fin à l'analyse du fichier en cours.
### INPUT :
Expand Down
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guava library is not necessary, use java collection framework instead

<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>12.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.9.0</version>
</dependency>


<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220924</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/ensao/gi5/lint/Linter.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void run() {
final List<CompilationUnitWrapper> compilationUnitWrappers = new ArrayList<>();
for (File source : sources) {
compilationUnitWrappers.add(Utils.getCompilationUnit(source));
System.out.println(Utils.getCompilationUnit(source));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be removed

}

for (CompilationUnitWrapper compilationUnit : compilationUnitWrappers) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/ensao/gi5/lint/constantes/Constantes.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
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_005 = "LINT_REG_005";
public static final String LINT_REG_006 = "LINT_REG_006";
public static final String LINT_REG_002 = "LINT_REG_002";

public static final String LINT_REG_008 = "LINT_REG_008";
public static final String LINT_REG_009 = "LINT_REG_009";
public static final String LINT_REG_010 = "LINT_REG_010";
public static final String LINT_REG_013 = "LINT_REG_013";


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 com.ensao.gi5.lint.rules.violations.Violation;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;

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

public class CSVFileWriter implements Printer {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏽

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 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏽

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 org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;


public class JsonPrinter implements Printer{
@Override
public void printViolations(Collection<Violation> violations) {
JSONArray jsonArray = new JSONArray();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use fasterxml or Gson to automatically render json objects

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{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏽


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();
}
}

}

35 changes: 35 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/AnonymousRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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.visitor.AnonymousVisitors;
import com.ensao.gi5.lint.visitor.ConstVisitors;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;

import java.util.LinkedHashSet;
import java.util.Set;

public class AnonymousRule extends Rule{
public AnonymousRule() {
super(Constantes.LINT_REG_009, Level.HIGH);
}

@Override
public void apply(CompilationUnitWrapper compilationUnit) {
final Set<String> m = new LinkedHashSet<>();
compilationUnit.accept(new AnonymousVisitors(), m);

if(m.size()>0){
final Violation violation = new Violation();
violation.setDescription(m.size()+" Anonymous instanciation violation ");
violation.setFileName(compilationUnit.getFileName());
// violation.setLine(n.getLine());
addViolation(violation);
}
}

@Override
public boolean isActive() {
return true;
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/ConstRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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.visitor.ConstVisitors;
import com.ensao.gi5.lint.visitor.MembersVisitors;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class ConstRule extends Rule{
public ConstRule() {
super(Constantes.LINT_REG_005, Level.MEDIUM);
}

@Override
public void apply(CompilationUnitWrapper compilationUnit) {
final Set<String> m = new LinkedHashSet<>();
compilationUnit.accept(new ConstVisitors(), m);


for(String s:m) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use significant variable names, s and m are hard to understand


if (!s.equals(s.toUpperCase())) {
final Violation violation = new Violation();
violation.setDescription("Const name violation ");
violation.setFileName(compilationUnit.getFileName());
//violation.setLine(n.getLine());
addViolation(violation);
}
}

}

@Override
public boolean isActive() {
return true;
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/LocalvarRule.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.visitor.LocalvarVisitors;
import com.ensao.gi5.lint.visitor.NominationVisitors;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.ensao.gi5.lint.wrapper.NominationWrapper;

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class LocalvarRule extends Rule{
public LocalvarRule() {
super(Constantes.LINT_REG_003, Level.HIGHEST);
}
@Override
public void apply(CompilationUnitWrapper compilationUnit) {
final Set<String> localvariables = new LinkedHashSet<>();
compilationUnit.accept(new LocalvarVisitors(), localvariables);


Set<String> localvar=localvariables.stream().filter(p->Character.isUpperCase(p.charAt(1))).collect(Collectors.toSet());

if(!localvar.isEmpty()){
for (String lvar:localvar){
final Violation violation = new Violation();
violation.setDescription("local variable name violation '" + lvar + "'");
violation.setFileName(compilationUnit.getFileName());
// violation.setLine(n.getLine());
addViolation(violation);
}}

}

@Override
public boolean isActive() {
return true;
}
}
55 changes: 55 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/MembersRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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.visitor.LocalvarVisitors;
import com.ensao.gi5.lint.visitor.MembersVisitors;
import com.ensao.gi5.lint.visitor.NominationVisitors;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.ensao.gi5.lint.wrapper.NominationWrapper;
import com.github.javaparser.ast.body.BodyDeclaration;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class MembersRule extends Rule{
public MembersRule() {
super(Constantes.LINT_REG_004, Level.HIGHEST);
}
@Override
public void apply(CompilationUnitWrapper compilationUnit) {
final Set<String> m = new LinkedHashSet<>();
compilationUnit.accept(new MembersVisitors(), m);



Predicate<Object> p1= p->(Character.isUpperCase(p.toString().charAt(1)));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use significant variable names instead of p1




Set<String> answer=m.stream().filter(p1)
.collect(Collectors.toSet());


if (!answer.isEmpty()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this condition is useled, the for loop deals perfectly with empty collections


for (String n:answer){
final Violation violation = new Violation();
violation.setDescription("Class name violation '" + n + "'");
violation.setFileName(compilationUnit.getFileName());
//violation.setLine(n.getLine());
addViolation(violation);
}}


}

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