Skip to content

Commit

Permalink
Remove redundant analyzer comments for hamming and two-fer (#122
Browse files Browse the repository at this point in the history
)

* Gradlew.bat update

* Removing redundant analyzer comments for hamming exercise

* Removing redundant analyzer comments for two fer exercise

* Removing tests that there aren't used anymore

* Applying suggestions
Restoring MustUseStringCharAtOrCodePointAt analyzer functionality
Deleting unused usesConditional variable
Removing tests/hamming/.meta/src/reference/java/Hamming.java and tests/hamming/.meta/tests.toml

* Removing validateNotNull outdated check
  • Loading branch information
manumafe98 authored Feb 14, 2024
1 parent aa58443 commit d7b8abe
Show file tree
Hide file tree
Showing 22 changed files with 0 additions and 373 deletions.
21 changes: 0 additions & 21 deletions src/main/java/analyzer/exercises/hamming/HammingAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,6 @@ 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;
Expand Down
58 changes: 0 additions & 58 deletions src/main/java/analyzer/exercises/hamming/HammingWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/java/analyzer/exercises/hamming/MustUseConstructor.java

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
Expand Up @@ -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) {
Expand Down
11 changes: 0 additions & 11 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.*;
Expand All @@ -12,10 +10,7 @@
class TwoferWalker implements Consumer<Node> {
boolean hasHardCodedTestCases;
boolean usesIfStatement;
boolean usesConditional;
boolean hasMethodCall;
boolean usesLambda;
boolean usesLoops;
boolean usesFormat;
int returnCount;

Expand All @@ -27,18 +22,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;
}
}
}
18 changes: 0 additions & 18 deletions src/main/java/analyzer/exercises/twofer/UseConditionalLogic.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ 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()}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()}),
Expand Down

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/UsesLambda.java.txt

This file was deleted.

Loading

0 comments on commit d7b8abe

Please sign in to comment.