Skip to content

Commit

Permalink
refactor: OpenRewrite recipe best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek and TeamModerne committed Dec 8, 2023
1 parent 65613a8 commit 19563ea
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
protected JavadocVisitor<ExecutionContext> getJavadocVisitor() {
return new JavadocVisitor<ExecutionContext>(this) {
@Override
public Javadoc visitDocComment(Javadoc.DocComment javadoc, ExecutionContext executionContext) {
public Javadoc visitDocComment(Javadoc.DocComment javadoc, ExecutionContext ctx) {
return javadoc;
}
};
}

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext p) {
J.MethodInvocation mi = super.visitMethodInvocation(method, p);
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if (!LOGGERFACTORY_GETLOGGER.matches(mi)) {
return mi;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Set<String> getTags() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesMethod<>(SLF4J_LOG), new JavaVisitor<ExecutionContext>() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (SLF4J_LOG.matches(method)) {
String name = method.getSimpleName();
if ("trace".equals(name) || "debug".equals(name) || "info".equals(name) || "warn".equals(name) || "error".equals(name)) {
Expand Down Expand Up @@ -112,7 +112,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext execu
}
}
}
return super.visitMethodInvocation(method, executionContext);
return super.visitMethodInvocation(method, ctx);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,28 @@ void basic() {
//language=java
java(
"""
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class Test {
private static final Logger log = LoggerFactory.getLogger(Test.class);
class Test {
private static final Logger log = LoggerFactory.getLogger(Test.class);
void test() {
log.info("LaunchDarkly Hello");
}
void test() {
log.info("LaunchDarkly Hello");
}
}
""",
"""
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class Test {
private static final Logger log = LoggerFactory.getLogger(Test.class);
class Test {
private static final Logger log = LoggerFactory.getLogger(Test.class);
void test() {
log.debug("LaunchDarkly Hello");
}
void test() {
log.debug("LaunchDarkly Hello");
}
}
""")
);
}
Expand All @@ -68,28 +68,28 @@ void concatenatedString() {
//language=java
java(
"""
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class Test {
private static final Logger log = LoggerFactory.getLogger(Test.class);
class Test {
private static final Logger log = LoggerFactory.getLogger(Test.class);
void test() {
log.info("LaunchDarkly " + 1 + "Hello");
}
void test() {
log.info("LaunchDarkly " + 1 + "Hello");
}
}
""",
"""
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class Test {
private static final Logger log = LoggerFactory.getLogger(Test.class);
class Test {
private static final Logger log = LoggerFactory.getLogger(Test.class);
void test() {
log.debug("LaunchDarkly " + 1 + "Hello");
}
void test() {
log.debug("LaunchDarkly " + 1 + "Hello");
}
}
""")
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ void test() {
void toStringWithoutSelect() {
//language=java
rewriteRun(
java("""
java(
"""
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class A {
Expand Down Expand Up @@ -320,7 +321,8 @@ void method() {
void notObjectToString() {
//language=java
rewriteRun(
java("""
java(
"""
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
Expand Down

0 comments on commit 19563ea

Please sign in to comment.