Skip to content

Commit 7954b4b

Browse files
akiralytimtebeek
andauthored
JacocoReportDeprecations should replace deprecated methods with assignments to make it work with Gradle 8 (#6282)
* Refactor JacocoReportDeprecations method handling to replace deprecated invocations (original support added in #6232) with assignment statements instead of method calls that do not exist in Gradle 8. The previous implementation replaced calls like `enabled(true)` with `required(true)` but the latter doesn't work in Gradle 8. But `required = true` does. * Apply formatter * Clear empty lines * Remove most confusing duplicate assignments --------- Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent 7f7cbd2 commit 7954b4b

2 files changed

Lines changed: 131 additions & 34 deletions

File tree

rewrite-gradle/src/main/java/org/openrewrite/gradle/gradle8/JacocoReportDeprecations.java

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,20 @@
2323
import org.openrewrite.TreeVisitor;
2424
import org.openrewrite.gradle.IsBuildGradle;
2525
import org.openrewrite.internal.StringUtils;
26-
import org.openrewrite.java.JavaIsoVisitor;
26+
import org.openrewrite.java.JavaVisitor;
2727
import org.openrewrite.java.tree.Expression;
2828
import org.openrewrite.java.tree.J;
29+
import org.openrewrite.java.tree.J.Assignment;
30+
import org.openrewrite.java.tree.J.FieldAccess;
31+
import org.openrewrite.java.tree.J.Identifier;
32+
import org.openrewrite.java.tree.J.MethodInvocation;
33+
import org.openrewrite.java.tree.JLeftPadded;
34+
import org.openrewrite.marker.Markers;
35+
36+
import static java.util.Collections.emptyList;
37+
import static org.openrewrite.Tree.randomId;
38+
import static org.openrewrite.java.tree.Space.EMPTY;
39+
import static org.openrewrite.java.tree.Space.SINGLE_SPACE;
2940

3041
@Value
3142
@EqualsAndHashCode(callSuper = false)
@@ -46,7 +57,7 @@ public String getDescription() {
4657

4758
@Override
4859
public TreeVisitor<?, ExecutionContext> getVisitor() {
49-
return Preconditions.check(new IsBuildGradle<>(), new JavaIsoVisitor<ExecutionContext>() {
60+
return Preconditions.check(new IsBuildGradle<>(), new JavaVisitor<ExecutionContext>() {
5061
@Override
5162
public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext ctx) {
5263
Integer index = getCursor().getNearestMessage(JACOCO_SETTINGS_INDEX);
@@ -67,7 +78,7 @@ public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext ct
6778
}
6879

6980
@Override
70-
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
81+
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
7182
Integer parent = getCursor().getNearestMessage(JACOCO_SETTINGS_INDEX);
7283
if (parent == null) {
7384
parent = 0;
@@ -76,7 +87,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
7687
}
7788

7889
// Handle method invocation syntax at various nesting levels
79-
if (!method.getArguments().isEmpty()) {
90+
if (method.getArguments().size() == 1) {
8091
boolean shouldReplace = false;
8192

8293
// xml.enabled(false) or csv.enabled(false) - inside reports closure
@@ -90,7 +101,7 @@ else if (parent == 1 && method.getSelect() instanceof J.FieldAccess) {
90101
if (selectField.getTarget() instanceof J.Identifier) {
91102
J.Identifier target = (J.Identifier) selectField.getTarget();
92103
shouldReplace = "reports".equalsIgnoreCase(target.getSimpleName()) &&
93-
isReportType(selectField.getSimpleName());
104+
isReportType(selectField.getSimpleName());
94105
}
95106
}
96107
// enabled(false) - inside xml/csv/html closure
@@ -99,7 +110,7 @@ else if (parent == 3 && method.getSelect() == null) {
99110
}
100111

101112
if (shouldReplace) {
102-
J.MethodInvocation replacement = replaceDeprecatedMethodName(method);
113+
J replacement = replaceDeprecatedMethodInvocation(method);
103114
if (replacement != method) {
104115
return replacement;
105116
}
@@ -120,12 +131,12 @@ private boolean isReportType(String name) {
120131
"html".equalsIgnoreCase(name);
121132
}
122133

123-
private J.MethodInvocation replaceDeprecatedMethodName(J.MethodInvocation method) {
134+
private J replaceDeprecatedMethodInvocation(J.MethodInvocation method) {
124135
String methodName = method.getSimpleName();
125136
if ("enabled".equalsIgnoreCase(methodName) || "isEnabled".equalsIgnoreCase(methodName) || "setEnabled".equalsIgnoreCase(methodName)) {
126-
return method.withName(method.getName().withSimpleName("required"));
137+
return replaceMethodInvocationWithAssignment(method, "required");
127138
} else if ("destination".equalsIgnoreCase(methodName) || "setDestination".equalsIgnoreCase(methodName)) {
128-
return method.withName(method.getName().withSimpleName("outputLocation"));
139+
return replaceMethodInvocationWithAssignment(method, "outputLocation");
129140
}
130141
return method;
131142
}
@@ -198,4 +209,42 @@ private String getFieldName(J.FieldAccess fieldAccess) {
198209
}
199210
});
200211
}
212+
213+
private static Assignment replaceMethodInvocationWithAssignment(MethodInvocation method, String identifierName) {
214+
Identifier id = new Identifier(
215+
randomId(),
216+
EMPTY,
217+
Markers.EMPTY,
218+
emptyList(),
219+
identifierName,
220+
null,
221+
null
222+
);
223+
Expression variable;
224+
Expression select = method.getSelect();
225+
if (select == null) {
226+
variable = id;
227+
} else {
228+
variable = new FieldAccess(
229+
randomId(),
230+
EMPTY,
231+
Markers.EMPTY,
232+
select,
233+
new JLeftPadded<>(EMPTY, id, Markers.EMPTY),
234+
null
235+
);
236+
}
237+
return new Assignment(
238+
randomId(),
239+
method.getPrefix(),
240+
Markers.EMPTY,
241+
variable,
242+
new JLeftPadded<>(
243+
SINGLE_SPACE,
244+
method.getArguments().get(0).withPrefix(SINGLE_SPACE),
245+
Markers.EMPTY
246+
),
247+
null
248+
);
249+
}
201250
}

rewrite-gradle/src/test/java/org/openrewrite/gradle/gradle8/JacocoReportDeprecationsTest.java

Lines changed: 73 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ void alsoWorkOnKotlinDsl() {
451451
tasks.jacocoTestReport {
452452
reports {
453453
xml.required = false
454-
html.required(true)
454+
html.required = true
455455
html.outputLocation = layout.buildDirectory.dir("jacocoHtml")
456456
}
457457
}
@@ -461,7 +461,7 @@ void alsoWorkOnKotlinDsl() {
461461
}
462462

463463
@Nested
464-
class AsMethodInvocation {
464+
class MethodInvocationToAssignments {
465465
@Test
466466
void deprecationsInMethodInvocationSyntax() {
467467
rewriteRun(
@@ -471,7 +471,7 @@ void deprecationsInMethodInvocationSyntax() {
471471
id "java"
472472
id "jacoco"
473473
}
474-
474+
475475
jacocoTestReport {
476476
reports {
477477
xml.enabled false
@@ -485,12 +485,12 @@ void deprecationsInMethodInvocationSyntax() {
485485
id "java"
486486
id "jacoco"
487487
}
488-
488+
489489
jacocoTestReport {
490490
reports {
491-
xml.required false
492-
csv.required true
493-
html.required false
491+
xml.required = false
492+
csv.required = true
493+
html.required = false
494494
}
495495
}
496496
"""
@@ -529,13 +529,13 @@ void deprecationsInMethodInvocationInsideNestedClosure() {
529529
jacocoTestReport {
530530
reports {
531531
xml {
532-
required false
532+
required = false
533533
}
534534
csv {
535-
required true
535+
required = true
536536
}
537537
html {
538-
required false
538+
required = false
539539
}
540540
}
541541
}
@@ -565,9 +565,9 @@ void deprecationsInMethodInvocationWithFieldAccess() {
565565
id "jacoco"
566566
}
567567
jacocoTestReport {
568-
reports.xml.required false
569-
reports.csv.required true
570-
reports.html.required false
568+
reports.xml.required = false
569+
reports.csv.required = true
570+
reports.html.required = false
571571
}
572572
"""
573573
)
@@ -598,9 +598,9 @@ void destinationInMethodInvocationSyntax() {
598598
}
599599
jacocoTestReport {
600600
reports {
601-
xml.outputLocation layout.buildDirectory.dir('jacocoXml')
602-
csv.outputLocation layout.buildDirectory.dir('jacocoCsv')
603-
html.outputLocation layout.buildDirectory.dir('jacocoHtml')
601+
xml.outputLocation = layout.buildDirectory.dir('jacocoXml')
602+
csv.outputLocation = layout.buildDirectory.dir('jacocoCsv')
603+
html.outputLocation = layout.buildDirectory.dir('jacocoHtml')
604604
}
605605
}
606606
"""
@@ -639,13 +639,13 @@ void destinationInMethodInvocationInsideNestedClosure() {
639639
jacocoTestReport {
640640
reports {
641641
xml {
642-
outputLocation layout.buildDirectory.dir('jacocoXml')
642+
outputLocation = layout.buildDirectory.dir('jacocoXml')
643643
}
644644
csv {
645-
outputLocation layout.buildDirectory.dir('jacocoCsv')
645+
outputLocation = layout.buildDirectory.dir('jacocoCsv')
646646
}
647647
html {
648-
outputLocation layout.buildDirectory.dir('jacocoHtml')
648+
outputLocation = layout.buildDirectory.dir('jacocoHtml')
649649
}
650650
}
651651
}
@@ -656,7 +656,7 @@ void destinationInMethodInvocationInsideNestedClosure() {
656656
}
657657

658658
@Test
659-
void allNotationsMixed() {
659+
void allNotationsMixedFromReports() {
660660
rewriteRun(
661661
buildGradle(
662662
"""
@@ -686,12 +686,60 @@ void allNotationsMixed() {
686686
jacocoTestReport {
687687
reports {
688688
xml.required = true
689-
xml.required(false)
690-
xml.required(true)
691-
xml.required false
689+
xml.required = false
690+
xml.required = true
691+
xml.required = false
692692
xml.outputLocation = layout.buildDirectory.dir('jacocoXml')
693-
xml.outputLocation layout.buildDirectory.dir('jacocoXml2')
694-
xml.outputLocation(layout.buildDirectory.dir('jacocoXml3'))
693+
xml.outputLocation = layout.buildDirectory.dir('jacocoXml2')
694+
xml.outputLocation = layout.buildDirectory.dir('jacocoXml3')
695+
}
696+
}
697+
"""
698+
)
699+
);
700+
}
701+
702+
@Test
703+
void allNotationsMixedFromReportsXml() {
704+
rewriteRun(
705+
buildGradle(
706+
"""
707+
plugins {
708+
id "java"
709+
id "jacoco"
710+
}
711+
712+
jacocoTestReport {
713+
reports {
714+
xml {
715+
enabled = true
716+
setEnabled(false)
717+
enabled(true)
718+
enabled false
719+
destination = layout.buildDirectory.dir('jacocoXml')
720+
setDestination layout.buildDirectory.dir('jacocoXml2')
721+
destination(layout.buildDirectory.dir('jacocoXml3'))
722+
}
723+
}
724+
}
725+
""",
726+
"""
727+
plugins {
728+
id "java"
729+
id "jacoco"
730+
}
731+
732+
jacocoTestReport {
733+
reports {
734+
xml {
735+
required = true
736+
required = false
737+
required = true
738+
required = false
739+
outputLocation = layout.buildDirectory.dir('jacocoXml')
740+
outputLocation = layout.buildDirectory.dir('jacocoXml2')
741+
outputLocation = layout.buildDirectory.dir('jacocoXml3')
742+
}
695743
}
696744
}
697745
"""

0 commit comments

Comments
 (0)