Commit 16dc6230 authored by Lubomir Bulej's avatar Lubomir Bulej

Format AdvancedSorter for better look

parent 52b49feb
package ch.usi.dag.disl;
import ch.usi.dag.disl.AsmHelper.Insns;
import java.util.Collections;
import java.util.Comparator;
import org.objectweb.asm.commons.TryCatchBlockSorter;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.TryCatchBlockNode;
import java.util.Collections;
import java.util.Comparator;
import ch.usi.dag.disl.AsmHelper.Insns;
public class AdvancedSorter extends TryCatchBlockSorter {
AdvancedSorter(MethodNode method) {
super(null, method.access, method.name, method.desc, method.signature,
null);
AdvancedSorter (final MethodNode method) {
super (
null, method.access, method.name, method.desc, method.signature, null
);
this.instructions = method.instructions;
this.tryCatchBlocks = method.tryCatchBlocks;
instructions = method.instructions;
tryCatchBlocks = method.tryCatchBlocks;
}
void validate() {
TryCatchBlockNode[] tcbs = new TryCatchBlockNode[tryCatchBlocks.size()];
tcbs = tryCatchBlocks.toArray(tcbs);
void validate () {
TryCatchBlockNode [] tcbs = new TryCatchBlockNode [tryCatchBlocks.size ()];
tcbs = tryCatchBlocks.toArray (tcbs);
for (int i = 0; i < tcbs.length; i++) {
int istart = instructions.indexOf(Insns.FORWARD.firstRealInsn (tcbs[i].start));
int iend = instructions.indexOf(tcbs[i].end);
final int istart = instructions.indexOf (
Insns.FORWARD.firstRealInsn (tcbs [i].start)
);
final int iend = instructions.indexOf (tcbs [i].end);
for (int j = i; j < tcbs.length; j++) {
int jstart = instructions.indexOf(Insns.FORWARD.firstRealInsn (tcbs[j].start));
int jend = instructions.indexOf(tcbs[j].end);
final int jstart = instructions.indexOf (
Insns.FORWARD.firstRealInsn (tcbs [j].start)
);
final int jend = instructions.indexOf (tcbs [j].end);
if ((
AsmHelper.offsetBefore(instructions, istart, jstart) &&
AsmHelper.offsetBefore(instructions, jstart, iend) &&
AsmHelper.offsetBefore(instructions, iend, jend)
) || (
AsmHelper.offsetBefore(instructions, jstart, istart) &&
AsmHelper.offsetBefore(instructions, istart, jend) &&
AsmHelper.offsetBefore(instructions, jend, iend)
AsmHelper.offsetBefore (instructions, istart, jstart)
&& AsmHelper.offsetBefore (instructions, jstart, iend)
&& AsmHelper.offsetBefore (instructions, iend, jend)
) || (
AsmHelper.offsetBefore (instructions, jstart, istart)
&& AsmHelper.offsetBefore (instructions, istart, jend)
&& AsmHelper.offsetBefore (instructions, jend, iend)
)) {
throw new DiSLFatalException (String.format (
throw new DiSLFatalException (
"Overlapping exception handlers #%d [%d,%d) and #%d [%d, %d)",
i, istart, iend, j, jstart, jend
));
);
}
}
}
}
public void visitEnd() {
// Compares TryCatchBlockNodes by the length of their "try" block.
Comparator<TryCatchBlockNode> comp = new Comparator<TryCatchBlockNode>() {
public int compare(TryCatchBlockNode t1, TryCatchBlockNode t2) {
int len1 = blockLength(t1);
int len2 = blockLength(t2);
@Override
public void visitEnd () {
// Compares TryCatchBlockNodes by the length of their "try" block.
final Comparator <TryCatchBlockNode> comp = new Comparator <TryCatchBlockNode> () {
@Override
public int compare (
final TryCatchBlockNode t1, final TryCatchBlockNode t2
) {
final int len1 = blockLength (t1);
final int len2 = blockLength (t2);
return len1 - len2;
}
private int blockLength(TryCatchBlockNode block) {
int startidx = instructions.indexOf(Insns.FORWARD.firstRealInsn (block.start));
int endidx = instructions.indexOf(block.end);
private int blockLength (final TryCatchBlockNode block) {
final int startidx = instructions.indexOf (
Insns.FORWARD.firstRealInsn (block.start)
);
final int endidx = instructions.indexOf (block.end);
return endidx - startidx;
}
};
Collections.sort(tryCatchBlocks, comp);
Collections.sort (tryCatchBlocks, comp);
}
public static void sort(MethodNode method) {
public static void sort (final MethodNode method) {
try {
AdvancedSorter sorter = new AdvancedSorter(method);
sorter.visitEnd();
sorter.validate();
final AdvancedSorter sorter = new AdvancedSorter (method);
sorter.visitEnd ();
sorter.validate ();
} catch (final Exception ex) {
throw new DiSLFatalException (
"error instrumenting method %s%s: %s",
method.name, method.desc, ex.getMessage ()
);
throw new DiSLFatalException ("error instrumenting method %s%s: %s",
method.name, method.desc, ex.getMessage ());
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment