19 * limitations under the License. |
19 * limitations under the License. |
20 */ |
20 */ |
21 |
21 |
22 package com.sun.org.apache.bcel.internal.generic; |
22 package com.sun.org.apache.bcel.internal.generic; |
23 |
23 |
24 |
|
25 /** |
24 /** |
26 * Equality of instructions isn't clearly to be defined. You might |
25 * Equality of instructions isn't clearly to be defined. You might |
27 * wish, for example, to compare whether instructions have the same |
26 * wish, for example, to compare whether instructions have the same |
28 * meaning. E.g., whether two INVOKEVIRTUALs describe the same |
27 * meaning. E.g., whether two INVOKEVIRTUALs describe the same |
29 * call.<br>The DEFAULT comparator however, considers two instructions |
28 * call.<br>The DEFAULT comparator however, considers two instructions |
30 * to be equal if they have same opcode and point to the same indexes |
29 * to be equal if they have same opcode and point to the same indexes |
31 * (if any) in the constant pool or the same local variable index. Branch |
30 * (if any) in the constant pool or the same local variable index. Branch |
32 * instructions must have the same target. |
31 * instructions must have the same target. |
33 * |
32 * |
34 * @see Instruction |
33 * @see Instruction |
35 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> |
34 * @version $Id: InstructionComparator.java 1749597 2016-06-21 20:28:51Z ggregory $ |
36 */ |
35 */ |
37 public interface InstructionComparator { |
36 public interface InstructionComparator { |
38 public static final InstructionComparator DEFAULT = |
|
39 new InstructionComparator() { |
|
40 public boolean equals(Instruction i1, Instruction i2) { |
|
41 if(i1.opcode == i2.opcode) { |
|
42 if(i1 instanceof Select) { |
|
43 InstructionHandle[] t1 = ((Select)i1).getTargets(); |
|
44 InstructionHandle[] t2 = ((Select)i2).getTargets(); |
|
45 |
37 |
46 if(t1.length == t2.length) { |
38 InstructionComparator DEFAULT = new InstructionComparator() { |
47 for(int i = 0; i < t1.length; i++) { |
39 |
48 if(t1[i] != t2[i]) { |
40 @Override |
|
41 public boolean equals( final Instruction i1, final Instruction i2 ) { |
|
42 if (i1.getOpcode() == i2.getOpcode()) { |
|
43 if (i1 instanceof BranchInstruction) { |
|
44 // BIs are never equal to make targeters work correctly (BCEL-195) |
49 return false; |
45 return false; |
50 } |
46 // } else if (i1 == i2) { TODO consider adding this shortcut |
|
47 // return true; // this must be AFTER the BI test |
|
48 } else if (i1 instanceof ConstantPushInstruction) { |
|
49 return ((ConstantPushInstruction) i1).getValue().equals( |
|
50 ((ConstantPushInstruction) i2).getValue()); |
|
51 } else if (i1 instanceof IndexedInstruction) { |
|
52 return ((IndexedInstruction) i1).getIndex() == ((IndexedInstruction) i2) |
|
53 .getIndex(); |
|
54 } else if (i1 instanceof NEWARRAY) { |
|
55 return ((NEWARRAY) i1).getTypecode() == ((NEWARRAY) i2).getTypecode(); |
|
56 } else { |
|
57 return true; |
51 } |
58 } |
|
59 } |
|
60 return false; |
|
61 } |
|
62 }; |
52 |
63 |
53 return true; |
|
54 } |
|
55 } else if(i1 instanceof BranchInstruction) { |
|
56 return ((BranchInstruction)i1).target == |
|
57 ((BranchInstruction)i2).target; |
|
58 } else if(i1 instanceof ConstantPushInstruction) { |
|
59 return ((ConstantPushInstruction)i1).getValue(). |
|
60 equals(((ConstantPushInstruction)i2).getValue()); |
|
61 } else if(i1 instanceof IndexedInstruction) { |
|
62 return ((IndexedInstruction)i1).getIndex() == |
|
63 ((IndexedInstruction)i2).getIndex(); |
|
64 } else if(i1 instanceof NEWARRAY) { |
|
65 return ((NEWARRAY)i1).getTypecode() == ((NEWARRAY)i2).getTypecode(); |
|
66 } else { |
|
67 return true; |
|
68 } |
|
69 } |
|
70 |
64 |
71 return false; |
65 boolean equals( Instruction i1, Instruction i2 ); |
72 } |
|
73 }; |
|
74 |
|
75 public boolean equals(Instruction i1, Instruction i2); |
|
76 } |
66 } |