-
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
MbarkiPr #13
base: develop
Are you sure you want to change the base?
MbarkiPr #13
Changes from all commits
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 |
---|---|---|
|
@@ -46,6 +46,7 @@ public void run() { | |
final List<CompilationUnitWrapper> compilationUnitWrappers = new ArrayList<>(); | ||
for (File source : sources) { | ||
compilationUnitWrappers.add(Utils.getCompilationUnit(source)); | ||
System.out.println(Utils.getCompilationUnit(source)); | ||
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. to be removed |
||
} | ||
|
||
for (CompilationUnitWrapper compilationUnit : compilationUnitWrappers) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.ensao.gi5.lint.printer; | ||
|
||
import com.ensao.gi5.lint.rules.violations.Violation; | ||
import org.apache.commons.csv.CSVFormat; | ||
import org.apache.commons.csv.CSVPrinter; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.Collection; | ||
|
||
public class CSVFileWriter implements Printer { | ||
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. 👍🏽 |
||
private static final String NEW_LINE_SEPARATOR = "\n"; | ||
|
||
@Override | ||
public void printViolations(Collection<Violation> violations) { | ||
try ( | ||
BufferedWriter writer = Files.newBufferedWriter(Paths.get("violations.csv")); | ||
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withFirstRecordAsHeader()); | ||
) { | ||
csvPrinter.printRecord("LEVEL", "RULE", "DESCRIPTION"); | ||
|
||
for (Violation violation : violations) { | ||
csvPrinter.printRecord(violation.getLevel(), violation.getRuleId(), violation.getDescription()); | ||
} | ||
|
||
csvPrinter.flush(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.ensao.gi5.lint.printer; | ||
|
||
import com.ensao.gi5.lint.rules.violations.Violation; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.Collection; | ||
|
||
public class HtmlPrinter implements Printer { | ||
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. 👍🏽 |
||
private static final String NEW_LINE_SEPARATOR = "\n"; | ||
|
||
@Override | ||
public void printViolations(Collection<Violation> violations) { | ||
try ( | ||
BufferedWriter writer = Files.newBufferedWriter(Paths.get("violations.html")); | ||
) { | ||
writer.write("<html><head><title>Violations</title></head><body><table>"); | ||
writer.write("<tr><th>LEVEL</th><th>RULE</th><th>DESCRIPTION</th></tr>"); | ||
|
||
for (Violation violation : violations) { | ||
writer.write("<tr><td>" + violation.getLevel() + "</td><td>" + violation.getRuleId() + "</td><td>" + violation.getDescription() + "</td></tr>"); | ||
} | ||
|
||
writer.write("</table></body></html>"); | ||
writer.flush(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.ensao.gi5.lint.printer; | ||
|
||
import com.ensao.gi5.lint.rules.violations.Violation; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.Collection; | ||
|
||
|
||
public class JsonPrinter implements Printer{ | ||
@Override | ||
public void printViolations(Collection<Violation> violations) { | ||
JSONArray jsonArray = new JSONArray(); | ||
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 (Violation violation : violations) { | ||
JSONObject json = new JSONObject(); | ||
json.put("level", violation.getLevel()); | ||
json.put("ruleId", violation.getRuleId()); | ||
json.put("description", violation.getDescription()); | ||
jsonArray.put(json); | ||
} | ||
writeToFile(jsonArray); | ||
} | ||
|
||
private void writeToFile(JSONArray json) { | ||
try { | ||
FileWriter fileWriter = new FileWriter(new File("violations.json")); | ||
fileWriter.write(json.toString()); | ||
fileWriter.flush(); | ||
fileWriter.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.ensao.gi5.lint.printer; | ||
|
||
import com.ensao.gi5.lint.rules.violations.Violation; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.Collection; | ||
|
||
public class MarkdownPrinter implements Printer{ | ||
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. 👍🏽 |
||
|
||
private static final String NEW_LINE_SEPARATOR = "\n"; | ||
|
||
@Override | ||
public void printViolations(Collection<Violation> violations) { | ||
try ( | ||
BufferedWriter writer = Files.newBufferedWriter(Paths.get("violations.md")); | ||
) { | ||
writer.write("| LEVEL | RULE | DESCRIPTION |"); | ||
writer.write(NEW_LINE_SEPARATOR); | ||
writer.write("| ----- | ---- | ----------- |"); | ||
writer.write(NEW_LINE_SEPARATOR); | ||
|
||
for (Violation violation : violations) { | ||
writer.write("| " + violation.getLevel() + " | " + violation.getRuleId() + " | " + violation.getDescription() + " |"); | ||
writer.write(NEW_LINE_SEPARATOR); | ||
} | ||
|
||
writer.flush(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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.AnonymousVisitors; | ||
import com.ensao.gi5.lint.visitor.ConstVisitors; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
public class AnonymousRule extends Rule{ | ||
public AnonymousRule() { | ||
super(Constantes.LINT_REG_009, Level.HIGH); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
final Set<String> m = new LinkedHashSet<>(); | ||
compilationUnit.accept(new AnonymousVisitors(), m); | ||
|
||
if(m.size()>0){ | ||
final Violation violation = new Violation(); | ||
violation.setDescription(m.size()+" Anonymous instanciation violation "); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
// violation.setLine(n.getLine()); | ||
addViolation(violation); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
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.visitor.ConstVisitors; | ||
import com.ensao.gi5.lint.visitor.MembersVisitors; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
public class ConstRule extends Rule{ | ||
public ConstRule() { | ||
super(Constantes.LINT_REG_005, Level.MEDIUM); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
final Set<String> m = new LinkedHashSet<>(); | ||
compilationUnit.accept(new ConstVisitors(), m); | ||
|
||
|
||
for(String s:m) { | ||
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 significant variable names, |
||
|
||
if (!s.equals(s.toUpperCase())) { | ||
final Violation violation = new Violation(); | ||
violation.setDescription("Const name violation "); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
//violation.setLine(n.getLine()); | ||
addViolation(violation); | ||
} | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
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.LocalvarVisitors; | ||
import com.ensao.gi5.lint.visitor.NominationVisitors; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
import com.ensao.gi5.lint.wrapper.NominationWrapper; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class LocalvarRule extends Rule{ | ||
public LocalvarRule() { | ||
super(Constantes.LINT_REG_003, Level.HIGHEST); | ||
} | ||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
final Set<String> localvariables = new LinkedHashSet<>(); | ||
compilationUnit.accept(new LocalvarVisitors(), localvariables); | ||
|
||
|
||
Set<String> localvar=localvariables.stream().filter(p->Character.isUpperCase(p.charAt(1))).collect(Collectors.toSet()); | ||
|
||
if(!localvar.isEmpty()){ | ||
for (String lvar:localvar){ | ||
final Violation violation = new Violation(); | ||
violation.setDescription("local variable name violation '" + lvar + "'"); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
// violation.setLine(n.getLine()); | ||
addViolation(violation); | ||
}} | ||
|
||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
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.LocalvarVisitors; | ||
import com.ensao.gi5.lint.visitor.MembersVisitors; | ||
import com.ensao.gi5.lint.visitor.NominationVisitors; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
import com.ensao.gi5.lint.wrapper.NominationWrapper; | ||
import com.github.javaparser.ast.body.BodyDeclaration; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
public class MembersRule extends Rule{ | ||
public MembersRule() { | ||
super(Constantes.LINT_REG_004, Level.HIGHEST); | ||
} | ||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
final Set<String> m = new LinkedHashSet<>(); | ||
compilationUnit.accept(new MembersVisitors(), m); | ||
|
||
|
||
|
||
Predicate<Object> p1= p->(Character.isUpperCase(p.toString().charAt(1))); | ||
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 significant variable names instead of |
||
|
||
|
||
|
||
Set<String> answer=m.stream().filter(p1) | ||
.collect(Collectors.toSet()); | ||
|
||
|
||
if (!answer.isEmpty()) { | ||
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. this condition is useled, the |
||
|
||
for (String n:answer){ | ||
final Violation violation = new Violation(); | ||
violation.setDescription("Class name violation '" + n + "'"); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
//violation.setLine(n.getLine()); | ||
addViolation(violation); | ||
}} | ||
|
||
|
||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
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.
Guava library is not necessary, use java collection framework instead