Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make HiddenField recipe predictable #2596

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,116 @@ public void enhancedForLoop(int[] arr) {
);
}

@Test
void blocks() {
rewriteRun(
java(
"""
public class A {
int n;

public void blocks() {
{
int n = 0;
int x = n;
}
{
int n = 0;
int x = n;
}
}
}
""",
"""
public class A {
int n;

public void blocks() {
{
int n1 = 0;
int x = n1;
}
{
int n1 = 0;
int x = n1;
}
}
}
"""
)
);
}

@Test
@SuppressWarnings("ConstantValue")
void tryResources() {
rewriteRun(
java(
"""
public class A {
int n;

public void tryWithResources(int n4) {
Object n1 = null;
try (java.io.InputStream n = null; java.io.OutputStream n2 = null) {
Object n3 = null;
Object x = n;
}
}
}
""",
"""
public class A {
int n;

public void tryWithResources(int n4) {
Object n1 = null;
try (java.io.InputStream n5 = null; java.io.OutputStream n2 = null) {
Object n3 = null;
Object x = n5;
}
}
}
"""
)
);
}

@Test
@SuppressWarnings({"EmptyTryBlock", "CatchMayIgnoreException", "TryWithIdenticalCatches"})
void catchClause() {
rewriteRun(
java(
"""
public class A {
int e;

public void tryCatch() {
try (java.io.InputStream e1 = null) {
} catch (RuntimeException e) {
} catch (java.io.IOException e) {
} catch (Exception e) {
}
}
}
""",
"""
public class A {
int e;

public void tryCatch() {
try (java.io.InputStream e1 = null) {
} catch (RuntimeException e2) {
} catch (java.io.IOException e3) {
} catch (Exception e4) {
}
}
}
"""
)
);
}

@SuppressWarnings({"Convert2MethodRef", "ResultOfMethodCallIgnored"})
@Test
void lambdaWithTypedParameterHides() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -100,7 +97,7 @@ private FindExistingVariableDeclarations(Cursor childTargetReference, String chi
* @return A set of existing variable definition of the {@param childTargetName} within the same name scope as the {@param childTargetName}.
*/
public static Set<J.VariableDeclarations.NamedVariable> find(J j, Cursor childTargetReference, String childTargetName) {
Set<J.VariableDeclarations.NamedVariable> references = new HashSet<>();
Set<J.VariableDeclarations.NamedVariable> references = new LinkedHashSet<>();
new FindExistingVariableDeclarations(childTargetReference, childTargetName).visit(j, references);
return references;
}
Expand Down Expand Up @@ -187,7 +184,7 @@ public FindNameShadows(J.VariableDeclarations.NamedVariable targetVariable, J.Cl
* @return A set representing any found {@link J.VariableDeclarations.NamedVariable} which shadow the provided {@param targetVariable}.
*/
public static Set<J.VariableDeclarations.NamedVariable> find(J j, J.VariableDeclarations.NamedVariable targetVariable, J.ClassDeclaration targetVariableEnclosingClass, HiddenFieldStyle hiddenFieldStyle) {
Set<J.VariableDeclarations.NamedVariable> references = new HashSet<>();
Set<J.VariableDeclarations.NamedVariable> references = new LinkedHashSet<>();
new FindNameShadows(targetVariable, targetVariableEnclosingClass, hiddenFieldStyle).visit(j, references);
return references;
}
Expand Down