001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.bcel.generic;
018
019/**
020 * Equality of instructions isn't clearly to be defined. You might wish, for example, to compare whether instructions
021 * have the same meaning. E.g., whether two INVOKEVIRTUALs describe the same call.
022 * <p>
023 * The DEFAULT comparator however, considers two instructions to be equal if they have same opcode and point to the same
024 * indexes (if any) in the constant pool or the same local variable index. Branch instructions must have the same
025 * target.
026 * </p>
027 *
028 * @see Instruction
029 */
030public interface InstructionComparator {
031
032    InstructionComparator DEFAULT = (i1, i2) -> {
033        if (i1.getOpcode() == i2.getOpcode()) {
034            if (i1 instanceof BranchInstruction) {
035                // BIs are never equal to make targeters work correctly (BCEL-195)
036                return false;
037//                } else if (i1 == i2) { TODO consider adding this shortcut
038//                    return true; // this must be AFTER the BI test
039            }
040            if (i1 instanceof ConstantPushInstruction) {
041                return ((ConstantPushInstruction) i1).getValue().equals(((ConstantPushInstruction) i2).getValue());
042            }
043            if (i1 instanceof IndexedInstruction) {
044                return ((IndexedInstruction) i1).getIndex() == ((IndexedInstruction) i2).getIndex();
045            }
046            if (i1 instanceof NEWARRAY) {
047                return ((NEWARRAY) i1).getTypecode() == ((NEWARRAY) i2).getTypecode();
048            }
049            return true;
050        }
051        return false;
052    };
053
054    boolean equals(Instruction i1, Instruction i2);
055}