    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.<br>The DEFAULT comparator however, considers two instructions
    025 * to be equal if they have same opcode and point to the same indexes
    026 * (if any) in the constant pool or the same local variable index. Branch
    027 * instructions must have the same target.
    028 *
    029 * @see Instruction
    030 * @version $Id$
    031 */
    032public interface InstructionComparator {
    033
    034    InstructionComparator DEFAULT = new InstructionComparator() {
    035
    036        @Override
    037        public boolean equals( final Instruction i1, final Instruction 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                } else if (i1 instanceof ConstantPushInstruction) {
    045                    return ((ConstantPushInstruction) i1).getValue().equals(
    046                            ((ConstantPushInstruction) i2).getValue());
    047                } else if (i1 instanceof IndexedInstruction) {
    048                    return ((IndexedInstruction) i1).getIndex() == ((IndexedInstruction) i2)
    049                            .getIndex();
    050                } else if (i1 instanceof NEWARRAY) {
    051                    return ((NEWARRAY) i1).getTypecode() == ((NEWARRAY) i2).getTypecode();
    052                } else {
    053                    return true;
    054                }
    055            }
    056            return false;
    057        }
    058    };
    059
    060
    061    boolean equals( Instruction i1, Instruction i2 );
    062}