-
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 all 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
133 changes: 133 additions & 0 deletions
133
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,133 @@ | ||
| /* | ||
| * 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.RequiredArgsConstructor; | ||
| import lombok.Value; | ||
| 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; | ||
|
|
||
| @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"; | ||
|
|
||
| private static final TypeMatcher DATE_MATCHER = new TypeMatcher(DATE_TYPE); | ||
|
|
||
| @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( | ||
| Preconditions.and( | ||
| new UsesType<>(DATE_TYPE, true), | ||
| new UsesType<>(ENTITY_ANNOTATION, true), | ||
| new UsesType<>(TEMPORAL_ANNOTATION, true) | ||
| ), | ||
| new TemporalRefactorVisitor(Boolean.TRUE.equals(useOffsetDateTime)) | ||
| ); | ||
| } | ||
|
|
||
| @RequiredArgsConstructor | ||
| private static class TemporalRefactorVisitor extends JavaIsoVisitor<ExecutionContext> { | ||
|
|
||
| private final boolean useOffsetDT; | ||
|
|
||
| @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; | ||
| } | ||
|
|
||
| String newTypeToUse = FindAnnotations.find(decls, TEMPORAL_ANNOTATION) | ||
| .stream() | ||
| .findFirst() | ||
| .map(Annotation::getArguments) | ||
| .map(args -> args.get(0)) | ||
| .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()); | ||
|
|
||
| maybeRemoveImport(DATE_TYPE); | ||
| maybeAddImport(newTypeToUse); | ||
| return (J.VariableDeclarations) new ChangeType(DATE_TYPE, newTypeToUse, null).getVisitor().visitNonNull(decls, ctx); | ||
| } | ||
|
|
||
| private @Nullable String getNewType(String temporalConstant) { | ||
| switch (temporalConstant) { | ||
| case "DATE": | ||
| return JAVA_TIME_LOCAL_DATE; | ||
| case "TIME": | ||
| return JAVA_TIME_LOCAL_TIME; | ||
| case "TIMESTAMP": | ||
| if (useOffsetDT) { | ||
| return JAVA_TIME_OFFSETDATETIME; | ||
| } else { | ||
| return JAVA_TIME_LOCAL_DATE_TIME; | ||
| } | ||
| default: | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
| } |
255 changes: 255 additions & 0 deletions
255
src/test/java/org/openrewrite/quarkus/RefactorTemporalAnnotationTest.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,255 @@ | ||
| /* | ||
| * 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 org.junit.jupiter.api.Test; | ||
| import org.openrewrite.DocumentExample; | ||
| import org.openrewrite.java.JavaParser; | ||
| import org.openrewrite.test.RecipeSpec; | ||
| import org.openrewrite.test.RewriteTest; | ||
|
|
||
| import static org.openrewrite.java.Assertions.java; | ||
|
|
||
| class RefactorTemporalAnnotationTest implements RewriteTest { | ||
|
|
||
| @Override | ||
| public void defaults(RecipeSpec spec) { | ||
| spec.recipes(new RefactorTemporalAnnotation(null)) | ||
| .parser(JavaParser.fromJavaVersion() | ||
| .classpath("jakarta.persistence-api")); | ||
| } | ||
|
|
||
| @DocumentExample | ||
| @Test | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| void shouldRemoveTemporalAnnotationAndKeepOtherAnnotations() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import jakarta.persistence.Temporal; | ||
|
|
||
| import java.util.Date; | ||
|
|
||
| @Entity | ||
| @Table(name = "rent_house") | ||
| class RentHouseEntity { | ||
| @Id | ||
| @Column(name = "rent_house_id") | ||
| private Long id; | ||
|
|
||
| @Column(name = "status") | ||
| private String status; | ||
|
|
||
| @Column(name = "start_date") | ||
| @Temporal(TemporalType.DATE) | ||
| private Date startDate; | ||
|
|
||
| @Column(name = "end_date") | ||
| @Temporal(TemporalType.DATE) | ||
| private Date endDate; | ||
|
|
||
| @Column(name = "creation_date") | ||
| @Temporal(TemporalType.TIMESTAMP) | ||
| private Date creationDate; | ||
| } | ||
| """, | ||
| """ | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.LocalDateTime; | ||
|
|
||
| @Entity | ||
| @Table(name = "rent_house") | ||
| class RentHouseEntity { | ||
| @Id | ||
| @Column(name = "rent_house_id") | ||
| private Long id; | ||
|
|
||
| @Column(name = "status") | ||
| private String status; | ||
|
|
||
| @Column(name = "start_date") | ||
| private LocalDate startDate; | ||
|
|
||
| @Column(name = "end_date") | ||
| private LocalDate endDate; | ||
|
|
||
| @Column(name = "creation_date") | ||
| private LocalDateTime creationDate; | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldRemoveTemporalAnnotationAndUseCorrespondingType() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import java.util.Date; | ||
| import jakarta.persistence.Temporal; | ||
| import jakarta.persistence.TemporalType; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Table; | ||
|
|
||
| @Entity | ||
| @Table(name = "some_entity") | ||
| class MultiTemporalEntity { | ||
| @Temporal(TemporalType.DATE) | ||
| private Date dateField; | ||
|
|
||
| @Temporal(TemporalType.TIMESTAMP) | ||
| private Date timestampField; | ||
|
|
||
| @Temporal(TemporalType.TIME) | ||
| private Date timeField; | ||
| } | ||
| """, | ||
| """ | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Table; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.LocalDateTime; | ||
| import java.time.LocalTime; | ||
|
|
||
| @Entity | ||
| @Table(name = "some_entity") | ||
| class MultiTemporalEntity { | ||
| private LocalDate dateField; | ||
|
|
||
| private LocalDateTime timestampField; | ||
|
|
||
| private LocalTime timeField; | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldRemoveTemporalAnnotationAndUseLocalDateTimeTypeWhenStaticImport() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import java.util.Date; | ||
| import static jakarta.persistence.TemporalType.DATE; | ||
| import jakarta.persistence.Temporal; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Table; | ||
|
|
||
| @Entity | ||
| @Table(name = "some_entity") | ||
| class SomeEntity { | ||
| @Temporal(DATE) | ||
| private Date dateField; | ||
| } | ||
| """, | ||
| """ | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Table; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| @Entity | ||
| @Table(name = "some_entity") | ||
| class SomeEntity { | ||
| private LocalDate dateField; | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldRemoveTemporalAnnotationWithOffsetDateTime() { | ||
| rewriteRun( | ||
| spec -> spec.recipe(new RefactorTemporalAnnotation(true)), | ||
| //language=java | ||
| java( | ||
| """ | ||
| import java.util.Date; | ||
| import jakarta.persistence.Temporal; | ||
| import jakarta.persistence.TemporalType; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Table; | ||
|
|
||
| @Entity | ||
| @Table(name = "some_entity") | ||
| class MultiTemporalEntity { | ||
| @Temporal(TemporalType.DATE) | ||
| private Date dateField; | ||
|
|
||
| @Temporal(TemporalType.TIMESTAMP) | ||
| private Date timestampField; | ||
|
|
||
| @Temporal(TemporalType.TIME) | ||
| private Date timeField; | ||
| } | ||
| """, | ||
| """ | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Table; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.LocalTime; | ||
| import java.time.OffsetDateTime; | ||
|
|
||
| @Entity | ||
| @Table(name = "some_entity") | ||
| class MultiTemporalEntity { | ||
| private LocalDate dateField; | ||
|
|
||
| private OffsetDateTime timestampField; | ||
|
|
||
| private LocalTime timeField; | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void noAnnotationNoChange() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Table; | ||
| import java.util.Date; | ||
|
|
||
| @Entity | ||
| @Table(name = "some_entity") | ||
| class SomeEntity { | ||
| private Date createdOn; | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
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.