class InstructionList {
/**
* Take all instructions (handles) from "start" to "end" and append them after the
* new location "target". Of course, "end" must be after "start" and target must
* not be located withing this range. If you want to move something to the start of
* the list use null as value for target.<br>
* Any instruction targeters pointing to handles within the block, keep their targets.
*
* @param start of moved block
* @param end of moved block
* @param target of moved block
*/
public void move( InstructionHandle start, InstructionHandle end, InstructionHandle target ) {
// Step 1: Check constraints
if ((start == null) || (end == null)) {
throw new ClassGenException("Invalid null handle: From " + start + " to " + end);
}
if ((target == start) || (target == end)) {
throw new ClassGenException("Invalid range: From " + start + " to " + end
+ " contains target " + target);
}
for (InstructionHandle ih = start; ih != end.getNext(); ih = ih.getNext()) {
if (ih == null) {
throw new ClassGenException("Invalid range: From " + start + " to " + end);
} else if (ih == target) {
throw new ClassGenException("Invalid range: From " + start + " to " + end
+ " contains target " + target);
}
}
// Step 2: Temporarily remove the given instructions from the list
InstructionHandle prev = start.getPrev();
InstructionHandle next = end.getNext();
if (prev != null) {
prev.setNext(next);
} else {
this.start = next;
}
if (next != null) {
next.setPrev(prev);
} else {
this.end = prev;
}
start.setPrev(end.setNext(null));
// Step 3: append after target
if (target == null) { // append to start of list
if (this.start != null) {
this.start.setPrev(end);
}
end.setNext(this.start);
this.start = start;
} else {
next = target.getNext();
target.setNext(start);
start.setPrev(target);
end.setNext(next);
if (next != null) {
next.setPrev(end);
} else {
this.end = end;
}
}
}
}