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 @@ -36,6 +36,7 @@
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

Expand All @@ -48,6 +49,7 @@
summary = "Prefer using type-safe safe-logging loggers rather than safety-oblivious implementations.")
public final class PreferSafeLogger extends BugChecker implements BugChecker.VariableTreeMatcher {

private static final int MAX_SUPPORTED_ARGS = 10;
private static final Matcher<ExpressionTree> SLF4J_METHOD =
MethodMatchers.instanceMethod().onDescendantOf("org.slf4j.Logger");

Expand Down Expand Up @@ -78,6 +80,7 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
}
Symbol.VarSymbol sym = ASTHelpers.getSymbol(tree);
AtomicBoolean foundUnknownUsage = new AtomicBoolean();
SuggestedFix.Builder fix = SuggestedFix.builder();
new TreeScanner<Void, Void>() {
@Override
public Void visitIdentifier(IdentifierTree tree, Void _unused) {
Expand All @@ -100,7 +103,7 @@ public Void visitMethodInvocation(MethodInvocationTree tree, Void _unused) {
if (SLF4J_METHOD.matches(tree, state)) {
ExpressionTree receiver = ASTHelpers.getReceiver(tree);
if (sym.equals(ASTHelpers.getSymbol(receiver))) {
if (!isSafeSlf4jInteraction(tree, state)) {
if (!isSafeSlf4jInteraction(tree, fix, state)) {
foundUnknownUsage.set(true);
} else {
// Scan arguments for findings that may not compile
Expand All @@ -115,7 +118,6 @@ public Void visitMethodInvocation(MethodInvocationTree tree, Void _unused) {
if (foundUnknownUsage.get()) {
return Description.NO_MATCH;
}
SuggestedFix.Builder fix = SuggestedFix.builder();
String qualifiedLogger = SuggestedFixes.qualifyType(state, fix, "com.palantir.logsafe.logger.SafeLogger");
String qualifiedFactory =
SuggestedFixes.qualifyType(state, fix, "com.palantir.logsafe.logger.SafeLoggerFactory");
Expand All @@ -125,7 +127,9 @@ public Void visitMethodInvocation(MethodInvocationTree tree, Void _unused) {
return buildDescription(tree).addFix(fix.build()).build();
}

private static boolean isSafeSlf4jInteraction(MethodInvocationTree tree, VisitorState state) {
@SuppressWarnings("checkstyle:CyclomaticComplexity")
private static boolean isSafeSlf4jInteraction(
MethodInvocationTree tree, SuggestedFix.Builder fix, VisitorState state) {
if (!SLF4J_SAFE_METHODS.matches(tree, state)) {
return false;
}
Expand All @@ -140,16 +144,37 @@ private static boolean isSafeSlf4jInteraction(MethodInvocationTree tree, Visitor
}
Type argType = state.getTypeFromString("com.palantir.logsafe.Arg");
Type throwableType = state.getTypeFromString(Throwable.class.getName());
int args = 0;
int firstArgStartPosition = -1;
int lastArgEndPosition = -1;
for (int i = 1; i < arguments.size(); i++) {
Type type = ASTHelpers.getType(arguments.get(i));
Tree argument = arguments.get(i);
Type type = ASTHelpers.getType(argument);
boolean isArg = ASTHelpers.isSubtype(type, argType, state);
if (isArg) {
if (i == 1) {
firstArgStartPosition = ASTHelpers.getStartPosition(argument);
}
args++;
lastArgEndPosition = state.getEndPosition(argument);
}
boolean valid = isArg
// Throwable is valid only in the last position
|| (ASTHelpers.isSubtype(type, throwableType, state) && i == arguments.size() - 1);
if (!valid) {
return false;
}
}
if (args > MAX_SUPPORTED_ARGS) {
// Update the call to wrap args with 'Arrays.asList' so the result will compile.
CharSequence argsSource = state.getSourceCode().subSequence(firstArgStartPosition, lastArgEndPosition);
String qualifiedArrays = SuggestedFixes.qualifyType(state, fix, Arrays.class.getName());
fix.replace(
firstArgStartPosition,
lastArgEndPosition,
String.format("%s.asList(%s)", qualifiedArrays, argsSource));
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,106 @@ void testIsTraceEnabledInArg() {
"}")
.doTest();
}

@Test
void testTooManyArgs() {
fix().addInputLines(
"Test.java",
"import com.palantir.logsafe.*;",
"import org.slf4j.*;",
"class Test {",
" private static final Logger log = LoggerFactory.getLogger(Test.class);",
" void action() {",
" log.info(\"msg\",",
" SafeArg.of(\"1\", 1),",
" SafeArg.of(\"2\", 2),",
" SafeArg.of(\"3\", 3),",
" SafeArg.of(\"4\", 4),",
" SafeArg.of(\"5\", 5),",
" SafeArg.of(\"6\", 6),",
" SafeArg.of(\"7\", 7),",
" SafeArg.of(\"8\", 8),",
" SafeArg.of(\"9\", 9),",
" SafeArg.of(\"10\", 10),",
" SafeArg.of(\"11\", 11));",
" }",
"}")
.addOutputLines(
"Test.java",
"import com.palantir.logsafe.*;",
"import com.palantir.logsafe.logger.SafeLogger;",
"import com.palantir.logsafe.logger.SafeLoggerFactory;",
"import java.util.Arrays;",
"import org.slf4j.*;",
"class Test {",
" private static final SafeLogger log = SafeLoggerFactory.get(Test.class);",
" void action() {",
" log.info(\"msg\",",
" Arrays.asList(SafeArg.of(\"1\", 1),",
" SafeArg.of(\"2\", 2),",
" SafeArg.of(\"3\", 3),",
" SafeArg.of(\"4\", 4),",
" SafeArg.of(\"5\", 5),",
" SafeArg.of(\"6\", 6),",
" SafeArg.of(\"7\", 7),",
" SafeArg.of(\"8\", 8),",
" SafeArg.of(\"9\", 9),",
" SafeArg.of(\"10\", 10),",
" SafeArg.of(\"11\", 11)));",
" }",
"}")
.doTest();
}

@Test
void testTooManyArgsWithThrowable() {
fix().addInputLines(
"Test.java",
"import com.palantir.logsafe.*;",
"import org.slf4j.*;",
"class Test {",
" private static final Logger log = LoggerFactory.getLogger(Test.class);",
" void action(Throwable t) {",
" log.info(\"msg\",",
" SafeArg.of(\"1\", 1),",
" SafeArg.of(\"2\", 2),",
" SafeArg.of(\"3\", 3),",
" SafeArg.of(\"4\", 4),",
" SafeArg.of(\"5\", 5),",
" SafeArg.of(\"6\", 6),",
" SafeArg.of(\"7\", 7),",
" SafeArg.of(\"8\", 8),",
" SafeArg.of(\"9\", 9),",
" SafeArg.of(\"10\", 10),",
" SafeArg.of(\"11\", 11),",
" t);",
" }",
"}")
.addOutputLines(
"Test.java",
"import com.palantir.logsafe.*;",
"import com.palantir.logsafe.logger.SafeLogger;",
"import com.palantir.logsafe.logger.SafeLoggerFactory;",
"import java.util.Arrays;",
"import org.slf4j.*;",
"class Test {",
" private static final SafeLogger log = SafeLoggerFactory.get(Test.class);",
" void action(Throwable t) {",
" log.info(\"msg\",",
" Arrays.asList(SafeArg.of(\"1\", 1),",
" SafeArg.of(\"2\", 2),",
" SafeArg.of(\"3\", 3),",
" SafeArg.of(\"4\", 4),",
" SafeArg.of(\"5\", 5),",
" SafeArg.of(\"6\", 6),",
" SafeArg.of(\"7\", 7),",
" SafeArg.of(\"8\", 8),",
" SafeArg.of(\"9\", 9),",
" SafeArg.of(\"10\", 10),",
" SafeArg.of(\"11\", 11)),",
" t);",
" }",
"}")
.doTest();
}
}
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1866.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: PreferSafeLogger produces suggestions which compile with >10 args
links:
- https://github.com/palantir/gradle-baseline/pull/1866