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

Mini_Projet [DERRAZ_Ouissal] & [ASBAI_Halima] #24

Open
wants to merge 4 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
26 changes: 13 additions & 13 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## java linter
Le but de ce mini-projet est de créer un linter sous java.
Ce mini-projet est un linter sous java crée par [DERRAZ_Ouissal] et [ASBAI Halima].
Définition du linter : [Wikipedia](https://en.wikipedia.org/wiki/Lint_%28software%29)

#### Travail à faire
Expand All @@ -10,23 +10,23 @@ Implémenter les règles suivantes :
|--|--|--|
| LINT_REG_000 |BLOCKER|Les erreurs de syntaxe ne sont pas autorisées|
| LINT_REG_001 |MEDIUM |Les imports non utilisés sont à supprimer |
| LINT_REG_002 |HIGHEST|Les types java `(class, interface, annotation, enum)` commence par une majuscule, elles ne doivent pas avoir des sous-tirets `_` |
| ~~LINT_REG_002~~|HIGHEST|Les types java `(class, interface, annotation, enum)` commence par une majuscule, elles ne doivent pas avoir des sous-tirets `_` |
| 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_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_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 |
| INT_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
8 changes: 8 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,14 @@
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_007 = "LINT_REG_007";
public static final String LINT_REG_011 = "LINT_REG_011";
public static final String LINT_REG_012 = "LINT_REG_012";



private Constantes() {
throw new IllegalStateException("not to be instantiated");
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/AttributesRule.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.rules.violations.Violation;
import com.ensao.gi5.lint.visitor.LowerCaseVisitors;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.ensao.gi5.lint.wrapper.LowerCaseWrapper;

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

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

public void apply(CompilationUnitWrapper compilationUnit) {
final Set<LowerCaseWrapper> fieldNames = new LinkedHashSet<>();
compilationUnit.accept(new LowerCaseVisitors(), fieldNames);
for (LowerCaseWrapper fieldName : fieldNames) {
if (!fieldName.getFieldName().matches("^[a-z].*")) {
final Violation violation = new Violation();
violation.setDescription("attribute name must start with lower case '" + fieldName.getFieldName() + "'");
violation.setFileName(compilationUnit.getFileName());
violation.setLine(fieldName.getLine());
addViolation(violation);
}
}

}



@Override
public boolean isActive() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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.ConstanteUpperVisitor;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.ensao.gi5.lint.wrapper.LowUpperWrapper;

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

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

@Override
public void apply(CompilationUnitWrapper compilationUnit) {
final Set<LowUpperWrapper> fieldNames = new LinkedHashSet<>();
compilationUnit.accept(new ConstanteUpperVisitor(), fieldNames);
for (LowUpperWrapper fieldName : fieldNames) {
if (!fieldName.getFieldName().matches("^[A-Z_]*")) {
final Violation violation = new Violation();
violation.setDescription("constant name must be in upper case and concatenated with (_) '" + fieldName.getFieldName()+ "'");
violation.setFileName(compilationUnit.getFileName());
violation.setLine(fieldName.getLine());
addViolation(violation);
}
}

}

@Override
public boolean isActive() {
return true;
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/ConstructorRule.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.util.Constructor;
import com.ensao.gi5.lint.visitor.ClassVisitor;
import com.ensao.gi5.lint.wrapper.ClassWrapper;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;

import java.util.ArrayList;
import java.util.List;

public class ConstructorRule extends Rule{
public ConstructorRule(){
super(Constantes.LINT_REG_012, Level.HIGHEST);
}

@Override
public void apply(CompilationUnitWrapper compilationUnit) {
List<ClassWrapper> classes = new ArrayList<>();
compilationUnit.accept(new ClassVisitor(), classes);

for (ClassWrapper classe : classes) {
for (Constructor constructor : classe.getConstructors()) {
if (constructor.getParameters().size() > 2) {
final Violation violation = new Violation();
violation.setDescription("Parameters of the constructor should be less than 2");
violation.setFileName(compilationUnit.getFileName());
violation.setLine(classe.getLine());
addViolation(violation);
}

}
}
}

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

}
37 changes: 37 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/EnumRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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.EnumVisitors;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.ensao.gi5.lint.wrapper.LowUpperWrapper;

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

public class EnumRule extends Rule {
public EnumRule(){
super(Constantes.LINT_REG_007, Level.LOW);

}
@Override
public void apply(CompilationUnitWrapper compilationUnit) {
final Set<LowUpperWrapper> fieldNames = new LinkedHashSet<>();
compilationUnit.accept(new EnumVisitors(), fieldNames);
for (LowUpperWrapper fieldName : fieldNames) {
if (!fieldName.getFieldName().matches("[A-Z]*")) {
final Violation violation = new Violation();
violation.setDescription("elements of the enumeration must be in uppercase '" + fieldName.getFieldName()+ "'");
violation.setFileName(compilationUnit.getFileName());
violation.setLine(fieldName.getLine());
addViolation(violation);
}
}

}

@Override
public boolean isActive() {
return true;
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/LowerCaseRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.ensao.gi5.lint.rules;

import java.util.LinkedHashSet;
import java.util.Set;
import com.ensao.gi5.lint.constantes.Constantes;
import com.ensao.gi5.lint.rules.violations.Violation;
import com.ensao.gi5.lint.visitor.LowerCaseVisitors;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.ensao.gi5.lint.wrapper.LowerCaseWrapper;
public class LowerCaseRule extends Rule {
public LowerCaseRule() {
super(Constantes.LINT_REG_003, Level.HIGH);
}
@Override
public void apply(CompilationUnitWrapper compilationUnitWrapper) {
final Set<LowerCaseWrapper> fieldNames = new LinkedHashSet<>();
compilationUnitWrapper.accept(new LowerCaseVisitors(), fieldNames);
for (LowerCaseWrapper fieldName : fieldNames) {
if (!fieldName.getFieldName().matches("^[\t*a-z].*")) {
final Violation violation = new Violation();
violation.setDescription("variable name starts with upper case '" + fieldName.getFieldName()+ "'");
violation.setFileName(compilationUnitWrapper.getFileName());
violation.setLine(fieldName.getLine());
addViolation(violation);
}
}

}

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



}
38 changes: 38 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/MethodRule.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.rules.violations.Violation;
import com.ensao.gi5.lint.visitor.ClassVisitor;
import com.ensao.gi5.lint.wrapper.ClassWrapper;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;

import java.util.ArrayList;
import java.util.List;

public class MethodRule extends Rule {
public MethodRule(){
super(Constantes.LINT_REG_011, Level.HIGHEST);

}
@Override
public void apply(CompilationUnitWrapper compilationUnitWrapper) {
List<ClassWrapper> classes = new ArrayList<>();
compilationUnitWrapper.accept(new ClassVisitor(), classes);

for(ClassWrapper classe : classes) {
if (classe.getMethods().size() > 20) {
final Violation violation = new Violation();
violation.setDescription("Methods of the classe should be less than 20 " );
violation.setFileName(compilationUnitWrapper.getFileName());
violation.setLine(classe.getLine());
addViolation(violation);
}
}

}

@Override
public boolean isActive() {
return true;
}
}
9 changes: 8 additions & 1 deletion src/main/java/com/ensao/gi5/lint/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.ensao.gi5.lint.Linter;
import com.ensao.gi5.lint.printer.ConsolePrinter;
import com.ensao.gi5.lint.rules.UnusedImportsRule;
import com.ensao.gi5.lint.rules.*;

public class Runner {
public static void main(String[] args) {
Expand All @@ -25,6 +25,13 @@ public static void main(String[] args) {
}
final Linter linter = new Linter();
linter.registerRule(new UnusedImportsRule());
linter.registerRule(new AttributesRule());
linter.registerRule(new ConstantesUpperCaseRule());
linter.registerRule(new ConstructorRule());
linter.registerRule(new EnumRule());
linter.registerRule(new LowerCaseRule());
linter.registerRule(new LowerCaseRule());

linter.registerPrinter(new ConsolePrinter());
linter.registerSource(directory);
linter.run();
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/ensao/gi5/lint/util/Constructor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.ensao.gi5.lint.util;


import java.util.HashSet;
import java.util.Set;

public class Constructor {
public Set<Parameter> getParameters() {
return parameters;
}

public int getLine() {
return line;
}

public String getName() {
return name;
}

final private Set<Parameter> parameters;
final private int line;
final private String name;

public Constructor(String name, int line) {
this.name = name;
this.parameters = new HashSet<>();
this.line = line;
}

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

public class Method {
final private String name;
private int linesCount;

public Method(String name){
this.linesCount = 0;
this.name = name;

}

public void setLinesCount(int linesCounts) {
this.linesCount = linesCounts;
}

public int getLinesCount() {
return linesCount;
}

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

public class Parameter {
public String getType() {
return type;
}

public String getName() {
return name;
}

final private String type;
final private String name;

public Parameter(String type, String name) {
this.type = type;
this.name = name;
}
}
Loading