    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    InstructionComparator DEFAULT = (i1, i2) -> {
    035        if (i1.getOpcode() == i2.getOpcode()) {
    036            if (i1 instanceof BranchInstruction) {
    037                // BIs are never equal to make targeters work correctly (BCEL-195)
    038                return false;
    039//                } else if (i1 == i2) { TODO consider adding this shortcut
    040//                    return true; // this must be AFTER the BI test
    041            }
    042            if (i1 instanceof ConstantPushInstruction) {
    043                return ((ConstantPushInstruction) i1).getValue().equals(((ConstantPushInstruction) i2).getValue());
    044            }
    045            if (i1 instanceof IndexedInstruction) {
    046                return ((IndexedInstruction) i1).getIndex() == ((IndexedInstruction) i2).getIndex();
    047            }
    048            if (i1 instanceof NEWARRAY) {
    049                return ((NEWARRAY) i1).getTypecode() == ((NEWARRAY) i2).getTypecode();
    050            }
    051            return true;
    052        }
    053        return false;
    054    };
    055
    056    boolean equals(Instruction i1, Instruction i2);
    057}