Skip to content

Commit

Permalink
Fix ChangeLogLevel to accommodate string concatenations
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Jul 12, 2023
1 parent 92576cc commit dbbd155
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
if(args.size() == 0) {
return m;
}
Expression arg = args.get(0);
if(!(arg instanceof J.Literal)) {
return m;
}
J.Literal lit = (J.Literal) arg;
if(lit.getValue() == null) {
J.Literal lit = leftMostLiteral(args.get(0));
if(lit == null || lit.getValue() == null) {
return m;
}
if(!StringUtils.isBlank(startsWith) && !lit.getValue().toString().startsWith(startsWith)) {
Expand All @@ -101,4 +97,15 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}
});
}

@Nullable
J.Literal leftMostLiteral(Expression arg) {
if(arg instanceof J.Literal) {
return (J.Literal) arg;
}
if(arg instanceof J.Binary) {
return leftMostLiteral(((J.Binary) arg).getLeft());
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,38 @@ void test() {
""")
);
}

@Test
void concatenatedString() {
rewriteRun(
//language=java
java(
"""
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class Test {
private static final Logger log = LoggerFactory.getLogger(Test.class);
void test() {
log.info("LaunchDarkly " + 1 + "Hello");
}
}
""",
"""
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class Test {
private static final Logger log = LoggerFactory.getLogger(Test.class);
void test() {
log.debug("LaunchDarkly " + 1 + "Hello");
}
}
""")
);
}


}

0 comments on commit dbbd155

Please sign in to comment.