开发者

Java: Are there some tools out there which are able to refactor X[][] to List<List<X>>?

开发者 https://www.devze.com 2022-12-18 07:59 出处:网络
I have to refactor an existing project, which is not that small. It contains a lot of arrays declarations and accesses:

I have to refactor an existing project, which is not that small. It contains a lot of arrays declarations and accesses:

(X is not开发者_开发技巧 a generic type, it's just a placeholder).

declarations: X[] and X[][],

access: someArray[i] and someArray[i][j].

I have to rewrite everything to use generic Lists:

declaration: List<X> and List<List<X>>,

access: someList.get(i) and someList.get(i).get(j).

I couldn't find a possibility to automatize such refactoring, neither in Eclipse, nor in Netbeans (both newest versions).

Are there some tools available to do such refactoring?

EDIT:

The project is very algorithm-oriented. Internal implementation of algorithms will not be touched. But the exposure to the external world must be changed. Most classes are made in such a way, that they only hold some result arrays or arrays of arrays.


exposure to the external world must be changed

In that case I'd avise not to change it everywhere, but to :

  • change only return types of the public methods
  • write an utility method, which looks like:

    public static List<List<X>> asList(X[][] x) {
        List<X[]> list = Arrays.asList(x);
        List<List<X>> newList = new ArrayList<List<X>>(list.size());
        for (X[] xArray : list) {
            newList.add(Arrays.asList(xArray));
        }
        return list;
    }
    
  • use that method to change the only result of each public method. I.e.

    public List<List<X>> someAlgorithm(...) {
        // algorithm code
        X[][] result = ...;
        return Utils.asList(result); // add only this line
    }
    


  1. I can hardly agree that things you are going to do could be called 'refactoring'.

  2. Arrays has some possibilities that lists don't have (and of course vise versa).

For example, if you create new array of 10 integer elements then its size is 10, and it has ten zero values.

You can also use its index in some tricky way. For example, think about radix sort algorithm. To implement it with lists you should first add a lot of zeros to this list and you'll get twice worse performance.

I'm telling this to explain the idea that it's almost impossible to implement robust tool for doing what you want to do.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号