Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license
* <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.java.migrate.lang;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.RenameVariable;
import org.openrewrite.java.search.UsesJavaVersion;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

@EqualsAndHashCode(callSuper = false)
@Value
public class RenameUnderscoreIdentifier extends Recipe {

private static final String UNDERSCORE = "_";
private static final String DOUBLE_UNDERSCORE = "__";

String displayName = "Rename `_` identifier to `__`";

String description = "Renames single-underscore identifiers to double-underscore " +
"in Java source files with source compatibility of Java 8 or below. " +
"In Java 9+, `_` is a reserved keyword and causes a compile error.";

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
new UsesJavaVersion<>(1, 8),
new JavaIsoVisitor<ExecutionContext>() {

@Override
public J.VariableDeclarations visitVariableDeclarations(
J.VariableDeclarations multiVariable, ExecutionContext ctx) {
for (J.VariableDeclarations.NamedVariable v : multiVariable.getVariables()) {
if (UNDERSCORE.equals(v.getSimpleName())) {
doAfterVisit(new RenameVariable<>(v, DOUBLE_UNDERSCORE));
}
}
return super.visitVariableDeclarations(multiVariable, ctx);
}

@Override
public J.MethodDeclaration visitMethodDeclaration(
J.MethodDeclaration method, ExecutionContext ctx) {
method = super.visitMethodDeclaration(method, ctx);
if (UNDERSCORE.equals(method.getSimpleName())) {
JavaType.Method type = method.getMethodType();
if (type != null) {
type = type.withName(DOUBLE_UNDERSCORE);
}
method = method.withName(method.getName().withSimpleName(DOUBLE_UNDERSCORE)
.withType(type))
.withMethodType(type);
}
return method;
}

@Override
public J.MethodInvocation visitMethodInvocation(
J.MethodInvocation method, ExecutionContext ctx) {
method = super.visitMethodInvocation(method, ctx);
if (UNDERSCORE.equals(method.getSimpleName())) {
JavaType.Method type = method.getMethodType();
if (type != null) {
type = type.withName(DOUBLE_UNDERSCORE);
}
method = method.withName(method.getName().withSimpleName(DOUBLE_UNDERSCORE)
.withType(type))
.withMethodType(type);
}
return method;
}

@Override
public J.MemberReference visitMemberReference(
J.MemberReference memberRef, ExecutionContext ctx) {
memberRef = super.visitMemberReference(memberRef, ctx);
if (UNDERSCORE.equals(memberRef.getReference().getSimpleName())) {
JavaType.Method type = memberRef.getMethodType();
if (type != null) {
type = type.withName(DOUBLE_UNDERSCORE);
}
memberRef = memberRef.withReference(memberRef.getReference().withSimpleName(DOUBLE_UNDERSCORE))
.withMethodType(type);
}
return memberRef;
}

@Override
public J.ClassDeclaration visitClassDeclaration(
J.ClassDeclaration classDecl, ExecutionContext ctx) {
classDecl = super.visitClassDeclaration(classDecl, ctx);
if (UNDERSCORE.equals(classDecl.getSimpleName())) {
classDecl = classDecl.withName(classDecl.getName().withSimpleName(DOUBLE_UNDERSCORE));
}
return classDecl;
}
}
);
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/java-version-11.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ preconditions:
- org.openrewrite.Singleton
recipeList:
- org.openrewrite.java.migrate.UpgradeToJava8
- org.openrewrite.java.migrate.lang.RenameUnderscoreIdentifier
- org.openrewrite.java.migrate.UseJavaUtilBase64
- org.openrewrite.java.migrate.CastArraysAsListToList
# Add an explicit JAXB/JAX-WS runtime and upgrade the dependencies to Jakarta EE 8
Expand Down
Loading
Loading