|
15 | 15 | */ |
16 | 16 | package org.openrewrite.java.migrate.joda.templates; |
17 | 17 |
|
| 18 | +import lombok.Getter; |
18 | 19 | import org.openrewrite.java.JavaTemplate; |
19 | 20 | import org.openrewrite.java.MethodMatcher; |
| 21 | +import org.openrewrite.java.tree.Expression; |
| 22 | +import org.openrewrite.java.tree.J; |
| 23 | +import org.openrewrite.java.tree.MethodCall; |
20 | 24 |
|
21 | 25 | import java.util.ArrayList; |
| 26 | +import java.util.Arrays; |
22 | 27 | import java.util.List; |
| 28 | +import java.util.regex.Pattern; |
23 | 29 |
|
24 | | -import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JAVA_UTIL_DATE; |
25 | | -import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JODA_ABSTRACT_INSTANT; |
| 30 | +import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.*; |
26 | 31 |
|
27 | | -public class AbstractInstantTemplates { |
| 32 | +public class AbstractInstantTemplates implements Templates { |
| 33 | + private final MethodMatcher equals = new MethodMatcher(JODA_ABSTRACT_INSTANT + " equals(java.lang.Object)"); |
| 34 | + private final MethodMatcher getZone = new MethodMatcher(JODA_ABSTRACT_INSTANT + " getZone()"); |
| 35 | + private final MethodMatcher isAfterLong = new MethodMatcher(JODA_ABSTRACT_INSTANT + " isAfter(long)"); |
| 36 | + private final MethodMatcher isAfter = new MethodMatcher(JODA_ABSTRACT_INSTANT + " isAfter(org.joda.time.ReadableInstant)"); |
| 37 | + private final MethodMatcher isBeforeLong = new MethodMatcher(JODA_ABSTRACT_INSTANT + " isBefore(long)"); |
| 38 | + private final MethodMatcher isBefore = new MethodMatcher(JODA_ABSTRACT_INSTANT + " isBefore(org.joda.time.ReadableInstant)"); |
| 39 | + private final MethodMatcher isBeforeNow = new MethodMatcher(JODA_ABSTRACT_INSTANT + " isBeforeNow()"); |
| 40 | + private final MethodMatcher isEqualLong = new MethodMatcher(JODA_ABSTRACT_INSTANT + " isEqual(long)"); |
| 41 | + private final MethodMatcher isEqualReadableInstant = new MethodMatcher(JODA_ABSTRACT_INSTANT + " isEqual(org.joda.time.ReadableInstant)"); |
28 | 42 | private final MethodMatcher toDate = new MethodMatcher(JODA_ABSTRACT_INSTANT + " toDate()"); |
| 43 | + private final MethodMatcher toInstant = new MethodMatcher(JODA_ABSTRACT_INSTANT + " toInstant()"); |
| 44 | + private final MethodMatcher toString = new MethodMatcher(JODA_ABSTRACT_INSTANT + " toString()"); |
| 45 | + private final MethodMatcher toStringFormatter = new MethodMatcher(JODA_ABSTRACT_INSTANT + " toString(org.joda.time.format.DateTimeFormatter)"); |
29 | 46 |
|
30 | | - private final JavaTemplate toDateTemplate = JavaTemplate.builder("Date.from(#{any(java.time.ZonedDateTime)}.toInstant())") |
| 47 | + private final JavaTemplate equalsTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.equals(#{any(java.lang.Object)})").build(); |
| 48 | + private final JavaTemplate getZoneTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.getZone()").build(); |
| 49 | + private final JavaTemplate isAfterLongTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.isAfter(Instant.ofEpochMilli(#{any(long)}).atZone(ZoneId.systemDefault()))") |
| 50 | + .imports(JAVA_INSTANT, JAVA_ZONE_ID).build(); |
| 51 | + private final JavaTemplate isAfterLongTemplateWithInstant = JavaTemplate.builder("#{any(" + JAVA_INSTANT + ")}.isAfter(Instant.ofEpochMilli(#{any(long)}))") |
| 52 | + .imports(JAVA_INSTANT).build(); |
| 53 | + private final JavaTemplate isAfterTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.isAfter(#{any(" + JAVA_DATE_TIME + ")})").build(); |
| 54 | + private final JavaTemplate isAfterTemplateWithInstant = JavaTemplate.builder("#{any(" + JAVA_INSTANT + ")}.isAfter(#{any(" + JAVA_INSTANT + ")})").build(); |
| 55 | + private final JavaTemplate isBeforeLongTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.isBefore(Instant.ofEpochMilli(#{any(long)}).atZone(ZoneId.systemDefault()))") |
| 56 | + .imports(JAVA_INSTANT, JAVA_ZONE_ID).build(); |
| 57 | + private final JavaTemplate isBeforeLongTemplateWithInstant = JavaTemplate.builder("#{any(" + JAVA_INSTANT + ")}.isBefore(Instant.ofEpochMilli(#{any(long)}))") |
| 58 | + .imports(JAVA_INSTANT).build(); |
| 59 | + private final JavaTemplate isBeforTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.isBefore(#{any(" + JAVA_DATE_TIME + ")})").build(); |
| 60 | + private final JavaTemplate isBeforeTemplateWithInstant = JavaTemplate.builder("#{any(" + JAVA_INSTANT + ")}.isBefore(#{any(" + JAVA_INSTANT + ")})").build(); |
| 61 | + private final JavaTemplate isBeforeNowTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.isBefore(ZonedDateTime.now())") |
| 62 | + .imports(JAVA_DATE_TIME).build(); |
| 63 | + private final JavaTemplate isEqualLongTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.isEqual(Instant.ofEpochMilli(#{any(long)}).atZone(ZoneId.systemDefault()))") |
| 64 | + .imports(JAVA_INSTANT, JAVA_ZONE_ID).build(); |
| 65 | + private final JavaTemplate isEqualReadableInstantTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.isEqual(#{any(" + JAVA_DATE_TIME + ")})").build(); |
| 66 | + private final JavaTemplate toDateTemplate = JavaTemplate.builder("Date.from(#{any(" + JAVA_DATE_TIME + ")}.toInstant())") |
31 | 67 | .imports(JAVA_UTIL_DATE) |
32 | 68 | .build(); |
| 69 | + private final JavaTemplate toInstantTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.toInstant()").build(); |
| 70 | + private final JavaTemplate toStringTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.toString()").build(); |
| 71 | + private final JavaTemplate toStringFormatterTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.format(#{any(" + JAVA_TIME_FORMATTER + ")})").build(); |
33 | 72 |
|
| 73 | + @Getter |
34 | 74 | private final List<MethodTemplate> templates = new ArrayList<MethodTemplate>() { |
35 | 75 | { |
| 76 | + add(new MethodTemplate(equals, equalsTemplate)); |
| 77 | + add(new MethodTemplate(getZone, getZoneTemplate)); |
| 78 | + add(new MethodTemplate(isAfterLong, isAfterLongTemplate)); |
| 79 | + add(new MethodTemplate(isAfterLong, isAfterLongTemplateWithInstant)); |
| 80 | + add(new MethodTemplate(isAfter, isAfterTemplate)); |
| 81 | + add(new MethodTemplate(isAfter, isAfterTemplateWithInstant)); |
| 82 | + add(new MethodTemplate(isBeforeLong, isBeforeLongTemplate)); |
| 83 | + add(new MethodTemplate(isBeforeLong, isBeforeLongTemplateWithInstant)); |
| 84 | + add(new MethodTemplate(isBefore, isBeforTemplate)); |
| 85 | + add(new MethodTemplate(isBefore, isBeforeTemplateWithInstant)); |
| 86 | + add(new MethodTemplate(isBeforeNow, isBeforeNowTemplate)); |
| 87 | + add(new MethodTemplate(isEqualLong, isEqualLongTemplate)); |
| 88 | + add(new MethodTemplate(isEqualReadableInstant, isEqualReadableInstantTemplate)); |
36 | 89 | add(new MethodTemplate(toDate, toDateTemplate)); |
| 90 | + add(new MethodTemplate(toInstant, toInstantTemplate)); |
| 91 | + add(new MethodTemplate(toString, toStringTemplate)); |
| 92 | + add(new MethodTemplate(toStringFormatter, toStringFormatterTemplate)); |
37 | 93 | } |
38 | 94 | }; |
39 | 95 |
|
40 | | - public static List<MethodTemplate> getTemplates() { |
41 | | - return new AbstractInstantTemplates().templates; |
| 96 | + @Override |
| 97 | + public boolean matchesMethodCall(MethodCall method, MethodTemplate template) { |
| 98 | + if (method instanceof J.NewClass) { |
| 99 | + return true; |
| 100 | + } |
| 101 | + Expression select = ((J.MethodInvocation) method).getSelect(); |
| 102 | + if (select != null && select.getType() != null && select.getType().isAssignableFrom(Pattern.compile(JODA_INSTANT))) { |
| 103 | + return Arrays.asList( |
| 104 | + isAfterLongTemplateWithInstant, |
| 105 | + isAfterTemplateWithInstant, |
| 106 | + isBeforeLongTemplateWithInstant, |
| 107 | + isBeforeTemplateWithInstant |
| 108 | + ).contains(template.getTemplate()); |
| 109 | + } |
| 110 | + return true; |
42 | 111 | } |
43 | 112 | } |
0 commit comments