Skip to content

Implementation of analyzer for wizards-and-warriors-2 #262

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/main/java/analyzer/AnalyzerRoot.java
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
import analyzer.exercises.secrets.SecretsAnalyzer;
import analyzer.exercises.twofer.TwoferAnalyzer;
import analyzer.exercises.wizardsandwarriors.WizardsAndWarriorsAnalyzer;
import analyzer.exercises.wizardsandwarriors2.WizardsAndWarriors2Analyzer;

import java.util.ArrayList;
import java.util.List;
@@ -33,7 +34,6 @@ private AnalyzerRoot() {
*/
public static Output analyze(Solution solution) {
var outputBuilder = new OutputBuilder();

for (Analyzer analyzer : createAnalyzers(solution.getSlug())) {
analyzer.analyze(solution, outputBuilder);
}
@@ -49,7 +49,6 @@ private static List<Analyzer> createAnalyzers(String slug) {
var analyzers = new ArrayList<Analyzer>();

analyzers.add(new GlobalAnalyzer());

switch (slug) {
case "annalyns-infiltration" -> analyzers.add(new AnnalynsInfiltrationAnalyzer());
case "hamming" -> analyzers.add(new HammingAnalyzer());
@@ -61,6 +60,7 @@ private static List<Analyzer> createAnalyzers(String slug) {
case "secrets" -> analyzers.add(new SecretsAnalyzer());
case "two-fer" -> analyzers.add(new TwoferAnalyzer());
case "wizards-and-warriors" -> analyzers.add(new WizardsAndWarriorsAnalyzer());
case "wizards-and-warriors-2" -> analyzers.add(new WizardsAndWarriors2Analyzer());
}

return List.copyOf(analyzers);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package analyzer.exercises.wizardsandwarriors2;

import analyzer.Comment;

public class ReuseCodeHardcodedThreeParameters extends Comment {

@Override
public String getKey() {
return "java.wizards-and-warriors-2.reuse_code_hardcoded_three_parameters";
}

@Override
public Type getType() {
return Type.ACTIONABLE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package analyzer.exercises.wizardsandwarriors2;

import analyzer.Comment;

public class ReuseCodeHardcodedTwoParameters extends Comment {

@Override
public String getKey() {
return "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters";
}

@Override
public Type getType() {
return Type.ACTIONABLE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package analyzer.exercises.wizardsandwarriors2;

import analyzer.Analyzer;
import analyzer.OutputCollector;
import analyzer.Solution;
import analyzer.comments.ExemplarSolution;
import analyzer.comments.PreferStringConcatenation;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;

import java.util.List;

/**
* The {@link WizardsAndWarriors2Analyzer} is the analyzer implementation for the {@code wizards-and-warriors-2} concept exercise.
*
* @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>
*/
public class WizardsAndWarriors2Analyzer extends VoidVisitorAdapter<OutputCollector> implements Analyzer {

private static final String EXERCISE_NAME = "Wizards and Warriors 2";
private static final String GAME_MASTER = "GameMaster";
private static final String DESCRIBE = "describe";
private static final String FORMAT = "format";
private static final String DESTINATION = "Destination";
private static final String TRAVEL_METHOD = "TravelMethod";
private static final String CHARACTER = "Character";

@Override
public void analyze(Solution solution, OutputCollector output) {

for (var compilationUnit : solution.getCompilationUnits()) {
compilationUnit.getClassByName(GAME_MASTER).ifPresent(c -> c.accept(this, output));
}

if (output.getComments().isEmpty()) {
output.addComment(new ExemplarSolution(EXERCISE_NAME));
}
}

@Override
public void visit(MethodDeclaration node, OutputCollector output) {

if(!node.getNameAsString().equals(DESCRIBE)) {
return;
}

if(node.getParameters().size() == 2 && !reuseMethod(node, 2)) {
output.addComment(new ReuseCodeHardcodedTwoParameters());
}

if(node.getParameters().size() == 3 && !reuseMethod(node, 3)) {
output.addComment(new ReuseCodeHardcodedThreeParameters());
}

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

super.visit(node, output);
}

private static boolean reuseMethod(MethodDeclaration node, int paramCount) {

List<String> params = node.getParameters().stream().map(Parameter::getTypeAsString).toList();
List<MethodCallExpr> describeCalls = node.findAll(MethodCallExpr.class).stream()
.filter(m -> m.getNameAsString().equals(DESCRIBE))
.toList();

if (paramCount == 2 && params.contains(DESTINATION) && params.contains(CHARACTER)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest using more descriptive names for some of the variables, for example:

  • paramTypes for params
  • DESTINATION_TYPE for DESTINATION
  • CHARACTER_TYPE for CHARACTER
  • TRAVEL_METHOD_TYPE for TRAVEL_METHOD

When I first read this method, I had initially thought params was the list of parameter names and DESTINATION was the name of the destination variable and CHARACTER was name of the character variable. It took me a while they were actually the names of the types (hence the suggestion).

return describeCalls.size() == 1 || describeCalls.size() == 3;
}

if (paramCount == 3 && params.contains(DESTINATION) && params.contains(TRAVEL_METHOD) && params.contains(CHARACTER)) {
return describeCalls.size() == 3;
}

return false;
}

private static boolean useStringFormat(MethodDeclaration node) {
return node.findAll(MethodCallExpr.class).stream()
.anyMatch(m -> m.getNameAsString().contains(FORMAT));
}

}
16 changes: 16 additions & 0 deletions src/test/java/analyzer/AnalyzerIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -205,4 +205,20 @@ void salarycalculator(String scenario) throws IOException {

Approvals.verify(serialize(output.analysis()), Approvals.NAMES.withParameters(scenario));
}

@ParameterizedTest
@ValueSource(strings = {
"ExemplarSolution",
"NotReuseMethod",
"PartialReuseOfMethod",
"UseStringFormat",
"UseStringFormatAndNotReuseMethod",
})
void wizardsandwarriors2(String scenario) throws IOException {
var path = Path.of("wizards-and-warriors-2", scenario + ".java");
var solution = new SolutionFromFiles("wizards-and-warriors-2", SCENARIOS.resolve(path));
var output = AnalyzerRoot.analyze(solution);

Approvals.verify(serialize(output.analysis()), Approvals.NAMES.withParameters(scenario));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"comments": [
{
"comment": "java.general.exemplar",
"params": {
"exerciseName": "Wizards and Warriors 2"
},
"type": "celebratory"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"comments": [
{
"comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_three_parameters",
"params": {},
"type": "actionable"
},
{
"comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters",
"params": {},
"type": "actionable"
},
{
"comment": "java.general.feedback_request",
"params": {},
"type": "informative"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"comments": [
{
"comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters",
"params": {},
"type": "actionable"
},
{
"comment": "java.general.feedback_request",
"params": {},
"type": "informative"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"comments": [
{
"comment": "java.general.prefer_string_concatenation",
"params": {},
"type": "informative"
},
{
"comment": "java.general.feedback_request",
"params": {},
"type": "informative"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"comments": [
{
"comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_three_parameters",
"params": {},
"type": "actionable"
},
{
"comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters",
"params": {},
"type": "actionable"
},
{
"comment": "java.general.prefer_string_concatenation",
"params": {},
"type": "informative"
},
{
"comment": "java.general.feedback_request",
"params": {},
"type": "informative"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class GameMaster {

public String describe(Character character) {
return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points.";
}

public String describe(Destination destination) {
return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants.";
}

public String describe(TravelMethod travelMethod) {
if (travelMethod == TravelMethod.WALKING) {
return "You're traveling to your destination by walking.";
}
return "You're traveling to your destination on horseback.";
}

public String describe(Character character, Destination destination, TravelMethod travelMethod) {
return describe(character) + " " + describe(travelMethod) + " " + describe(destination);
}

public String describe(Character character, Destination destination) {
return describe(character, destination, TravelMethod.WALKING);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class GameMaster {

public String describe(Character character) {
return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points.";
}

public String describe(Destination destination) {
return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants.";
}

public String describe(TravelMethod travelMethod) {
if (travelMethod == TravelMethod.WALKING) {
return "You're traveling to your destination by walking.";
}
return "You're traveling to your destination on horseback.";
}

public String describe(Character character, Destination destination, TravelMethod travelMethod) {
return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points. " +
(travelMethod == TravelMethod.WALKING ? "You're traveling to your destination by walking. " : "You're traveling to your destination on horseback. ") +
"You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants.";
}

public String describe(Character character, Destination destination) {
return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points. " +
"You're traveling to your destination by walking. " +
"You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants.";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class GameMaster {

public String describe(Character character) {
return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points.";
}

public String describe(Destination destination) {
return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants.";
}

public String describe(TravelMethod travelMethod) {
if (travelMethod == TravelMethod.WALKING) {
return "You're traveling to your destination by walking.";
}
return "You're traveling to your destination on horseback.";
}

public String describe(Character character, Destination destination, TravelMethod travelMethod) {
return describe(character) + " " + describe(travelMethod) + " " + describe(destination);
}

public String describe(Character character, Destination destination) {
return describe(character) + " " + describe(destination) + " You're traveling to your destination by walking.";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
public class GameMaster {

public String describe(Character character) {
return "You're a level %d %s with %d hit points.".formatted(character.getLevel(),
character.getCharacterClass(), character.getHitPoints());
}

public String describe(Destination destination) {
return "You've arrived at %s, which has %d inhabitants.".formatted(destination.getName(),
destination.getInhabitants());
}

public String describe(TravelMethod travelMethod) {
if (travelMethod == TravelMethod.WALKING) {
return "You're traveling to your destination by walking.";
}
return "You're traveling to your destination on horseback.";

}

public String describe(Character character, Destination destination, TravelMethod travelMethod) {
return "%s %s %s".formatted(describe(character), describe(travelMethod), describe(destination));
}

public String describe(Character character, Destination destination) {
return describe(character, destination, TravelMethod.WALKING);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public class GameMaster {

public String describe(Character character) {
return "You're a level %d %s with %d hit points.".formatted(character.getLevel(),
character.getCharacterClass(), character.getHitPoints());
}

public String describe(Destination destination) {
return "You've arrived at %s, which has %d inhabitants.".formatted(destination.getName(),
destination.getInhabitants());
}

public String describe(TravelMethod travelMethod) {
if (travelMethod == TravelMethod.WALKING) {
return "You're traveling to your destination by walking.";
}
return "You're traveling to your destination on horseback.";

}

public String describe(Character character, Destination destination, TravelMethod travelMethod) {
return "You're a level %d %s with %d hit points. ".formatted(character.getLevel(),
character.getCharacterClass(), character.getHitPoints()) +
(travelMethod == TravelMethod.WALKING ? "You're traveling to your destination by walking. "
: "You're traveling to your destination on horseback. ") +
"You've arrived at %s, which has %d inhabitants.".formatted(destination.getName(),
destination.getInhabitants());
}

public String describe(Character character, Destination destination) {
return "You're a level %d %s with %d hit points. You're traveling to your destination by walking. You've arrived at %s, which has %d inhabitants."
.formatted(character.getLevel(), character.getCharacterClass(), character.getHitPoints(),
destination.getName(), destination.getInhabitants());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"authors": [
"sougat818"
],
"contributors": [
"jagdish-15",
"sanderploegsma"
],
"files": {
"solution": [
"src/main/java/GameMaster.java"
],
"test": [
"src/test/java/GameMasterTest.java"
],
"exemplar": [
".meta/src/reference/java/GameMaster.java"
],
"editor": [
"src/main/java/Character.java",
"src/main/java/Destination.java",
"src/main/java/TravelMethod.java"
],
"invalidator": [
"build.gradle"
]
},
"forked_from": [
"csharp/wizards-and-warriors-2"
],
"blurb": "Learn about method overloading by extending your favorite RPG."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"comments": [
{
"comment": "java.general.exemplar",
"params": {
"exerciseName": "Wizards and Warriors 2"
},
"type": "celebratory"
}
]
}
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,25 @@
public class GameMaster {

public String describe(Character character) {
return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points.";
}

public String describe(Destination destination) {
return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants.";
}

public String describe(TravelMethod travelMethod) {
if (travelMethod == TravelMethod.WALKING) {
return "You're traveling to your destination by walking.";
}
return "You're traveling to your destination on horseback.";
}

public String describe(Character character, Destination destination, TravelMethod travelMethod) {
return describe(character) + " " + describe(travelMethod) + " " + describe(destination);
}

public String describe(Character character, Destination destination) {
return describe(character, destination, TravelMethod.WALKING);
}
}
32 changes: 32 additions & 0 deletions tests/wizards-and-warriors-2/not-reuse-method/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"authors": [
"sougat818"
],
"contributors": [
"jagdish-15",
"sanderploegsma"
],
"files": {
"solution": [
"src/main/java/GameMaster.java"
],
"test": [
"src/test/java/GameMasterTest.java"
],
"exemplar": [
".meta/src/reference/java/GameMaster.java"
],
"editor": [
"src/main/java/Character.java",
"src/main/java/Destination.java",
"src/main/java/TravelMethod.java"
],
"invalidator": [
"build.gradle"
]
},
"forked_from": [
"csharp/wizards-and-warriors-2"
],
"blurb": "Learn about method overloading by extending your favorite RPG."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"comments": [
{
"comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_three_parameters",
"params": {},
"type": "actionable"
},
{
"comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters",
"params": {},
"type": "actionable"
},
{
"comment": "java.general.feedback_request",
"params": {},
"type": "informative"
}
]
}
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,29 @@
public class GameMaster {

public String describe(Character character) {
return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points.";
}

public String describe(Destination destination) {
return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants.";
}

public String describe(TravelMethod travelMethod) {
if (travelMethod == TravelMethod.WALKING) {
return "You're traveling to your destination by walking.";
}
return "You're traveling to your destination on horseback.";
}

public String describe(Character character, Destination destination, TravelMethod travelMethod) {
return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points. " +
(travelMethod == TravelMethod.WALKING ? "You're traveling to your destination by walking. " : "You're traveling to your destination on horseback. ") +
"You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants.";
}

public String describe(Character character, Destination destination) {
return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points. " +
"You're traveling to your destination by walking. " +
"You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants.";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"authors": [
"sougat818"
],
"contributors": [
"jagdish-15",
"sanderploegsma"
],
"files": {
"solution": [
"src/main/java/GameMaster.java"
],
"test": [
"src/test/java/GameMasterTest.java"
],
"exemplar": [
".meta/src/reference/java/GameMaster.java"
],
"editor": [
"src/main/java/Character.java",
"src/main/java/Destination.java",
"src/main/java/TravelMethod.java"
],
"invalidator": [
"build.gradle"
]
},
"forked_from": [
"csharp/wizards-and-warriors-2"
],
"blurb": "Learn about method overloading by extending your favorite RPG."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"comments": [
{
"comment": "java.general.prefer_string_concatenation",
"params": {},
"type": "informative"
},
{
"comment": "java.general.feedback_request",
"params": {},
"type": "informative"
}
]
}
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,28 @@
public class GameMaster {

public String describe(Character character) {
return "You're a level %d %s with %d hit points.".formatted(character.getLevel(),
character.getCharacterClass(), character.getHitPoints());
}

public String describe(Destination destination) {
return "You've arrived at %s, which has %d inhabitants.".formatted(destination.getName(),
destination.getInhabitants());
}

public String describe(TravelMethod travelMethod) {
if (travelMethod == TravelMethod.WALKING) {
return "You're traveling to your destination by walking.";
}
return "You're traveling to your destination on horseback.";

}

public String describe(Character character, Destination destination, TravelMethod travelMethod) {
return "%s %s %s".formatted(describe(character), describe(travelMethod), describe(destination));
}

public String describe(Character character, Destination destination) {
return describe(character, destination, TravelMethod.WALKING);
}
}