KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jbet > CpMethodRef


1 /*
2  * JBET - Java Binary Enhancement Tool
3  * Copyright (c) 2003 Networks Associates Technology, Inc.
4  *
5  * This software was developed under DARPA/SPAWAR contract
6  * N66001-00-C-8602 "SPMA" as part of the
7  * DARPA OASIS research program.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */

30
31 /**
32  * Represent a reference to a method in the constant pool. Each
33  * instruction that references a method (variable) indexes one of
34  * these.
35  *
36  * @author Larry D'Anna
37  * @author Lee Badger
38  * @version 0.1
39  * @since JDK 1.1.8
40  */

41 package jbet;
42 import java.io.*;
43
44 class CpMethodRef extends CpEntry {
45     /** private
46      */

47     String JavaDoc cname;
48     String JavaDoc mname;
49     Descriptor t;
50
51     /** set by ConstantPool.internMethodRef()
52      */

53     public int classIndex = -1;
54     public int nameAndTypeIndex = -1;
55
56     /** Constructor.
57      * @param i index in pool
58      * @param cp constant pool
59      * @param iface TRUE if an interface, false if a method
60      */

61     CpMethodRef(int i, CPInterface cp, boolean iface) {
62     super(i, (iface ?
63           CPInterface.CONSTANT_InterfaceMethodref :
64           CPInterface.CONSTANT_Methodref), cp);
65     }
66
67     /** Constructor.
68      * @param cp constant pool
69      * @param iface TRUE if an interface, false if a method
70      * @param cname class name
71      * @param mname method name
72      * @param t type descriptor
73      */

74     CpMethodRef(CPInterface cp, boolean iface,
75         String JavaDoc cname, String JavaDoc mname, Descriptor t) {
76     super(cp.poolCount(), (iface ?
77           CPInterface.CONSTANT_InterfaceMethodref :
78           CPInterface.CONSTANT_Methodref), cp);
79     this.cname = cname;
80     this.mname = mname;
81     this.t = t;
82     }
83     
84     /** Override toString for printable version.
85      * @return printable value
86      */

87     public String JavaDoc toString() {
88     return classname() + " " + name() + " " + descriptor();
89     }
90
91     String JavaDoc classname() { return cname; }
92     String JavaDoc name() { return mname; }
93     Descriptor descriptor() { return t; }
94
95     /** Override setup.
96      * Shd be public, called from ConstantPool().
97      * Resolve index methods into direct references to objects. Called
98      * in pass-3 of the ConstantPool's initialization.
99      */

100     void setup() throws ClassFileException {
101     if (cname == null || mname == null || t == null) {
102         CpClass cpc = constantPool.cpClassAt(classIndex);
103         cpc.setup();
104         CpNameAndType cpnat = constantPool.cpNameAndTypeAt(nameAndTypeIndex);
105         cpnat.setup();
106         cname = cpc.string();
107         mname = cpnat.name();
108         t = cpnat.descriptor();
109     }
110     }
111
112     /** Override hashCode
113      * @return hashcode
114      */

115     public int hashCode() {
116     return cname.hashCode() + mname.hashCode() + t.hashCode();
117     }
118
119     /** Override equals, compare class and method name and type
120      * @param o Object to compare to this
121      * @return true if class and method and signature match
122      */

123     public boolean equals(Object JavaDoc o) {
124     if (!(o instanceof CpMethodRef)) return false;
125     CpMethodRef cp = (CpMethodRef) o;
126     return cp.cname.equals(cname) && cp.mname.equals(mname) &&
127         cp.t.equals(t);
128     }
129
130     /** Override write. Write out tag, class index, and name.
131      * @param dataOut output stream
132      */

133     void write(DataOutputStream dataOut) throws IOException {
134     dataOut.writeByte(tag);
135     dataOut.writeShort(classIndex);
136     dataOut.writeShort(nameAndTypeIndex);
137     }
138 }
139
Popular Tags