diff --git a/pom.xml b/pom.xml
index afe60dc..390ee16 100755
--- a/pom.xml
+++ b/pom.xml
@@ -54,6 +54,12 @@
5.8.2
test
+
+ org.projectlombok
+ lombok
+ RELEASE
+ compile
+
diff --git a/src/main/java/com/ensao/gi5/lint/constantes/Constantes.java b/src/main/java/com/ensao/gi5/lint/constantes/Constantes.java
index 32a57fb..f7258c7 100644
--- a/src/main/java/com/ensao/gi5/lint/constantes/Constantes.java
+++ b/src/main/java/com/ensao/gi5/lint/constantes/Constantes.java
@@ -1,9 +1,58 @@
package com.ensao.gi5.lint.constantes;
public class Constantes {
+
+ //La règle 0
public static final String LINT_REG_000 = "LINT_REG_000";
+
+ //La règle 1
public static final String LINT_REG_001 = "LINT_REG_001";
+ //La règle 2
+ public static final String LINT_REG_002 = "LINT_REG_002";
+
+ //La règle 3
+ public static final String LINT_REG_003 = "LINT_REG_003";
+
+ //La règle 4
+ public static final String LINT_REG_004 = "LINT_REG_004";
+
+ //La règle 5
+ public static final String LINT_REG_005 = "LINT_REG_005";
+
+ //La règle 6
+ public static final String LINT_REG_006 = "LINT_REG_006";
+
+ //La règle 7
+ public static final String LINT_REG_007 = "LINT_REG_007";
+
+ //La règle 8
+ public static final String LINT_REG_008 = "LINT_REG_008";
+
+ //La règle 9
+ public static final String LINT_REG_009 = "LINT_REG_009";
+
+ //La règle 10
+ public static final String LINT_REG_010 = "LINT_REG_010";
+
+ //La règle 11
+ public static final String LINT_REG_011 = "LINT_REG_011";
+
+ //La règle 12
+ public static final String LINT_REG_012 = "LINT_REG_012";
+
+ //La règle 13
+ public static final String LINT_REG_013 = "LINT_REG_013";
+
+ //La règle 14
+ public static final String LINT_REG_014 = "LINT_REG_014";
+
+ //La règle 15
+ public static final String LINT_REG_015 = "LINT_REG_015";
+
+ //La règle 18
+ public static final String LINT_REG_018 = "LINT_REG_018";
+
private Constantes() {
throw new IllegalStateException("not to be instantiated");
}
diff --git a/src/main/java/com/ensao/gi5/lint/rules/ClassRule.java b/src/main/java/com/ensao/gi5/lint/rules/ClassRule.java
new file mode 100644
index 0000000..a553e74
--- /dev/null
+++ b/src/main/java/com/ensao/gi5/lint/rules/ClassRule.java
@@ -0,0 +1,74 @@
+package com.ensao.gi5.lint.rules;
+
+import com.ensao.gi5.lint.constantes.Constantes;
+import com.ensao.gi5.lint.util.ViolationFactory;
+import com.ensao.gi5.lint.visitor.ClassVisitor;
+import com.ensao.gi5.lint.wrapper.ClassWrapper;
+import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
+import com.ensao.gi5.lint.wrapper.Constructor;
+import com.ensao.gi5.lint.wrapper.Method;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ClassRule extends Rule{
+
+ //Le constructeur par défaut
+ public ClassRule() {
+
+ super(Constantes.LINT_REG_005, Level.HIGHEST);
+ }
+
+ //La redéfinition de la méthode apply
+ @Override
+ public void apply(CompilationUnitWrapper compilationUnit) {
+
+ List classesList = new ArrayList<>();
+ compilationUnit.accept(new ClassVisitor(), classesList);
+
+
+ for(ClassWrapper classWrapper : classesList) {
+
+ //L'implémentation de la règle LINT_REG_011
+ if(classWrapper.getMethods().size() > 20) {
+
+ addViolation(ViolationFactory.createViolation(Constantes.LINT_REG_011, compilationUnit, classWrapper.getLine(), classWrapper.getName()));
+ }
+
+ //L'implémentation de la règle LINT_REG_012
+ for(Constructor constructor : classWrapper.getConstructors()) {
+
+ if(constructor.getParameters().size() > 2) {
+
+ addViolation(ViolationFactory.createViolation(Constantes.LINT_REG_012, compilationUnit, constructor.getLine(), constructor.getName()));
+ }
+ }
+
+ for(Method method : classWrapper.getMethods()) {
+
+ //Implémentation de la règle LINT_REG_008
+ if(method.getLinesCounts() > 30) {
+
+ addViolation(ViolationFactory.createViolation(Constantes.LINT_REG_008, compilationUnit, method.getLine(), method.getName(), classWrapper.getName()));
+ }
+
+ //Implémentation de la règle LINT_REG_014
+ if(method.getReturnCount() > 1 || method.getThrowCount() > 1) {
+
+ addViolation(ViolationFactory.createViolation(Constantes.LINT_REG_014, compilationUnit, method.getLine(), method.getName()));
+ }
+
+ }
+
+
+ }
+
+
+
+ }
+
+ @Override
+ public boolean isActive() {
+ return true;
+ }
+}
diff --git a/src/main/java/com/ensao/gi5/lint/rules/EnumerationRule.java b/src/main/java/com/ensao/gi5/lint/rules/EnumerationRule.java
new file mode 100644
index 0000000..6e416e1
--- /dev/null
+++ b/src/main/java/com/ensao/gi5/lint/rules/EnumerationRule.java
@@ -0,0 +1,38 @@
+package com.ensao.gi5.lint.rules;
+
+import com.ensao.gi5.lint.constantes.Constantes;
+import com.ensao.gi5.lint.util.ViolationFactory;
+import com.ensao.gi5.lint.visitor.EnumerationVisitor;
+import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
+import com.ensao.gi5.lint.wrapper.EnumerationWrapper;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class EnumerationRule extends Rule{
+
+ //Le constructeur par défaut
+ public EnumerationRule() {
+
+ super(Constantes.LINT_REG_007, Level.LOW);
+ }
+ @Override
+ public void apply(CompilationUnitWrapper compilationUnit) {
+
+ List enumerationWrapperList = new ArrayList<>();
+ compilationUnit.accept(new EnumerationVisitor(), enumerationWrapperList);
+
+ //L'implémentation de la règle LINT_REG_007
+ enumerationWrapperList.forEach(en -> en.getElementList().forEach(e -> {
+
+ if(!e.getName().matches("[A-Z_]+")){
+ addViolation(ViolationFactory.createViolation(this.getId(), compilationUnit, e.getLine()));
+ }
+ }));
+ }
+
+ @Override
+ public boolean isActive() {
+ return true;
+ }
+}
diff --git a/src/main/java/com/ensao/gi5/lint/rules/StatementRule.java b/src/main/java/com/ensao/gi5/lint/rules/StatementRule.java
new file mode 100644
index 0000000..be840cd
--- /dev/null
+++ b/src/main/java/com/ensao/gi5/lint/rules/StatementRule.java
@@ -0,0 +1,84 @@
+package com.ensao.gi5.lint.rules;
+
+import com.ensao.gi5.lint.constantes.Constantes;
+import com.ensao.gi5.lint.util.ViolationFactory;
+import com.ensao.gi5.lint.visitor.StatementVisitor;
+import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
+import com.ensao.gi5.lint.wrapper.StatementWrapper;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class StatementRule extends Rule{
+
+ //Les patterns
+ final static String IF = "([\\S\\s])*(\\{[\\S\\s]*\\})";
+ final static String ANONYMOUS = "new [\\s\\S]+\\{[\\s\\S]*\\}";
+ final static String ANY = "[\\S\\s]+";
+ final static String BOOLEAN = "(([\\w\\d\\s])+(==|!=|<|>|>=|<=)[\\w\\d\\s]+([&\\|]{2})?)+";
+ final static String CATCH = "(print|log)";
+
+ //Le constructeur par défaut
+ public StatementRule() {
+
+ super(Constantes.LINT_REG_006, Level.LOW);
+ }
+
+ //La redéfinition de la méthode apply
+ @Override
+ public void apply(CompilationUnitWrapper compilationUnitWrapper) {
+
+ Map> statements = new HashMap<>();
+ compilationUnitWrapper.accept(new StatementVisitor(), statements);
+
+ //L'implémentation de la méthode LINT_REG_006
+ Optional.ofNullable(statements.get(Constantes.LINT_REG_006)).ifPresent(stats -> {
+
+ for(StatementWrapper statementWrapper : stats) {
+
+ Matcher matcher = Pattern.compile(BOOLEAN).matcher(statementWrapper.getStatement().toString());
+
+ if(matcher.find()){
+ if(matcher.group().split("&&|\\|\\|").length>2) {
+ addViolation(ViolationFactory.createViolation(statementWrapper.getRuleId(), compilationUnitWrapper, statementWrapper.getLine()));
+ }
+ }
+ }
+ });
+
+ //L'implémentation de la règle LINT_REG_009
+ checkViolationPattern(Constantes.LINT_REG_009, statements, compilationUnitWrapper, ANONYMOUS, true);
+
+ //L'implémentation de la règle LINT_REG_010
+ checkViolationPattern(Constantes.LINT_REG_010, statements, compilationUnitWrapper, ANY, true);
+
+ //L'implémentation de la règle LINT_REG_015
+ checkViolationPattern(Constantes.LINT_REG_015, statements, compilationUnitWrapper, CATCH, true);
+
+ //L'implémentation de la règle LINT_REG_018
+ checkViolationPattern(Constantes.LINT_REG_018, statements, compilationUnitWrapper, IF, true);
+
+ }
+
+ //La méthode checkViolationPattern()
+ private void checkViolationPattern(String ruleId, Map> statements, CompilationUnitWrapper cu, String regex, boolean value){
+ Optional.ofNullable(statements.get(ruleId)).ifPresent(stmts ->{
+
+ for(StatementWrapper statementWrapper : stmts ){
+ if(Pattern.compile(regex).matcher(statementWrapper.getStatement().toString()).find() == value){
+ addViolation(ViolationFactory.createViolation(statementWrapper.getRuleId(), cu, statementWrapper.getLine()));
+ }
+ }
+ });
+ }
+
+ @Override
+ public boolean isActive() {
+ return true;
+ }
+
+}
diff --git a/src/main/java/com/ensao/gi5/lint/rules/TypesNameRule.java b/src/main/java/com/ensao/gi5/lint/rules/TypesNameRule.java
new file mode 100644
index 0000000..28a87e6
--- /dev/null
+++ b/src/main/java/com/ensao/gi5/lint/rules/TypesNameRule.java
@@ -0,0 +1,81 @@
+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.util.Utils;
+import com.ensao.gi5.lint.util.ViolationFactory;
+import com.ensao.gi5.lint.visitor.NameVisitors;
+import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
+import com.ensao.gi5.lint.wrapper.SimpleWrapper;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Pattern;
+
+public class TypesNameRule extends Rule{
+
+ //Le constructeur par défaut
+ public TypesNameRule() {
+
+ super(Constantes.LINT_REG_002, Level.HIGH);
+ }
+
+ //Implémentation de la première méthode abstraite
+ @Override
+ public void apply(CompilationUnitWrapper compilationUnit) {
+
+ List listVariables = new ArrayList<>();
+
+ compilationUnit.accept(new NameVisitors(), listVariables);
+
+ //Implémentation de la règle LINT_REG_002
+ listVariables.stream().filter(e -> e.isRuleOf(Constantes.LINT_REG_002)).forEach(var -> {
+ String variableName = var.getSimpleName().asString();
+
+ if(!Pattern.compile("^[A-Z].*").matcher(variableName).matches() || variableName.contains("_")){
+ final Violation v = ViolationFactory.createViolation(Constantes.LINT_REG_002, compilationUnit,
+ Utils.getLine(var.getSimpleName()), var.getSimpleName());
+ addViolation(v);
+ }
+ });
+
+ //Implémentation des deux règles LINT_REG_003 et LINT_REG_004
+ listVariables.stream().filter(e -> e.isRuleOf(Constantes.LINT_REG_003, Constantes.LINT_REG_004)).forEach(var -> {
+ if(!Character.isLowerCase(var.getSimpleName().asString().charAt(0))){
+ final Violation violation = ViolationFactory.createViolation(Constantes.LINT_REG_003, compilationUnit, Utils.getLine(var.getSimpleName()), var.getSimpleName());
+ addViolation(violation);
+ }
+ });
+
+ //Implémentation de la règle LINT_REG_005
+ listVariables.stream().filter(e -> e.isRuleOf(Constantes.LINT_REG_005)).forEach(var -> {
+
+ if(Pattern.compile("[^A-Z0-9_]").matcher(var.getSimpleName().asString()).find()) {
+
+ final Violation violation = ViolationFactory.createViolation(Constantes.LINT_REG_005, compilationUnit, Utils.getLine(var.getSimpleName()), var.getSimpleName());
+
+ addViolation(violation);
+ }
+ });
+
+ //Implémentation de la règle LINT_REG_013
+ listVariables.stream().filter(e -> e.isRuleOf(Constantes.LINT_REG_013)).forEach(var ->{
+
+ if(var.getAccess().isEmpty()){
+ final Violation violation = ViolationFactory.createViolation(Constantes.LINT_REG_013, compilationUnit, Utils.getLine(var.getSimpleName()), var.getSimpleName());
+ addViolation(violation);
+ }
+ });
+
+
+
+ }
+
+ //Implémentation de la seconde méthode abstraite
+ @Override
+ public boolean isActive() {
+
+ return true;
+ }
+
+}
diff --git a/src/main/java/com/ensao/gi5/lint/util/Utils.java b/src/main/java/com/ensao/gi5/lint/util/Utils.java
index 7f4eb61..9294eb6 100644
--- a/src/main/java/com/ensao/gi5/lint/util/Utils.java
+++ b/src/main/java/com/ensao/gi5/lint/util/Utils.java
@@ -4,6 +4,7 @@
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ast.CompilationUnit;
+import com.github.javaparser.ast.expr.SimpleName;
import org.apache.commons.io.FileUtils;
import java.io.File;
@@ -52,4 +53,9 @@ public static Collection getFilesFromDirectory(String directoryPath) {
public static Collection getFilesFromDirectory(File directory) {
return FileUtils.listFiles(directory, new String[]{"java"}, true);
}
+
+ public static int getLine(SimpleName simpleName) {
+
+ return simpleName.getBegin().map(l -> l.line).orElse(-1);
+ }
}
diff --git a/src/main/java/com/ensao/gi5/lint/util/ViolationFactory.java b/src/main/java/com/ensao/gi5/lint/util/ViolationFactory.java
new file mode 100644
index 0000000..62b3019
--- /dev/null
+++ b/src/main/java/com/ensao/gi5/lint/util/ViolationFactory.java
@@ -0,0 +1,29 @@
+package com.ensao.gi5.lint.util;
+
+import com.ensao.gi5.lint.rules.Level;
+import com.ensao.gi5.lint.rules.violations.Violation;
+import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
+
+import java.util.Objects;
+import java.util.ResourceBundle;
+
+public class ViolationFactory {
+
+ final static ResourceBundle levelBundles = ResourceBundle.getBundle("levelBundles");
+ final static ResourceBundle stringBundles = ResourceBundle.getBundle("stringBundles");
+
+ public static Violation createViolation(String ruleId, CompilationUnitWrapper compilationUnitWrapper, int line, Object... args) {
+
+ final Violation violation = new Violation();
+
+ violation.setDescription(String.format(stringBundles.getString(ruleId), args));
+ violation.setFileName(compilationUnitWrapper.getFileName());
+ violation.setLevel(Level.valueOf(levelBundles.getString("levelBundles")));
+ violation.setLine(line);
+ violation.setRuleId(ruleId);
+
+ return violation;
+ }
+
+
+}
diff --git a/src/main/java/com/ensao/gi5/lint/visitor/ClassVisitor.java b/src/main/java/com/ensao/gi5/lint/visitor/ClassVisitor.java
new file mode 100644
index 0000000..63d1101
--- /dev/null
+++ b/src/main/java/com/ensao/gi5/lint/visitor/ClassVisitor.java
@@ -0,0 +1,45 @@
+package com.ensao.gi5.lint.visitor;
+
+import com.ensao.gi5.lint.util.Utils;
+import com.ensao.gi5.lint.wrapper.ClassWrapper;
+import com.ensao.gi5.lint.wrapper.Constructor;
+import com.ensao.gi5.lint.wrapper.Method;
+import com.ensao.gi5.lint.wrapper.Parameter;
+import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
+
+import com.github.javaparser.ast.stmt.Statement;
+import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
+
+import java.util.List;
+
+public class ClassVisitor extends VoidVisitorAdapter> {
+
+ @Override
+ public void visit(ClassOrInterfaceDeclaration n, List arg) {
+ ClassWrapper classWrapper = new ClassWrapper(n.getNameAsString(), Utils.getLine(n.getName()));
+
+ n.getConstructors().forEach(c -> {
+ Constructor constructor = new Constructor(c.getNameAsString(), Utils.getLine(c.getName()));
+ c.getParameters().forEach(p -> {
+ constructor.getParameters().add(new Parameter(p.getTypeAsString(), p.getNameAsString()));
+ });
+ classWrapper.getConstructors().add(constructor);
+ });
+
+ n.getMethods().forEach(m ->{
+ Method method = new Method(m.getNameAsString(), m.getTypeAsString(), m.getBegin().map(p->p.line).orElse(-1));
+ m.getParameters().forEach(p -> method.getParameters().add(new Parameter(p.getTypeAsString(), p.getNameAsString())));
+ int lineCount = m.getEnd().map(p->p.line).orElse(0) - m.getBegin().map(p->p.line).orElse(0);
+ method.setLinesCount(lineCount);
+ m.getBody().ifPresent(e -> method.setReturnCount((int) e.getStatements().stream().filter(Statement::isReturnStmt).count()));
+ m.getBody().ifPresent(e -> method.setThrowCount((int) e.getStatements().stream().filter(Statement::isThrowStmt).count()));
+
+ classWrapper.getMethods().add(method);
+ });
+ arg.add(classWrapper);
+ super.visit(n, arg);
+ }
+
+
+
+}
diff --git a/src/main/java/com/ensao/gi5/lint/visitor/EnumerationVisitor.java b/src/main/java/com/ensao/gi5/lint/visitor/EnumerationVisitor.java
new file mode 100644
index 0000000..e3d0964
--- /dev/null
+++ b/src/main/java/com/ensao/gi5/lint/visitor/EnumerationVisitor.java
@@ -0,0 +1,22 @@
+package com.ensao.gi5.lint.visitor;
+
+import com.ensao.gi5.lint.rules.EnumerationRule;
+import com.ensao.gi5.lint.util.Utils;
+import com.ensao.gi5.lint.wrapper.Element;
+import com.ensao.gi5.lint.wrapper.EnumerationWrapper;
+import com.github.javaparser.ast.body.EnumDeclaration;
+import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class EnumerationVisitor extends VoidVisitorAdapter> {
+
+ @Override
+ public void visit(EnumDeclaration enumDeclaration, List arg) {
+ arg.add(new EnumerationWrapper(enumDeclaration.getNameAsString(), enumDeclaration.getEntries().stream()
+ .map(e -> new Element(e.getNameAsString(), Utils.getLine(e.getName()))).collect(Collectors.toList())));
+ super.visit(enumDeclaration, arg);
+ }
+
+}
diff --git a/src/main/java/com/ensao/gi5/lint/visitor/NameVisitors.java b/src/main/java/com/ensao/gi5/lint/visitor/NameVisitors.java
new file mode 100644
index 0000000..eee10dd
--- /dev/null
+++ b/src/main/java/com/ensao/gi5/lint/visitor/NameVisitors.java
@@ -0,0 +1,30 @@
+package com.ensao.gi5.lint.visitor;
+
+import com.ensao.gi5.lint.constantes.Constantes;
+import com.ensao.gi5.lint.wrapper.SimpleWrapper;
+import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
+import com.github.javaparser.ast.expr.VariableDeclarationExpr;
+import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
+
+import java.util.List;
+
+public class NameVisitors extends VoidVisitorAdapter> {
+
+ @Override
+ public void visit(ClassOrInterfaceDeclaration classOrInterfaceDeclaration, List arg) {
+
+ arg.add(new SimpleWrapper(classOrInterfaceDeclaration.getName(), Constantes.LINT_REG_002));
+ super.visit(classOrInterfaceDeclaration, arg);
+ }
+
+ @Override
+ public void visit(VariableDeclarationExpr variableDeclarationExpr, List arg) {
+
+ variableDeclarationExpr.getVariables().forEach(variable -> arg.add(new SimpleWrapper(variable.getName(), Constantes.LINT_REG_003)));
+
+ super.visit(variableDeclarationExpr, arg);
+ }
+
+
+
+}
diff --git a/src/main/java/com/ensao/gi5/lint/visitor/StatementVisitor.java b/src/main/java/com/ensao/gi5/lint/visitor/StatementVisitor.java
new file mode 100644
index 0000000..2126f89
--- /dev/null
+++ b/src/main/java/com/ensao/gi5/lint/visitor/StatementVisitor.java
@@ -0,0 +1,44 @@
+package com.ensao.gi5.lint.visitor;
+
+import com.ensao.gi5.lint.constantes.Constantes;
+import com.ensao.gi5.lint.wrapper.StatementWrapper;
+import com.github.javaparser.ast.expr.LambdaExpr;
+import com.github.javaparser.ast.stmt.BlockStmt;
+import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+public class StatementVisitor extends VoidVisitorAdapter