View Javadoc
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  package org.apache.bcel.generic;
18  
19  /**
20   * Equality of instructions isn't clearly to be defined. You might wish, for example, to compare whether instructions
21   * have the same meaning. E.g., whether two INVOKEVIRTUALs describe the same call.
22   * <p>
23   * The DEFAULT comparator however, considers two instructions to be equal if they have same opcode and point to the same
24   * indexes (if any) in the constant pool or the same local variable index. Branch instructions must have the same
25   * target.
26   * </p>
27   *
28   * @see Instruction
29   */
30  public interface InstructionComparator {
31  
32      InstructionComparator DEFAULT = (i1, i2) -> {
33          if (i1.getOpcode() == i2.getOpcode()) {
34              if (i1 instanceof BranchInstruction) {
35                  // BIs are never equal to make targeters work correctly (BCEL-195)
36                  return false;
37  //                } else if (i1 == i2) { TODO consider adding this shortcut
38  //                    return true; // this must be AFTER the BI test
39              }
40              if (i1 instanceof ConstantPushInstruction) {
41                  return ((ConstantPushInstruction) i1).getValue().equals(((ConstantPushInstruction) i2).getValue());
42              }
43              if (i1 instanceof IndexedInstruction) {
44                  return ((IndexedInstruction) i1).getIndex() == ((IndexedInstruction) i2).getIndex();
45              }
46              if (i1 instanceof NEWARRAY) {
47                  return ((NEWARRAY) i1).getTypecode() == ((NEWARRAY) i2).getTypecode();
48              }
49              return true;
50          }
51          return false;
52      };
53  
54      boolean equals(Instruction i1, Instruction i2);
55  }