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 *
017 */
018package org.apache.bcel.generic;
019
020/**
021 * Equality of instructions isn't clearly to be defined. You might
022 * wish, for example, to compare whether instructions have the same
023 * meaning. E.g., whether two INVOKEVIRTUALs describe the same
024 * call.
025 * <p>
026 * The DEFAULT comparator however, considers two instructions
027 * to be equal if they have same opcode and point to the same indexes
028 * (if any) in the constant pool or the same local variable index. Branch
029 * instructions must have the same target.
030 * </p>
031 *
032 * @see Instruction
033 */
034public interface InstructionComparator {
035
036    InstructionComparator DEFAULT = (i1, i2) -> {
037        if (i1.getOpcode() == i2.getOpcode()) {
038            if (i1 instanceof BranchInstruction) {
039             // BIs are never equal to make targeters work correctly (BCEL-195)
040                return false;
041//                } else if (i1 == i2) { TODO consider adding this shortcut
042//                    return true; // this must be AFTER the BI test
043            } else if (i1 instanceof ConstantPushInstruction) {
044                return ((ConstantPushInstruction) i1).getValue().equals(
045                        ((ConstantPushInstruction) i2).getValue());
046            } else if (i1 instanceof IndexedInstruction) {
047                return ((IndexedInstruction) i1).getIndex() == ((IndexedInstruction) i2)
048                        .getIndex();
049            } else if (i1 instanceof NEWARRAY) {
050                return ((NEWARRAY) i1).getTypecode() == ((NEWARRAY) i2).getTypecode();
051            } else {
052                return true;
053            }
054        }
055        return false;
056    };
057
058
059    boolean equals( Instruction i1, Instruction i2 );
060}