KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bcel > generic > InstructionComparator


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.bcel.generic;
18
19 /**
20  * Equality of instructions isn't clearly to be defined. You might
21  * wish, for example, to compare whether instructions have the same
22  * meaning. E.g., whether two INVOKEVIRTUALs describe the same
23  * call.<br>The DEFAULT comparator however, considers two instructions
24  * to be equal if they have same opcode and point to the same indexes
25  * (if any) in the constant pool or the same local variable index. Branch
26  * instructions must have the same target.
27  *
28  * @see Instruction
29  * @version $Id: InstructionComparator.java 386056 2006-03-15 11:31:56Z tcurdt $
30  * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
31  */

32 public interface InstructionComparator {
33
34     public static final InstructionComparator DEFAULT = new InstructionComparator() {
35
36         public boolean equals( Instruction i1, Instruction i2 ) {
37             if (i1.opcode == i2.opcode) {
38                 if (i1 instanceof Select) {
39                     InstructionHandle[] t1 = ((Select) i1).getTargets();
40                     InstructionHandle[] t2 = ((Select) i2).getTargets();
41                     if (t1.length == t2.length) {
42                         for (int i = 0; i < t1.length; i++) {
43                             if (t1[i] != t2[i]) {
44                                 return false;
45                             }
46                         }
47                         return true;
48                     }
49                 } else if (i1 instanceof BranchInstruction) {
50                     return ((BranchInstruction) i1).target == ((BranchInstruction) i2).target;
51                 } else if (i1 instanceof ConstantPushInstruction) {
52                     return ((ConstantPushInstruction) i1).getValue().equals(
53                             ((ConstantPushInstruction) i2).getValue());
54                 } else if (i1 instanceof IndexedInstruction) {
55                     return ((IndexedInstruction) i1).getIndex() == ((IndexedInstruction) i2)
56                             .getIndex();
57                 } else if (i1 instanceof NEWARRAY) {
58                     return ((NEWARRAY) i1).getTypecode() == ((NEWARRAY) i2).getTypecode();
59                 } else {
60                     return true;
61                 }
62             }
63             return false;
64         }
65     };
66
67
68     public boolean equals( Instruction i1, Instruction i2 );
69 }
70
Popular Tags