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

Remove redundant analyzer comments for hamming and two-fer #122

Merged
Changes from 5 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
26 changes: 0 additions & 26 deletions src/main/java/analyzer/exercises/hamming/HammingAnalyzer.java
Original file line number Diff line number Diff line change
@@ -22,37 +22,11 @@ public void analyze(Solution solution, Analysis analysis) {

solution.getCompilationUnits().forEach(cu -> cu.walk(ClassOrInterfaceDeclaration.class, walker));

if (!walker.hasConstructor()) {
analysis.addComment(new MustUseConstructor());
return;
}

if (!walker.constructorHasIfStatements() && !walker.constructorHasMethodCalls()) {
analysis.addComment(new MustUseConditionalLogicInConstructor());
return;
}

if (!walker.constructorThrowsIllegalArgument()) {
analysis.addComment(new MustThrowInConstructor());
return;
}

if (!walker.getHammingDistanceMethodMayCalculateDistance()
&& !walker.constructorMayCalculateDistance()) {
analysis.addComment(new MustCalculateHammingDistance());
return;
}

if (walker.usesCharacterLiterals()) {
analysis.addComment(new AvoidCharacterLiterals());
return;
}

if (!walker.usesStringCharAtOrCodePointAt()) {
analysis.addComment(new MustUseStringCharAtOrCodePointAt());
return;
}
manumafe98 marked this conversation as resolved.
Show resolved Hide resolved

if (!walker.constructorMayCalculateDistance()) {
analysis.addComment(new CalculateDistanceInConstructor());
}
62 changes: 0 additions & 62 deletions src/main/java/analyzer/exercises/hamming/HammingWalker.java
Original file line number Diff line number Diff line change
@@ -21,8 +21,6 @@ class HammingWalker implements Consumer<ClassOrInterfaceDeclaration> {
private ConstructorDeclaration constructor;
private final Map<String, List<MethodDeclaration>> methods = new HashMap<>();
private final Set<String> methodsCalledByConstructor = new HashSet<>();
private boolean constructorHasIfStatements;
private boolean constructorThrowsIllegalArgumentDirectly;
private boolean constructorMayCalculateDistanceDirectly;
private final Set<String> methodsCalledByGetHammingDistance = new HashSet<>();
private boolean getHammingDistanceMayCalculateDistanceDirectly;
@@ -62,13 +60,6 @@ private void walkConstructor(ConstructorDeclaration foundConstructor) {
}

private void walkConstructorStatement(Statement statement) {
if (statement.isIfStmt()) {
constructorHasIfStatements = true;
}

if (isThrowNewIllegalArgument(statement)) {
constructorThrowsIllegalArgumentDirectly = true;
}

if (statementMayCalculateHammingDistance(statement)) {
constructorMayCalculateDistanceDirectly = true;
@@ -79,21 +70,6 @@ private void walkConstructorStatement(Statement statement) {
recursivelyAddMethodsCalled(methodName, methodsCalledByConstructor));
}

private boolean isThrowNewIllegalArgument(Statement statement) {
return statement.findAll(ThrowStmt.class).stream()
.anyMatch(this::isCreatingIllegalArgumentException);
}

private boolean isCreatingIllegalArgumentException(ThrowStmt throwStmt) {
return throwStmt.getExpression().isObjectCreationExpr()
&& throwStmt
.getExpression()
.asObjectCreationExpr()
.getType()
.getNameAsString()
.equals("IllegalArgumentException");
}

private boolean isMethodCall(Statement statement) {
return !statement.findAll(MethodCallExpr.class).isEmpty();
}
@@ -160,44 +136,10 @@ private Stream<String> getMethodsCalledBy(BlockStmt body) {
.flatMap(this::getMethodCallNames);
}

public boolean hasConstructor() {
return constructor != null;
}

public boolean constructorHasIfStatements() {
return constructorHasIfStatements;
}

public boolean constructorHasMethodCalls() {
return !methodsCalledByConstructor.isEmpty();
}

public boolean constructorThrowsIllegalArgument() {
return constructorThrowsIllegalArgumentDirectly
|| constructorThrowsIllegarArgumentIndirectly();
}

private boolean constructorThrowsIllegarArgumentIndirectly() {
return methodsCalledByConstructor.stream()
.anyMatch(this::methodThrowsIllegalArgumentException);
}

private boolean methodThrowsIllegalArgumentException(String methodName) {
return methods.getOrDefault(methodName, List.of()).stream()
.anyMatch(this::methodThrowsIllegalArgumentException);
}

private boolean methodThrowsIllegalArgumentException(MethodDeclaration method) {
return method.getBody()
.map(this::methodBodyThrowsIllegalArgumentException)
.orElse(false);
}

private boolean methodBodyThrowsIllegalArgumentException(BlockStmt body) {
return body.getStatements().stream()
.anyMatch(this::isThrowNewIllegalArgument);
}

public boolean constructorMayCalculateDistance() {
return constructorMayCalculateDistanceDirectly
|| constructorCallsMethodThatMayCalculateDistance();
@@ -271,10 +213,6 @@ public boolean usesCharacterLiterals() {
return !hammingClass.findAll(CharLiteralExpr.class).isEmpty();
}

public boolean usesStringCharAtOrCodePointAt() {
return usesMethod("charAt") || usesMethod("codePointAt");
}

public boolean shouldUseStreamFilterAndCount() {
return usesMethod("reduce");
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

6 changes: 0 additions & 6 deletions src/main/java/analyzer/exercises/twofer/TwoferAnalyzer.java
Original file line number Diff line number Diff line change
@@ -20,12 +20,6 @@ public void analyze(Solution solution, Analysis analysis) {

if (walker.hasHardCodedTestCases) {
analysis.addComment(new AvoidHardCodedTestCases());
} else if (walker.usesLambda) {
// could be used later for additional comments?
} else if (walker.usesLoops) {
// could be used later for additional comments?
} else if (!walker.hasMethodCall && !(walker.usesIfStatement || walker.usesConditional)) {
analysis.addComment(new UseConditionalLogic());
} else if (walker.usesFormat) {
analysis.addComment(new AvoidStringFormat());
} else if (walker.returnCount > 1) {
10 changes: 0 additions & 10 deletions src/main/java/analyzer/exercises/twofer/TwoferWalker.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package analyzer.exercises.twofer;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.ConditionalExpr;
import com.github.javaparser.ast.expr.LambdaExpr;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.StringLiteralExpr;
import com.github.javaparser.ast.stmt.*;
@@ -14,8 +12,6 @@ class TwoferWalker implements Consumer<Node> {
boolean usesIfStatement;
boolean usesConditional;
manumafe98 marked this conversation as resolved.
Show resolved Hide resolved
boolean hasMethodCall;
boolean usesLambda;
boolean usesLoops;
boolean usesFormat;
int returnCount;

@@ -27,18 +23,12 @@ public void accept(Node node) {
this.returnCount++;
} else if (node instanceof IfStmt) {
this.usesIfStatement = true;
} else if (node instanceof ConditionalExpr) {
this.usesConditional = true;
} else if (node instanceof MethodCallExpr && !this.hasMethodCall) {
this.hasMethodCall = true;

if (((MethodCallExpr) node).getName().toString().equals("format")) {
this.usesFormat = true;
}
} else if (node instanceof LambdaExpr) {
this.usesLambda = true;
} else if (node instanceof WhileStmt || node instanceof ForStmt || node instanceof ForEachStmt) {
this.usesLoops = true;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -17,12 +17,7 @@ public class HammingAnalyzerTest {

private static Stream<Arguments> testCases() {
return Stream.of(
Arguments.of("NoConstructor.java.txt", new Comment[]{new MustUseConstructor()}),
Arguments.of("NoConditionalInConstructor.java.txt", new Comment[]{new MustUseConditionalLogicInConstructor()}),
Arguments.of("DoesNotThrowInConstructor.java.txt", new Comment[]{new MustThrowInConstructor()}),
Arguments.of("NoCalculationOfHammingDistance.java.txt", new Comment[]{new MustCalculateHammingDistance()}),
Arguments.of("UsesCharacterLiterals.java.txt", new Comment[]{new AvoidCharacterLiterals()}),
Arguments.of("MustUseCharAtOrCodePointAt.java.txt", new Comment[]{new MustUseStringCharAtOrCodePointAt()}),
Arguments.of("NestedValidation.java.txt", new Comment[]{new CalculateDistanceInConstructor()}),
Arguments.of("NestedCalculation.java.txt", new Comment[0]),
Arguments.of("OptimalWithCalculationInGetHammingDistance.java.txt", new Comment[]{new CalculateDistanceInConstructor()}),
Original file line number Diff line number Diff line change
@@ -16,10 +16,7 @@ public class TwoferAnalyzerTest {

private static Stream<Arguments> testCases() {
return Stream.of(
Arguments.of("UsesLambda.java.txt", new Comment[0]),
Arguments.of("UsesLoop.java.txt", new Comment[0]),
Arguments.of("HardCodedTestCases.java.txt", new Comment[]{new AvoidHardCodedTestCases()}),
Arguments.of("NoConditionalLogic.java.txt", new Comment[]{new UseConditionalLogic()}),
Arguments.of("UsesStringFormat.java.txt", new Comment[]{new AvoidStringFormat()}),
Arguments.of("UsesMultipleReturns.java.txt", new Comment[]{new UseOneReturn()}),
Arguments.of("OptimalNoTernary.java.txt", new Comment[]{new UseTernaryOperator()}),

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

11 changes: 0 additions & 11 deletions src/test/resources/analyzer/exercises/twofer/UsesLoop.java.txt

This file was deleted.

6 changes: 0 additions & 6 deletions tests/hamming/.meta/src/reference/java/Hamming.java
sanderploegsma marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -7,12 +7,6 @@ class Hamming {
Hamming(String leftStrand, String rightStrand) {
String exceptionMessage = "leftStrand and rightStrand must be of equal length.";
if (leftStrand.length() != rightStrand.length()) {
if (leftStrand.isEmpty()) {
exceptionMessage = "left strand must not be empty.";
}
if (rightStrand.isEmpty()) {
exceptionMessage = "right strand must not be empty.";
}
throw new IllegalArgumentException(exceptionMessage);
}

1 change: 0 additions & 1 deletion tests/hamming/.meta/version

This file was deleted.

9 changes: 0 additions & 9 deletions tests/hamming/src/main/java/Hamming.java
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@ class Hamming {

private void validate(String leftStrand, String rightStrand) {
validateNotNull(leftStrand, rightStrand);
validateNotOneEmpty(leftStrand, rightStrand);
validateSameLength(leftStrand, rightStrand);
}

@@ -23,14 +22,6 @@ private void validateNotNull(String leftStrand, String rightStrand) {
}
}

private void validateNotOneEmpty(String leftStrand, String rightStrand) {
if (leftStrand.length() == 0 && rightStrand.length() > 0) {
throw new IllegalArgumentException("left strand must not be empty.");
} else if (rightStrand.length() == 0 && leftStrand.length() > 0) {
throw new IllegalArgumentException("right strand must not be empty.");
}
}

private void validateSameLength(String leftStrand, String rightStrand) {
if (leftStrand.length() != rightStrand.length()) {
throw new IllegalArgumentException("leftStrand and rightStrand must be of equal length.");