|
| 1 | +package analyzer.exercises.wizardsandwarriors2; |
| 2 | + |
| 3 | +import analyzer.Analyzer; |
| 4 | +import analyzer.OutputCollector; |
| 5 | +import analyzer.Solution; |
| 6 | +import analyzer.comments.ExemplarSolution; |
| 7 | +import analyzer.comments.PreferStringConcatenation; |
| 8 | +import com.github.javaparser.ast.body.MethodDeclaration; |
| 9 | +import com.github.javaparser.ast.body.Parameter; |
| 10 | +import com.github.javaparser.ast.expr.MethodCallExpr; |
| 11 | +import com.github.javaparser.ast.visitor.VoidVisitorAdapter; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +/** |
| 16 | + * The {@link WizardsAndWarriors2Analyzer} is the analyzer implementation for the {@code wizards-and-warriors-2} concept exercise. |
| 17 | + * |
| 18 | + * @see <a href="https://github.com/exercism/java/tree/main/exercises/concept/wizards-and-warriors-2">The wizards-and-warriors exercise on the Java track</a> |
| 19 | + */ |
| 20 | +public class WizardsAndWarriors2Analyzer extends VoidVisitorAdapter<OutputCollector> implements Analyzer { |
| 21 | + |
| 22 | + private static final String EXERCISE_NAME = "Wizards and Warriors 2"; |
| 23 | + private static final String GAME_MASTER = "GameMaster"; |
| 24 | + private static final String DESCRIBE = "describe"; |
| 25 | + private static final String FORMAT = "format"; |
| 26 | + private static final String DESTINATION_TYPE = "Destination"; |
| 27 | + private static final String TRAVEL_METHOD_TYPE = "TravelMethod"; |
| 28 | + private static final String CHARACTER_TYPE = "Character"; |
| 29 | + |
| 30 | + @Override |
| 31 | + public void analyze(Solution solution, OutputCollector output) { |
| 32 | + |
| 33 | + for (var compilationUnit : solution.getCompilationUnits()) { |
| 34 | + compilationUnit.getClassByName(GAME_MASTER).ifPresent(c -> c.accept(this, output)); |
| 35 | + } |
| 36 | + |
| 37 | + if (output.getComments().isEmpty()) { |
| 38 | + output.addComment(new ExemplarSolution(EXERCISE_NAME)); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void visit(MethodDeclaration node, OutputCollector output) { |
| 44 | + |
| 45 | + if(!node.getNameAsString().equals(DESCRIBE)) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if(node.getParameters().size() == 2 && !reuseMethod(node, 2)) { |
| 50 | + output.addComment(new ReuseCodeFromDescribeTwoParams()); |
| 51 | + } |
| 52 | + |
| 53 | + if(node.getParameters().size() == 3 && !reuseMethod(node, 3)) { |
| 54 | + output.addComment(new ReuseCodeFromDescribeThreeParams()); |
| 55 | + } |
| 56 | + |
| 57 | + if(useStringFormat(node)) { |
| 58 | + output.addComment(new PreferStringConcatenation()); |
| 59 | + } |
| 60 | + |
| 61 | + super.visit(node, output); |
| 62 | + } |
| 63 | + |
| 64 | + private static boolean reuseMethod(MethodDeclaration node, int paramCount) { |
| 65 | + |
| 66 | + List<String> paramsType = node.getParameters().stream().map(Parameter::getTypeAsString).toList(); |
| 67 | + List<MethodCallExpr> describeCalls = node.findAll(MethodCallExpr.class).stream() |
| 68 | + .filter(m -> m.getNameAsString().equals(DESCRIBE)) |
| 69 | + .toList(); |
| 70 | + |
| 71 | + if (paramCount == 2 && paramsType.contains(DESTINATION_TYPE) && paramsType.contains(CHARACTER_TYPE)) { |
| 72 | + return describeCalls.size() == 1 || describeCalls.size() == 3; |
| 73 | + } |
| 74 | + |
| 75 | + if (paramCount == 3 && paramsType.contains(DESTINATION_TYPE) && paramsType.contains(TRAVEL_METHOD_TYPE) && paramsType.contains(CHARACTER_TYPE)) { |
| 76 | + return describeCalls.size() == 3; |
| 77 | + } |
| 78 | + |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + private static boolean useStringFormat(MethodDeclaration node) { |
| 83 | + return node.findAll(MethodCallExpr.class).stream() |
| 84 | + .anyMatch(m -> m.getNameAsString().contains(FORMAT)); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments