Bug 52433 - Why using unstable sort at MethodGen.getLocalVariables() ?
Summary: Why using unstable sort at MethodGen.getLocalVariables() ?
Status: RESOLVED FIXED
Alias: None
Product: BCEL - Now in Jira
Classification: Unclassified
Component: Main (show other bugs)
Version: 5.3
Hardware: PC Linux
: P2 enhancement
Target Milestone: ---
Assignee: issues@commons.apache.org
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-01-05 20:29 UTC by Thiago
Modified: 2012-03-11 15:07 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thiago 2012-01-05 20:29:45 UTC
I noticed that MethodGen.getLocalVariables sorts the local variables by index. However, it uses a hand written sort algorithm that is unstable. That is, it does not guarantee that variables with the same index are kept in the same order. In fact, it always swaps them.

This does not cause a major problem apart from having a different order of variables in the local variables table when a class is simply parsed and dumped.

A simple enhancement is to use java's own merge sort implementation (available in version 1.3, which seems to be the target platform). In getLocalVariables(), instead of calling:

    sort(lg, 0, size - 1);

use something like (maybe remove generics):

    Arrays.sort(lg, new Comparator<LocalVariableGen>() {
        public int compare(LocalVariableGen o1, LocalVariableGen o2) {
            return o1.getIndex() - o2.getIndex();
        }
    });

The advantage, besides using a stable sort, is that this is less code to maintain, compared to the trick sort method.
Comment 1 Torsten Curdt 2012-02-04 19:55:02 UTC
Indeed. Good suggestion.
Comment 2 Sebb 2012-03-11 15:07:03 UTC
URL: http://svn.apache.org/viewvc?rev=1299373&view=rev
Log:
Bug 52433 - Why using unstable sort at MethodGen.getLocalVariables() ?

Added:
   commons/proper/bcel/trunk/src/changes/
   commons/proper/bcel/trunk/src/changes/changes.xml   (with props)
   commons/proper/bcel/trunk/src/changes/release-notes.vm   (with props)
Modified:
   commons/proper/bcel/trunk/src/main/java/org/apache/bcel/generic/MethodGen.java