4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.generic; 23 24 25 /** 26 * Equality of instructions isn't clearly to be defined. You might 27 * wish, for example, to compare whether instructions have the same 28 * meaning. E.g., whether two INVOKEVIRTUALs describe the same 29 * call.<br>The DEFAULT comparator however, considers two instructions 30 * 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 32 * instructions must have the same target. 33 * 34 * @see Instruction 35 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> 36 */ 37 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 46 if(t1.length == t2.length) { 47 for(int i = 0; i < t1.length; i++) { 48 if(t1[i] != t2[i]) { 49 return false; 50 } 51 } 52 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 71 return false; 72 } 73 }; 74 75 public boolean equals(Instruction i1, Instruction i2); 76 } | 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.generic; 23 24 /** 25 * Equality of instructions isn't clearly to be defined. You might 26 * wish, for example, to compare whether instructions have the same 27 * meaning. E.g., whether two INVOKEVIRTUALs describe the same 28 * call.<br>The DEFAULT comparator however, considers two instructions 29 * to be equal if they have same opcode and point to the same indexes 30 * (if any) in the constant pool or the same local variable index. Branch 31 * instructions must have the same target. 32 * 33 * @see Instruction 34 * @version $Id: InstructionComparator.java 1749597 2016-06-21 20:28:51Z ggregory $ 35 */ 36 public interface InstructionComparator { 37 38 InstructionComparator DEFAULT = new InstructionComparator() { 39 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) 45 return false; 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; 58 } 59 } 60 return false; 61 } 62 }; 63 64 65 boolean equals( Instruction i1, Instruction i2 ); 66 } |