-
Notifications
You must be signed in to change notification settings - Fork 37
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
Boutahar Naoual #29
base: develop
Are you sure you want to change the base?
Boutahar Naoual #29
Changes from all commits
8affc49
bc0895e
8838b5e
418c8da
97587bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.wrapper.CompilationUnitWrapper; | ||
import com.github.javaparser.ast.body.FieldDeclaration; | ||
import com.github.javaparser.ast.body.TypeDeclaration; | ||
|
||
import java.util.List; | ||
|
||
public class AttributesNamingRule extends Rule{ | ||
|
||
protected AttributesNamingRule(String id, Level level) { | ||
super(Constantes.LINT_REG_004, Level.HIGH); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
final List<TypeDeclaration<?>> types = compilationUnit.getTypes(); | ||
types.stream() | ||
.forEach(type -> { | ||
type.getMembers().stream() | ||
.filter(member -> member instanceof FieldDeclaration && !((FieldDeclaration) member).isFinal()) | ||
.map(member -> (FieldDeclaration) member) | ||
.forEach(field -> { | ||
field.getVariables().stream() | ||
.filter(variable -> !Character.isLowerCase(variable.getNameAsString().charAt(0))) | ||
.forEach(variable -> { | ||
String message = "attribute " + variable.getNameAsString() + " must start with lowercase letter."; | ||
final Violation violation = new Violation(); | ||
violation.setDescription(message); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
violation.setLine(field.getBegin().get().line); | ||
addViolation(violation); | ||
|
||
}); | ||
}); | ||
}); | ||
} | ||
|
||
|
||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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.wrapper.CompilationUnitWrapper; | ||
import com.github.javaparser.ast.body.TypeDeclaration; | ||
|
||
import java.util.List; | ||
|
||
public class ConstantsNamingRule extends Rule{ | ||
protected ConstantsNamingRule() { | ||
super(Constantes.LINT_REG_005, Level.MEDIUM); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
final List<TypeDeclaration<?>> types = compilationUnit.getTypes(); | ||
|
||
types.stream() | ||
.forEach(type -> { | ||
type.getFields().stream() | ||
.filter(field -> field.isStatic() && field.isFinal() && !field.getVariables().get(0).getNameAsString().matches("^[A-Z_]+$")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. get(0) has to be generalized, private String foo, bar; |
||
.forEach(field -> { | ||
String message = "The constant " + field.getVariables().get(0).getNameAsString() + " must be in uppercase and separated with _ ."; | ||
final Violation violation = new Violation(); | ||
violation.setDescription(message); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
violation.setLine(type.getName().getBegin().get().line); | ||
addViolation(violation); | ||
}); | ||
}); | ||
|
||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
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.wrapper.CompilationUnitWrapper; | ||
import com.github.javaparser.ast.body.MethodDeclaration; | ||
import com.github.javaparser.ast.body.TypeDeclaration; | ||
import com.github.javaparser.ast.body.VariableDeclarator; | ||
|
||
import java.util.List; | ||
|
||
public class LocalVariablesNamingRule extends Rule { | ||
protected LocalVariablesNamingRule() { | ||
super(Constantes.LINT_REG_003, Level.HIGH); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
final List<TypeDeclaration<?>> types = compilationUnit.getTypes(); | ||
types.stream() | ||
.forEach(type -> { | ||
type.findAll(MethodDeclaration.class) | ||
.stream() | ||
.forEach(method -> { | ||
method.findAll(VariableDeclarator.class) | ||
.stream() | ||
.filter(var -> !Character.isLowerCase(var.getNameAsString().charAt(0))) | ||
.forEach(var -> { | ||
String message = "The local variable " + var.getNameAsString() + " must start with lowercase letter."; | ||
final Violation violation = new Violation(); | ||
violation.setDescription(message); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
violation.setLine(var.getName().getBegin().get().line); | ||
addViolation(violation); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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.wrapper.CompilationUnitWrapper; | ||
import com.github.javaparser.ast.body.MethodDeclaration; | ||
import com.github.javaparser.ast.body.TypeDeclaration; | ||
|
||
import java.util.List; | ||
|
||
public class MethodLinesRule extends Rule{ | ||
protected MethodLinesRule(String id, Level level) { | ||
super(Constantes.LINT_REG_008, Level.HIGHEST); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnitWrapper) { | ||
List<TypeDeclaration<?>> types = compilationUnitWrapper.getTypes(); | ||
for (TypeDeclaration<?> type : types) { | ||
List<MethodDeclaration> methods = type.getMethods(); | ||
for (MethodDeclaration method : methods) { | ||
int lineCount = method.getEnd().get().line - method.getBegin().get().line; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optinal::get is to be called conditionally |
||
if (lineCount > 30) { | ||
String message = "The method " + method.getNameAsString() + " has a body that exceeds 30 lines."; | ||
final Violation violation = new Violation(); | ||
violation.setDescription(message); | ||
violation.setFileName(compilationUnitWrapper.getFileName()); | ||
violation.setLine(method.getBegin().get().line); | ||
addViolation(violation); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
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.wrapper.CompilationUnitWrapper; | ||
import com.github.javaparser.ast.body.TypeDeclaration; | ||
|
||
import java.util.List; | ||
|
||
public class MethodsNumberRule extends Rule{ | ||
protected MethodsNumberRule(String id, Level level) { | ||
super(Constantes.LINT_REG_011, Level.HIGHEST); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnitWrapper) { | ||
List<TypeDeclaration<?>> types = compilationUnitWrapper.getTypes(); | ||
for (TypeDeclaration<?> type : types) { | ||
int methodCount = (int) type.getMethods().stream() | ||
.filter(bodyDeclaration -> bodyDeclaration.isMethodDeclaration()) | ||
.count(); | ||
if (methodCount > 20) { | ||
String message = "The class " + type.getNameAsString() + " has more than 20 methods declared."; | ||
final Violation violation = new Violation(); | ||
violation.setDescription(message); | ||
violation.setFileName(compilationUnitWrapper.getFileName()); | ||
violation.setLine(type.getName().getBegin().get().line); | ||
addViolation(violation); | ||
} | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.ensao.gi5.lint.rules; | ||
|
||
import com.ensao.gi5.lint.rules.violations.Violation; | ||
import com.ensao.gi5.lint.constantes.Constantes; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
import com.github.javaparser.ast.body.TypeDeclaration; | ||
import java.util.List; | ||
|
||
public class TypesNamingRule extends Rule{ | ||
|
||
protected TypesNamingRule() { | ||
super(Constantes.LINT_REG_002, Level.HIGHEST); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
// Obtenir tous les types déclarés dans le fichier | ||
final List<TypeDeclaration<?>> types = compilationUnit.getTypes(); | ||
|
||
types.stream() | ||
.filter(type -> !Character.isUpperCase(type.getNameAsString().charAt(0)) || type.getNameAsString().contains("_")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. !Character.isUpperCase ==> Character.isLowerCase |
||
.forEach(type -> { | ||
String message = ""; | ||
if (!Character.isUpperCase(type.getNameAsString().charAt(0))) { | ||
message += "NAme of Type " + type.getNameAsString() + " must start with uppercase letter."; | ||
} | ||
if (type.getNameAsString().contains("_")) { | ||
message += " Le nom du type " + type.getNameAsString() + " ne doit pas contenir de tirets bas."; | ||
} | ||
final Violation violation = new Violation(); | ||
violation.setDescription(message); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
violation.setLine(type.getName().getBegin().get().line); | ||
addViolation(violation); | ||
}); | ||
|
||
|
||
|
||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.ensao.gi5.lint.wrapper; | ||
|
||
import java.lang.reflect.Modifier; | ||
import com.github.javaparser.ast.body.TypeDeclaration; | ||
|
||
public class TypeDeclarationWrapper { | ||
private TypeDeclaration typeDeclaration; | ||
private String fileName; | ||
|
||
public TypeDeclarationWrapper(TypeDeclaration typeDeclaration, String fileName) { | ||
this.typeDeclaration = typeDeclaration; | ||
this.fileName = fileName; | ||
} | ||
|
||
public String getName() { | ||
return typeDeclaration.getName().asString(); | ||
} | ||
|
||
public String getFileName() { | ||
return fileName; | ||
} | ||
|
||
|
||
|
||
public boolean isPrivate() { | ||
return typeDeclaration.getModifiers().contains(Modifier.PRIVATE); | ||
} | ||
|
||
public boolean isProtected() { | ||
return typeDeclaration.getModifiers().contains(Modifier.PROTECTED); | ||
} | ||
|
||
public boolean isPublic() { | ||
return typeDeclaration.getModifiers().contains(Modifier.PUBLIC); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use visitor pattern to avoid going through the structure