Skip to content

Commit

Permalink
Replace Lombok logging annotations when switching frameworks (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Nov 19, 2023
1 parent 12a181d commit 7b89f27
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.logging;

import lombok.AllArgsConstructor;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.ChangeType;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@AllArgsConstructor
public class ChangeLombokLogAnnotation extends Recipe {

@Override
public String getDisplayName() {
return "Replace any Lombok log annotations with target logging framework annotation";
}

@Override
public String getDescription() {
return "Replace Lombok annotations such as `@CommonsLog` and `@Log4j` with the target logging framework annotation, or `@Sl4fj` if not provided.";
}

@Option(displayName = "Logging framework",
description = "The logging framework to use.",
valid = {"SLF4J", "Log4J1", "Log4J2", "JUL", "COMMONS"},
required = false)
@Nullable
private String loggingFramework;

@Override
public List<Recipe> getRecipeList() {
String targetLogAnnotationType = getTargetAnnotationType(loggingFramework);
return Stream.of(
"lombok.extern.java.Log",
"lombok.extern.apachecommons.CommonsLog",
"lombok.extern.flogger.Flogger",
"lombok.extern.jbosslog.JBossLog",
"lombok.extern.log4j.Log4j",
"lombok.extern.log4j.Log4j2",
"lombok.extern.slf4j.Slf4j",
"lombok.extern.slf4j.XSlf4j",
"lombok.CustomLog")
.filter(annotationType -> !annotationType.equals(targetLogAnnotationType))
.map(annotationType -> new ChangeType(annotationType, targetLogAnnotationType, true))
.collect(Collectors.toList());
}

private static String getTargetAnnotationType(@Nullable String loggingFramework) {
if (loggingFramework != null) {
switch (LoggingFramework.fromOption(loggingFramework)) {
case Log4J1:
return "lombok.extern.log4j.Log4j";
case Log4J2:
return "lombok.extern.log4j.Log4j2";
case JUL:
return "lombok.extern.java.Log";
case COMMONS:
return "lombok.extern.apachecommons.CommonsLog";
}
}
return "lombok.extern.slf4j.Slf4j";
}
}
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/rewrite/log4j.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ tags:
- logging
- log4j
recipeList:
- org.openrewrite.java.logging.ChangeLombokLogAnnotation:
loggingFramework: Log4J2
- org.openrewrite.java.ChangePackage:
oldPackageName: org.apache.log4j
newPackageName: org.apache.logging.log4j
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/rewrite/slf4j.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ recipeList:
oldFullyQualifiedTypeName: org.apache.logging.log4j.Logger
newFullyQualifiedTypeName: org.slf4j.Logger
- org.openrewrite.java.logging.slf4j.ParameterizedLogging
- org.openrewrite.java.logging.ChangeLombokLogAnnotation
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.logging.slf4j.Log4j1ToSlf4j1
Expand Down Expand Up @@ -166,3 +167,4 @@ recipeList:
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.commons.logging.Log
newFullyQualifiedTypeName: org.slf4j.Logger
- org.openrewrite.java.logging.ChangeLombokLogAnnotation
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void defaults(RecipeSpec spec) {
.scanRuntimeClasspath("org.openrewrite.java.logging")
.build()
.activateRecipes("org.openrewrite.java.logging.slf4j.CommonsLogging1ToSlf4j1"))
.parser(JavaParser.fromJavaVersion().classpath("commons-logging", "slf4j-api"));
.parser(JavaParser.fromJavaVersion().classpath("commons-logging", "slf4j-api", "lombok"));
}

@DocumentExample
Expand Down Expand Up @@ -130,4 +130,34 @@ static void method(Logger logger) {
)
);
}

@Test
void changeLombokLogAnnotation() {
//language=java
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.builder().identifiers(false).methodInvocations(false).build()),
java(
"""
import lombok.extern.apachecommons.CommonsLog;
@CommonsLog
class Test {
void method() {
log.info("uh oh");
}
}
""",
"""
import lombok.extern.slf4j.Slf4j;
@Slf4j
class Test {
void method() {
log.info("uh oh");
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;

import static org.openrewrite.java.Assertions.java;

Expand All @@ -31,7 +32,7 @@ public void defaults(RecipeSpec spec) {
.scanRuntimeClasspath("org.openrewrite.java.logging")
.build()
.activateRecipes("org.openrewrite.java.logging.slf4j.Log4j2ToSlf4j1"))
.parser(JavaParser.fromJavaVersion().classpath("log4j-api", "log4j-core"));
.parser(JavaParser.fromJavaVersion().classpath("log4j-api", "log4j-core", "lombok"));
}

@DocumentExample
Expand Down Expand Up @@ -102,4 +103,34 @@ public static void main(String[] args) {
)
);
}

@Test
void changeLombokLogAnnotation() {
//language=java
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.builder().identifiers(false).methodInvocations(false).build()),
java(
"""
import lombok.extern.log4j.Log4j;
@Log4j
class Test {
void method() {
log.info("uh oh");
}
}
""",
"""
import lombok.extern.slf4j.Slf4j;
@Slf4j
class Test {
void method() {
log.info("uh oh");
}
}
"""
)
);
}
}

0 comments on commit 7b89f27

Please sign in to comment.