Collections.sort(keys, new Comparator() { public int compare(Object a, Object b) { LocalVariableTag taga = (LocalVariableTag) a; LocalVariableTag tagb = (LocalVariableTag) b; if (taga.getName().startsWith("arg")) { if (tagb.getName().startsWith("arg")) { return -taga.getName().compareTo(tagb.getName()); } else { return 1; } } else if (tagb.getName().startsWith("arg")) { return -1; } else { return -taga.getName().compareTo(tagb.getName()); } } });
    public void packBody(MethodGen gen) {        HashMap map = new HashMap();        InstructionList fresh = gen.getInstructionList();                /* Make copies of all instructions, append them to the new list         * and associate old instruction references with the new ones, i.e.,         * a 1:1 mapping.         */        for (InstructionHandle ih = getBody().getStart(); ih != null; ih = ih.getNext()) {            if (Range.isRangeHandle(ih)) {                continue;            }            Instruction i = ih.getInstruction();            Instruction c = Utility.copyInstruction(i);            if (c instanceof BranchInstruction)                map.put(ih, fresh.append((BranchInstruction) c));            else                map.put(ih, fresh.append(c));        }        // at this point, no rangeHandles are in fresh.  Let's use that...        /* Update branch targets and insert various attributes.           * Insert our exceptionHandlers         * into a sorted list, so they can be added in order later.         */        InstructionHandle ih = getBody().getStart();        InstructionHandle jh = fresh.getStart();        LinkedList exnList = new LinkedList();   		// map from localvariabletag to instruction handle        Map localVariableStarts = new HashMap();        Map localVariableEnds = new HashMap();        int currLine = -1;                while (ih != null) {            if (map.get(ih) == null) {                // we're a range instruction                Range r = Range.getRange(ih);                if (r instanceof ExceptionRange) {                    ExceptionRange er = (ExceptionRange) r;                    if (er.getStart() == ih) {                    	//System.err.println("er " + er);                    	if (!er.isEmpty()){                        	// order is important, insert handlers in order of start                        	insertHandler(er, exnList);                    	}                    }                } else {                    // we must be a shadow range or something equally useless,                     // so forget about doing anything                }                // just increment ih.                 ih = ih.getNext();            } else {                // assert map.get(ih) == jh                Instruction i = ih.getInstruction();                Instruction j = jh.getInstruction();                    if (i instanceof BranchInstruction) {                    BranchInstruction bi = (BranchInstruction) i;                    BranchInstruction bj = (BranchInstruction) j;                    InstructionHandle itarget = bi.getTarget(); // old target    //    				try {                    // New target is in hash map                    bj.setTarget(remap(itarget, map));//    				} catch (NullPointerException e) {//    					print();//    					System.out.println("Was trying to remap " + bi);//    					System.out.println("who's target was supposedly " + itarget);//    					throw e;//    				}                        if (bi instanceof Select) {                         // Either LOOKUPSWITCH or TABLESWITCH                        InstructionHandle[] itargets = ((Select) bi).getTargets();                        InstructionHandle[] jtargets = ((Select) bj).getTargets();                            for (int k = itargets.length - 1; k >= 0; k--) {                             // Update all targets                            jtargets[k] = remap(itargets[k], map);                            jtargets[k].addTargeter(bj);                        }                    }                }                                // now deal with line numbers                 // and store up info for local variables                InstructionTargeter[] targeters = ih.getTargeters();				int lineNumberOffset =					(fromFilename == null)						? 0						: getEnclosingClass().getSourceDebugExtensionOffset(fromFilename);                if (targeters != null) {                    for (int k = targeters.length - 1; k >= 0; k--) {                        InstructionTargeter targeter = targeters[k];                        if (targeter instanceof LineNumberTag) {                            int line = ((LineNumberTag)targeter).getLineNumber();                            if (line != currLine) {                                gen.addLineNumber(jh, line + lineNumberOffset);                                currLine = line;                            }                        } else if (targeter instanceof LocalVariableTag) {                            LocalVariableTag lvt = (LocalVariableTag) targeter;			    if (localVariableStarts.get(lvt) == null) {				localVariableStarts.put(lvt, jh);			    }			    localVariableEnds.put(lvt, jh);                        }                    }                }                // now continue                ih = ih.getNext();                jh = jh.getNext();            }        }	        // now add exception handlers        for (Iterator iter = exnList.iterator(); iter.hasNext();) {            ExceptionRange r = (ExceptionRange) iter.next();            if (r.isEmpty()) continue;            gen.addExceptionHandler(                remap(r.getRealStart(), map),                 remap(r.getRealEnd(), map),                remap(r.getHandler(), map),                (r.getCatchType() == null)                ? null                 : (ObjectType) BcelWorld.makeBcelType(r.getCatchType()));        }        // now add local variables        gen.removeLocalVariables();                // BCEL sometimes creates extra local variables with the same name.  We now        // remove them and add back to the gen.                Map duplicatedLocalMap = new HashMap();        // Reverse sort these keys	List keys = new ArrayList();         keys.addAll(localVariableStarts.keySet());//		System.err.println("Keys for local variable tags"); //		for (int i = 0;i <keys.size();i++) {//			System.err.println("Before sort: #"+i+"="+keys.get(i));//		}        Collections.sort(keys, new Comparator() {            public int compare(Object a, Object b) {                LocalVariableTag taga = (LocalVariableTag) a;                LocalVariableTag tagb = (LocalVariableTag) b;                if (taga.getName().startsWith("arg")) {                    if (tagb.getName().startsWith("arg")) {                        return -taga.getName().compareTo(tagb.getName());                    } else {                        return 1; // Whatever tagb is, it must come out before 'arg'                    }                } else if (tagb.getName().startsWith("arg")) {                    return -1; // Whatever taga is, it must come out before 'arg'                } else {                    return -taga.getName().compareTo(tagb.getName());                }            }        });//		for (int i = 0;i <keys.size();i++) {//			System.err.println("After  sort: #"+i+"="+keys.get(i));//		}        for (Iterator iter = keys.iterator(); iter.hasNext(); ) {            LocalVariableTag tag = (LocalVariableTag) iter.next();        	// have we already added one with the same slot number and start location?          	// if so, just continue.        	InstructionHandle start = (InstructionHandle) localVariableStarts.get(tag);        	Set slots = (Set) duplicatedLocalMap.get(start);	       	if (slots == null) {	       		slots = new HashSet();	       		duplicatedLocalMap.put(start, slots);		       	}	       	if (slots.contains(new Integer(tag.getSlot()))) {	       		// we already have a var starting at this tag with this slot	       		continue;	       	}	       	slots.add(new Integer(tag.getSlot()));            gen.addLocalVariable(                tag.getName(),                 BcelWorld.makeBcelType(tag.getType()),                tag.getSlot(),                (InstructionHandle) localVariableStarts.get(tag),                (InstructionHandle) localVariableEnds.get(tag));        }    }