Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 0 additions & 18 deletions src/main/java/analyzer/exercises/twofer/AvoidStringFormat.java

This file was deleted.

77 changes: 65 additions & 12 deletions src/main/java/analyzer/exercises/twofer/TwoferAnalyzer.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,86 @@
package analyzer.exercises.twofer;

import analyzer.OutputCollector;

import java.util.List;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.StringLiteralExpr;
import com.github.javaparser.ast.stmt.IfStmt;
import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;

import analyzer.Analyzer;
import analyzer.Solution;
import analyzer.comments.AvoidHardCodedTestCases;
import analyzer.comments.PreferStringConcatenation;

/**
* The {@link TwoferAnalyzer} is the analyzer implementation for the {@code two-fer} practice exercise.
*
* @see <a href="https://github.com/exercism/java/tree/main/exercises/practice/two-fer">The two-fer exercise on the Java track</a>
*/
public class TwoferAnalyzer implements Analyzer {
public class TwoferAnalyzer extends VoidVisitorAdapter<OutputCollector> implements Analyzer {

@Override
public void analyze(Solution solution, OutputCollector output) {
TwoferWalker walker = new TwoferWalker();

solution.getCompilationUnits().forEach(cu -> cu.walk(walker));
for (CompilationUnit compilationUnit : solution.getCompilationUnits()) {
compilationUnit.accept(this, output);
}
}

if (walker.hasHardCodedTestCases) {
@Override
public void visit(MethodDeclaration node, OutputCollector output) {
if (hasHardCodedTestCases(node)) {
output.addComment(new AvoidHardCodedTestCases());
} else if (walker.usesFormat) {
output.addComment(new AvoidStringFormat());
} else if (walker.returnCount > 1) {
return;
}

if (hasIfStatement(node)) {
output.addComment(new UseTernaryOperator());
}

if (hasMoreThanOneReturnStatement(node)) {
output.addComment(new UseOneReturn());
} else {
if (walker.usesIfStatement) {
output.addComment(new UseTernaryOperator());
}
}

if (callsFormatMethod(node)) {
output.addComment(new PreferStringConcatenation());
}
}

private static boolean hasHardCodedTestCases(MethodDeclaration node) {
List<StringLiteralExpr> hardcodedStrings = node.findAll(StringLiteralExpr.class,
x -> x.getValue().contains("Alice") || x.getValue().contains("Bob"));

return hardcodedStrings.size() > 1;
}

private static boolean hasMoreThanOneReturnStatement(MethodDeclaration node) {
long returnStmtCount = node.getBody()
.map(body -> body.getStatements().stream().filter(TwoferAnalyzer::isReturnStatement).count())
.orElse(0L);

return returnStmtCount > 1;
}

private static boolean callsFormatMethod(MethodDeclaration node) {
return !node.findAll(MethodCallExpr.class, x -> x.getNameAsString().contains("format")).isEmpty();
}

private static boolean hasIfStatement(MethodDeclaration node) {
return node.getBody().map(body -> body.getStatements().stream().anyMatch(TwoferAnalyzer::isIfStatement))
.orElse(false);
}

private static boolean isIfStatement(Statement statement) {
return !statement.findAll(IfStmt.class).isEmpty();
}

private static boolean isReturnStatement(Statement statement) {
return !statement.findAll(ReturnStmt.class).isEmpty();
}
}
33 changes: 0 additions & 33 deletions src/main/java/analyzer/exercises/twofer/TwoferWalker.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"comments": [
{
"comment": "java.two-fer.use_ternary_operator",
"params": {},
"type": "actionable"
},
{
"comment": "java.two-fer.use_one_return",
"params": {},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"comments": [
{
"comment": "java.two-fer.avoid_string_format",
"comment": "java.general.prefer_string_concatenation",
"params": {},
"type": "actionable"
"type": "informative"
},
{
"comment": "java.general.feedback_request",
Expand Down
14 changes: 14 additions & 0 deletions tests/two-fer/hardcoding-tests/expected_analysis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"comments": [
{
"comment": "java.general.avoid_hard_coded_test_cases",
"params": {},
"type": "essential"
},
{
"comment": "java.general.feedback_request",
"params": {},
"type": "informative"
}
]
}
12 changes: 12 additions & 0 deletions tests/two-fer/hardcoding-tests/src/main/java/TwoFer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Twofer {
public String twofer(String name) {
if (name == null) {
// fall through
} else if (name.equals("Alice")) {
return "One for Alice, one for me.";
} else if (name.equals("Bob")) {
return "One for Bob, one for me.";
}
return "One for you, one for me.";
}
}

This file was deleted.

39 changes: 39 additions & 0 deletions tests/two-fer/using-if-statement/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"authors": [
"Smarticles101"
],
"contributors": [
"FridaTveit",
"ikhadykin",
"jmrunkle",
"jssander",
"kytrinyx",
"lemoncurry",
"msomji",
"muzimuzhi",
"rdavid1099",
"sjwarner-bp",
"SleeplessByte",
"sshine",
"stkent",
"uzilan",
"Valkryst",
"ymoskovits"
],
"files": {
"solution": [
"src/main/java/Twofer.java"
],
"test": [
"src/test/java/TwoferTest.java"
],
"example": [
".meta/src/reference/java/Twofer.java"
],
"invalidator": [
"build.gradle"
]
},
"blurb": "Create a sentence of the form \"One for X, one for me.\".",
"source_url": "https://github.com/exercism/problem-specifications/issues/757"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"comments": [
{
"comment": "java.two-fer.avoid_string_format",
"comment": "java.two-fer.use_ternary_operator",
"params": {},
"type": "actionable"
},
Expand Down
3 changes: 3 additions & 0 deletions tests/two-fer/using-if-statement/expected_tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tags": []
}
7 changes: 7 additions & 0 deletions tests/two-fer/using-if-statement/src/main/java/TwoFer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Twofer {
public String twofer(String rawName) {
String name = rawName;
if (name == null) name = "you";
return "One for " + name + ", one for me.";
}
}
39 changes: 39 additions & 0 deletions tests/two-fer/using-multiple-returns/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"authors": [
"Smarticles101"
],
"contributors": [
"FridaTveit",
"ikhadykin",
"jmrunkle",
"jssander",
"kytrinyx",
"lemoncurry",
"msomji",
"muzimuzhi",
"rdavid1099",
"sjwarner-bp",
"SleeplessByte",
"sshine",
"stkent",
"uzilan",
"Valkryst",
"ymoskovits"
],
"files": {
"solution": [
"src/main/java/Twofer.java"
],
"test": [
"src/test/java/TwoferTest.java"
],
"example": [
".meta/src/reference/java/Twofer.java"
],
"invalidator": [
"build.gradle"
]
},
"blurb": "Create a sentence of the form \"One for X, one for me.\".",
"source_url": "https://github.com/exercism/problem-specifications/issues/757"
}
19 changes: 19 additions & 0 deletions tests/two-fer/using-multiple-returns/expected_analysis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"comments": [
{
"comment": "java.two-fer.use_ternary_operator",
"params": {},
"type": "actionable"
},
{
"comment": "java.two-fer.use_one_return",
"params": {},
"type": "actionable"
},
{
"comment": "java.general.feedback_request",
"params": {},
"type": "informative"
}
]
}
3 changes: 3 additions & 0 deletions tests/two-fer/using-multiple-returns/expected_tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tags": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Twofer {
public String twofer(String name) {
if (name == null) return "One for you, one for me.";
return "One for " + name + ", one for me.";
}
}
39 changes: 39 additions & 0 deletions tests/two-fer/using-string-format/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"authors": [
"Smarticles101"
],
"contributors": [
"FridaTveit",
"ikhadykin",
"jmrunkle",
"jssander",
"kytrinyx",
"lemoncurry",
"msomji",
"muzimuzhi",
"rdavid1099",
"sjwarner-bp",
"SleeplessByte",
"sshine",
"stkent",
"uzilan",
"Valkryst",
"ymoskovits"
],
"files": {
"solution": [
"src/main/java/Twofer.java"
],
"test": [
"src/test/java/TwoferTest.java"
],
"example": [
".meta/src/reference/java/Twofer.java"
],
"invalidator": [
"build.gradle"
]
},
"blurb": "Create a sentence of the form \"One for X, one for me.\".",
"source_url": "https://github.com/exercism/problem-specifications/issues/757"
}
Loading