Skip to content

Instantly share code, notes, and snippets.

@Bit-Flipper
Created January 14, 2024 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bit-Flipper/b052515c9b41a22a83da30764536df66 to your computer and use it in GitHub Desktop.
Save Bit-Flipper/b052515c9b41a22a83da30764536df66 to your computer and use it in GitHub Desktop.
package org.example;
import org.junit.jupiter.api.Test;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import static org.openrewrite.java.Assertions.java;
public class TestJavaGetterToLombokGetter implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new JavaGetterToLombokGetter());
}
@Test
public void doesNotChangeWhenGetterNamesAndTypesDoNotMatch() {
rewriteRun(
java(
//language=java
"""
public class TestClass {
public String fooFoo = "foo";
public int bar = 10;
public String getFoo() {
return fooFoo;
}
public String getBar() {
return "bar";
}
}
"""
)
);
}
@Test
public void getMethodIsReplacedWithLombokAnnotation() {
rewriteRun(
java(
//language=java
"""
public class TestClass {
public String foo = "foo";
public int a = 10;
public String getFoo() {
return foo;
}
public int getA() {
return a;
}
}
""",
//language=java
"""
import lombok.Getter;
public class TestClass {
@Getter
public String foo = "foo";
@Getter
public int a = 10;
}
"""
)
);
}
@Test
public void genericsAreSupported() {
rewriteRun(
java(
//language=java
"""
import java.util.List;
public class TestClass {
public List<String> g = List.of("g", "gG");
public List<String> getG() {
return g;
}
}
""",
//language=java
"""
import lombok.Getter;
import java.util.List;
public class TestClass {
@Getter
public List<String> g = List.of("g", "gG");
}
"""
)
);
}
@Test
public void nonStandardGettersAreNotConverted() {
rewriteRun(
java(
//language=java
"""
public class TestClass {
public String foo = "foo";
public int bar = 10;
public int getFoo() {
return bar;
}
public int getBar() {
if (Math.random() > 0.5) {
return 1;
}
return 2;
}
}
"""
)
);
}
@Test
public void voidMethodsAreIgnored() {
rewriteRun(
java(
//language=java
"""
public class TestClass {
public String foo = "foo";
public int bar = 10;
public void getFoo() {
System.out.println(foo);
}
public void getBar() { }
}
"""
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment