|
| 1 | +/* |
| 2 | + * Copyright 2026 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.gradle.gradle9; |
| 17 | + |
| 18 | +import lombok.EqualsAndHashCode; |
| 19 | +import lombok.Value; |
| 20 | +import org.openrewrite.ExecutionContext; |
| 21 | +import org.openrewrite.Preconditions; |
| 22 | +import org.openrewrite.Recipe; |
| 23 | +import org.openrewrite.TreeVisitor; |
| 24 | +import org.openrewrite.gradle.IsBuildGradle; |
| 25 | +import org.openrewrite.java.JavaVisitor; |
| 26 | +import org.openrewrite.java.tree.Expression; |
| 27 | +import org.openrewrite.java.tree.J; |
| 28 | + |
| 29 | +@Value |
| 30 | +@EqualsAndHashCode(callSuper = false) |
| 31 | +public class UseMainClassPropertyForApplication extends Recipe { |
| 32 | + |
| 33 | + private static final String IN_APPLICATION = "IN_APPLICATION"; |
| 34 | + |
| 35 | + String displayName = "Use `mainClass` instead of `mainClassName` in the `application` block"; |
| 36 | + |
| 37 | + String description = "The `mainClassName` property on the `application` extension was deprecated in Gradle 6.4 and removed in Gradle 9.0. " + |
| 38 | + "Use the `mainClass` property instead. " + |
| 39 | + "See the [Gradle upgrade guide](https://docs.gradle.org/9.0.0/userguide/upgrading_major_version_9.html) for more information."; |
| 40 | + |
| 41 | + @Override |
| 42 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 43 | + return Preconditions.check(new IsBuildGradle<>(), new JavaVisitor<ExecutionContext>() { |
| 44 | + @Override |
| 45 | + public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { |
| 46 | + if ("application".equals(method.getSimpleName())) { |
| 47 | + getCursor().putMessage(IN_APPLICATION, true); |
| 48 | + } |
| 49 | + return super.visitMethodInvocation(method, ctx); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext ctx) { |
| 54 | + if (getCursor().getNearestMessage(IN_APPLICATION) == null) { |
| 55 | + return assignment; |
| 56 | + } |
| 57 | + Expression variable = assignment.getVariable(); |
| 58 | + if (variable instanceof J.Identifier) { |
| 59 | + J.Identifier id = (J.Identifier) variable; |
| 60 | + if ("mainClassName".equals(id.getSimpleName())) { |
| 61 | + return assignment.withVariable(id.withSimpleName("mainClass")); |
| 62 | + } |
| 63 | + } else if (variable instanceof J.FieldAccess) { |
| 64 | + J.FieldAccess fieldAccess = (J.FieldAccess) variable; |
| 65 | + if ("mainClassName".equals(fieldAccess.getSimpleName())) { |
| 66 | + return assignment.withVariable( |
| 67 | + fieldAccess.withName(fieldAccess.getName().withSimpleName("mainClass")) |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | + return assignment; |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | +} |
0 commit comments