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