Skip to content

Commit

Permalink
Simplify LoggersNamedForEnclosingClass in attempt to replicate issue (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Jul 24, 2024
1 parent 3492463 commit 62591d1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*/
package org.openrewrite.java.logging.slf4j;

import org.jetbrains.annotations.NotNull;
import org.openrewrite.*;
import org.openrewrite.java.*;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavadocVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
Expand Down Expand Up @@ -81,25 +85,22 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
Expression firstArgument = mi.getArguments().get(0);
if (firstArgument instanceof J.FieldAccess) {
String argumentClazzName = ((J.FieldAccess) firstArgument).toString();
if (enclosingClazzName.equals(argumentClazzName)) {
return mi;
if (argumentClazzName.endsWith(".class") && !enclosingClazzName.equals(argumentClazzName)) {
return replaceMethodArgument(mi, enclosingClazzName);
}
} else if (firstArgument instanceof J.MethodInvocation &&
"getClass".equals(((J.MethodInvocation) firstArgument).getName().toString())) {
if (!firstEnclosingClass.hasModifier(J.Modifier.Type.Final)) {
return mi;
if (firstEnclosingClass.hasModifier(J.Modifier.Type.Final)) {
return replaceMethodArgument(mi, enclosingClazzName);
}
} else {
return mi;
}

return JavaTemplate.builder("LoggerFactory.getLogger(#{})")
.contextSensitive()
.imports("org.slf4j.LoggerFactory")
.javaParser(JavaParser.fromJavaVersion()
.classpathFromResources(ctx, "slf4j-api-2.1"))
.build()
.apply(new Cursor(getCursor().getParent(), mi), mi.getCoordinates().replace(), enclosingClazzName);
return mi;
}

private J.MethodInvocation replaceMethodArgument(J.MethodInvocation mi, String enclosingClazzName) {
return JavaTemplate.builder("#{}").contextSensitive().build()
.apply(getCursor(), mi.getCoordinates().replaceArguments(), enclosingClazzName);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void defaults(RecipeSpec spec) {
.classpathFromResources(new InMemoryExecutionContext(), "slf4j-api-2.1"));
}

@DocumentExample
@Issue("https://github.com/openrewrite/rewrite-logging-frameworks/issues/65")
@Test
void shouldRenameLogger() {
Expand Down Expand Up @@ -157,29 +158,22 @@ public Logger getLoggerFor(Class<?> clazz) {
);
}

@DocumentExample
@Test
void shouldNotReplaceLoggerInMethodIfNotConstant() {
void shouldNotReplaceGetBeanType() {
//language=java
rewriteRun(
java(
"""
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class WrongClass {}
class A {
public Logger getLogger() {
return LoggerFactory.getLogger(WrongClass.class);
class SomeProvider {
Class<?> getBeanType() {
return SomeProvider.class;
}
}
""",
"""
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class WrongClass {}
class A {
public Logger getLogger() {
return LoggerFactory.getLogger(A.class);
public Logger getLogger(SomeProvider provider) {
return LoggerFactory.getLogger(provider.getBeanType());
}
}
"""
Expand Down

0 comments on commit 62591d1

Please sign in to comment.