-
Notifications
You must be signed in to change notification settings - Fork 32
LoggerLevelArgumentToMethod for JBoss Logging #243
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fb0528b
LoggerLevelArgumentToMethod for JBoss Logging
pdelagrave 0ea11c2
Fixes from review
pdelagrave b74f286
Merge branch 'main' into loggerlevelarg-jboss
pdelagrave 704b3f2
Polish
jevanlingen a6e6b0e
Add package-info
jevanlingen 72edcc6
Polish
jevanlingen 45155b0
Merge branch 'main' into loggerlevelarg-jboss
timtebeek d534c7d
Add to best practices already
timtebeek 6d276d2
Create a separate test for documentation purposes
timtebeek 3f8e2a0
Add recipe examples documentation
timtebeek d7f3289
Merge branch 'main' into loggerlevelarg-jboss
timtebeek 6b28f2b
Don't change if we can not determine level
timtebeek 6f2f0bf
Only transfer the whitespace from the first argument
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
src/main/java/org/openrewrite/java/logging/jboss/LoggerLevelArgumentToMethod.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /* | ||
| * Copyright 2025 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license | ||
| * <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.jboss; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Preconditions; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.internal.ListUtils; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.MethodMatcher; | ||
| import org.openrewrite.java.search.UsesMethod; | ||
| import org.openrewrite.java.search.UsesType; | ||
| import org.openrewrite.java.tree.Expression; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.JavaType; | ||
| import org.openrewrite.java.tree.TypeUtils; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class LoggerLevelArgumentToMethod extends Recipe { | ||
| private static final MethodMatcher LOG_MATCHER = new MethodMatcher("org.jboss.logging.Logger log(*,*,..)", true); | ||
| private static final MethodMatcher LOGF_MATCHER = new MethodMatcher("org.jboss.logging.Logger logf(*,*,..)", true); | ||
| private static final MethodMatcher LOGV_MATCHER = new MethodMatcher("org.jboss.logging.Logger logv(*,*,..)", true); | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Replace JBoss Logging Level arguments with the corresponding eponymous level method calls"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Replace calls to `Logger.log(Level, ...)` with the corresponding eponymous level method calls. For example `Logger.log(Level.INFO, ...)` to `Logger.info(...)`."; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| TreeVisitor<?, ExecutionContext> preconditions = Preconditions.and( | ||
| new UsesType<>("org.jboss.logging.Logger", true), | ||
| new UsesType<>("org.jboss.logging.Logger.Level", true), | ||
| Preconditions.or( | ||
| new UsesMethod<>(LOG_MATCHER), | ||
| new UsesMethod<>(LOGF_MATCHER), | ||
| new UsesMethod<>(LOGV_MATCHER) | ||
| ) | ||
| ); | ||
| return Preconditions.check(preconditions, new JavaIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J.MethodInvocation visitMethodInvocation(J.MethodInvocation mi, ExecutionContext ctx) { | ||
| J.MethodInvocation m = super.visitMethodInvocation(mi, ctx); | ||
| if (!(LOG_MATCHER.matches(m) || LOGF_MATCHER.matches(m) || LOGV_MATCHER.matches(m))) { | ||
| return m; | ||
| } | ||
| List<Expression> args = m.getArguments(); | ||
| Expression firstArgument = args.get(0); | ||
| Expression secondArgument = args.get(1); | ||
|
|
||
| String logLevelName; | ||
| List<Expression> updatedArguments; | ||
| if (TypeUtils.isAssignableTo("org.jboss.logging.Logger.Level", firstArgument.getType())) { | ||
| String suffix = ""; | ||
| if (LOGF_MATCHER.matches(m)) { | ||
| suffix = "f"; | ||
| } else if (LOGV_MATCHER.matches(m)) { | ||
| suffix = "v"; | ||
| } | ||
| logLevelName = extractLogLevelName(firstArgument); | ||
| if (logLevelName != null) { | ||
| logLevelName += suffix; | ||
| } | ||
| updatedArguments = ListUtils.concat( | ||
| (Expression) secondArgument.withPrefix(secondArgument.getPrefix() | ||
| .withWhitespace(firstArgument.getPrefix().getWhitespace())), | ||
| args.subList(2, args.size())); | ||
| } else if (TypeUtils.isAssignableTo("java.lang.String", firstArgument.getType()) && | ||
| TypeUtils.isAssignableTo("org.jboss.logging.Logger.Level", secondArgument.getType()) && | ||
| LOG_MATCHER.matches(m)) { // `logf(String, ..)` and `logv(String, ..)` don't have a logger.level() equivalent | ||
| logLevelName = extractLogLevelName(secondArgument); | ||
| updatedArguments = ListUtils.filter(args, it -> it != secondArgument); | ||
| } else { | ||
| return m; | ||
| } | ||
|
|
||
| // If we can't extract a log level name, we don't change the method call | ||
| if (logLevelName == null) { | ||
| return m; | ||
| } | ||
|
|
||
| JavaType.Method updatedMethodType = requireNonNull(m.getMethodType()) | ||
| .withParameterTypes(ListUtils.filter(m.getMethodType().getParameterTypes(), it -> !TypeUtils.isAssignableTo("org.jboss.logging.Logger.Level", it))) | ||
| .withParameterNames(ListUtils.filter(m.getMethodType().getParameterNames(), it -> !"level".equals(it))) | ||
| .withName(logLevelName.toLowerCase()); | ||
|
|
||
| return m | ||
| .withArguments(updatedArguments) | ||
| .withMethodType(updatedMethodType) | ||
| .withName(m.getName().withSimpleName(logLevelName.toLowerCase())); | ||
| } | ||
|
|
||
| @Nullable | ||
| String extractLogLevelName(Expression expression) { | ||
| if (expression instanceof J.Identifier) { | ||
| J.Identifier identifier = (J.Identifier) expression; | ||
| if (identifier.getFieldType() != null && | ||
| identifier.getFieldType().getOwner() instanceof JavaType.Class) { | ||
| return identifier.getSimpleName(); | ||
| } | ||
| } else if (expression instanceof J.FieldAccess) { | ||
| return ((J.FieldAccess) expression).getSimpleName(); | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| } | ||
21 changes: 21 additions & 0 deletions
21
src/main/java/org/openrewrite/java/logging/jboss/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| * Copyright 2024 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license | ||
| * <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. | ||
| */ | ||
| @NullMarked | ||
| @NonNullFields | ||
| package org.openrewrite.java.logging.jboss; | ||
|
|
||
| import org.jspecify.annotations.NullMarked; | ||
| import org.openrewrite.internal.lang.NonNullFields; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.