Skip to content

Recipe InlineVariable and Enum array results in non compilable code #9

Closed

Description

@jbessels

I have the following public enum ImportType. With the following method

 public static ImportType[] getList() {
   	ImportType[] list = {ImportType.TIMETABLE, ImportType.DUTIES};
   	return list;
   }

org.openrewrite.java.cleanup.InlineVariable translates this to

public static ImportType[] getList() {
    	return {ImportType.TIMETABLE, ImportType.DUTIES};
    }

This does no longer compile. Correct code suggested by IntelliJ

 public static ImportType[] getList() {
    	return new ImportType[]{ImportType.TIMETABLE, ImportType.DUTIES};
    }

I hope this is enough to reproduce the issue.

Activity

  1. changed the title [-]InlineVariable and Enum array results in non compilable code[/-] [+]Recipe InlineVariable and Enum array results in non compilable code[/+] on Mar 30, 2023
  2. added
    bug
    on Mar 30, 2023
  3. knutwannheden commented on Mar 31, 2023

    @knutwannheden
    Contributor

    When refactoring the following Java class using the InlineVariable recipe, the result looks correct (it inlines the newMapSortedByOrder variable), but as stated in the issue, it doesn't compile anymore:

      import java.util.Comparator;
      import java.util.LinkedHashMap;
      import java.util.Map;
      import java.util.stream.Collectors;
    
      class Test {
          void test() {
              new MapDropdownChoice<String, Integer>(() -> {
                  Map<String, Integer> statuses = new java.util.HashMap<>();
                  Map<String, Integer> newMapSortedByOrder = statuses.entrySet().stream()
                          .sorted(Comparator.comparingInt(e -> e.getValue()))
                          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
                  return newMapSortedByOrder;
              });
          }
          static class MapDropdownChoice<K, V> extends DropdownChoice<K> {
              public MapDropdownChoice(final IModel<? extends Map<K, ? extends V>> choiceMap) {
              }
          }
      
          interface IModel<T> {
              T getObject();
          }
          static class DropdownChoice<T> {
          }
      }

    Note
    The problem can probably be boiled down even more.

  4. added this to the milestone on May 30, 2023
  5. removed this from the milestone on Nov 30, 2023
  6. moved this from Backlog to Done in OpenRewriteon Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions