/
SimplifyDurationCreationUnits.java
146 lines (127 loc) · 5.4 KB
/
SimplifyDurationCreationUnits.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright 2022 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.staticanalysis;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
public class SimplifyDurationCreationUnits extends Recipe {
private enum DurationUnits {
// order is important; factors are tried in this order (largest-to-smallest)
// (the Java spec states that `.values()` uses declaration order)
DAYS("ofDays", ChronoUnit.DAYS),
HOURS("ofHours", ChronoUnit.HOURS),
MINUTES("ofMinutes", ChronoUnit.MINUTES),
SECONDS("ofSeconds", ChronoUnit.SECONDS),
MILLIS("ofMillis", ChronoUnit.MILLIS);
final String methodName;
final long millisFactor;
final MethodMatcher methodMatcher;
DurationUnits(String methodName, TemporalUnit unit) {
this.methodName = methodName;
this.millisFactor = Duration.of(1, unit).toMillis();
this.methodMatcher = new MethodMatcher("java.time.Duration " + methodName + "(long)");
}
}
@Override
public String getDisplayName() {
return "Simplify `java.time.Duration` units";
}
@Override
public String getDescription() {
return "Simplifies `java.time.Duration` units to be more human-readable.";
}
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesMethod<>("java.time.Duration of*(long)"), new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
method = super.visitMethodInvocation(method, ctx);
DurationUnits invocationUnits = null;
for (DurationUnits maybeUnit : DurationUnits.values()) {
if (maybeUnit.methodMatcher.matches(method)) {
invocationUnits = maybeUnit;
break;
}
}
if (invocationUnits == null) {
return method;
}
Long invocationUnitCount = getConstantIntegralValue(method.getArguments().get(0));
if (invocationUnitCount == null || invocationUnitCount == 0L) {
return method;
}
long millis = invocationUnitCount * invocationUnits.millisFactor;
DurationUnits simplifiedUnits = null;
for (DurationUnits maybeUnit : DurationUnits.values()) {
if (millis % maybeUnit.millisFactor == 0) {
simplifiedUnits = maybeUnit;
break;
}
}
if (simplifiedUnits == null || simplifiedUnits == invocationUnits) {
return method;
}
JavaTemplate template = JavaTemplate.builder("#{}(#{})").contextSensitive().build();
return maybeAutoFormat(
method,
template.apply(updateCursor(method), method.getCoordinates().replaceMethod(), simplifiedUnits.methodName,
millis / simplifiedUnits.millisFactor),
ctx
);
}
});
}
@Nullable
public static Long getConstantIntegralValue(Expression expression) {
if (expression instanceof J.Literal) {
J.Literal literal = (J.Literal) expression;
if (literal.getType() != JavaType.Primitive.Int && literal.getType() != JavaType.Primitive.Long) {
return null;
}
Object value = literal.getValue();
if (value instanceof Number) {
return ((Number) value).longValue();
}
} else if (expression instanceof J.Binary) {
J.Binary binary = (J.Binary) expression;
if (binary.getOperator() != J.Binary.Type.Multiplication) {
return null;
}
Long left = getConstantIntegralValue(binary.getLeft());
if (left == null) {
return null;
}
Long right = getConstantIntegralValue(binary.getRight());
if (right == null) {
return null;
}
return left * right;
}
return null;
}
}