diff --git a/src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNull.java b/src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNull.java index 9ba2b97f..e8236573 100644 --- a/src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNull.java +++ b/src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNull.java @@ -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; } } diff --git a/src/main/java/org/openrewrite/staticanalysis/InstanceOfPatternMatch.java b/src/main/java/org/openrewrite/staticanalysis/InstanceOfPatternMatch.java index 032eeebe..bff256af 100644 --- a/src/main/java/org/openrewrite/staticanalysis/InstanceOfPatternMatch.java +++ b/src/main/java/org/openrewrite/staticanalysis/InstanceOfPatternMatch.java @@ -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; @@ -171,6 +171,9 @@ private static class InstanceOfPatternReplacements { public void registerInstanceOf(J.InstanceOf instanceOf, Set contexts) { Expression expression = instanceOf.getExpression(); JavaType type = ((TypedTree) instanceOf.getClazz()).getType(); + if (type == null) { + return; + } Optional existing = instanceOfs.keySet().stream() .filter(k -> TypeUtils.isAssignableTo(type, k.getType()) && @@ -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); } @@ -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; } @@ -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(), @@ -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())); } diff --git a/src/test/java/org/openrewrite/staticanalysis/InstanceOfPatternMatchTest.java b/src/test/java/org/openrewrite/staticanalysis/InstanceOfPatternMatchTest.java index cfcbc9d0..fb23b21b 100644 --- a/src/test/java/org/openrewrite/staticanalysis/InstanceOfPatternMatchTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/InstanceOfPatternMatchTest.java @@ -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 @@ -34,7 +34,7 @@ public void defaults(RecipeSpec spec) { .allSources(sourceSpec -> version(sourceSpec, 17)); } - @SuppressWarnings({"ImplicitArrayToString", "PatternVariableCanBeUsed", "UnnecessaryLocalVariable"}) + @Nested class If { @Test @@ -177,7 +177,6 @@ void typeParameters_2() { import java.util.Map; import java.util.stream.Collectors; public class A { - @SuppressWarnings("unchecked") public static List> applyRoutesType(Object routes) { if (routes instanceof List) { List routesList = (List) routes; @@ -195,7 +194,6 @@ public static List> applyRoutesType(Object routes) { import java.util.Map; import java.util.stream.Collectors; public class A { - @SuppressWarnings("unchecked") public static List> applyRoutesType(Object routes) { if (routes instanceof List routesList) { if (routesList.isEmpty()) { @@ -386,7 +384,7 @@ private Collection addValueToList(List previousValues, Object va return cl; } } - """ + """ ) ); } @@ -1121,6 +1119,7 @@ void test(Object t) { } } + @SuppressWarnings({"unchecked", "rawtypes"}) @Nested class Various { @Test @@ -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 transformGatewayParameters(Map parameters) { - Map 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 transformGatewayParameters(Map parameters) { + Map 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 transformGatewayParameters(Map parameters) { Map environment = new HashMap<>(); Object applicationSecurityGroups = parameters.get(APPLICATION_SECURITY_GROUPS); diff --git a/src/test/java/org/openrewrite/staticanalysis/UseCollectionInterfacesTest.java b/src/test/java/org/openrewrite/staticanalysis/UseCollectionInterfacesTest.java index ed52a933..19e86a31 100644 --- a/src/test/java/org/openrewrite/staticanalysis/UseCollectionInterfacesTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/UseCollectionInterfacesTest.java @@ -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) { @@ -45,7 +45,7 @@ void noTargetInUse() { """ import java.util.Collections; import java.util.Set; - + class Test { Set method() { return Collections.emptySet(); @@ -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( """ @@ -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( """ @@ -741,7 +741,7 @@ void enumSetHasDifferentGenericTypeThanSet() { java( """ import java.util.EnumSet; - + class Test { public EnumSet values = EnumSet.allOf(A.class); void iterate() { @@ -993,11 +993,9 @@ void groovyDefVariable() { //language=groovy """ library('other-library') - def myMap = [ myEntry: [[ key: value ]] ] - runPipeline(myMap: myMap) """ ) diff --git a/src/test/java/org/openrewrite/staticanalysis/UseDiamondOperatorTest.java b/src/test/java/org/openrewrite/staticanalysis/UseDiamondOperatorTest.java index 72c19b9a..f6986b6b 100644 --- a/src/test/java/org/openrewrite/staticanalysis/UseDiamondOperatorTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/UseDiamondOperatorTest.java @@ -115,14 +115,14 @@ void useDiamondOperatorTest2() { import java.util.function.Predicate; import java.util.List; import java.util.Map; - + class Foo { Map map; Map unknownMap; public Foo(Predicate p) {} public void something(Foo> foos){} public void somethingEasy(List> l){} - + Foo getFoo() { // variable type initializer Foo> f = new Foo>(it -> it.stream().anyMatch(baz -> true)); @@ -137,7 +137,7 @@ Foo getFoo() { // return type unknown return new Foo>(it -> it.stream().anyMatch(baz -> true)); } - + Foo> getFoo2() { // return type expression return new Foo>(it -> it.stream().anyMatch(baz -> true)); @@ -150,14 +150,14 @@ Foo> getFoo2() { import java.util.function.Predicate; import java.util.List; import java.util.Map; - + class Foo { Map map; Map unknownMap; public Foo(Predicate p) {} public void something(Foo> foos){} public void somethingEasy(List> l){} - + Foo getFoo() { // variable type initializer Foo> f = new Foo<>(it -> it.stream().anyMatch(baz -> true)); @@ -172,7 +172,7 @@ Foo getFoo() { // return type unknown return new Foo>(it -> it.stream().anyMatch(baz -> true)); } - + Foo> getFoo2() { // return type expression return new Foo<>(it -> it.stream().anyMatch(baz -> true)); @@ -193,13 +193,13 @@ void returnTypeParamsDoNotMatchNewClassParams() { """ import java.util.List; import java.util.function.Predicate; - + class Test { interface MyInterface { } class MyClass implements MyInterface{ public MyClass(Predicate p, T check) {} } - + public MyInterface a() { return new MyClass, Integer>(l -> l.stream().anyMatch(String::isEmpty), 0); } @@ -211,13 +211,13 @@ public MyClass, Integer> b() { """ import java.util.List; import java.util.function.Predicate; - + class Test { interface MyInterface { } class MyClass implements MyInterface{ public MyClass(Predicate p, T check) {} } - + public MyInterface a() { return new MyClass, Integer>(l -> l.stream().anyMatch(String::isEmpty), 0); } @@ -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( """ diff --git a/src/test/java/org/openrewrite/staticanalysis/UseObjectNotifyAllTest.java b/src/test/java/org/openrewrite/staticanalysis/UseObjectNotifyAllTest.java index e1887d51..58aef038 100644 --- a/src/test/java/org/openrewrite/staticanalysis/UseObjectNotifyAllTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/UseObjectNotifyAllTest.java @@ -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(); @@ -65,7 +64,7 @@ public static void main(String[] args) { } } } - """, + """, """ public final class ProcessStep implements Runnable { private static final Object lock = new Object(); @@ -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(); @@ -96,10 +94,8 @@ public static void main(String[] args) { } } } - """ + """ ) ); } - - }