Bug 572566 - add "change array index order" refactoring
Summary: add "change array index order" refactoring
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.7.3   Edit
Hardware: All All
: P3 enhancement with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-04-03 05:34 EDT by Felix Pahl CLA
Modified: 2021-04-15 05:27 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Felix Pahl CLA 2021-04-03 05:34:14 EDT
It would be nice if the order of array indices could be refactored, similar to the "Change Method Signature" refactoring. The admissible permutations might be constrained if an array is allocated with only some of the lengths specified or if references to subarrays are stored or passed (the specified and unspecified dimensions would each have to be permuted among themselves), but even the constrained reordering might be useful, and quite often I allocate all dimensions at once and only use the array as a whole, and then the refactoring would be straightforward.
Comment 1 Manoj N Palat CLA 2021-04-05 00:19:41 EDT
@Felix: Could you please give a "before" and "after" code example ?
Comment 2 Felix Pahl CLA 2021-04-05 05:15:00 EDT
Here are "before" and "after" code examples, as requested.

Simple case: The array is only used as a whole:

Before:

public class ArrayIndexReordering {
    public void useArray(int n) {
        int [] [] [] [] arr = new int [n] [4] [3] [2];
        for (int i = 0;i < n;i++)
            arr [i] [1] [2] [0] = 1;
    }
}

After swapping the first two and the last two indices:

public class ArrayIndexReordering {
    public void useArray(int n) {
        int [] [] [] [] arr = new int [4] [n] [2] [3];
        for (int i = 0;i < n;i++)
            arr [1] [i] [0] [2] = 1;
    }
}

More complicated case: Only some of the dimensions are allocated, and a subarray is passed:

Before:

import java.util.Arrays;

public class ArrayIndexReordering {
    public void useArray(int n) {
        int [] [] [] [] arr = new int [n] [4] [] [];
        for (int i = 0;i < n;i++)
            for (int j = 0;j < 4;j++)
                arr [i] [j] = new int [i + 3] [j + 2];
        System.out.println(Arrays.deepToString(arr [0] [1]));
    }
}

Here, the admissible permutations would have to permute the specified and unspecified indices among themselves. For instance, swapping the first two and the last two indices would still be allowed, and the result would be:

import java.util.Arrays;

public class ArrayIndexReordering {
    public void useArray(int n) {
        int [] [] [] [] arr = new int [4] [n] [] [];
        for (int i = 0;i < n;i++)
            for (int j = 0;j < 4;j++)
                arr [j] [i] = new int [j + 2] [i + 3];
        System.out.println(Arrays.deepToString(arr [1] [0]));
    }
}
Comment 3 Manoj N Palat CLA 2021-04-05 09:11:35 EDT
Moving to ui as this is a refactoring request