Skip to content

Commit

Permalink
Prove C# support for ExplicitInitialization recipe (#401)
Browse files Browse the repository at this point in the history
* Improve tests and visitor for  ExplicitInitialization recipe

* Add C# test for  ExplicitInitialization recipe

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Fix license header

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
jevanlingen and github-actions[bot] authored Dec 3, 2024
1 parent ea74635 commit 428347d
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,9 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
if (service(AnnotationService.class).matches(variableDeclsCursor, LOMBOK_BUILDER_DEFAULT)) {
return v;
}
J.Literal literalInit = variable.getInitializer() instanceof J.Literal ?
(J.Literal) variable.getInitializer() :
null;
if (literalInit != null && !variableDecls.hasModifier(J.Modifier.Type.Final)) {
if (TypeUtils.asFullyQualified(variable.getType()) != null &&
JavaType.Primitive.Null.equals(literalInit.getType())) {
if (variable.getInitializer() instanceof J.Literal && !variableDecls.hasModifier(J.Modifier.Type.Final)) {
J.Literal literalInit = (J.Literal) variable.getInitializer();
if (TypeUtils.asFullyQualified(variable.getType()) != null && JavaType.Primitive.Null.equals(literalInit.getType())) {
v = v.withInitializer(null);
} else if (primitive != null && !Boolean.TRUE.equals(style.getOnlyObjectReferences())) {
switch (primitive) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@
import org.openrewrite.ExecutionContext;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Tree;
import org.openrewrite.csharp.tree.Cs;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.Space;
import org.openrewrite.marker.Markers;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.test.RewriteTest.toRecipe;
import static org.openrewrite.staticanalysis.charp.JavaToCsharp.toCsRecipe;

@SuppressWarnings("ALL")
class CatchClauseOnlyRethrowsTest implements RewriteTest {
Expand Down Expand Up @@ -340,14 +338,7 @@ void foo() throws IOException {
@Test
void verifyCsharpImplicitThrow() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
@Override
public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
Cs.CompilationUnit cscu = JavaToCsharp.compilationUnit(cu);
// Exercise the regular recipe with the now modified CSharp compilation unit
return (J) new CatchClauseOnlyRethrows().getVisitor().visit(cscu, ctx);
}
})),
spec -> spec.recipe(toCsRecipe(new CatchClauseOnlyRethrows())),
//language=java
java(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,13 @@ class Test {
}

@Test
void ignoreInterfaces() {
void ignoreVariablesInMethods() {
rewriteRun(
//language=java
java(
"""
interface Test {
private int a = 0;
void s() {
class Test {
private void test() {
int i = 0;
}
}
Expand All @@ -97,6 +96,20 @@ void s() {
);
}

@Test
void ignoreInterfaces() {
rewriteRun(
//language=java
java(
"""
interface Test {
int a = 0;
}
"""
)
);
}

@Test
void blockStatement() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.staticanalysis.charp;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.JavaParser;
import org.openrewrite.staticanalysis.ExplicitInitialization;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.staticanalysis.charp.JavaToCsharp.toCsRecipe;

class ExplicitInitializationVisitorCsharpTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(toCsRecipe(new ExplicitInitialization()))
.parser(JavaParser.fromJavaVersion()
.dependsOn(
// C# defines nullable primitive. These are actually in the CLR wrapper objects.
//language=java
"""
class Nullable<T> {
Integer a;
Nullable(Integer a) {
this.a = a;
}
}
"""
)
);
}

@DocumentExample
@Test
void removeExplicitInitialization() {
rewriteRun(
//language=java
java(
"""
class A {
// C#: int? a
Nullable<Integer> a = null;
// C#: int? a = 0
Nullable<Integer> a = new Nullable<>(0);
}
""",
"""
class A {
// C#: int? a
Nullable<Integer> a;
// C#: int? a = 0
Nullable<Integer> a = new Nullable<>(0);
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,35 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.staticanalysis;
package org.openrewrite.staticanalysis.charp;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.csharp.tree.Cs;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JRightPadded;
import org.openrewrite.java.tree.Statement;
import org.openrewrite.test.AdHocRecipe;

import java.util.List;

import static org.openrewrite.test.RewriteTest.toRecipe;

public class JavaToCsharp {

public static Cs.CompilationUnit compilationUnit(J.CompilationUnit cu) {
public static AdHocRecipe toCsRecipe(Recipe recipe) {
return toRecipe(() -> new JavaVisitor<>() {
@Override
public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
Cs.CompilationUnit cscu = compilationUnit(cu);
// Exercise the regular recipe with the now modified CSharp compilation unit
return (J) recipe.getVisitor().visit(cscu, ctx);
}
});
}

private static Cs.CompilationUnit compilationUnit(J.CompilationUnit cu) {
return new Cs.CompilationUnit(
cu.getId(),
cu.getPrefix(),
Expand Down

0 comments on commit 428347d

Please sign in to comment.