-
-
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.
Adding analyzer for log-levels concept exercise
- Loading branch information
1 parent
5e16028
commit ce6df28
Showing
43 changed files
with
769 additions
and
2 deletions.
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
19 changes: 19 additions & 0 deletions
19
src/main/java/analyzer/exercises/loglevels/AvoidUsingStringFormat.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,19 @@ | ||
package analyzer.exercises.loglevels; | ||
|
||
import analyzer.Comment; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/log-levels/avoid_using_string_format.md">Markdown Template</a> | ||
*/ | ||
class AvoidUsingStringFormat extends Comment { | ||
|
||
@Override | ||
public String getKey() { | ||
return "java.log-levels.avoid_using_string_format"; | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return Type.INFORMATIVE; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/analyzer/exercises/loglevels/DoNotHardcodeLogLevels.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,19 @@ | ||
package analyzer.exercises.loglevels; | ||
|
||
import analyzer.Comment; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/log-levels/do_not_hardcode_log_levels.md">Markdown Template</a> | ||
*/ | ||
class DoNotHardcodeLogLevels extends Comment{ | ||
|
||
@Override | ||
public String getKey() { | ||
return "java.log-levels.do_not_hardcode_log_levels"; | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return Type.ESSENTIAL; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/main/java/analyzer/exercises/loglevels/LogLevelsAnalyzer.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,81 @@ | ||
package analyzer.exercises.loglevels; | ||
|
||
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.visitor.VoidVisitorAdapter; | ||
|
||
import analyzer.Analyzer; | ||
import analyzer.OutputCollector; | ||
import analyzer.Solution; | ||
import analyzer.comments.ExemplarSolution; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* The {@link LogLevelsAnalyzer} is the analyzer implementation for the {@code log-levels} 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/concept/log-levels">The log-levels exercise on the Java track</a> | ||
*/ | ||
public class LogLevelsAnalyzer extends VoidVisitorAdapter<OutputCollector> implements Analyzer { | ||
private static final String EXERCISE_NAME = "Log Levels"; | ||
private static final String REFORMAT = "reformat"; | ||
private static final String MESSAGE = "message"; | ||
private static final String LOG_LEVEL = "logLevel"; | ||
private static final String SUBSTRING = "substring"; | ||
private static final String FORMAT = "format"; | ||
|
||
@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(MethodDeclaration node, OutputCollector output) { | ||
if (containsHarcodedString(node)) { | ||
output.addComment(new DoNotHardcodeLogLevels()); | ||
} | ||
|
||
if (node.getNameAsString().equals(REFORMAT) && doesNotCallMethod(node, MESSAGE)) { | ||
output.addComment(new ReuseCode(REFORMAT, MESSAGE)); | ||
} | ||
|
||
if (node.getNameAsString().equals(REFORMAT) && doesNotCallMethod(node, LOG_LEVEL)) { | ||
output.addComment(new ReuseCode(REFORMAT, LOG_LEVEL)); | ||
} | ||
|
||
if (node.getNameAsString().equals(MESSAGE) && doesNotCallMethod(node, SUBSTRING)) { | ||
output.addComment(new UseSubstringMethod(MESSAGE, SUBSTRING)); | ||
} | ||
|
||
if (node.getNameAsString().equals(LOG_LEVEL) && doesNotCallMethod(node, SUBSTRING)) { | ||
output.addComment(new UseSubstringMethod(LOG_LEVEL, SUBSTRING)); | ||
} | ||
|
||
if (node.getNameAsString().equals(REFORMAT) && !doesNotCallMethod(node, FORMAT)) { | ||
output.addComment(new AvoidUsingStringFormat()); | ||
} | ||
|
||
super.visit(node, output); | ||
} | ||
|
||
private static boolean containsHarcodedString(MethodDeclaration node) { | ||
List<StringLiteralExpr> hardcodedStrings = node.findAll(StringLiteralExpr.class, | ||
x -> x.getValue().equals("[ERROR]:") || x.getValue().equals("[WARNING]:") | ||
|| x.getValue().equals("[INFO]:")); | ||
|
||
return hardcodedStrings.size() > 1; | ||
} | ||
|
||
private static boolean doesNotCallMethod(MethodDeclaration node, String otherMethodName) { | ||
return node.findAll(MethodCallExpr.class, x -> x.getNameAsString().contains(otherMethodName)).isEmpty(); | ||
} | ||
} |
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,36 @@ | ||
package analyzer.exercises.loglevels; | ||
|
||
import analyzer.Comment; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/log-levels/reuse_code.md">Markdown Template</a> | ||
*/ | ||
class ReuseCode extends Comment { | ||
private final String callingMethod; | ||
private final String methodToCall; | ||
|
||
ReuseCode(String callingMethod, String methodToCall) { | ||
this.callingMethod = callingMethod; | ||
this.methodToCall = methodToCall; | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return "java.log-levels.reuse_code"; | ||
} | ||
|
||
@Override | ||
public Map<String, String> getParameters() { | ||
return Map.of( | ||
"callingMethod", this.callingMethod, | ||
"methodToCall", this.methodToCall | ||
); | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return Type.ACTIONABLE; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/analyzer/exercises/loglevels/UseSubstringMethod.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,37 @@ | ||
package analyzer.exercises.loglevels; | ||
|
||
import analyzer.Comment; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/log-levels/use_substring_method.md">Markdown Template</a> | ||
*/ | ||
class UseSubstringMethod extends Comment { | ||
private final String callingMethod; | ||
private final String methodToCall; | ||
|
||
|
||
UseSubstringMethod(String callingMethod, String methodToCall) { | ||
this.callingMethod = callingMethod; | ||
this.methodToCall = methodToCall; | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return "java.log-levels.use_substring_method"; | ||
} | ||
|
||
@Override | ||
public Map<String, String> getParameters() { | ||
return Map.of( | ||
"callingMethod", this.callingMethod, | ||
"methodToCall", this.methodToCall | ||
); | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return Type.ACTIONABLE; | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
src/test/resources/analyzer/AnalyzerIntegrationTest.loglevels.ExemplarSolution.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": "Log Levels" | ||
}, | ||
"type": "celebratory" | ||
} | ||
] | ||
} |
22 changes: 22 additions & 0 deletions
22
...est/resources/analyzer/AnalyzerIntegrationTest.loglevels.HardCodingLogLevels.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,22 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.log-levels.do_not_hardcode_log_levels", | ||
"params": {}, | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.log-levels.use_substring_method", | ||
"params": { | ||
"callingMethod": "message", | ||
"methodToCall": "substring" | ||
}, | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/resources/analyzer/AnalyzerIntegrationTest.loglevels.NoReuseLogLevel.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,17 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.log-levels.reuse_code", | ||
"params": { | ||
"callingMethod": "reformat", | ||
"methodToCall": "logLevel" | ||
}, | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/resources/analyzer/AnalyzerIntegrationTest.loglevels.NoReuseMessage.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,17 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.log-levels.reuse_code", | ||
"params": { | ||
"callingMethod": "reformat", | ||
"methodToCall": "message" | ||
}, | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
25 changes: 25 additions & 0 deletions
25
...st/resources/analyzer/AnalyzerIntegrationTest.loglevels.NoReuseOfBothMethods.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,25 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.log-levels.reuse_code", | ||
"params": { | ||
"callingMethod": "reformat", | ||
"methodToCall": "message" | ||
}, | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.log-levels.reuse_code", | ||
"params": { | ||
"callingMethod": "reformat", | ||
"methodToCall": "logLevel" | ||
}, | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
17 changes: 17 additions & 0 deletions
17
...urces/analyzer/AnalyzerIntegrationTest.loglevels.NotUsingSubstringOnLogLevel.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,17 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.log-levels.use_substring_method", | ||
"params": { | ||
"callingMethod": "logLevel", | ||
"methodToCall": "substring" | ||
}, | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
17 changes: 17 additions & 0 deletions
17
...ources/analyzer/AnalyzerIntegrationTest.loglevels.NotUsingSubstringOnMessage.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,17 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.log-levels.use_substring_method", | ||
"params": { | ||
"callingMethod": "message", | ||
"methodToCall": "substring" | ||
}, | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
Oops, something went wrong.