Skip to content

Commit

Permalink
Fix tests which broke because a new version of annotations was releas…
Browse files Browse the repository at this point in the history
…ed. Cleanup test warnings & whitespace
  • Loading branch information
sambsnyd committed Oct 1, 2024
1 parent 35606d1 commit acc1af7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (style == null) {
style = Checkstyle.equalsAvoidsNull();
}
return new EqualsAvoidsNullVisitor<>(style).visit(cu, ctx);
return new EqualsAvoidsNullVisitor<>(style).visitNonNull(cu, ctx);
}
//noinspection DataFlowIssue
return (J) tree;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
import static org.openrewrite.Tree.randomId;
import static org.openrewrite.java.VariableNameUtils.GenerationStrategy.INCREMENT_NUMBER;

Expand Down Expand Up @@ -171,6 +171,9 @@ private static class InstanceOfPatternReplacements {
public void registerInstanceOf(J.InstanceOf instanceOf, Set<J> contexts) {
Expression expression = instanceOf.getExpression();
JavaType type = ((TypedTree) instanceOf.getClazz()).getType();
if (type == null) {
return;
}

Optional<ExpressionAndType> existing = instanceOfs.keySet().stream()
.filter(k -> TypeUtils.isAssignableTo(type, k.getType()) &&
Expand Down Expand Up @@ -200,8 +203,9 @@ public void registerTypeCast(J.TypeCast typeCast, Cursor cursor) {
if (validContexts.contains(next)) {
if (isAcceptableTypeCast(typeCast) && isTheSameAsOtherTypeCasts(typeCast, instanceOf)) {
if (parent.getValue() instanceof J.VariableDeclarations.NamedVariable &&
!variablesToDelete.containsKey(instanceOf)) {
variablesToDelete.put(instanceOf, new VariableAndTypeTree(parent.getValue(), parent.firstEnclosing(J.VariableDeclarations.class).getTypeExpression()));
!variablesToDelete.containsKey(instanceOf)) {
variablesToDelete.put(instanceOf, new VariableAndTypeTree(parent.getValue(),
requireNonNull(parent.firstEnclosing(J.VariableDeclarations.class).getTypeExpression())));
} else {
replacements.put(typeCast, instanceOf);
}
Expand All @@ -224,7 +228,7 @@ public void registerTypeCast(J.TypeCast typeCast, Cursor cursor) {
private boolean isAcceptableTypeCast(J.TypeCast typeCast) {
TypeTree typeTree = typeCast.getClazz().getTree();
if (typeTree instanceof J.ParameterizedType) {
return ((J.ParameterizedType) typeTree).getTypeParameters().stream().allMatch(J.Wildcard.class::isInstance);
return requireNonNull(((J.ParameterizedType) typeTree).getTypeParameters()).stream().allMatch(J.Wildcard.class::isInstance);
}
return true;
}
Expand All @@ -247,7 +251,7 @@ public J.InstanceOf processInstanceOf(J.InstanceOf instanceOf, Cursor cursor) {
if (!contextScopes.containsKey(instanceOf)) {
return instanceOf;
}
@Nullable JavaType type = ((TypedTree) instanceOf.getClazz()).getType();
JavaType type = ((TypedTree) instanceOf.getClazz()).getType();
String name = patternVariableName(instanceOf, cursor);
J.InstanceOf result = instanceOf.withPattern(new J.Identifier(
randomId(),
Expand All @@ -260,8 +264,8 @@ public J.InstanceOf processInstanceOf(J.InstanceOf instanceOf, Cursor cursor) {

J currentTypeTree = instanceOf.getClazz();
TypeTree typeCastTypeTree = computeTypeTreeFromTypeCasts(instanceOf);
// If type tree from typa cast is not parameterized then NVM. Instance of should already have proper type
if (typeCastTypeTree != null && typeCastTypeTree instanceof J.ParameterizedType) {
// If type tree from type cast is not parameterized then NVM. Instance of should already have proper type
if (typeCastTypeTree instanceof J.ParameterizedType) {
J.ParameterizedType parameterizedType = (J.ParameterizedType) typeCastTypeTree;
result = result.withClazz(parameterizedType.withId(Tree.randomId()).withPrefix(currentTypeTree.getPrefix()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.version;

@SuppressWarnings({"RedundantCast", "DataFlowIssue", "ConstantValue"})
@SuppressWarnings({"RedundantCast", "DataFlowIssue", "ConstantValue", "ImplicitArrayToString", "PatternVariableCanBeUsed", "UnnecessaryLocalVariable", "SizeReplaceableByIsEmpty", "rawtypes", "ResultOfMethodCallIgnored", "ArraysAsListWithZeroOrOneArgument", "DuplicateCondition"})
class InstanceOfPatternMatchTest implements RewriteTest {

@Override
Expand All @@ -34,7 +34,7 @@ public void defaults(RecipeSpec spec) {
.allSources(sourceSpec -> version(sourceSpec, 17));
}

@SuppressWarnings({"ImplicitArrayToString", "PatternVariableCanBeUsed", "UnnecessaryLocalVariable"})

@Nested
class If {
@Test
Expand Down Expand Up @@ -177,7 +177,6 @@ void typeParameters_2() {
import java.util.Map;
import java.util.stream.Collectors;
public class A {
@SuppressWarnings("unchecked")
public static List<Map<String, Object>> applyRoutesType(Object routes) {
if (routes instanceof List) {
List routesList = (List) routes;
Expand All @@ -195,7 +194,6 @@ public static List<Map<String, Object>> applyRoutesType(Object routes) {
import java.util.Map;
import java.util.stream.Collectors;
public class A {
@SuppressWarnings("unchecked")
public static List<Map<String, Object>> applyRoutesType(Object routes) {
if (routes instanceof List routesList) {
if (routesList.isEmpty()) {
Expand Down Expand Up @@ -386,7 +384,7 @@ private Collection<Object> addValueToList(List<String> previousValues, Object va
return cl;
}
}
"""
"""
)
);
}
Expand Down Expand Up @@ -1121,6 +1119,7 @@ void test(Object t) {
}
}

@SuppressWarnings({"unchecked", "rawtypes"})
@Nested
class Various {
@Test
Expand Down Expand Up @@ -1176,39 +1175,36 @@ String test(Object o) {
)
);
}

@Test
void iterableParameter() {
rewriteRun(
//language=java
java(
"""
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ApplicationSecurityGroupsParameterHelper {
static final String APPLICATION_SECURITY_GROUPS = "application-security-groups";
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public Map<String, Object> transformGatewayParameters(Map<String, Object> parameters) {
Map<String, Object> environment = new HashMap<>();
Object applicationSecurityGroups = parameters.get(APPLICATION_SECURITY_GROUPS);
if (applicationSecurityGroups instanceof List) {
environment.put(APPLICATION_SECURITY_GROUPS, String.join(",", (List) applicationSecurityGroups));
}
return environment;
}
}
""",
"""
public class ApplicationSecurityGroupsParameterHelper {
static final String APPLICATION_SECURITY_GROUPS = "application-security-groups";
public Map<String, Object> transformGatewayParameters(Map<String, Object> parameters) {
Map<String, Object> environment = new HashMap<>();
Object applicationSecurityGroups = parameters.get(APPLICATION_SECURITY_GROUPS);
if (applicationSecurityGroups instanceof List) {
environment.put(APPLICATION_SECURITY_GROUPS, String.join(",", (List) applicationSecurityGroups));
}
return environment;
}
}
""",
"""
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ApplicationSecurityGroupsParameterHelper {
static final String APPLICATION_SECURITY_GROUPS = "application-security-groups";
public Map<String, Object> transformGatewayParameters(Map<String, Object> parameters) {
Map<String, Object> environment = new HashMap<>();
Object applicationSecurityGroups = parameters.get(APPLICATION_SECURITY_GROUPS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.javaVersion;

@SuppressWarnings("rawtypes")
@SuppressWarnings({"rawtypes", "StatementWithEmptyBody"})
class UseCollectionInterfacesTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
Expand All @@ -45,7 +45,7 @@ void noTargetInUse() {
"""
import java.util.Collections;
import java.util.Set;
class Test {
Set<Integer> method() {
return Collections.emptySet();
Expand Down Expand Up @@ -138,7 +138,7 @@ void annotatedReturnType() {
rewriteRun(
spec -> spec
.allSources(s -> s.markers(javaVersion(9)))
.parser(JavaParser.fromJavaVersion().classpath("annotations-24.1.0")),
.parser(JavaParser.fromJavaVersion().classpath("annotations")),
//language=java
java(
"""
Expand Down Expand Up @@ -266,7 +266,7 @@ void annotatedFieldType() {
rewriteRun(
spec -> spec
.allSources(s -> s.markers(javaVersion(9)))
.parser(JavaParser.fromJavaVersion().classpath("annotations-24.1.0")),
.parser(JavaParser.fromJavaVersion().classpath("annotations")),
//language=java
java(
"""
Expand Down Expand Up @@ -741,7 +741,7 @@ void enumSetHasDifferentGenericTypeThanSet() {
java(
"""
import java.util.EnumSet;
class Test {
public EnumSet values = EnumSet.allOf(A.class);
void iterate() {
Expand Down Expand Up @@ -993,11 +993,9 @@ void groovyDefVariable() {
//language=groovy
"""
library('other-library')
def myMap = [
myEntry: [[ key: value ]]
]
runPipeline(myMap: myMap)
"""
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ void useDiamondOperatorTest2() {
import java.util.function.Predicate;
import java.util.List;
import java.util.Map;
class Foo<T> {
Map<String, Integer> map;
Map unknownMap;
public Foo(Predicate<T> p) {}
public void something(Foo<List<String>> foos){}
public void somethingEasy(List<List<String>> l){}
Foo getFoo() {
// variable type initializer
Foo<List<String>> f = new Foo<List<String>>(it -> it.stream().anyMatch(baz -> true));
Expand All @@ -137,7 +137,7 @@ Foo getFoo() {
// return type unknown
return new Foo<List<String>>(it -> it.stream().anyMatch(baz -> true));
}
Foo<List<String>> getFoo2() {
// return type expression
return new Foo<List<String>>(it -> it.stream().anyMatch(baz -> true));
Expand All @@ -150,14 +150,14 @@ Foo<List<String>> getFoo2() {
import java.util.function.Predicate;
import java.util.List;
import java.util.Map;
class Foo<T> {
Map<String, Integer> map;
Map unknownMap;
public Foo(Predicate<T> p) {}
public void something(Foo<List<String>> foos){}
public void somethingEasy(List<List<String>> l){}
Foo getFoo() {
// variable type initializer
Foo<List<String>> f = new Foo<>(it -> it.stream().anyMatch(baz -> true));
Expand All @@ -172,7 +172,7 @@ Foo getFoo() {
// return type unknown
return new Foo<List<String>>(it -> it.stream().anyMatch(baz -> true));
}
Foo<List<String>> getFoo2() {
// return type expression
return new Foo<>(it -> it.stream().anyMatch(baz -> true));
Expand All @@ -193,13 +193,13 @@ void returnTypeParamsDoNotMatchNewClassParams() {
"""
import java.util.List;
import java.util.function.Predicate;
class Test {
interface MyInterface<T> { }
class MyClass<S, T> implements MyInterface<T>{
public MyClass(Predicate<S> p, T check) {}
}
public MyInterface<Integer> a() {
return new MyClass<List<String>, Integer>(l -> l.stream().anyMatch(String::isEmpty), 0);
}
Expand All @@ -211,13 +211,13 @@ public MyClass<List<String>, Integer> b() {
"""
import java.util.List;
import java.util.function.Predicate;
class Test {
interface MyInterface<T> { }
class MyClass<S, T> implements MyInterface<T>{
public MyClass(Predicate<S> p, T check) {}
}
public MyInterface<Integer> a() {
return new MyClass<List<String>, Integer>(l -> l.stream().anyMatch(String::isEmpty), 0);
}
Expand Down Expand Up @@ -525,7 +525,7 @@ void doNotChangeAnnotatedTypeParameters() {
rewriteRun(
spec -> spec
.allSources(s -> s.markers(javaVersion(9)))
.parser(JavaParser.fromJavaVersion().classpath("annotations-24.1.0")),
.parser(JavaParser.fromJavaVersion().classpath("annotations")),
//language=java
java(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ public final class ProcessStep implements Runnable {
// reaches this value
public ProcessStep(int step) {
this.step = step;
}
}
@Override public void run() {
try {
synchronized (lock) {
while (time != step) {
lock.wait();
}
}
time++;
lock.notify();
Thread.notify();
Expand All @@ -65,7 +64,7 @@ public static void main(String[] args) {
}
}
}
""",
""",
"""
public final class ProcessStep implements Runnable {
private static final Object lock = new Object();
Expand All @@ -74,14 +73,13 @@ public final class ProcessStep implements Runnable {
// reaches this value
public ProcessStep(int step) {
this.step = step;
}
}
@Override public void run() {
try {
synchronized (lock) {
while (time != step) {
lock.wait();
}
}
time++;
lock.notifyAll();
Thread.notifyAll();
Expand All @@ -96,10 +94,8 @@ public static void main(String[] args) {
}
}
}
"""
"""
)
);
}


}

0 comments on commit acc1af7

Please sign in to comment.