-
Notifications
You must be signed in to change notification settings - Fork 11
New Recipe to use java.time api instead of @Temporal annotation #102
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
Draft
FredMencier
wants to merge
8
commits into
openrewrite:main
Choose a base branch
from
FredMencier:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7ccc007
add RefactorTemporalAnnotation recipe
FredMencier 6306c25
add License
FredMencier f917e73
format source
FredMencier d1a4d2d
Apply suggestions
timtebeek 1b9ef4c
Polish
timtebeek 9da4fda
Expand precondition to include all three types needed
timtebeek 69a44eb
Collapse annotation to argument new type wrangling
timtebeek dadbf09
Polish markdown
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
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
142 changes: 142 additions & 0 deletions
142
src/main/java/org/openrewrite/quarkus/RefactorTemporalAnnotation.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,142 @@ | ||
| /* | ||
| * Copyright 2025 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.quarkus; | ||
|
|
||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Value; | ||
| import org.jetbrains.annotations.NotNull; | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import org.jspecify.annotations.Nullable; | ||
| import org.openrewrite.*; | ||
| import org.openrewrite.java.ChangeType; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.RemoveAnnotation; | ||
| import org.openrewrite.java.TypeMatcher; | ||
| import org.openrewrite.java.search.FindAnnotations; | ||
| import org.openrewrite.java.search.UsesType; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.J.Annotation; | ||
| import org.openrewrite.java.tree.J.FieldAccess; | ||
| import org.openrewrite.java.tree.J.Identifier; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| @Value | ||
| @EqualsAndHashCode(callSuper = false) | ||
| public class RefactorTemporalAnnotation extends Recipe { | ||
|
|
||
| private static final String TEMPORAL_ANNOTATION = "jakarta.persistence.Temporal"; | ||
| private static final String ENTITY_ANNOTATION = "jakarta.persistence.Entity"; | ||
| private static final String DATE_TYPE = "java.util.Date"; | ||
| private static final String JAVA_TIME_LOCAL_DATE = "java.time.LocalDate"; | ||
| private static final String JAVA_TIME_LOCAL_TIME = "java.time.LocalTime"; | ||
| private static final String JAVA_TIME_OFFSETDATETIME = "java.time.OffsetDateTime"; | ||
| private static final String JAVA_TIME_LOCAL_DATE_TIME = "java.time.LocalDateTime"; | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Refactor @Temporal annotation java.util.Date fields to java.time API"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Replace java.util.Date fields annotated with @Temporal " + | ||
| "with java.time.LocalDate, java.time.LocalTime, java.time.LocalDateTime or java.time.OffsetDateTime."; | ||
| } | ||
|
|
||
| @Option(displayName = "Use offsetDateTime", | ||
| description = "If `true` the recipe will use OffsetDateTime instead of LocalDateTime. Default `false`.", | ||
| required = false) | ||
| @Nullable | ||
| Boolean useOffsetDateTime; | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return Preconditions.check( | ||
| new UsesType<>(ENTITY_ANNOTATION, true), | ||
| new TemporalRefactorVisitor(useOffsetDateTime) | ||
| ); | ||
| } | ||
|
|
||
| private static class TemporalRefactorVisitor extends JavaIsoVisitor<ExecutionContext> { | ||
|
|
||
| private static final TypeMatcher DATE_MATCHER = new TypeMatcher(DATE_TYPE); | ||
|
|
||
| private final Boolean useOffsetDT; | ||
|
|
||
| public TemporalRefactorVisitor(Boolean useOffsetDateTime) { | ||
| useOffsetDT = useOffsetDateTime; | ||
| } | ||
|
|
||
| @Override | ||
| public J. VariableDeclarations visitVariableDeclarations(J. VariableDeclarations multiVariable, ExecutionContext ctx) { | ||
| J.VariableDeclarations decls = super.visitVariableDeclarations(multiVariable, ctx); | ||
|
|
||
| if (!DATE_MATCHER.matches(decls.getTypeExpression())) { | ||
| return decls; | ||
| } | ||
|
|
||
| Annotation temporalAnnotation = FindAnnotations.find(decls, TEMPORAL_ANNOTATION).stream() | ||
| .findFirst() | ||
| .orElse(null); | ||
|
|
||
| if (temporalAnnotation == null) { | ||
| return decls; | ||
| } | ||
|
|
||
| // Extract the enum constant (DATE or TIMESTAMP) | ||
| String newTypeToUse = Objects.requireNonNull(temporalAnnotation.getArguments()).stream() | ||
| .findFirst() | ||
| .map(arg -> { | ||
| if (arg instanceof FieldAccess) { | ||
| return getNewType(((FieldAccess) arg).getSimpleName()); | ||
| } | ||
| if (arg instanceof Identifier) { | ||
| return getNewType(((Identifier) arg).getSimpleName()); | ||
| } | ||
| return null; | ||
| }) | ||
| .orElse(null); | ||
|
|
||
| if (newTypeToUse == null) { | ||
| return decls; | ||
| } | ||
|
|
||
| doAfterVisit(new RemoveAnnotation(TEMPORAL_ANNOTATION).getVisitor()); | ||
| decls = (J.VariableDeclarations) new ChangeType(DATE_TYPE, newTypeToUse, null).getVisitor().visitNonNull(decls, ctx); | ||
|
|
||
| maybeRemoveImport(DATE_TYPE); | ||
| maybeAddImport(newTypeToUse); | ||
| return decls; | ||
| } | ||
|
|
||
| private String getNewType(String temporalConstant) { | ||
| switch (temporalConstant) { | ||
| case "DATE": | ||
| return JAVA_TIME_LOCAL_DATE; | ||
| case "TIME": | ||
| return JAVA_TIME_LOCAL_TIME; | ||
| case "TIMESTAMP": | ||
| if (Boolean.TRUE.equals(useOffsetDT)) { | ||
| return JAVA_TIME_OFFSETDATETIME; | ||
| } else { | ||
| return JAVA_TIME_LOCAL_DATE_TIME; | ||
| } | ||
| default: | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.