-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into ImplementAnalyzerForNeedForSpeed
- Loading branch information
Showing
37 changed files
with
624 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/analyzer/exercises/annalynsinfiltration/AnnalynsInfiltrationAnalyzer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package analyzer.exercises.annalynsinfiltration; | ||
|
||
import analyzer.Analyzer; | ||
import analyzer.OutputCollector; | ||
import analyzer.Solution; | ||
import analyzer.comments.ExemplarSolution; | ||
import com.github.javaparser.ast.CompilationUnit; | ||
import com.github.javaparser.ast.Node; | ||
import com.github.javaparser.ast.expr.BinaryExpr; | ||
import com.github.javaparser.ast.expr.BooleanLiteralExpr; | ||
import com.github.javaparser.ast.expr.EnclosedExpr; | ||
import com.github.javaparser.ast.stmt.IfStmt; | ||
import com.github.javaparser.ast.stmt.ReturnStmt; | ||
import com.github.javaparser.ast.visitor.VoidVisitorAdapter; | ||
|
||
/** | ||
* The {@link AnnalynsInfiltrationAnalyzer} is the analyzer implementation for the {@code annalyns-infiltration} practice exercise. | ||
* It extends from the {@link VoidVisitorAdapter} and uses the visitor pattern to traverse each compilation unit. | ||
* | ||
* @see <a href="https://github.com/exercism/java/tree/main/exercises/practice/annalyns-infiltration">The Annalyns Infiltration exercise on the Java track</a> | ||
*/ | ||
public class AnnalynsInfiltrationAnalyzer extends VoidVisitorAdapter<OutputCollector> implements Analyzer { | ||
private static final String EXERCISE_NAME = "Annalyn's Infiltration"; | ||
|
||
@Override | ||
public void analyze(Solution solution, OutputCollector output) { | ||
for (CompilationUnit compilationUnit : solution.getCompilationUnits()) { | ||
compilationUnit.accept(this, output); | ||
} | ||
|
||
if (output.getComments().isEmpty()) { | ||
output.addComment(new ExemplarSolution(EXERCISE_NAME)); | ||
} | ||
} | ||
|
||
@Override | ||
public void visit(IfStmt node, OutputCollector output) { | ||
output.addComment(new AvoidIfStatements()); | ||
super.visit(node, output); | ||
} | ||
|
||
@Override | ||
public void visit(ReturnStmt node, OutputCollector output) { | ||
if (node.getExpression().isPresent() && node.getExpression().get() instanceof BooleanLiteralExpr) { | ||
output.addComment(new AvoidBooleanLiteralReturns()); | ||
} | ||
super.visit(node, output); | ||
} | ||
|
||
@Override | ||
public void visit(BinaryExpr node, OutputCollector output) { | ||
if (node.getOperator().equals(BinaryExpr.Operator.OR)) { | ||
for (Node childNode : node.getChildNodes()) { | ||
if (childNode instanceof EnclosedExpr | ||
&& ((EnclosedExpr) childNode).getInner() instanceof BinaryExpr | ||
&& ((BinaryExpr) ((EnclosedExpr) childNode).getInner()).getOperator().equals(BinaryExpr.Operator.AND)) { | ||
output.addComment(new RedundantParentheses()); | ||
} | ||
} | ||
} | ||
if (node.getOperator().equals(BinaryExpr.Operator.EQUALS) | ||
&& (node.getLeft() instanceof BooleanLiteralExpr || node.getRight() instanceof BooleanLiteralExpr)) { | ||
output.addComment(new AvoidComparisonWithLiteral()); | ||
} | ||
super.visit(node, output); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/analyzer/exercises/annalynsinfiltration/AvoidBooleanLiteralReturns.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package analyzer.exercises.annalynsinfiltration; | ||
|
||
import analyzer.Comment; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/annalyns-infiltration/avoid_boolean_literal_returns.md">Markdown Template</a> | ||
*/ | ||
public class AvoidBooleanLiteralReturns extends Comment { | ||
@Override | ||
public String getKey() { | ||
return "java.annalyns-infiltration.avoid_boolean_literal_returns"; | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return Type.ESSENTIAL; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/analyzer/exercises/annalynsinfiltration/AvoidComparisonWithLiteral.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package analyzer.exercises.annalynsinfiltration; | ||
|
||
import analyzer.Comment; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/annalyns-infiltration/avoid_comparison_with_literal.md">Markdown Template</a> | ||
*/ | ||
public class AvoidComparisonWithLiteral extends Comment { | ||
@Override | ||
public String getKey() { | ||
return "java.annalyns-infiltration.avoid_comparison_with_literal"; | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return Type.ESSENTIAL; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/analyzer/exercises/annalynsinfiltration/AvoidIfStatements.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package analyzer.exercises.annalynsinfiltration; | ||
|
||
import analyzer.Comment; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/annalyns-infiltration/avoid_if_statements.md">Markdown Template</a> | ||
*/ | ||
public class AvoidIfStatements extends Comment { | ||
@Override | ||
public String getKey() { | ||
return "java.annalyns-infiltration.avoid_if_statements"; | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return Type.ESSENTIAL; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/analyzer/exercises/annalynsinfiltration/RedundantParentheses.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package analyzer.exercises.annalynsinfiltration; | ||
|
||
import analyzer.Comment; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/annalyns-infiltration/some_parentheses_are_redundant.md">Markdown Template</a> | ||
*/ | ||
public class RedundantParentheses extends Comment { | ||
@Override | ||
public String getKey() { | ||
return "java.annalyns-infiltration.some_parentheses_are_redundant"; | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return Type.INFORMATIVE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...zer/AnalyzerIntegrationTest.annalynsinfiltration.ComparingBooleanWithLiteral.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.annalyns-infiltration.avoid_comparison_with_literal", | ||
"params": {}, | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
11 changes: 11 additions & 0 deletions
11
...test/resources/analyzer/AnalyzerIntegrationTest.annalynsinfiltration.Optimal.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.general.exemplar", | ||
"params": { | ||
"exerciseName": "Annalyn\u0027s Infiltration" | ||
}, | ||
"type": "celebratory" | ||
} | ||
] | ||
} |
19 changes: 19 additions & 0 deletions
19
...nalyzer/AnalyzerIntegrationTest.annalynsinfiltration.ReturningBooleanLiteral.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.annalyns-infiltration.avoid_if_statements", | ||
"params": {}, | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.annalyns-infiltration.avoid_boolean_literal_returns", | ||
"params": {}, | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
19 changes: 19 additions & 0 deletions
19
...urces/analyzer/AnalyzerIntegrationTest.annalynsinfiltration.UsingIfStatement.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.annalyns-infiltration.avoid_if_statements", | ||
"params": {}, | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.annalyns-infiltration.avoid_boolean_literal_returns", | ||
"params": {}, | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
14 changes: 14 additions & 0 deletions
14
...lyzer/AnalyzerIntegrationTest.annalynsinfiltration.UsingRedundantParenthesis.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.annalyns-infiltration.some_parentheses_are_redundant", | ||
"params": {}, | ||
"type": "informative" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/resources/scenarios/annalynsinfiltration/ComparingBooleanWithLiteral.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class AnnalynsInfiltration { | ||
public static boolean canFastAttack(boolean knightIsAwake) { | ||
return !knightIsAwake; | ||
} | ||
|
||
public static boolean canSpy(boolean knightIsAwake, boolean archerIsAwake, boolean prisonerIsAwake) { | ||
return knightIsAwake || archerIsAwake || prisonerIsAwake; | ||
} | ||
|
||
public static boolean canSignalPrisoner(boolean archerIsAwake, boolean prisonerIsAwake) { | ||
return prisonerIsAwake == true && archerIsAwake == false; | ||
} | ||
|
||
public static boolean canFreePrisoner(boolean knightIsAwake, boolean archerIsAwake, boolean prisonerIsAwake, boolean petDogIsPresent) { | ||
return !archerIsAwake && (petDogIsPresent || prisonerIsAwake && !knightIsAwake); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/resources/scenarios/annalynsinfiltration/Optimal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class AnnalynsInfiltration { | ||
public static boolean canFastAttack(boolean knightIsAwake) { | ||
return !knightIsAwake; | ||
} | ||
|
||
public static boolean canSpy(boolean knightIsAwake, boolean archerIsAwake, boolean prisonerIsAwake) { | ||
return knightIsAwake || archerIsAwake || prisonerIsAwake; | ||
} | ||
|
||
public static boolean canSignalPrisoner(boolean archerIsAwake, boolean prisonerIsAwake) { | ||
return prisonerIsAwake && !archerIsAwake; | ||
} | ||
|
||
public static boolean canFreePrisoner(boolean knightIsAwake, boolean archerIsAwake, boolean prisonerIsAwake, boolean petDogIsPresent) { | ||
return !archerIsAwake && (petDogIsPresent || prisonerIsAwake && !knightIsAwake); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/test/resources/scenarios/annalynsinfiltration/ReturningBooleanLiteral.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class AnnalynsInfiltration { | ||
public static boolean canFastAttack(boolean knightIsAwake) { | ||
return !knightIsAwake; | ||
} | ||
|
||
public static boolean canSpy(boolean knightIsAwake, boolean archerIsAwake, boolean prisonerIsAwake) { | ||
if(knightIsAwake) return true; | ||
if(archerIsAwake) return true; | ||
if(prisonerIsAwake) return true; | ||
return false; | ||
} | ||
|
||
public static boolean canSignalPrisoner(boolean archerIsAwake, boolean prisonerIsAwake) { | ||
return prisonerIsAwake && !archerIsAwake; | ||
} | ||
|
||
public static boolean canFreePrisoner(boolean knightIsAwake, boolean archerIsAwake, boolean prisonerIsAwake, boolean petDogIsPresent) { | ||
return !archerIsAwake && (petDogIsPresent || prisonerIsAwake && !knightIsAwake); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/test/resources/scenarios/annalynsinfiltration/UsingIfStatement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class AnnalynsInfiltration { | ||
public static boolean canFastAttack(boolean knightIsAwake) { | ||
return !knightIsAwake; | ||
} | ||
|
||
public static boolean canSpy(boolean knightIsAwake, boolean archerIsAwake, boolean prisonerIsAwake) { | ||
return knightIsAwake || archerIsAwake || prisonerIsAwake; | ||
} | ||
|
||
public static boolean canSignalPrisoner(boolean archerIsAwake, boolean prisonerIsAwake) { | ||
return prisonerIsAwake && !archerIsAwake; | ||
} | ||
|
||
public static boolean canFreePrisoner(boolean knightIsAwake, boolean archerIsAwake, boolean prisonerIsAwake, boolean petDogIsPresent) { | ||
if(!archerIsAwake){ | ||
return petDogIsPresent || prisonerIsAwake && !knightIsAwake; | ||
} | ||
return false; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/resources/scenarios/annalynsinfiltration/UsingRedundantParenthesis.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class AnnalynsInfiltration { | ||
public static boolean canFastAttack(boolean knightIsAwake) { | ||
return !knightIsAwake; | ||
} | ||
|
||
public static boolean canSpy(boolean knightIsAwake, boolean archerIsAwake, boolean prisonerIsAwake) { | ||
return knightIsAwake || archerIsAwake || prisonerIsAwake; | ||
} | ||
|
||
public static boolean canSignalPrisoner(boolean archerIsAwake, boolean prisonerIsAwake) { | ||
return prisonerIsAwake && !archerIsAwake; | ||
} | ||
|
||
public static boolean canFreePrisoner(boolean knightIsAwake, boolean archerIsAwake, boolean prisonerIsAwake, boolean petDogIsPresent) { | ||
return !archerIsAwake && (petDogIsPresent || (prisonerIsAwake && !knightIsAwake)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/annalyns-infiltration/comparing-boolean-with-literal/.meta/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"authors": [ | ||
"mikedamay" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/main/java/AnnalynsInfiltration.java" | ||
], | ||
"test": [ | ||
"src/test/java/AnnalynsInfiltrationTest.java" | ||
], | ||
"exemplar": [ | ||
".meta/src/reference/java/AnnalynsInfiltration.java" | ||
], | ||
"invalidator": [ | ||
"build.gradle" | ||
] | ||
}, | ||
"forked_from": [ | ||
"csharp/annalyns-infiltration" | ||
], | ||
"blurb": "Learn about booleans while helping Annalyn rescue her friend." | ||
} |
Oops, something went wrong.