Skip to content

Commit 7faaa66

Browse files
Replace getDisplayName() and getDescription() methods with fields
Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.java.recipes.UseDisplayNameAndDescriptionFields?organizationId=QUxML01vZGVybmUvTW9kZXJuZSArIE9wZW5SZXdyaXRl Co-authored-by: Moderne <[email protected]>
1 parent 011170e commit 7faaa66

13 files changed

+89
-128
lines changed

src/main/java/org/openrewrite/java/logging/ArgumentArrayToVarargs.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.openrewrite.java.logging;
1717

18+
import lombok.Getter;
1819
import org.openrewrite.ExecutionContext;
1920
import org.openrewrite.Preconditions;
2021
import org.openrewrite.Recipe;
@@ -32,15 +33,11 @@ public class ArgumentArrayToVarargs extends Recipe {
3233
// Match logger methods that end with Object[] - but we'll verify if it's varargs later
3334
private static final MethodMatcher LOGGER_METHOD = new MethodMatcher("*..Log* *(.., Object[])");
3435

35-
@Override
36-
public String getDisplayName() {
37-
return "Unpack Logger method `new Object[] {...}` into varargs";
38-
}
36+
@Getter
37+
final String displayName = "Unpack Logger method `new Object[] {...}` into varargs";
3938

40-
@Override
41-
public String getDescription() {
42-
return "For Logger methods that support varargs, convert any final explicit `Object[]` arguments into their unpacked values.";
43-
}
39+
@Getter
40+
final String description = "For Logger methods that support varargs, convert any final explicit `Object[]` arguments into their unpacked values.";
4441

4542
@Override
4643
public Duration getEstimatedEffortPerOccurrence() {

src/main/java/org/openrewrite/java/logging/ChangeLombokLogAnnotation.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.openrewrite.java.logging;
1717

1818
import lombok.AllArgsConstructor;
19+
import lombok.Getter;
1920
import org.jspecify.annotations.Nullable;
2021
import org.openrewrite.Option;
2122
import org.openrewrite.Recipe;
@@ -29,15 +30,11 @@
2930
@AllArgsConstructor
3031
public class ChangeLombokLogAnnotation extends Recipe {
3132

32-
@Override
33-
public String getDisplayName() {
34-
return "Replace any Lombok log annotations with target logging framework annotation";
35-
}
33+
@Getter
34+
final String displayName = "Replace any Lombok log annotations with target logging framework annotation";
3635

37-
@Override
38-
public String getDescription() {
39-
return "Replace Lombok annotations such as `@CommonsLog` and `@Log4j` with the target logging framework annotation, or `@Sl4fj` if not provided.";
40-
}
36+
@Getter
37+
final String description = "Replace Lombok annotations such as `@CommonsLog` and `@Log4j` with the target logging framework annotation, or `@Sl4fj` if not provided.";
4138

4239
@Option(displayName = "Logging framework",
4340
description = "The logging framework to use.",

src/main/java/org/openrewrite/java/logging/jboss/LoggerLevelArgumentToMethod.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.openrewrite.java.logging.jboss;
1717

18+
import lombok.Getter;
1819
import org.jspecify.annotations.Nullable;
1920
import org.openrewrite.ExecutionContext;
2021
import org.openrewrite.Preconditions;
@@ -39,15 +40,11 @@ public class LoggerLevelArgumentToMethod extends Recipe {
3940
private static final MethodMatcher LOGF_MATCHER = new MethodMatcher("org.jboss.logging.Logger logf(*,*,..)", true);
4041
private static final MethodMatcher LOGV_MATCHER = new MethodMatcher("org.jboss.logging.Logger logv(*,*,..)", true);
4142

42-
@Override
43-
public String getDisplayName() {
44-
return "Replace JBoss Logging Level arguments with the corresponding eponymous level method calls";
45-
}
43+
@Getter
44+
final String displayName = "Replace JBoss Logging Level arguments with the corresponding eponymous level method calls";
4645

47-
@Override
48-
public String getDescription() {
49-
return "Replace calls to `Logger.log(Level, ...)` with the corresponding eponymous level method calls. For example `Logger.log(Level.INFO, ...)` to `Logger.info(...)`.";
50-
}
46+
@Getter
47+
final String description = "Replace calls to `Logger.log(Level, ...)` with the corresponding eponymous level method calls. For example `Logger.log(Level.INFO, ...)` to `Logger.info(...)`.";
5148

5249
@Override
5350
public TreeVisitor<?, ExecutionContext> getVisitor() {

src/main/java/org/openrewrite/java/logging/log4j/PrependRandomName.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.openrewrite.java.logging.log4j;
1717

1818
import com.fasterxml.jackson.annotation.JsonIgnore;
19+
import lombok.Getter;
1920
import org.kohsuke.randname.RandomNameGenerator;
2021
import org.openrewrite.ExecutionContext;
2122
import org.openrewrite.Preconditions;
@@ -40,15 +41,11 @@ public PrependRandomName(int seed) {
4041
randomName = new RandomNameGenerator(seed);
4142
}
4243

43-
@Override
44-
public String getDisplayName() {
45-
return "Prepend a random name to each Log4J statement";
46-
}
44+
@Getter
45+
final String displayName = "Prepend a random name to each Log4J statement";
4746

48-
@Override
49-
public String getDescription() {
50-
return "To make finding the callsite of a logging statement easier in code search.";
51-
}
47+
@Getter
48+
final String description = "To make finding the callsite of a logging statement easier in code search.";
5249

5350
@Override
5451
public TreeVisitor<?, ExecutionContext> getVisitor() {

src/main/java/org/openrewrite/java/logging/logback/Log4jAppenderToLogback.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.openrewrite.java.logging.logback;
1717

18+
import lombok.Getter;
1819
import org.openrewrite.*;
1920
import org.openrewrite.internal.ListUtils;
2021
import org.openrewrite.java.*;
@@ -24,19 +25,15 @@
2425
import org.openrewrite.java.tree.TypeUtils;
2526

2627
public class Log4jAppenderToLogback extends Recipe {
27-
@Override
28-
public String getDisplayName() {
29-
return "Migrate Log4j 2.x Appender to logback-classic equivalents";
30-
}
28+
@Getter
29+
final String displayName = "Migrate Log4j 2.x Appender to logback-classic equivalents";
3130

32-
@Override
33-
public String getDescription() {
34-
return "Migrates custom Log4j 2.x Appender components to `logback-classic`. This recipe operates on the following assumptions: " +
35-
"1.) The contents of the `append()` method remains unchanged. " +
36-
"2.) The `requiresLayout()` method is not used in logback and can be removed. " +
37-
"3.) In logback, the `stop()` method is the equivalent of log4j's close() method. " +
38-
"For more details, see this page from logback: [`Migration from log4j`](http://logback.qos.ch/manual/migrationFromLog4j.html).";
39-
}
31+
@Getter
32+
final String description = "Migrates custom Log4j 2.x Appender components to `logback-classic`. This recipe operates on the following assumptions: " +
33+
"1.) The contents of the `append()` method remains unchanged. " +
34+
"2.) The `requiresLayout()` method is not used in logback and can be removed. " +
35+
"3.) In logback, the `stop()` method is the equivalent of log4j's close() method. " +
36+
"For more details, see this page from logback: [`Migration from log4j`](http://logback.qos.ch/manual/migrationFromLog4j.html).";
4037

4138
@Override
4239
public TreeVisitor<?, ExecutionContext> getVisitor() {

src/main/java/org/openrewrite/java/logging/logback/Log4jLayoutToLogback.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.openrewrite.java.logging.logback;
1717

18+
import lombok.Getter;
1819
import org.openrewrite.*;
1920
import org.openrewrite.internal.ListUtils;
2021
import org.openrewrite.java.*;
@@ -28,22 +29,18 @@
2829
import static java.util.Objects.requireNonNull;
2930

3031
public class Log4jLayoutToLogback extends Recipe {
31-
@Override
32-
public String getDisplayName() {
33-
return "Migrate Log4j 2.x Layout to logback-classic equivalents";
34-
}
35-
36-
@Override
37-
public String getDescription() {
38-
return "Migrates custom Log4j 2.x Layout components to `logback-classic`. This recipe operates on the following assumptions: " +
39-
"1. A logback-classic layout must extend the `LayoutBase<ILoggingEvent>` class. " +
40-
"2. log4j's `format()` is renamed to `doLayout()` in a logback-classic layout. " +
41-
"3. LoggingEvent `getRenderedMessage()` is converted to LoggingEvent `getMessage()`. " +
42-
"4. The log4j ignoresThrowable() method is not needed and has no equivalent in logback-classic. " +
43-
"5. The activateOptions() method merits further discussion. In log4j, a layout will have its activateOptions() method invoked by log4j configurators, that is PropertyConfigurator or DOMConfigurator just after all the options of the layout have been set. Thus, the layout will have an opportunity to check that its options are coherent and if so, proceed to fully initialize itself. " +
44-
"6. In logback-classic, layouts must implement the LifeCycle interface which includes a method called start(). The start() method is the equivalent of log4j's activateOptions() method. " +
45-
"For more details, see this page from logback: [`Migration from log4j`](http://logback.qos.ch/manual/migrationFromLog4j.html).";
46-
}
32+
@Getter
33+
final String displayName = "Migrate Log4j 2.x Layout to logback-classic equivalents";
34+
35+
@Getter
36+
final String description = "Migrates custom Log4j 2.x Layout components to `logback-classic`. This recipe operates on the following assumptions: " +
37+
"1. A logback-classic layout must extend the `LayoutBase<ILoggingEvent>` class. " +
38+
"2. log4j's `format()` is renamed to `doLayout()` in a logback-classic layout. " +
39+
"3. LoggingEvent `getRenderedMessage()` is converted to LoggingEvent `getMessage()`. " +
40+
"4. The log4j ignoresThrowable() method is not needed and has no equivalent in logback-classic. " +
41+
"5. The activateOptions() method merits further discussion. In log4j, a layout will have its activateOptions() method invoked by log4j configurators, that is PropertyConfigurator or DOMConfigurator just after all the options of the layout have been set. Thus, the layout will have an opportunity to check that its options are coherent and if so, proceed to fully initialize itself. " +
42+
"6. In logback-classic, layouts must implement the LifeCycle interface which includes a method called start(). The start() method is the equivalent of log4j's activateOptions() method. " +
43+
"For more details, see this page from logback: [`Migration from log4j`](http://logback.qos.ch/manual/migrationFromLog4j.html).";
4744

4845
@Override
4946
public Duration getEstimatedEffortPerOccurrence() {

src/main/java/org/openrewrite/java/logging/slf4j/CompleteExceptionLogging.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.openrewrite.java.logging.slf4j;
1717

18+
import lombok.Getter;
1819
import org.jspecify.annotations.Nullable;
1920
import org.openrewrite.ExecutionContext;
2021
import org.openrewrite.Preconditions;
@@ -44,22 +45,18 @@ public class CompleteExceptionLogging extends Recipe {
4445
private static final MethodMatcher THROWABLE_GET_LOCALIZED_MESSAGE = new MethodMatcher("java.lang.Throwable getLocalizedMessage()");
4546

4647

47-
@Override
48-
public String getDisplayName() {
49-
return "Enhances logging of exceptions by including the full stack trace in addition to the exception message";
50-
}
48+
@Getter
49+
final String displayName = "Enhances logging of exceptions by including the full stack trace in addition to the exception message";
5150

52-
@Override
53-
public String getDescription() {
54-
return "It is a common mistake to call `Exception.getMessage()` when passing an exception into a log method. " +
55-
"Not all exception types have useful messages, and even if the message is useful this omits the stack " +
56-
"trace. Including a complete stack trace of the error along with the exception message in the log " +
57-
"allows developers to better understand the context of the exception and identify the source of the " +
58-
"error more quickly and accurately. \n" +
59-
"If the method invocation includes any call to `Exception.getMessage()` or `Exception.getLocalizedMessage()` " +
60-
"and not an exception is already passed as the last parameter to the log method, then we will append " +
61-
"the exception as the last parameter in the log method.";
62-
}
51+
@Getter
52+
final String description = "It is a common mistake to call `Exception.getMessage()` when passing an exception into a log method. " +
53+
"Not all exception types have useful messages, and even if the message is useful this omits the stack " +
54+
"trace. Including a complete stack trace of the error along with the exception message in the log " +
55+
"allows developers to better understand the context of the exception and identify the source of the " +
56+
"error more quickly and accurately. \n" +
57+
"If the method invocation includes any call to `Exception.getMessage()` or `Exception.getLocalizedMessage()` " +
58+
"and not an exception is already passed as the last parameter to the log method, then we will append " +
59+
"the exception as the last parameter in the log method.";
6360

6461
@Override
6562
public Set<String> getTags() {

src/main/java/org/openrewrite/java/logging/slf4j/JulParameterizedArguments.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.openrewrite.java.logging.slf4j;
1717

18+
import lombok.Getter;
1819
import org.jspecify.annotations.Nullable;
1920
import org.openrewrite.ExecutionContext;
2021
import org.openrewrite.Preconditions;
@@ -44,15 +45,11 @@ public class JulParameterizedArguments extends Recipe {
4445
private static final MethodMatcher METHOD_MATCHER_PARAM = new MethodMatcher("java.util.logging.Logger log(java.util.logging.Level, java.lang.String, java.lang.Object)");
4546
private static final MethodMatcher METHOD_MATCHER_ARRAY = new MethodMatcher("java.util.logging.Logger log(java.util.logging.Level, java.lang.String, java.lang.Object[])");
4647

47-
@Override
48-
public String getDisplayName() {
49-
return "Replace parameterized JUL level call with corresponding SLF4J method calls";
50-
}
48+
@Getter
49+
final String displayName = "Replace parameterized JUL level call with corresponding SLF4J method calls";
5150

52-
@Override
53-
public String getDescription() {
54-
return "Replace calls to parameterized `Logger.log(Level,String,…)` call with the corresponding slf4j method calls transforming the formatter and parameter lists.";
55-
}
51+
@Getter
52+
final String description = "Replace calls to parameterized `Logger.log(Level,String,…)` call with the corresponding slf4j method calls transforming the formatter and parameter lists.";
5653

5754
@Override
5855
public TreeVisitor<?, ExecutionContext> getVisitor() {

src/main/java/org/openrewrite/java/logging/slf4j/LoggersNamedForEnclosingClass.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.openrewrite.java.logging.slf4j;
1717

18+
import lombok.Getter;
1819
import org.openrewrite.ExecutionContext;
1920
import org.openrewrite.Preconditions;
2021
import org.openrewrite.Recipe;
@@ -39,15 +40,11 @@ public class LoggersNamedForEnclosingClass extends Recipe {
3940
private static final MethodMatcher LOGGERFACTORY_GETLOGGER = new MethodMatcher(
4041
"org.slf4j.LoggerFactory getLogger(Class)");
4142

42-
@Override
43-
public String getDisplayName() {
44-
return "Loggers should be named for their enclosing classes";
45-
}
43+
@Getter
44+
final String displayName = "Loggers should be named for their enclosing classes";
4645

47-
@Override
48-
public String getDescription() {
49-
return "Ensure `LoggerFactory#getLogger(Class)` is called with the enclosing class as argument.";
50-
}
46+
@Getter
47+
final String description = "Ensure `LoggerFactory#getLogger(Class)` is called with the enclosing class as argument.";
5148

5249
@Override
5350
public Duration getEstimatedEffortPerOccurrence() {

src/main/java/org/openrewrite/java/logging/slf4j/MatchIsLogLevelEnabledWithLogStatements.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.openrewrite.java.logging.slf4j;
1717

18+
import lombok.Getter;
1819
import org.jspecify.annotations.Nullable;
1920
import org.openrewrite.ExecutionContext;
2021
import org.openrewrite.Preconditions;
@@ -39,17 +40,13 @@ public class MatchIsLogLevelEnabledWithLogStatements extends Recipe {
3940
private static final MethodMatcher ENABLED_MATCHER = new MethodMatcher("org.slf4j.Logger is*Enabled()");
4041
private static final MethodMatcher LOG_MATCHER = new MethodMatcher("org.slf4j.Logger *(..)");
4142

42-
@Override
43-
public String getDisplayName() {
44-
return "Match `if (is*Enabled())` with logging statements";
45-
}
43+
@Getter
44+
final String displayName = "Match `if (is*Enabled())` with logging statements";
4645

47-
@Override
48-
public String getDescription() {
49-
return "Change any `if (is*Enabled())` statements that do not match the maximum log level used in the `then` " +
50-
"part to use the matching `is*Enabled()` method for that log level. " +
51-
"This ensures that the logging condition is consistent with the actual logging statements.";
52-
}
46+
@Getter
47+
final String description = "Change any `if (is*Enabled())` statements that do not match the maximum log level used in the `then` " +
48+
"part to use the matching `is*Enabled()` method for that log level. " +
49+
"This ensures that the logging condition is consistent with the actual logging statements.";
5350

5451
@Override
5552
public TreeVisitor<?, ExecutionContext> getVisitor() {

0 commit comments

Comments
 (0)