aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/javassist/bytecode/stackmap/BasicBlock.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/javassist/bytecode/stackmap/BasicBlock.java')
-rw-r--r--src/main/javassist/bytecode/stackmap/BasicBlock.java77
1 files changed, 42 insertions, 35 deletions
diff --git a/src/main/javassist/bytecode/stackmap/BasicBlock.java b/src/main/javassist/bytecode/stackmap/BasicBlock.java
index 7a643609..231ffc3a 100644
--- a/src/main/javassist/bytecode/stackmap/BasicBlock.java
+++ b/src/main/javassist/bytecode/stackmap/BasicBlock.java
@@ -16,9 +16,18 @@
package javassist.bytecode.stackmap;
-import javassist.bytecode.*;
-import java.util.HashMap;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javassist.bytecode.BadBytecode;
+import javassist.bytecode.CodeAttribute;
+import javassist.bytecode.CodeIterator;
+import javassist.bytecode.ExceptionTable;
+import javassist.bytecode.MethodInfo;
+import javassist.bytecode.Opcode;
/**
* A basic block is a sequence of bytecode that does not contain jump/branch
@@ -28,6 +37,9 @@ import java.util.ArrayList;
*/
public class BasicBlock {
static class JsrBytecode extends BadBytecode {
+ /** default serialVersionUID */
+ private static final long serialVersionUID = 1L;
+
JsrBytecode() { super("JSR"); }
}
@@ -46,11 +58,9 @@ public class BasicBlock {
public static BasicBlock find(BasicBlock[] blocks, int pos)
throws BadBytecode
{
- for (int i = 0; i < blocks.length; i++) {
- int iPos = blocks[i].position;
- if (iPos <= pos && pos < iPos + blocks[i].length)
- return blocks[i];
- }
+ for (BasicBlock b:blocks)
+ if (b.position <= pos && pos < b.position + b.length)
+ return b;
throw new BadBytecode("no basic block at " + pos);
}
@@ -66,6 +76,7 @@ public class BasicBlock {
}
}
+ @Override
public String toString() {
StringBuffer sbuf = new StringBuffer();
String cname = this.getClass().getName();
@@ -81,10 +92,9 @@ public class BasicBlock {
sbuf.append("pos=").append(position).append(", len=")
.append(length).append(", in=").append(incoming)
.append(", exit{");
- if (exit != null) {
- for (int i = 0; i < exit.length; i++)
- sbuf.append(exit[i].position).append(",");
- }
+ if (exit != null)
+ for (BasicBlock b:exit)
+ sbuf.append(b.position).append(",");
sbuf.append("}, {");
Catch th = toCatch;
@@ -101,7 +111,7 @@ public class BasicBlock {
* A Mark indicates the position of a branch instruction
* or a branch target.
*/
- static class Mark implements Comparable {
+ static class Mark implements Comparable<Mark> {
int position;
BasicBlock block;
BasicBlock[] jump;
@@ -118,13 +128,11 @@ public class BasicBlock {
catcher = null;
}
- public int compareTo(Object obj) {
- if (obj instanceof Mark) {
- int pos = ((Mark)obj).position;
- return position - pos;
- }
-
- return -1;
+ @Override
+ public int compareTo(Mark obj) {
+ if (null == obj)
+ return -1;
+ return position - obj.position;
}
void setJump(BasicBlock[] bb, int s, boolean always) {
@@ -172,7 +180,7 @@ public class BasicBlock {
ExceptionTable et)
throws BadBytecode
{
- HashMap marks = makeMarks(ci, begin, end, et);
+ Map<Integer,Mark> marks = makeMarks(ci, begin, end, et);
BasicBlock[] bb = makeBlocks(marks);
addCatchers(bb, et);
return bb;
@@ -180,24 +188,24 @@ public class BasicBlock {
/* Branch target
*/
- private Mark makeMark(HashMap table, int pos) {
+ private Mark makeMark(Map<Integer,Mark> table, int pos) {
return makeMark0(table, pos, true, true);
}
/* Branch instruction.
* size > 0
*/
- private Mark makeMark(HashMap table, int pos, BasicBlock[] jump,
+ private Mark makeMark(Map<Integer,Mark> table, int pos, BasicBlock[] jump,
int size, boolean always) {
Mark m = makeMark0(table, pos, false, false);
m.setJump(jump, size, always);
return m;
}
- private Mark makeMark0(HashMap table, int pos,
+ private Mark makeMark0(Map<Integer,Mark> table, int pos,
boolean isBlockBegin, boolean isTarget) {
- Integer p = Integer.valueOf(pos);
- Mark m = (Mark)table.get(p);
+ Integer p = pos;
+ Mark m = table.get(p);
if (m == null) {
m = new Mark(pos);
table.put(p, m);
@@ -214,13 +222,13 @@ public class BasicBlock {
return m;
}
- private HashMap makeMarks(CodeIterator ci, int begin, int end,
+ private Map<Integer,Mark> makeMarks(CodeIterator ci, int begin, int end,
ExceptionTable et)
throws BadBytecode
{
ci.begin();
ci.move(begin);
- HashMap marks = new HashMap();
+ Map<Integer,Mark> marks = new HashMap<Integer,Mark>();
while (ci.hasNext()) {
int index = ci.next();
if (index >= end)
@@ -296,7 +304,7 @@ public class BasicBlock {
return marks;
}
- private void makeGoto(HashMap marks, int pos, int target, int size) {
+ private void makeGoto(Map<Integer,Mark> marks, int pos, int target, int size) {
Mark to = makeMark(marks, target);
BasicBlock[] jumps = makeArray(to.block);
makeMark(marks, pos, jumps, size, true);
@@ -306,7 +314,7 @@ public class BasicBlock {
* We could ignore JSR since Java 7 or later does not allow it.
* See The JVM Spec. Sec. 4.10.2.5.
*/
- protected void makeJsr(HashMap marks, int pos, int target, int size) throws BadBytecode {
+ protected void makeJsr(Map<Integer,Mark> marks, int pos, int target, int size) throws BadBytecode {
/*
Mark to = makeMark(marks, target);
Mark next = makeMark(marks, pos + size);
@@ -316,11 +324,10 @@ public class BasicBlock {
throw new JsrBytecode();
}
- private BasicBlock[] makeBlocks(HashMap markTable) {
- Mark[] marks = (Mark[])markTable.values()
- .toArray(new Mark[markTable.size()]);
- java.util.Arrays.sort(marks);
- ArrayList blocks = new ArrayList();
+ private BasicBlock[] makeBlocks(Map<Integer,Mark> markTable) {
+ Mark[] marks = markTable.values().toArray(new Mark[markTable.size()]);
+ Arrays.sort(marks);
+ List<BasicBlock> blocks = new ArrayList<BasicBlock>();
int i = 0;
BasicBlock prev;
if (marks.length > 0 && marks[0].position == 0 && marks[0].block != null)
@@ -370,7 +377,7 @@ public class BasicBlock {
}
}
- return (BasicBlock[])blocks.toArray(makeArray(blocks.size()));
+ return blocks.toArray(makeArray(blocks.size()));
}
private static BasicBlock getBBlock(Mark m) {