11package codechicken .lib .asm ;
22
3+ import static org .objectweb .asm .tree .AbstractInsnNode .*;
4+
35import com .google .common .collect .BiMap ;
46import com .google .common .collect .HashBiMap ;
57import com .google .common .collect .ImmutableMap ;
8+ import java .util .*;
9+ import java .util .Map .Entry ;
610import org .objectweb .asm .tree .AbstractInsnNode ;
711import org .objectweb .asm .tree .InsnList ;
812import org .objectweb .asm .tree .JumpInsnNode ;
913import org .objectweb .asm .tree .LabelNode ;
1014
11- import java .util .*;
12- import java .util .Map .Entry ;
13-
14- import static org .objectweb .asm .tree .AbstractInsnNode .*;
15-
16- public class ASMBlock
17- {
15+ public class ASMBlock {
1816 public InsnListSection list ;
1917 private BiMap <String , LabelNode > labels ;
2018
@@ -37,8 +35,7 @@ public ASMBlock() {
3735
3836 public LabelNode getOrAdd (String s ) {
3937 LabelNode l = get (s );
40- if (l == null )
41- labels .put (s , l = new LabelNode ());
38+ if (l == null ) labels .put (s , l = new LabelNode ());
4239 return l ;
4340 }
4441
@@ -51,8 +48,8 @@ public void replaceLabels(Map<LabelNode, LabelNode> labelMap, Set<LabelNode> use
5148 switch (insn .getType ()) {
5249 case LABEL :
5350 AbstractInsnNode insn2 = insn .clone (labelMap );
54- if (insn2 == insn )// identity mapping
55- continue ;
51+ if (insn2 == insn ) // identity mapping
52+ continue ;
5653 if (usedLabels .contains (insn2 ))
5754 throw new IllegalStateException ("LabelNode cannot be a part of two InsnLists" );
5855 list .replace (insn , insn2 );
@@ -64,10 +61,9 @@ public void replaceLabels(Map<LabelNode, LabelNode> labelMap, Set<LabelNode> use
6461 list .replace (insn , insn .clone (labelMap ));
6562 }
6663
67- for (Entry <LabelNode , LabelNode > entry : labelMap .entrySet ()) {
64+ for (Entry <LabelNode , LabelNode > entry : labelMap .entrySet ()) {
6865 String key = labels .inverse ().get (entry .getKey ());
69- if (key != null )
70- labels .put (key , entry .getValue ());
66+ if (key != null ) labels .put (key , entry .getValue ());
7167 }
7268 }
7369
@@ -77,29 +73,25 @@ public void replaceLabels(Map<LabelNode, LabelNode> labelMap) {
7773
7874 public void replaceLabel (String s , LabelNode l ) {
7975 LabelNode old = get (s );
80- if (old != null )
81- replaceLabels (ImmutableMap .of (old , l ));
76+ if (old != null ) replaceLabels (ImmutableMap .of (old , l ));
8277 }
8378
8479 /**
8580 * Pulls all common labels from other into this
8681 * @return this
8782 */
8883 public ASMBlock mergeLabels (ASMBlock other ) {
89- if (labels .isEmpty () || other .labels .isEmpty ())
90- return this ;
84+ if (labels .isEmpty () || other .labels .isEmpty ()) return this ;
9185
92- //common labels, give them our nodes
86+ // common labels, give them our nodes
9387 HashMap <LabelNode , LabelNode > labelMap = list .identityLabelMap ();
94- for (Entry <String , LabelNode > entry : other .labels .entrySet ()) {
88+ for (Entry <String , LabelNode > entry : other .labels .entrySet ()) {
9589 LabelNode old = labels .get (entry .getKey ());
96- if (old != null )
97- labelMap .put (old , entry .getValue ());
90+ if (old != null ) labelMap .put (old , entry .getValue ());
9891 }
9992 HashSet <LabelNode > usedLabels = new HashSet <LabelNode >();
10093 for (AbstractInsnNode insn = other .list .list .getFirst (); insn != null ; insn = insn .getNext ())
101- if (insn .getType () == LABEL )
102- usedLabels .add ((LabelNode ) insn );
94+ if (insn .getType () == LABEL ) usedLabels .add ((LabelNode ) insn );
10395
10496 replaceLabels (labelMap , usedLabels );
10597 return this ;
@@ -118,50 +110,50 @@ public ASMBlock copy() {
118110 BiMap <String , LabelNode > labels = HashBiMap .create ();
119111 Map <LabelNode , LabelNode > labelMap = list .cloneLabels ();
120112
121- for (Entry <String , LabelNode > entry : this .labels .entrySet ())
113+ for (Entry <String , LabelNode > entry : this .labels .entrySet ())
122114 labels .put (entry .getKey (), labelMap .get (entry .getValue ()));
123115
124116 return new ASMBlock (list .copy (labelMap ), labels );
125117 }
126118
127119 public ASMBlock applyLabels (InsnListSection list2 ) {
128- if (labels .isEmpty ())
129- return new ASMBlock (list2 );
120+ if (labels .isEmpty ()) return new ASMBlock (list2 );
130121
131122 Set <LabelNode > cFlowLabels1 = labels .values ();
132123 Set <LabelNode > cFlowLabels2 = InsnComparator .getControlFlowLabels (list2 );
133124 ASMBlock block = new ASMBlock (list2 );
134125
135126 HashMap <LabelNode , LabelNode > labelMap = new HashMap <LabelNode , LabelNode >();
136127
137- for (int i = 0 , k = 0 ; i < list .size () && k < list2 .size (); ) {
128+ for (int i = 0 , k = 0 ; i < list .size () && k < list2 .size (); ) {
138129 AbstractInsnNode insn1 = list .get (i );
139- if (!InsnComparator .insnImportant (insn1 , cFlowLabels1 )) {
130+ if (!InsnComparator .insnImportant (insn1 , cFlowLabels1 )) {
140131 i ++;
141132 continue ;
142133 }
143134
144135 AbstractInsnNode insn2 = list2 .get (k );
145- if (!InsnComparator .insnImportant (insn2 , cFlowLabels2 )) {
136+ if (!InsnComparator .insnImportant (insn2 , cFlowLabels2 )) {
146137 k ++;
147138 continue ;
148139 }
149140
150- if (insn1 .getOpcode () != insn2 .getOpcode ())
151- throw new IllegalArgumentException ("Lists do not match:\n " + list + "\n \n " + list2 );
141+ if (insn1 .getOpcode () != insn2 .getOpcode ())
142+ throw new IllegalArgumentException ("Lists do not match:\n " + list + "\n \n " + list2 );
152143
153- switch (insn1 .getType ()) {
144+ switch (insn1 .getType ()) {
154145 case LABEL :
155146 labelMap .put ((LabelNode ) insn1 , (LabelNode ) insn2 );
156147 break ;
157148 case JUMP_INSN :
158149 labelMap .put (((JumpInsnNode ) insn1 ).label , ((JumpInsnNode ) insn2 ).label );
159150 break ;
160151 }
161- i ++; k ++;
152+ i ++;
153+ k ++;
162154 }
163155
164- for (Entry <String , LabelNode > entry : labels .entrySet ())
156+ for (Entry <String , LabelNode > entry : labels .entrySet ())
165157 block .labels .put (entry .getKey (), labelMap .get (entry .getValue ()));
166158
167159 return block ;
@@ -170,4 +162,4 @@ public ASMBlock applyLabels(InsnListSection list2) {
170162 public InsnList rawListCopy () {
171163 return list .copy ().list ;
172164 }
173- }
165+ }
0 commit comments