Skip to content

Instantly share code, notes, and snippets.

@mike-solomon
Last active January 27, 2023 19:22
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 mike-solomon/b39e8c3549f3cd1b63f80de2853c8771 to your computer and use it in GitHub Desktop.
Save mike-solomon/b39e8c3549f3cd1b63f80de2853c8771 to your computer and use it in GitHub Desktop.
package com.yourorg;
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 FinalizeLocalVariablesTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new FinalizeLocalVariables());
}
@Test
void doesNotModifyReassignedVariables() {
rewriteRun(
java(
"""
class A {
public void test() {
int a = 0;
int b = 0;
for(int i = 0; i < 50; i++) {
a = i + 5;
b++;
}
}
}
"""
)
);
}
@Test
void doesNotModifyUninitializedVariables() {
rewriteRun(
java(
"""
class A {
void test() {
int a;
a = 0;
}
}
"""
)
);
}
@Test
void doesNotModifyLoopControlVariables() {
rewriteRun(
java(
"""
class A {
void test() {
for (int i = 0; i < 100; i++) {
System.out.println(i);
}
}
}
"""
)
);
}
// aka "non-static fields"
@Test
void doesNotModifyInstanceVariables() {
rewriteRun(
java(
"""
class A {
int instanceVariableUninitialized;
int instanceVariableInitialized = 0;
}
"""
)
);
}
// aka "static fields"
@Test
void doesNotModifyClassVariables() {
rewriteRun(
java(
"""
class A {
static int classVariableInitialized = 0;
}
"""
)
);
}
@Test
void doesNotModifyVariablesInCatchBlocks() {
rewriteRun(
java(
"""
import java.io.IOException;
class Test {
static {
try {
throw new IOException();
} catch (RuntimeException | IOException e) {
System.out.println("oops");
}
}
}
"""
)
);
}
@Test
void doesNotModifyVariablesInStaticInitializers() {
rewriteRun(
java(
"""
class Test {
static {
int n = 1;
for(int i = 0; i < n; i++) {
}
}
}
"""
)
);
}
@Test
void doesNotModifyVariablesCreatedInMethodParameters() {
rewriteRun(
java(
"""
class Test {
private static int testMath(int x, int y) {
y = y + y;
return x + y;
}
public static void main(String[] args) { }
}
"""
)
);
}
@Test
void doesNotModifyLambdaVariables() {
rewriteRun(
java(
"""
import java.util.stream.Stream;
class A {
public boolean hasFoo(Stream<String> input) {
return input.anyMatch(word -> word.equalsIgnoreCase("foo"));
}
}
"""
)
);
}
@Test
void localVariablesAreMadeFinal() {
rewriteRun(
java(
"""
class A {
public void test() {
int n = 1;
for(int i = 0; i < n; i++) { }
}
}
""",
"""
class A {
public void test() {
final int n = 1;
for(int i = 0; i < n; i++) { }
}
}
"""
)
);
}
@Test
void localVariablesAreMadeFinalWithScopeAwareness() {
rewriteRun(
java(
"""
class Test {
public static void testA() {
int a = 0;
a = 1;
}
public static void testB() {
int a = 0;
}
}
""",
"""
class Test {
public static void testA() {
int a = 0;
a = 1;
}
public static void testB() {
final int a = 0;
}
}
"""
)
);
}
@Test
void nonModifyingUnaryOperatorAwareness() {
rewriteRun(
java(
"""
class Test {
void test() {
int i = 1;
int j = -i;
int k = +j;
int l = ~k;
}
}
""",
"""
class Test {
void test() {
final int i = 1;
final int j = -i;
final int k = +j;
final int l = ~k;
}
}
"""
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment