-
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
JAAOUAN TAHA #7
Open
Taha-ja
wants to merge
10
commits into
BelmoMusta:develop
Choose a base branch
from
Taha-ja:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
JAAOUAN TAHA #7
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bd96e23
rules from 2 to 5 and 8
Taha-ja b1727cf
rules 11,12,13,17,18
Taha-ja b3e03f1
rules 7 and 9
Taha-ja a2e18d5
rule 10
Taha-ja 5e7f3e1
rule 14
Taha-ja 59260ff
rule 15
Taha-ja 64c7a42
code refactoring
Taha-ja dda1238
adding unit tests
Taha-ja b4befb9
update & doc
Taha-ja f52bb17
update the readme file
Taha-ja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
* 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){ | ||
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. use |
||
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
45
src/main/java/com/ensao/gi5/lint/rules/AttributesRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/ensao/gi5/lint/rules/AttributesVisibilityRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
src/main/java/com/ensao/gi5/lint/rules/CatchExceptionRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
+1 for the good javadoc