1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18 package org.apache.bcel.generic;
19 
20 /**
21  * Equality of instructions isn't clearly to be defined. You might
22  * wish, for example, to compare whether instructions have the same
23  * meaning. E.g., whether two INVOKEVIRTUALs describe the same
24  * call.<br>The DEFAULT comparator however, considers two instructions
25  * to be equal if they have same opcode and point to the same indexes
26  * (if any) in the constant pool or the same local variable index. Branch
27  * instructions must have the same target.
28  *
29  * @see Instruction
30  * @version $Id$
31  */
32 public interface InstructionComparator {
33 
34     InstructionComparator DEFAULT = new InstructionComparator() {
35 
36         @Override
37         public boolean equals( final Instruction i1, final Instruction i2 ) {
38             if (i1.getOpcode() == i2.getOpcode()) {
39                 if (i1 instanceof BranchInstruction) {
40                  // BIs are never equal to make targeters work correctly (BCEL-195)
41                     return false;
42 //                } else if (i1 == i2) { TODO consider adding this shortcut
43 //                    return true; // this must be AFTER the BI test
44                 } else if (i1 instanceof ConstantPushInstruction) {
45                     return ((ConstantPushInstruction) i1).getValue().equals(
46                             ((ConstantPushInstruction) i2).getValue());
47                 } else if (i1 instanceof IndexedInstruction) {
48                     return ((IndexedInstruction) i1).getIndex() == ((IndexedInstruction) i2)
49                             .getIndex();
50                 } else if (i1 instanceof NEWARRAY) {
51                     return ((NEWARRAY) i1).getTypecode() == ((NEWARRAY) i2).getTypecode();
52                 } else {
53                     return true;
54                 }
55             }
56             return false;
57         }
58     };
59 
60 
equals( Instruction i1, Instruction i2 )61     boolean equals( Instruction i1, Instruction i2 );
62 }
63