Skip to content

Commit

Permalink
Remove illegal semicolons in Java 21 (#404)
Browse files Browse the repository at this point in the history
Fixes #396
  • Loading branch information
timtebeek authored Jan 27, 2024
1 parent aff2872 commit 13f7eba
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2024 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.java.migrate;

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.search.UsesJavaVersion;
import org.openrewrite.java.tree.J;

public class RemoveIllegalSemicolons extends Recipe {
@Override
public String getDisplayName() {
return "Remove illegal semicolons";
}

@Override
public String getDescription() {
//language=markdown
return "Remove semicolons after package declarations and imports, no longer accepted in Java 21 as of " +
"[JDK-8027682](https://bugs.openjdk.org/browse/JDK-8027682).";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesJavaVersion<>(21), new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.Import visitImport(J.Import _import, ExecutionContext ctx) {
J.Import im = super.visitImport(_import, ctx);
if (im.getPrefix().getWhitespace().contains(";")) {
im = im.withPrefix(im.getPrefix()
.withWhitespace(im.getPrefix().getWhitespace()
.replaceAll("\\s*;(\\R?)\\s*", "$1")));
}
return im;
}
});
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/java-version-21.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ tags:
recipeList:
- org.openrewrite.java.migrate.UpgradeToJava17
- org.openrewrite.java.migrate.JavaVersion21
- org.openrewrite.java.migrate.RemoveIllegalSemicolons
- org.openrewrite.java.migrate.lang.ThreadStopUnsupported
- org.openrewrite.java.migrate.net.URLConstructorsToURIRecipes
- org.openrewrite.java.migrate.util.SequencedCollection
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2024 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.java.migrate;

import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.javaVersion;

class RemoveIllegalSemicolonsTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new RemoveIllegalSemicolons())
.allSources(src -> src.markers(javaVersion(21)));
}

@Test
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/396")
void importSemicolon() {
//language=java
rewriteRun(
java(
"""
package p; ;
;; import java.util.List;
class AfterPackage { }
""",
"""
package p;
import java.util.List;
class AfterPackage { }
"""
),
java(
"""
package p;
import java.util.List; ;
;; import java.util.Set;
class BetweenImport { }
""",
"""
package p;
import java.util.List;
import java.util.Set;
class BetweenImport { }
"""
)
);
}
}

0 comments on commit 13f7eba

Please sign in to comment.