From 48ed9312d88baa2555a54b99d1c9e909d2f6aef1 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 22 Jul 2025 09:32:10 +0000 Subject: [PATCH] refactor: Unwrap else block after return or throw statement Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.staticanalysis.UnwrapElseAfterReturn?organizationId=ODQ2MGExMTUtNDg0My00N2EwLTgzMGMtNGE1NGExMTBmZDkw Co-authored-by: Moderne --- .../migrate/JREThrowableFinalMethods.java | 3 ++- .../java/migrate/JpaCacheProperties.java | 12 +++++++---- .../java/migrate/UseJavaUtilBase64.java | 3 ++- .../guava/NoGuavaListsNewArrayList.java | 6 ++++-- .../NoGuavaListsNewCopyOnWriteArrayList.java | 5 +++-- .../guava/NoGuavaListsNewLinkedList.java | 5 +++-- .../migrate/guava/NoGuavaMapsNewHashMap.java | 3 ++- .../guava/NoGuavaMapsNewLinkedHashMap.java | 3 ++- .../migrate/guava/NoGuavaMapsNewTreeMap.java | 6 ++++-- .../migrate/guava/NoGuavaSetsNewHashSet.java | 20 +++++++++---------- .../guava/NoGuavaSetsNewLinkedHashSet.java | 8 +++++--- .../guava/NoMapsAndSetsWithExpectedSize.java | 9 ++++++--- .../jakarta/UpdateBeanManagerMethods.java | 9 +++++---- .../java/migrate/lang/NullCheck.java | 3 ++- .../lang/SwitchCaseEnumGuardToLabel.java | 3 ++- .../java/migrate/lang/UseTextBlocks.java | 12 +++++------ .../java/migrate/lombok/LombokUtils.java | 9 +++++---- .../migrate/lombok/LombokValueToRecord.java | 3 +-- .../java/migrate/lombok/UseLombokGetter.java | 3 ++- .../java/migrate/lombok/UseLombokSetter.java | 3 ++- .../net/URLConstructorToURICreate.java | 3 ++- .../migrate/net/URLConstructorsToNewURI.java | 3 ++- 22 files changed, 80 insertions(+), 54 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/JREThrowableFinalMethods.java b/src/main/java/org/openrewrite/java/migrate/JREThrowableFinalMethods.java index 3442f86f28..d4ba502831 100644 --- a/src/main/java/org/openrewrite/java/migrate/JREThrowableFinalMethods.java +++ b/src/main/java/org/openrewrite/java/migrate/JREThrowableFinalMethods.java @@ -84,7 +84,8 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl JavaType.Method myAddSuppressed = mt.withName("myAddSuppressed"); return md.withName(md.getName().withSimpleName("myAddSuppressed").withType(myAddSuppressed)) .withMethodType(myAddSuppressed); - } else if (METHOD_GETSUPPRESSED.matches(md, classDeclaration)) { + } + if (METHOD_GETSUPPRESSED.matches(md, classDeclaration)) { JavaType.Method myGetSuppressed = mt.withName("myGetSuppressed"); return md.withName(md.getName().withSimpleName("myGetSuppressed").withType(myGetSuppressed)) .withMethodType(myGetSuppressed); diff --git a/src/main/java/org/openrewrite/java/migrate/JpaCacheProperties.java b/src/main/java/org/openrewrite/java/migrate/JpaCacheProperties.java index 6de052bfcb..e004414e26 100644 --- a/src/main/java/org/openrewrite/java/migrate/JpaCacheProperties.java +++ b/src/main/java/org/openrewrite/java/migrate/JpaCacheProperties.java @@ -289,11 +289,14 @@ private void getDataCacheProps(Xml.Tag puNode, SharedDataHolder sdh) { if (propVal != null) { if ("false".equalsIgnoreCase(propVal)) { return "NONE"; - } else if ("true".equalsIgnoreCase(propVal)) { + } + if ("true".equalsIgnoreCase(propVal)) { return "ALL"; - } else if (propVal.matches("(?i:true)\\(ExcludedTypes=.*")) { + } + if (propVal.matches("(?i:true)\\(ExcludedTypes=.*")) { return "DISABLE_SELECTIVE"; - } else if (propVal.matches("(?i:true)\\(Types=.*")) { + } + if (propVal.matches("(?i:true)\\(Types=.*")) { return "ENABLE_SELECTIVE"; } } @@ -305,7 +308,8 @@ private void getDataCacheProps(Xml.Tag puNode, SharedDataHolder sdh) { private @Nullable String convertScmValue(String scmValue) { if ("NONE".equals(scmValue)) { return "false"; - } else if ("ALL".equals(scmValue)) { + } + if ("ALL".equals(scmValue)) { return "true"; } // otherwise, don't process it diff --git a/src/main/java/org/openrewrite/java/migrate/UseJavaUtilBase64.java b/src/main/java/org/openrewrite/java/migrate/UseJavaUtilBase64.java index 6b2c5be7c3..60abbfba8e 100644 --- a/src/main/java/org/openrewrite/java/migrate/UseJavaUtilBase64.java +++ b/src/main/java/org/openrewrite/java/migrate/UseJavaUtilBase64.java @@ -129,7 +129,8 @@ public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) { .build() .apply(updateCursor(c), c.getCoordinates().replace()); - } else if (newBase64Decoder.matches(c)) { + } + if (newBase64Decoder.matches(c)) { return JavaTemplate.builder(useMimeCoder ? "Base64.getMimeDecoder()" : "Base64.getDecoder()") .contextSensitive() .imports("java.util.Base64") diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewArrayList.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewArrayList.java index 6db925f5db..19c9298f6a 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewArrayList.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewArrayList.java @@ -76,13 +76,15 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) maybeRemoveImport("com.google.common.collect.Lists"); maybeAddImport("java.util.ArrayList"); return newArrayList.apply(getCursor(), method.getCoordinates().replace()); - } else if (NEW_ARRAY_LIST_ITERABLE.matches(method) && method.getArguments().size() == 1 && + } + if (NEW_ARRAY_LIST_ITERABLE.matches(method) && method.getArguments().size() == 1 && TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) { maybeRemoveImport("com.google.common.collect.Lists"); maybeAddImport("java.util.ArrayList"); return newArrayListCollection.apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0)); - } else if (NEW_ARRAY_LIST_CAPACITY.matches(method)) { + } + if (NEW_ARRAY_LIST_CAPACITY.matches(method)) { maybeRemoveImport("com.google.common.collect.Lists"); maybeAddImport("java.util.ArrayList"); return newArrayListCapacity.apply(getCursor(), method.getCoordinates().replace(), diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewCopyOnWriteArrayList.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewCopyOnWriteArrayList.java index ec77f6b89c..09967c2b86 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewCopyOnWriteArrayList.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewCopyOnWriteArrayList.java @@ -63,8 +63,9 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) .imports("java.util.concurrent.CopyOnWriteArrayList") .build() .apply(getCursor(), method.getCoordinates().replace()); - } else if (NEW_ARRAY_LIST_ITERABLE.matches(method) && method.getArguments().size() == 1 && - TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) { + } + if (NEW_ARRAY_LIST_ITERABLE.matches(method) && method.getArguments().size() == 1 && + TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) { maybeRemoveImport("com.google.common.collect.Lists"); maybeAddImport("java.util.concurrent.CopyOnWriteArrayList"); return JavaTemplate.builder("new CopyOnWriteArrayList<>(#{any(java.util.Collection)})") diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewLinkedList.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewLinkedList.java index b5ccd29588..0bf89b38e6 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewLinkedList.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewLinkedList.java @@ -63,8 +63,9 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) .imports("java.util.LinkedList") .build() .apply(getCursor(), method.getCoordinates().replace()); - } else if (NEW_LINKED_LIST_ITERABLE.matches(method) && method.getArguments().size() == 1 && - TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) { + } + if (NEW_LINKED_LIST_ITERABLE.matches(method) && method.getArguments().size() == 1 && + TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) { maybeRemoveImport("com.google.common.collect.Lists"); maybeAddImport("java.util.LinkedList"); return JavaTemplate.builder("new LinkedList<>(#{any(java.util.Collection)})") diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaMapsNewHashMap.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaMapsNewHashMap.java index 79653d80f9..25b0769300 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaMapsNewHashMap.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaMapsNewHashMap.java @@ -62,7 +62,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) .imports("java.util.HashMap") .build() .apply(getCursor(), method.getCoordinates().replace()); - } else if (NEW_HASH_MAP_WITH_MAP.matches(method)) { + } + if (NEW_HASH_MAP_WITH_MAP.matches(method)) { maybeRemoveImport("com.google.common.collect.Maps"); maybeAddImport("java.util.HashMap"); return JavaTemplate.builder("new HashMap<>(#{any(java.util.Map)})") diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaMapsNewLinkedHashMap.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaMapsNewLinkedHashMap.java index c91e00003e..fe6748c2ba 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaMapsNewLinkedHashMap.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaMapsNewLinkedHashMap.java @@ -62,7 +62,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) .imports("java.util.LinkedHashMap") .build() .apply(getCursor(), method.getCoordinates().replace()); - } else if (NEW_LINKED_HASH_MAP_WITH_MAP.matches(method)) { + } + if (NEW_LINKED_HASH_MAP_WITH_MAP.matches(method)) { maybeRemoveImport("com.google.common.collect.Maps"); maybeAddImport("java.util.LinkedHashMap"); return JavaTemplate.builder("new LinkedHashMap<>(#{any(java.util.Map)})") diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaMapsNewTreeMap.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaMapsNewTreeMap.java index dfe64ca290..f306508e68 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaMapsNewTreeMap.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaMapsNewTreeMap.java @@ -64,7 +64,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) .imports("java.util.TreeMap") .build() .apply(getCursor(), method.getCoordinates().replace()); - } else if (NEW_TREE_MAP_WITH_COMPARATOR.matches(method)) { + } + if (NEW_TREE_MAP_WITH_COMPARATOR.matches(method)) { maybeRemoveImport("com.google.common.collect.Maps"); maybeAddImport("java.util.TreeMap"); return JavaTemplate.builder("new TreeMap<>(#{any(java.util.Comparator)})") @@ -72,7 +73,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) .imports("java.util.TreeMap") .build() .apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0)); - } else if (NEW_TREE_MAP_WITH_MAP.matches(method)) { + } + if (NEW_TREE_MAP_WITH_MAP.matches(method)) { maybeRemoveImport("com.google.common.collect.Maps"); maybeAddImport("java.util.TreeMap"); return JavaTemplate.builder("new TreeMap<>(#{any(java.util.Map)})") diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaSetsNewHashSet.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaSetsNewHashSet.java index ebcd2502c0..bbd65d699b 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaSetsNewHashSet.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaSetsNewHashSet.java @@ -62,22 +62,22 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) .imports("java.util.HashSet") .build() .apply(getCursor(), method.getCoordinates().replace()); - } else if (method.getArguments().size() == 1 && TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) { + } + if (method.getArguments().size() == 1 && TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) { return JavaTemplate.builder("new HashSet<>(#{any(java.util.Collection)})") .contextSensitive() .imports("java.util.HashSet") .build() .apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0)); - } else { - maybeAddImport("java.util.Arrays"); - JavaTemplate newHashSetVarargs = JavaTemplate.builder("new HashSet<>(Arrays.asList(" + method.getArguments().stream().map(a -> "#{any()}").collect(Collectors.joining(",")) + "))") - .contextSensitive() - .imports("java.util.Arrays") - .imports("java.util.HashSet") - .build(); - return newHashSetVarargs.apply(getCursor(), method.getCoordinates().replace(), - method.getArguments().toArray()); } + maybeAddImport("java.util.Arrays"); + JavaTemplate newHashSetVarargs = JavaTemplate.builder("new HashSet<>(Arrays.asList(" + method.getArguments().stream().map(a -> "#{any()}").collect(Collectors.joining(",")) + "))") + .contextSensitive() + .imports("java.util.Arrays") + .imports("java.util.HashSet") + .build(); + return newHashSetVarargs.apply(getCursor(), method.getCoordinates().replace(), + method.getArguments().toArray()); } return super.visitMethodInvocation(method, ctx); } diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaSetsNewLinkedHashSet.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaSetsNewLinkedHashSet.java index 2a4688ea6d..331525a258 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaSetsNewLinkedHashSet.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaSetsNewLinkedHashSet.java @@ -66,8 +66,9 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) .imports("java.util.LinkedHashSet") .build() .apply(getCursor(), method.getCoordinates().replace()); - } else if (NEW_LINKED_HASH_SET_ITERABLE.matches(method) && method.getArguments().size() == 1 && - TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) { + } + if (NEW_LINKED_HASH_SET_ITERABLE.matches(method) && method.getArguments().size() == 1 && + TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) { maybeRemoveImport("com.google.common.collect.Sets"); maybeAddImport("java.util.LinkedHashSet"); return JavaTemplate.builder("new LinkedHashSet<>(#{any(java.util.Collection)})") @@ -75,7 +76,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) .imports("java.util.LinkedHashSet") .build() .apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0)); - } else if (NEW_LINKED_HASH_SET_CAPACITY.matches(method)) { + } + if (NEW_LINKED_HASH_SET_CAPACITY.matches(method)) { maybeRemoveImport("com.google.common.collect.Sets"); maybeAddImport("java.util.LinkedHashSet"); return JavaTemplate.builder("new LinkedHashSet<>(#{any(int)})") diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoMapsAndSetsWithExpectedSize.java b/src/main/java/org/openrewrite/java/migrate/guava/NoMapsAndSetsWithExpectedSize.java index 76398631df..0588d06099 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoMapsAndSetsWithExpectedSize.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoMapsAndSetsWithExpectedSize.java @@ -68,7 +68,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) .imports("java.util.HashMap") .build() .apply(getCursor(), coordinates, j.getArguments().toArray()); - } else if (NEW_LINKED_HASHMAP.matches(j)) { + } + if (NEW_LINKED_HASHMAP.matches(j)) { maybeRemoveImport("com.google.common.collect.Maps"); maybeAddImport("java.util.LinkedHashMap"); JavaCoordinates coordinates = j.getCoordinates().replace(); @@ -76,7 +77,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) .imports("java.util.LinkedHashMap") .build() .apply(getCursor(), coordinates, j.getArguments().toArray()); - } else if (NEW_HASHSET.matches(j)) { + } + if (NEW_HASHSET.matches(j)) { maybeRemoveImport("com.google.common.collect.Sets"); maybeAddImport("java.util.HashSet"); JavaCoordinates coordinates = j.getCoordinates().replace(); @@ -84,7 +86,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) .imports("java.util.HashSet") .build() .apply(getCursor(), coordinates, j.getArguments().toArray()); - } else if (NEW_LINKED_HASHSET.matches(j)) { + } + if (NEW_LINKED_HASHSET.matches(j)) { maybeRemoveImport("com.google.common.collect.Sets"); maybeAddImport("java.util.LinkedHashSet"); JavaCoordinates coordinates = j.getCoordinates().replace(); diff --git a/src/main/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethods.java b/src/main/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethods.java index e64f4dee24..54278dae75 100644 --- a/src/main/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethods.java +++ b/src/main/java/org/openrewrite/java/migrate/jakarta/UpdateBeanManagerMethods.java @@ -52,7 +52,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu if (fireEventMatcher.matches(method) && mi.getSelect() != null) { if (arguments.size() <= 1) { return JavaTemplate.builder("#{any(jakarta.enterprise.inject.spi.BeanManager)}.getEvent()" + - ".fire(#{any(jakarta.enterprise.inject.spi.BeforeBeanDiscovery)})") + ".fire(#{any(jakarta.enterprise.inject.spi.BeforeBeanDiscovery)})") .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jakarta.enterprise.cdi-api-3.0.0-M4")) .build() .apply(updateCursor(mi), mi.getCoordinates().replace(), mi.getSelect(), arguments.get(0)); @@ -66,13 +66,14 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu args[arguments.size()] = arguments.get(0); String template = "#{any(jakarta.enterprise.inject.spi.BeanManager)}.getEvent()" + - ".select(" + String.join(", ", Collections.nCopies(arguments.size() - 1, "#{any(java.lang.annotation.Annotation)}")) + ')' + - ".fire(#{any(jakarta.enterprise.inject.spi.BeforeBeanDiscovery)})"; + ".select(" + String.join(", ", Collections.nCopies(arguments.size() - 1, "#{any(java.lang.annotation.Annotation)}")) + ')' + + ".fire(#{any(jakarta.enterprise.inject.spi.BeforeBeanDiscovery)})"; return JavaTemplate.builder(template) .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jakarta.enterprise.cdi-api-3.0.0-M4")) .build() .apply(updateCursor(mi), mi.getCoordinates().replace(), args); - } else if (createInjectionTargetMatcher.matches(method) && mi.getSelect() != null) { + } + if (createInjectionTargetMatcher.matches(method) && mi.getSelect() != null) { return JavaTemplate.builder("#{any(jakarta.enterprise.inject.spi.BeanManager)}.getInjectionTargetFactory(#{any(jakarta.enterprise.inject.spi.AnnotatedType)}).createInjectionTarget(null)") .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jakarta.enterprise.cdi-api-3.0.0-M4")) .build() diff --git a/src/main/java/org/openrewrite/java/migrate/lang/NullCheck.java b/src/main/java/org/openrewrite/java/migrate/lang/NullCheck.java index a2f1b0afa1..9121f4e020 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/NullCheck.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/NullCheck.java @@ -136,7 +136,8 @@ public static Matcher nullCheck() { if (binary.getOperator() == Equal) { if (J.Literal.isLiteralValue(binary.getLeft(), null)) { return new NullCheck(cursor, binary.getRight()); - } else if (J.Literal.isLiteralValue(binary.getRight(), null)) { + } + if (J.Literal.isLiteralValue(binary.getRight(), null)) { return new NullCheck(cursor, binary.getLeft()); } } diff --git a/src/main/java/org/openrewrite/java/migrate/lang/SwitchCaseEnumGuardToLabel.java b/src/main/java/org/openrewrite/java/migrate/lang/SwitchCaseEnumGuardToLabel.java index 49b70a2aba..2bdfd1a4d9 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/SwitchCaseEnumGuardToLabel.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/SwitchCaseEnumGuardToLabel.java @@ -113,7 +113,8 @@ public J.Case visitCase(J.Case case_, ExecutionContext ctx) { } if ((select instanceof J.FieldAccess || select instanceof J.Identifier) && equalTo instanceof J.Identifier && label.getName().getSimpleName().equals(((J.Identifier) equalTo).getSimpleName())) { return select; - } else if ((equalTo instanceof J.FieldAccess || equalTo instanceof J.Identifier) && select instanceof J.Identifier && label.getName().getSimpleName().equals(((J.Identifier) select).getSimpleName())) { + } + if ((equalTo instanceof J.FieldAccess || equalTo instanceof J.Identifier) && select instanceof J.Identifier && label.getName().getSimpleName().equals(((J.Identifier) select).getSimpleName())) { return equalTo; } return null; diff --git a/src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java b/src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java index 5d40e45181..c23f36a263 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java @@ -204,8 +204,9 @@ private static boolean flatAdditiveStringLiterals(Expression expression, concatenationSb.append(b.getPrefix().getWhitespace()).append("-"); concatenationSb.append(b.getPadding().getOperator().getBefore().getWhitespace()).append("-"); return flatAdditiveStringLiterals(b.getLeft(), stringLiterals, contentSb, concatenationSb) && - flatAdditiveStringLiterals(b.getRight(), stringLiterals, contentSb, concatenationSb); - } else if (isRegularStringLiteral(expression)) { + flatAdditiveStringLiterals(b.getRight(), stringLiterals, contentSb, concatenationSb); + } + if (isRegularStringLiteral(expression)) { J.Literal l = (J.Literal) expression; stringLiterals.add(l); contentSb.append(requireNonNull(l.getValue())); @@ -243,11 +244,10 @@ private static String getIndents(String concatenation, boolean useTabCharacter, int spaceCount = tabAndSpaceCounts[1]; if (useTabCharacter) { return StringUtils.repeat("\t", tabCount) + - StringUtils.repeat(" ", spaceCount); - } else { - // replace tab with spaces if the style is using spaces - return StringUtils.repeat(" ", tabCount * tabSize + spaceCount); + StringUtils.repeat(" ", spaceCount); } + // replace tab with spaces if the style is using spaces + return StringUtils.repeat(" ", tabCount * tabSize + spaceCount); } /** diff --git a/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java b/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java index 65e7ca59c3..b5484db79f 100644 --- a/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java +++ b/src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java @@ -109,9 +109,8 @@ public static String deriveGetterMethodName(@Nullable JavaType type, String fiel fieldName.substring(0, 3).matches("is[A-Z]"); if (alreadyStartsWithIs) { return fieldName; - } else { - return "is" + StringUtils.capitalize(fieldName); } + return "is" + StringUtils.capitalize(fieldName); } return "get" + StringUtils.capitalize(fieldName); } @@ -177,9 +176,11 @@ private static boolean hasMatchingSetterMethodName(J.MethodDeclaration method, S static AccessLevel getAccessLevel(J.MethodDeclaration methodDeclaration) { if (methodDeclaration.hasModifier(Public)) { return PUBLIC; - } else if (methodDeclaration.hasModifier(Protected)) { + } + if (methodDeclaration.hasModifier(Protected)) { return PROTECTED; - } else if (methodDeclaration.hasModifier(Private)) { + } + if (methodDeclaration.hasModifier(Private)) { return PRIVATE; } return PACKAGE; diff --git a/src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java b/src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java index ab1fa29d33..d283932a18 100644 --- a/src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java +++ b/src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java @@ -157,9 +157,8 @@ private boolean implementsConflictingInterfaces(J.ClassDeclaration classDeclarat JavaType type = implemented.getType(); if (type instanceof JavaType.FullyQualified) { return isConflictingInterface((JavaType.FullyQualified) type, memberVariableNames); - } else { - return false; } + return false; }); } diff --git a/src/main/java/org/openrewrite/java/migrate/lombok/UseLombokGetter.java b/src/main/java/org/openrewrite/java/migrate/lombok/UseLombokGetter.java index 1de484efeb..11dcc7978b 100644 --- a/src/main/java/org/openrewrite/java/migrate/lombok/UseLombokGetter.java +++ b/src/main/java/org/openrewrite/java/migrate/lombok/UseLombokGetter.java @@ -64,7 +64,8 @@ public TreeVisitor getVisitor() { ((J.Identifier) returnExpression).getFieldType(), LombokUtils.getAccessLevel(method))); return null; - } else if (returnExpression instanceof J.FieldAccess && + } + if (returnExpression instanceof J.FieldAccess && ((J.FieldAccess) returnExpression).getName().getFieldType() != null) { doAfterVisit(new FieldAnnotator( Getter.class, diff --git a/src/main/java/org/openrewrite/java/migrate/lombok/UseLombokSetter.java b/src/main/java/org/openrewrite/java/migrate/lombok/UseLombokSetter.java index 674babf366..5f82dc7739 100644 --- a/src/main/java/org/openrewrite/java/migrate/lombok/UseLombokSetter.java +++ b/src/main/java/org/openrewrite/java/migrate/lombok/UseLombokSetter.java @@ -63,7 +63,8 @@ public TreeVisitor getVisitor() { LombokUtils.getAccessLevel(method))); return null; //delete - } else if (assignmentVariable instanceof J.Identifier && + } + if (assignmentVariable instanceof J.Identifier && ((J.Identifier) assignmentVariable).getFieldType() != null) { doAfterVisit(new FieldAnnotator(Setter.class, ((J.Identifier) assignmentVariable).getFieldType(), diff --git a/src/main/java/org/openrewrite/java/migrate/net/URLConstructorToURICreate.java b/src/main/java/org/openrewrite/java/migrate/net/URLConstructorToURICreate.java index 01393b5e6a..47ad4a0063 100644 --- a/src/main/java/org/openrewrite/java/migrate/net/URLConstructorToURICreate.java +++ b/src/main/java/org/openrewrite/java/migrate/net/URLConstructorToURICreate.java @@ -81,7 +81,8 @@ public J visitNewClass(J.NewClass nc, ExecutionContext ctx) { String literalValueSource = ((J.Literal) arg).getValueSource(); // Remove quotations from string return literalValueSource != null ? literalValueSource.substring(1, literalValueSource.length() - 1).trim() : null; - } else if (arg instanceof J.Identifier && + } + if (arg instanceof J.Identifier && TypeUtils.isOfType(arg.getType(), JavaType.Primitive.String)) { // find constant value of the identifier try { diff --git a/src/main/java/org/openrewrite/java/migrate/net/URLConstructorsToNewURI.java b/src/main/java/org/openrewrite/java/migrate/net/URLConstructorsToNewURI.java index 969a3387e8..1eb0cb1dc4 100644 --- a/src/main/java/org/openrewrite/java/migrate/net/URLConstructorsToNewURI.java +++ b/src/main/java/org/openrewrite/java/migrate/net/URLConstructorsToNewURI.java @@ -61,7 +61,8 @@ public J visitNewClass(J.NewClass nc, ExecutionContext ctx) { nc.getArguments().get(0), nc.getArguments().get(1), nc.getArguments().get(2)); - } else if (methodMatcherFourArg.matches(nc)) { + } + if (methodMatcherFourArg.matches(nc)) { JavaTemplate template = JavaTemplate.builder("new URI(#{any(String)}, null, #{any(String)}, #{any(int)}, #{any(String)}, null, null).toURL()") .imports(URI_FQN, URL_FQN) .contextSensitive()