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

JAAOUAN TAHA #7

Open
wants to merge 10 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
36 changes: 17 additions & 19 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,23 @@ Implémenter les règles suivantes :

| Identifiant |Niveau | Description |
|--|--|--|
| 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_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 |
| <div style="color:green">LINT_REG_000 </div>|BLOCKER|Les erreurs de syntaxe ne sont pas autorisées|
| <span style="color:green">LINT_REG_001 </span> |MEDIUM |Les imports non utilisés sont à supprimer |
| <span style="color:green">LINT_REG_002 </span> |HIGHEST|Les types java `(class, interface, annotation, enum)` commence par une majuscule, elles ne doivent pas avoir des sous-tirets `_` |
| <span style="color:green">LINT_REG_003 </span> |HIGH|Les variables locales commencent par une minuscule |
| <span style="color:green">LINT_REG_004 </span> |HIGH|Les attributs d'une classe commencent par une minuscule |
| <span style="color:green">LINT_REG_005 </span> |MEDIUM|Les constantes d'une classe sont écrites en majuscule, avec des `_` comme séparateur|
| <span style="color:green">LINT_REG_007 </span> |LOW|Les éléments d'une énumeration sont en majuscule, avec des `_` comme séparateur|
| <span style="color:green">LINT_REG_008 </span> |HIGHEST|Le corps d'une méthode ne doit pas dépasser `30` lignes|
| <span style="color:green">LINT_REG_009 </span> |HIGH|Les instanciations anonymes sont à remplacer par des expressions `lambda`|
| <span style="color:green">LINT_REG_010 </span> |MEDIUM|Les expressions `lambda` intuitives sont à remplacer par `method reference` |
| <span style="color:green">LINT_REG_011 </span> |HIGHEST|Le nombre de méthodes ne doit pas dépasser `20` méthodes déclarées par classe|
| <span style="color:green">LINT_REG_012 </span> |HIGHEST|Le nombre de paramètres d'une méthode/constructeur ne doit dépasser `2`|
| <span style="color:green">LINT_REG_013 </span> |HIGHEST|Les attributs de classe doivent avoir une visibilité déclarée|
| <span style="color:green">LINT_REG_014 </span> |LOW|Préférer l'utilisation d'une seule instruction de sortie `(return, throw)` dans les méthodes|
| <span style="color:green">LINT_REG_015 </span>|LOW|Ne pas catcher les exceptions sans les logger|
| <span style="color:green">LINT_REG_017 </span>|MEDIUM|Les méthodes privées non utilisées sont à supprimer|
| <span style="color:green">LINT_REG_018 </span> |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 pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
Expand Down
17 changes: 17 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,23 @@
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_002 = "LINT_REG_002";
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_007 = "LINT_REG_007";
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_011 = "LINT_REG_011";
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_014 = "LINT_REG_014";
public static final String LINT_REG_015 = "LINT_REG_015";
public static final String LINT_REG_016 = "LINT_REG_016";
public static final String LINT_REG_017 = "LINT_REG_017";
public static final String LINT_REG_018 = "LINT_REG_018";

private Constantes() {
throw new IllegalStateException("not to be instantiated");
Expand Down
42 changes: 42 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,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.rules.violations.ViolationMaker;
import com.ensao.gi5.lint.visitor.AnonymousVisitor;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import java.util.Hashtable;

/**
* AnonymousRule is a Java class that defines a rule for linting of Java code.
Copy link
Owner

Choose a reason for hiding this comment

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

+1 for the good javadoc

* The rule checks for the use of anonymous inner classes in the code, and if found, generates violations.
* The rule is enforced by the AnonymousVisitor, which uses a Hashtable to keep track of anonymous class instantiations.
* The visitor has a Hashtable field to store information about anonymous class instantiations,
* which it finds by checking for the presence of an anonymous class body in each ObjectCreationExpr node.
* When an anonymous class body is found, the visitor adds the class body's string representation and the line number of the instantiation to the Hashtable.
* The isActive method is overriding the parent class method, returns always true, it means that this rule is always active.
*/
public class AnonymousRule extends Rule{
public AnonymousRule() {
super(Constantes.LINT_REG_009, Level.HIGH);
}
@Override
public void apply(CompilationUnitWrapper compilationUnit) {

Hashtable<String,Integer> anonymousInstanciations = new Hashtable<>();
compilationUnit.accept(new AnonymousVisitor(anonymousInstanciations), null);
if(anonymousInstanciations.size()!=0){
Copy link
Owner

Choose a reason for hiding this comment

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

use !isEmpty

for (String miss : anonymousInstanciations.keySet()) {
Violation violation=ViolationMaker.makeViolation(compilationUnit.getFileName()
,"Anonymous inner class found please use a lambda expression instead."
, anonymousInstanciations.get(miss));
addViolation(violation);
}
}
}

@Override
public boolean isActive() {
return true;
}
}
45 changes: 45 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,45 @@
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.rules.violations.ViolationMaker;
import com.ensao.gi5.lint.visitor.AttributeVisitor;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.ensao.gi5.lint.wrapper.RuleWrapper;
import java.util.ArrayList;
import java.util.List;
/**
* AttributesRule is a Java class that defines a rule for linting of Java code.
* It is a specific rule for analyzing attributes of a Java class.
* It use an AttributeVisitor instance that iterates through each field and checks if the name starts with an uppercase character
* and if the field is not final. If those conditions are met, it adds a new RuleWrapper object to the list of attributes
* with the field name and line number as the arguments. the rule then checks the size of the attributes list,
* if it is not empty, for each attribute in the list, it creates a Violation object with the file name,
* the error message, and the line number of the attribute and add it to the list of violations.
* The isActive method is overriding the parent class method, returns always true, it means that this rule is always active.
* */
public class AttributesRule extends Rule{
public AttributesRule() {
super(Constantes.LINT_REG_004, Level.HIGH);
}
@Override
public void apply(CompilationUnitWrapper compilationUnit) {
List<RuleWrapper> attributes = new ArrayList<>();
compilationUnit.accept(new AttributeVisitor(attributes),null);
if(attributes.size()!=0){
for(RuleWrapper attribute:attributes){
Violation violation =ViolationMaker.makeViolation(
compilationUnit.getFileName(),
"Attributes error",
attribute.getLine()
);
addViolation(violation);
}
}
}

@Override
public boolean isActive() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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.rules.violations.ViolationMaker;
import com.ensao.gi5.lint.visitor.AttributeVisitor;
import com.ensao.gi5.lint.visitor.AttributesVisibilityVisitor;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.ensao.gi5.lint.wrapper.RuleWrapper;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.FieldDeclaration;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* AttributesVisibilityRule is a Java class that defines a rule for linting of Java code.
* It is a specific rule for analyzing the visibility of attributes in a Java class.
* The linter is made up of two classes: AttributesVisibilityVisitor and AttributesVisibilityRule.
* The apply method checks for the visibility of attributes by using an instance of AttributesVisibilityVisitor to visit
* the CompilationUnitWrapper and adding any violations to a list of violations if the size of attributes list is != 0
* The AttributesVisibilityVisitor cheks all field declarations in a class if the field have no modifier(visibility) the name and line
* of the field are added to a list of RuleWrapper that will be returned by the visitor.
* The isActive method is overriding the parent class method, returns always true, it means that this rule is always active.
* */
public class AttributesVisibilityRule extends Rule{
public AttributesVisibilityRule() {
super(Constantes.LINT_REG_013, Level.HIGHEST);
}
@Override
public void apply(CompilationUnitWrapper compilationUnit) {
List<RuleWrapper> attributes = new ArrayList<>();
compilationUnit.accept(new AttributesVisibilityVisitor(attributes),null);
if(attributes.size()!=0){
for(RuleWrapper attribute:attributes){
Violation violation = ViolationMaker.makeViolation(
compilationUnit.getFileName(),
"Attribute visibility warning",
attribute.getLine()
);
addViolation(violation);
}
}
}

@Override
public boolean isActive() {
return true;
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/CatchExceptionRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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.rules.violations.ViolationMaker;
import com.ensao.gi5.lint.visitor.CatchExceptionVisitor;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.ensao.gi5.lint.wrapper.RuleWrapper;
import java.util.ArrayList;
import java.util.List;
/**
* This code defines a rule class called "CatchExceptionRule" that extends the "Rule" class.
* The "apply" method takes a "CompilationUnitWrapper" object and uses it to check for caught exceptions using the "CatchExceptionVisitor" visitor class.
* The "CatchExceptionVisitor" check for CatchClause in the method, for each catchClause it checks whether
* the catch block contains log statement or not, if it doesn't, it adds a new "RuleWrapper" object to the a list,
* with the name of the exception and the line number of the catch.
* The "apply" method iterates the list of caught exceptions and creates a new "Violation" object for each exception
* with the name of the file, a message, and the line number of the exception as arguments. It then adds the violation
* to the list of violations.
* The isActive method is overriding the parent class method, returns always true, it means that this rule is always active.
* */
public class CatchExceptionRule extends Rule{
public CatchExceptionRule() {
super(Constantes.LINT_REG_015, Level.LOW);
}
@Override
public void apply(CompilationUnitWrapper compilationUnit) {
List<RuleWrapper> catchExceptions = new ArrayList<>();
compilationUnit.accept(new CatchExceptionVisitor(catchExceptions),null);
for (RuleWrapper catchException: catchExceptions) {
Violation violation = ViolationMaker.makeViolation(
compilationUnit.getFileName(),
"exception catched without log ",
catchException.getLine()
);
addViolation(violation);
}
}
@Override
public boolean isActive() {
return true;
}
}
54 changes: 54 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/ClassNameRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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.rules.violations.ViolationMaker;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.ensao.gi5.lint.wrapper.RuleWrapper;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.TypeDeclaration;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* This code defines a rule class called "ClassNameRule" that extends the "Rule" class.
* The "apply" method takes a "CompilationUnitWrapper" object and uses it to check for the name of all classes in the code.
* The method iterates over all the types in the compilation unit,
* it checks whether the first letter of the type's name is uppercase and if the name contains an underscore "_",
* if either of these conditions is true, it creates a new "Violation" object and sets its description,
* file name, and line number as arguments and then add this violation to the list of violations.
* The isActive method is overriding the parent class method, returns always true, it means that this rule is always active.
* */
public class ClassNameRule extends Rule {

public ClassNameRule() {
super(Constantes.LINT_REG_002, Level.HIGHEST);
}
@Override
public void apply(CompilationUnitWrapper compilationUnit) {
try{
CompilationUnit unit = compilationUnit.getParser();
for (TypeDeclaration<?> type : unit.getTypes()
){
String typeName =type.getNameAsString();
if(!Character.isUpperCase(typeName.charAt(0)) || typeName.contains("_")){
final Violation violation = new Violation();
violation.setDescription("Naming error");
violation.setFileName(compilationUnit.getFileName());
int line =type.getRange().isPresent()?type.getRange().get().begin.line:0;
violation.setLine(line);
addViolation(violation);
}

}
}
catch (Exception e){
}
}

@Override
public boolean isActive() {
return true;
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/ConstantsRule.java
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.rules.violations.ViolationMaker;
import com.ensao.gi5.lint.visitor.ConstantVisitor;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.ensao.gi5.lint.wrapper.RuleWrapper;
import java.util.ArrayList;
import java.util.List;
/**
* This code defines a rule class called "ConstantsRule" that extends the "Rule" class.
* The "apply" method takes a "CompilationUnitWrapper" object and uses it to check for constants in the code using the "ConstantVisitor" visitor class.
* The method creates a new empty list called "constantNaming" and passes it as an argument to the "ConstantVisitor" class,
* then it checks the size of the list of constants, if the size is not equal to 0, it iterates over the list of constants
* and creates a new "Violation" object for each constant with the name of the file, a message, and the line number of the constant
* as arguments. It then adds the violation to the list of violations.
* The "ConstantVisitor" check if they are final constants. If a FieldDeclaration is a final constant,
* it checks the naming of the constant and adds a RuleWrapper object to a list of RuleWrappers if the naming does not follow
* certain conventions (starts with lowercase or does not contain an underscore).
* The isActive method is overriding the parent class method, returns always true, it means that this rule is always active.
* */
public class ConstantsRule extends Rule{
public ConstantsRule() {
super(Constantes.LINT_REG_005, Level.MEDIUM);
}
@Override
public void apply(CompilationUnitWrapper compilationUnit) {
List<RuleWrapper> constantNaming =new ArrayList<>();
compilationUnit.accept(new ConstantVisitor(constantNaming), null);
if(constantNaming.size()!=0){
for(RuleWrapper constant:constantNaming){
Violation violation = ViolationMaker.makeViolation(
compilationUnit.getFileName(),
"Constant naming error",
constant.getLine()
);
addViolation(violation);
}
}
}

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