Skip to content

Commit

Permalink
Updating log levels analyzer to allow usage of split (#160)
Browse files Browse the repository at this point in the history
* Updating log levels analyzer to allow usage of split without leaving a comment

* Leaving a comment to use substring when detecting that neither split or substring have been used
  • Loading branch information
manumafe98 authored Apr 9, 2024
1 parent b09c4e0 commit f5ba7ba
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class LogLevelsAnalyzer extends VoidVisitorAdapter<OutputCollector> imple
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";
private static List<String> EXPECTED_METHODS = List.of("substring", "split");

@Override
public void analyze(Solution solution, OutputCollector output) {
Expand All @@ -48,7 +48,7 @@ public void visit(MethodDeclaration node, OutputCollector output) {
return;
}

if (!node.getNameAsString().equals(REFORMAT) && doesNotCallMethod(node, SUBSTRING)) {
if (!node.getNameAsString().equals(REFORMAT) && doesNotCallMethods(node, EXPECTED_METHODS)) {
output.addComment(new UseSubstringMethod(node.getNameAsString()));
return;
}
Expand Down Expand Up @@ -81,6 +81,10 @@ private static boolean doesNotCallMethod(MethodDeclaration node, String otherMet
return node.findAll(MethodCallExpr.class, x -> x.getNameAsString().contains(otherMethodName)).isEmpty();
}

private static boolean doesNotCallMethods(MethodDeclaration node, List<String> allowedMethods) {
return allowedMethods.stream().allMatch(method -> doesNotCallMethod(node, method));
}

private static boolean callsMethod(MethodDeclaration node, String otherMethodName) {
return !node.findAll(MethodCallExpr.class, x -> x.getNameAsString().contains(otherMethodName)).isEmpty();
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/analyzer/AnalyzerIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ void needforspeed(String scenario) throws IOException {
"NoReuseLogLevel",
"NoReuseMessage",
"NoReuseOfBothMethods",
"NotUsingSubstringOnLogLevel",
"NotUsingSubstringOnMessage",
"NotUsingSubstringOnBothMethods",
"NotUsingExpectedMethodsOnLogLevel",
"NotUsingExpectedMethodsOnMessage",
"NotUsingExpectedMethodsOnLogLevelAndMessage",
"UsingStringFormat"
})
void loglevels(String scenario) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package scenarios.loglevels;

public class LogLevels {

public static String message(String logLine) {
return logLine.substring(logLine.indexOf(":") + 1).trim();
}

public static String logLevel(String logLine) {
int closingBracketIndex = logLine.indexOf("]");
if (closingBracketIndex == -1) {
return logLine.toLowerCase();
}

StringBuilder levelBuilder = new StringBuilder();
for (int i = 0; i <= closingBracketIndex; i++) {
levelBuilder.append(logLine.charAt(i));
}

return levelBuilder.toString().replace("[", "").replace("]", "").toLowerCase();
}


public static String reformat(String logLine) {
return message(logLine) + " (" + logLevel(logLine) + ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package scenarios.loglevels;

public class LogLevels {

public static String message(String logLine) {
int colonIndex = logLine.indexOf("]: ");
if (colonIndex == -1) {
return logLine;
}

StringBuilder messageBuilder = new StringBuilder();
for (int i = colonIndex + 3; i < logLine.length(); i++) {
messageBuilder.append(logLine.charAt(i));
}

return messageBuilder.toString().trim();
}

public static String logLevel(String logLine) {
int closingBracketIndex = logLine.indexOf("]");
if (closingBracketIndex == -1) {
return logLine.toLowerCase();
}

StringBuilder levelBuilder = new StringBuilder();
for (int i = 0; i <= closingBracketIndex; i++) {
levelBuilder.append(logLine.charAt(i));
}

return levelBuilder.toString().replace("[", "").replace("]", "").toLowerCase();
}


public static String reformat(String logLine) {
return message(logLine) + " (" + logLevel(logLine) + ")";
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package scenarios.loglevels;

public class LogLevels {

public static String message(String logLine) {
return logLine.split("]: ")[1].trim();
int colonIndex = logLine.indexOf("]: ");
if (colonIndex == -1) {
return logLine;
}

StringBuilder messageBuilder = new StringBuilder();
for (int i = colonIndex + 3; i < logLine.length(); i++) {
messageBuilder.append(logLine.charAt(i));
}

return messageBuilder.toString().trim();
}

public static String logLevel(String logLine) {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class LogLevels {
public static String message(String logLine) {
int colonIndex = logLine.indexOf("]: ");
if (colonIndex == -1) {
return logLine;
}

StringBuilder messageBuilder = new StringBuilder();
for (int i = colonIndex + 3; i < logLine.length(); i++) {
messageBuilder.append(logLine.charAt(i));
}

return messageBuilder.toString().trim();
}

public static String logLevel(String logLine) {
return logLine.substring(1, logLine.indexOf("]")).toLowerCase();
}

public static String reformat(String logLine) {
return message(logLine) + " (" + logLevel(logLine) + ")";
}
}
13 changes: 0 additions & 13 deletions tests/log-levels/no-substring-used/src/main/java/LogLevels.java

This file was deleted.

0 comments on commit f5ba7ba

Please sign in to comment.