Skip to content

Commit

Permalink
Apllying suggestions
Browse files Browse the repository at this point in the history
Renaming comment of string format to prefer_string_concatenation
Adding another helper method callsMethod
Making comment about substrings general
Adding another tests case for when substring is not being used in both methods
Making the analyzer to return only the hardcode and substring method if those trigger, so we do not overload the student of comments
  • Loading branch information
manumafe98 committed Feb 21, 2024
1 parent 417ce30 commit 01161d9
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 67 deletions.

This file was deleted.

22 changes: 12 additions & 10 deletions src/main/java/analyzer/exercises/loglevels/LogLevelsAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public void analyze(Solution solution, OutputCollector output) {
public void visit(MethodDeclaration node, OutputCollector output) {
if (containsHarcodedString(node)) {
output.addComment(new DoNotHardcodeLogLevels());
return;
}

if (!node.getNameAsString().equals(REFORMAT) && doesNotCallMethod(node, SUBSTRING)) {
output.addComment(new UseSubstringMethod());
return;
}

if (node.getNameAsString().equals(REFORMAT) && doesNotCallMethod(node, MESSAGE)) {
Expand All @@ -52,16 +58,8 @@ public void visit(MethodDeclaration node, OutputCollector output) {
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());
if (node.getNameAsString().equals(REFORMAT) && callsMethod(node, FORMAT)) {
output.addComment(new PreferStringConcatenation());
}

super.visit(node, output);
Expand All @@ -78,4 +76,8 @@ private static boolean containsHarcodedString(MethodDeclaration node) {
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();
}
}
18 changes: 0 additions & 18 deletions src/main/java/analyzer/exercises/loglevels/UseSubstringMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,16 @@

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;
Expand Down
1 change: 1 addition & 0 deletions src/test/java/analyzer/AnalyzerIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ void needforspeed(String scenario) throws IOException {
"NoReuseOfBothMethods",
"NotUsingSubstringOnLogLevel",
"NotUsingSubstringOnMessage",
"NotUsingSubstringOnBothMethods",
"UsingStringFormat"
})
void loglevels(String scenario) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@
"params": {},
"type": "essential"
},
{
"comment": "java.log-levels.use_substring_method",
"params": {
"callingMethod": "message",
"methodToCall": "substring"
},
"type": "actionable"
},
{
"comment": "java.general.feedback_request",
"params": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
"comments": [
{
"comment": "java.log-levels.use_substring_method",
"params": {
"callingMethod": "logLevel",
"methodToCall": "substring"
},
"params": {},
"type": "actionable"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
"comments": [
{
"comment": "java.log-levels.use_substring_method",
"params": {
"callingMethod": "message",
"methodToCall": "substring"
},
"params": {},
"type": "actionable"
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"comments": [
{
"comment": "java.log-levels.avoid_using_string_format",
"comment": "java.log-levels.prefer_string_concatenation",
"params": {},
"type": "informative"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

public class LogLevels {
public static String message(String logLine) {
return logLine.split("]: ")[1]
.trim();
return logLine.split("]: ")[1].trim();
}

public static String logLevel(String logLine) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"comments": [
{
"comment": "java.log-levels.avoid_using_string_format",
"comment": "java.log-levels.prefer_string_concatenation",
"params": {},
"type": "informative"
},
Expand Down

0 comments on commit 01161d9

Please sign in to comment.