/FinalizeLocalVariablesTest.java Secret
Last active
January 27, 2023 19:22
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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