Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Lombok log annotations in three recipes #115

Merged
merged 6 commits into from
Oct 27, 2023

Conversation

timtebeek
Copy link
Contributor

@timtebeek timtebeek commented Oct 26, 2023

What's changed?

Detect for instance @Slf4j in recipes that move away from System.out, System.err and printStackTrace.

What's your motivation?

Anything in particular you'd like reviewers to focus on?

Should we cover more recipes already?

Have you considered any alternatives or workarounds?

We could have generically fixed this by adding synthetic log fields to the associated classes, but I figured that's harder to do for a quick win here.

Any additional context

@timtebeek timtebeek added the enhancement New feature or request label Oct 26, 2023
@timtebeek timtebeek self-assigned this Oct 26, 2023
rewriteRun(
spec -> spec.recipe(new PrintStackTraceToLogError(null, null, null))
.parser(JavaParser.fromJavaVersion().classpath("slf4j-api", "lombok"))
.typeValidationOptions(TypeValidation.builder().identifiers(false).build()),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the fields are absent, we have missing type information in the result. I figured that's ok & to be expected.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess there is no way to get the type information from the annotation processor...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No we don't run the Lombok annotation processor, so we don't have that information available; it's good enough to make this code change; not quite sure if any followup recipes would work, but the JavaTemplates we use should have the type information, as per

public <P> JavaTemplate getErrorTemplateNoException(JavaVisitor<P> visitor) {
switch (framework) {
case SLF4J:
return JavaTemplate
.builder("#{any(org.slf4j.Logger)}.error(#{any(String)})")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpath("slf4j-api"))
.build();

J.Identifier logField = loggers.iterator().next().getVariables().get(0).getName();
m = replaceMethodInvocation(m, logField);
} else if (clazz.getAllAnnotations().stream().anyMatch(lombokLogAnnotationMatcher::matches)) {
String fieldName = loggerName == null ? "log" : loggerName;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default fields are named log, as per: https://projectlombok.org/features/log

Didn't quite feel like parsing any lombok configuration files in case folks set a different lombok.log.fieldName.
If they do that, then can set the loggerName option to have that be used for the logging statements instead.

@timtebeek
Copy link
Contributor Author

These three recipes are not included in composite recipes, mostly because they have options that folks might want to set. As such there's limited risk in these changes; folks will only run these explicitly, and can define their own options to tweak the behavior.

@timtebeek timtebeek merged commit ed33607 into main Oct 27, 2023
1 check passed
@timtebeek timtebeek deleted the lombok_log_annotations branch October 27, 2023 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Consider Lombok annotations for loggers
2 participants