Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +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 FORMAT = "format";
private static final String STRING_FORMAT = "format";
private static final String STRING_FORMATTED = "formatted";
private static List<String> EXPECTED_METHODS = List.of("substring", "split");

@Override
Expand Down Expand Up @@ -61,7 +62,8 @@ public void visit(MethodDeclaration node, OutputCollector output) {
output.addComment(new ReuseCode(REFORMAT, LOG_LEVEL));
}

if (node.getNameAsString().equals(REFORMAT) && callsMethod(node, FORMAT)) {
if (node.getNameAsString().equals(REFORMAT)
&& (callsMethod(node, STRING_FORMAT) || callsMethod(node, STRING_FORMATTED))) {
output.addComment(new PreferStringConcatenation());
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/java/analyzer/AnalyzerIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ void needforspeed(String scenario) throws IOException {
"NotUsingExpectedMethodsOnLogLevel",
"NotUsingExpectedMethodsOnMessage",
"NotUsingExpectedMethodsOnLogLevelAndMessage",
"UsingStringFormat"
"UsingStringFormat",
"UsingStringFormatted"
})
void loglevels(String scenario) throws IOException {
var path = Path.of("log-levels", scenario + ".java");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"comments": [
{
"comment": "java.general.prefer_string_concatenation",
"params": {},
"type": "informative"
},
{
"comment": "java.general.feedback_request",
"params": {},
"type": "informative"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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) {
return logLine.substring(1, logLine.indexOf("]")).toLowerCase();
}

public static String reformat(String logLine) {
return "%s (%s)".formatted(message(logLine), logLevel(logLine));
}
}
Loading