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

Ayoub HAJJI - Said OUBAASSINE #11

Open
wants to merge 1 commit 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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>


Expand Down
49 changes: 49 additions & 0 deletions src/main/java/com/ensao/gi5/lint/constantes/Constantes.java
Original file line number Diff line number Diff line change
@@ -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");
}
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/ClassRule.java
Original file line number Diff line number Diff line change
@@ -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<ClassWrapper> 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;
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/EnumerationRule.java
Original file line number Diff line number Diff line change
@@ -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<EnumerationWrapper> 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;
}
}
84 changes: 84 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/StatementRule.java
Original file line number Diff line number Diff line change
@@ -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<String, List<StatementWrapper>> 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<String, List<StatementWrapper>> 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;
}

}
81 changes: 81 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/TypesNameRule.java
Original file line number Diff line number Diff line change
@@ -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<SimpleWrapper> 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;
}

}
6 changes: 6 additions & 0 deletions src/main/java/com/ensao/gi5/lint/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,4 +53,9 @@ public static Collection<File> getFilesFromDirectory(String directoryPath) {
public static Collection<File> 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);
}
}
Loading