Skip to content

Commit

Permalink
update for reducing the validation violations.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed Aug 31, 2016
1 parent a8f36eb commit 490734b
Show file tree
Hide file tree
Showing 25 changed files with 171 additions and 92 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/github/ninerules/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.List;
import java.util.stream.Collectors;

import com.github.ninerules.rules.Results;
import com.github.ninerules.rules.results.Results;
import com.github.ninerules.traverser.Traverser;

/**
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/github/ninerules/NineRulesValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;

import com.github.ninerules.rules.Results;
import com.github.ninerules.rules.Rules;
import com.github.ninerules.rules.results.Results;
import com.github.ninerules.rules.results.ResultsAppender;

public class NineRulesValidator {
public Results validate(List<Path> list){
return list.stream()
.sorted()
.map(path -> parse(path))
.map(unit -> validate(unit))
.reduce((result1, result2) -> result1.append(result2))
.reduce((result1, result2) -> new ResultsAppender(result1).append(result2))
.orElse(Results.empty());
}

Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/github/ninerules/Reporter.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.github.ninerules;

import java.io.PrintWriter;
import java.util.List;

import com.github.ninerules.rules.Results;
import com.github.ninerules.entities.FileName;
import com.github.ninerules.rules.Violation;
import com.github.ninerules.rules.results.Results;

public class Reporter {
private PrintWriter out;
Expand All @@ -16,7 +19,13 @@ public Reporter(PrintWriter out){
}

public void report(Results results){
results.report(out);
results.report(this);
out.flush();
}

public void report(FileName name, List<Violation> list){
out.println(name);
list.stream()
.forEach(item -> out.println(item));
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/github/ninerules/Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import com.github.ninerules.entities.LineCount;
import com.github.ninerules.rules.JdtValidator;
import com.github.ninerules.rules.PlainSourceValidator;
import com.github.ninerules.rules.Results;
import com.github.ninerules.rules.Validator;
import com.github.ninerules.rules.results.Results;

public class Target {
private Path path;
Expand All @@ -40,7 +40,6 @@ public Results accept(PlainSourceValidator checker){
try(Stream<String> stream = Files.lines(path)){
visitLine(stream, checker);
} catch (IOException e) {
e.printStackTrace();
}
return checker.createResults(new FileName(path));
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/github/ninerules/entities/FileName.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public String toString(){
@Override
public boolean equals(Object object){
if(object instanceof FileName){
return Objects.equals(name, ((FileName)object).name);
String otherObjectName = ((FileName)object).name;
return Objects.equals(name, otherObjectName);
}
return false;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/github/ninerules/rules/JdtValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import com.github.ninerules.entities.FileName;
import com.github.ninerules.rules.results.Results;

public abstract class JdtValidator extends ASTVisitorPlus implements Validator{
private List<Violation> violations = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.github.ninerules.entities.FileName;
import com.github.ninerules.entities.LineCount;
import com.github.ninerules.rules.results.Results;

public abstract class PlainSourceValidator implements Validator{
private List<Violation> violations = new ArrayList<>();
Expand Down
55 changes: 0 additions & 55 deletions src/main/java/com/github/ninerules/rules/Results.java

This file was deleted.

4 changes: 3 additions & 1 deletion src/main/java/com/github/ninerules/rules/Rules.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.github.ninerules.rules.indentlevel.IndentLevelValidator;
import com.github.ninerules.rules.onedot.OneDotPerLineValidator;
import com.github.ninerules.rules.primitive.NoPrimitivesValidator;
import com.github.ninerules.rules.results.Results;
import com.github.ninerules.rules.results.ResultsAppender;
import com.github.ninerules.rules.smallobject.MethodLengthValidator;
import com.github.ninerules.rules.smallobject.SourceLengthValidator;

Expand All @@ -32,7 +34,7 @@ public Rules(){
public Results validate(final Target unit){
return list.stream()
.map(checker -> accept(unit, checker))
.reduce((r1, r2) -> r1.append(r2))
.reduce((r1, r2) -> new ResultsAppender(r1).append(r2))
.orElse(Results.empty());
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/github/ninerules/rules/Validator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.ninerules.rules;

import com.github.ninerules.entities.FileName;
import com.github.ninerules.rules.results.Results;

public interface Validator {
public void addViolation(Violation violation);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.ninerules.rules.elsestatement;

import java.util.Optional;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.Statement;
Expand All @@ -20,13 +22,15 @@ public boolean visit(IfStatement node){

private void checkViolation(IfStatement node){
Statement statement = node.getElseStatement();
if(isViolation(statement)){
if(isViolation(Optional.ofNullable(statement))){
addViolation(new Violation(NO_ELSE_STATEMENT, new LineCounts(startLine(node))));
}
}

private boolean isViolation(Statement statement){
return statement != null &&
statement.getNodeType() != ASTNode.IF_STATEMENT;
private boolean isViolation(Optional<Statement> item){
return item.map(statement -> {
int nodeType = statement.getNodeType();
return nodeType != ASTNode.IF_STATEMENT;
}).orElse(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,8 @@ private void checkViolation(){

private boolean isViolated(){
long fieldSize = computesFieldCount(predicate);
long primitivesSize = computesFieldCount(predicate.and(item -> checkPrimitives(item)));
long primitivesSize = computesFieldCount(predicate.and(
item -> new PrimitiveChecker().check(item)));
return fieldSize > 1 && primitivesSize > 0;
}

private boolean checkPrimitives(FieldDeclaration node){
Type type = node.getType();
return checkPrimitives(type.toString());
}

private boolean checkPrimitives(String type){
List<String> list = Arrays.asList("byte", "short", "int", "long", "float",
"double", "boolean", "char", "(java.lang.)?String", "(java.util.)?Date");
return list.stream()
.filter(item -> type.matches(item))
.count() > 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.ninerules.rules.primitive;

import java.util.Arrays;
import java.util.List;

import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.Type;

public class PrimitiveChecker {
private List<String> list = Arrays.asList("byte", "short", "int", "long", "float",
"double", "boolean", "char", "(java.lang.)?String", "(java.util.)?Date");

public boolean check(FieldDeclaration node){
Type type = node.getType();
return check(type.toString());
}

private boolean check(String type){
return list.stream()
.filter(item -> type.matches(item))
.count() > 0;
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/github/ninerules/rules/results/Results.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.ninerules.rules.results;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.ninerules.Reporter;
import com.github.ninerules.entities.FileName;
import com.github.ninerules.rules.Violation;

public class Results {
Map<FileName, List<Violation>> violations = new HashMap<>();

public Results(FileName fileName, List<Violation> list){
violations.put(fileName, list);
}

private Results(){
}

public static final Results empty(){
return new Results();
}

public boolean contains(FileName fileName, Violation violation){
List<Violation> list = violations.getOrDefault(fileName, new ArrayList<>());
return list.contains(violation);
}

public void report(Reporter reporter){
violations.entrySet()
.stream()
.forEach(item -> report(reporter, item));
}

public void report(Reporter reporter, Map.Entry<FileName, List<Violation>> entry){
FileName name = entry.getKey();
List<Violation> violations = entry.getValue();
reporter.report(name, violations);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.ninerules.rules.results;

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

import com.github.ninerules.entities.FileName;
import com.github.ninerules.rules.Violation;

public class ResultsAppender {
private Results results;

public ResultsAppender(Results results){
this.results = results;
}

public Results append(Results results){
results.violations
.entrySet()
.stream()
.forEach(item -> put(item));
return this.results;
}

private void put(Map.Entry<FileName, List<Violation>> entry){
FileName name = entry.getKey();
List<Violation> list = entry.getValue();
put(name, list);
}

private void put(FileName name, List<Violation> list){
Map<FileName, List<Violation>> map = results.violations;
List<Violation> origList = map.getOrDefault(name, new ArrayList<>());
origList.addAll(list);
map.put(name, origList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

import com.github.ninerules.entities.FileName;
import com.github.ninerules.entities.LineCountsBuilder;
import com.github.ninerules.rules.Results;
import com.github.ninerules.rules.Violation;
import com.github.ninerules.rules.accessor.NoAccessorValidator;
import com.github.ninerules.rules.results.Results;
import com.github.ninerules.traverser.Traverser;

public class NineRulesValidatorTest {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/github/ninerules/ReporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

import com.github.ninerules.entities.FileName;
import com.github.ninerules.entities.LineCountsBuilder;
import com.github.ninerules.rules.Results;
import com.github.ninerules.rules.Violation;
import com.github.ninerules.rules.ViolationType;
import com.github.ninerules.rules.results.Results;

public class ReporterTest {
private ByteArrayOutputStream out = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.ninerules.rules;
package com.github.ninerules.rules.results;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
Expand All @@ -15,7 +15,10 @@
import com.github.ninerules.Target;
import com.github.ninerules.entities.FileName;
import com.github.ninerules.entities.LineCountsBuilder;
import com.github.ninerules.rules.JdtValidator;
import com.github.ninerules.rules.Violation;
import com.github.ninerules.rules.fieldcount.FieldCountValidator;
import com.github.ninerules.rules.results.Results;

public class FieldCountValidatorTest {
private static final String FILE_PATH = "src/test/resources/hello/src/main/java/sample/hello/GodObject.java";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.ninerules.rules;
package com.github.ninerules.rules.results;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
Expand All @@ -15,7 +15,10 @@
import com.github.ninerules.Target;
import com.github.ninerules.entities.FileName;
import com.github.ninerules.entities.LineCountsBuilder;
import com.github.ninerules.rules.JdtValidator;
import com.github.ninerules.rules.Violation;
import com.github.ninerules.rules.firstclasscollection.FirstClassCollectionValidator;
import com.github.ninerules.rules.results.Results;

public class FirstClassCollectionValidatorTest {
private static final String FILE_PATH = "src/test/resources/hello/src/main/java/sample/hello/GodObject.java";
Expand Down

0 comments on commit 490734b

Please sign in to comment.