-
-
Notifications
You must be signed in to change notification settings - Fork 25
Adding analyzer for log-levels concept exercise
#136
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
Merged
manumafe98
merged 8 commits into
exercism:main
from
manumafe98:ImplementAnalyzerForLogLevels
Feb 29, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ce6df28
Adding analyzer for log-levels concept exercise
manumafe98 417ce30
Update src/main/java/analyzer/exercises/loglevels/LogLevelsAnalyzer.java
manumafe98 01161d9
Apllying suggestions
manumafe98 daa8d03
Adding missing files
manumafe98 cf8d131
Fixing smoke tests
manumafe98 c707caa
Applying suggestions
manumafe98 4196108
Adding missing files
manumafe98 ebb6d0f
Making ReuseCode a general comment and updating its usage from lasagn…
manumafe98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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/comments/PreferStringConcatenation.java
This file contains hidden or 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.comments; | ||
|
|
||
| import analyzer.Comment; | ||
|
|
||
| /** | ||
| * @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/general/prefer_string_concatenation.md">Markdown Template</a> | ||
| */ | ||
| public class PreferStringConcatenation extends Comment { | ||
|
|
||
| @Override | ||
| public String getKey() { | ||
| return "java.general.prefer_string_concatenation"; | ||
| } | ||
|
|
||
| @Override | ||
| public Type getType() { | ||
| return Type.INFORMATIVE; | ||
| } | ||
| } |
10 changes: 5 additions & 5 deletions
10
...analyzer/exercises/lasagna/ReuseCode.java → ...ain/java/analyzer/comments/ReuseCode.java
This file contains hidden or 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
This file contains hidden or 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
86 changes: 86 additions & 0 deletions
86
src/main/java/analyzer/exercises/loglevels/LogLevelsAnalyzer.java
This file contains hidden or 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,86 @@ | ||
| 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.AvoidHardCodedTestCases; | ||
| import analyzer.comments.ExemplarSolution; | ||
| import analyzer.comments.PreferStringConcatenation; | ||
| import analyzer.comments.ReuseCode; | ||
|
|
||
| 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 AvoidHardCodedTestCases()); | ||
| return; | ||
| } | ||
|
|
||
| if (!node.getNameAsString().equals(REFORMAT) && doesNotCallMethod(node, SUBSTRING)) { | ||
| output.addComment(new UseSubstringMethod()); | ||
| return; | ||
| } | ||
|
|
||
| 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(REFORMAT) && callsMethod(node, FORMAT)) { | ||
| output.addComment(new PreferStringConcatenation()); | ||
| } | ||
|
|
||
| super.visit(node, output); | ||
| } | ||
|
|
||
| private static boolean containsHarcodedString(MethodDeclaration node) { | ||
| List<StringLiteralExpr> hardcodedStrings = node.findAll(StringLiteralExpr.class, | ||
| x -> x.getValue().contains("ERROR") || x.getValue().contains("WARNING") | ||
| || x.getValue().contains("INFO")); | ||
|
|
||
| return hardcodedStrings.size() > 1; | ||
| } | ||
|
|
||
| private static boolean doesNotCallMethod(MethodDeclaration node, String otherMethodName) { | ||
| return node.findAll(MethodCallExpr.class, x -> x.getNameAsString().contains(otherMethodName)).isEmpty(); | ||
| } | ||
|
|
||
| private static boolean callsMethod(MethodDeclaration node, String otherMethodName) { | ||
| return !node.findAll(MethodCallExpr.class, x -> x.getNameAsString().contains(otherMethodName)).isEmpty(); | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/analyzer/exercises/loglevels/UseSubstringMethod.java
This file contains hidden or 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/use_substring_method.md">Markdown Template</a> | ||
| */ | ||
| class UseSubstringMethod extends Comment { | ||
|
|
||
| @Override | ||
| public String getKey() { | ||
| return "java.log-levels.use_substring_method"; | ||
| } | ||
|
|
||
| @Override | ||
| public Type getType() { | ||
| return Type.ACTIONABLE; | ||
| } | ||
| } |
This file contains hidden or 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
4 changes: 2 additions & 2 deletions
4
...test/resources/analyzer/AnalyzerIntegrationTest.lasagna.NoReuseOfBothMethods.approved.txt
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...rces/analyzer/AnalyzerIntegrationTest.lasagna.NoReuseOfExpectedMinutesInOven.approved.txt
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...s/analyzer/AnalyzerIntegrationTest.lasagna.NoReuseOfPreparationTimeInMinutes.approved.txt
This file contains hidden or 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 hidden or 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" | ||
| } | ||
| ] | ||
| } |
14 changes: 14 additions & 0 deletions
14
...est/resources/analyzer/AnalyzerIntegrationTest.loglevels.HardCodingLogLevels.approved.txt
This file contains hidden or 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.general.avoid_hard_coded_test_cases", | ||
| "params": {}, | ||
| "type": "essential" | ||
| }, | ||
| { | ||
| "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 hidden or 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.general.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 hidden or 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.general.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 hidden or 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.general.reuse_code", | ||
| "params": { | ||
| "callingMethod": "reformat", | ||
| "methodToCall": "message" | ||
| }, | ||
| "type": "actionable" | ||
| }, | ||
| { | ||
| "comment": "java.general.reuse_code", | ||
| "params": { | ||
| "callingMethod": "reformat", | ||
| "methodToCall": "logLevel" | ||
| }, | ||
| "type": "actionable" | ||
| }, | ||
| { | ||
| "comment": "java.general.feedback_request", | ||
| "params": {}, | ||
| "type": "informative" | ||
| } | ||
| ] | ||
| } |
14 changes: 14 additions & 0 deletions
14
...es/analyzer/AnalyzerIntegrationTest.loglevels.NotUsingSubstringOnBothMethods.approved.txt
This file contains hidden or 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.log-levels.use_substring_method", | ||
| "params": {}, | ||
| "type": "actionable" | ||
| }, | ||
| { | ||
| "comment": "java.general.feedback_request", | ||
| "params": {}, | ||
| "type": "informative" | ||
| } | ||
| ] | ||
| } |
14 changes: 14 additions & 0 deletions
14
...urces/analyzer/AnalyzerIntegrationTest.loglevels.NotUsingSubstringOnLogLevel.approved.txt
This file contains hidden or 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.log-levels.use_substring_method", | ||
| "params": {}, | ||
| "type": "actionable" | ||
| }, | ||
| { | ||
| "comment": "java.general.feedback_request", | ||
| "params": {}, | ||
| "type": "informative" | ||
| } | ||
| ] | ||
| } |
14 changes: 14 additions & 0 deletions
14
...ources/analyzer/AnalyzerIntegrationTest.loglevels.NotUsingSubstringOnMessage.approved.txt
This file contains hidden or 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.log-levels.use_substring_method", | ||
| "params": {}, | ||
| "type": "actionable" | ||
| }, | ||
| { | ||
| "comment": "java.general.feedback_request", | ||
| "params": {}, | ||
| "type": "informative" | ||
| } | ||
| ] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.