01: // Copyright (c) 1997 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.bytecode;
05:
06: import java.io.*;
07:
08: /** A CONSTANT_{Field,Method,InterfaceMethod}Ref entry in the constant pool. */
09:
10: public class CpoolRef extends CpoolEntry {
11: CpoolClass clas;
12: CpoolNameAndType nameAndType;
13:
14: /**
15: * The specific kind of Ref constant:
16: * CONSTANT_Fieldref (9), CONSTANT_Methodref (10), or
17: * CONSTANT_InterfaceMethodref (11).
18: */
19: int tag;
20:
21: public int getTag() {
22: return tag;
23: }
24:
25: public final CpoolClass getCpoolClass() {
26: return clas;
27: }
28:
29: public final CpoolNameAndType getNameAndType() {
30: return nameAndType;
31: }
32:
33: CpoolRef(int tag) {
34: this .tag = tag;
35: }
36:
37: CpoolRef(ConstantPool cpool, int hash, int tag, CpoolClass clas,
38: CpoolNameAndType nameAndType) {
39: super (cpool, hash);
40: this .tag = tag;
41: this .clas = clas;
42: this .nameAndType = nameAndType;
43: }
44:
45: final static int hashCode(CpoolClass clas,
46: CpoolNameAndType nameAndType) {
47: return clas.hashCode() ^ nameAndType.hashCode();
48: }
49:
50: public int hashCode() {
51: if (hash == 0)
52: hash = hashCode(clas, nameAndType);
53: return hash;
54: }
55:
56: void write(DataOutputStream dstr) throws java.io.IOException {
57: dstr.writeByte(tag);
58: dstr.writeShort(clas.index);
59: dstr.writeShort(nameAndType.index);
60: }
61:
62: public void print(ClassTypeWriter dst, int verbosity) {
63: String str;
64: switch (tag) {
65: case 9:
66: str = "Field";
67: break;
68: case 10:
69: str = "Method";
70: break;
71: case 11:
72: str = "InterfaceMethod";
73: break;
74: default:
75: str = "<Unknown>Ref";
76: break;
77: }
78: if (verbosity > 0) {
79: dst.print(str);
80: if (verbosity == 2) {
81: dst.print(" class: ");
82: dst.printOptionalIndex(clas);
83: } else
84: dst.print(' ');
85: }
86: clas.print(dst, 0);
87: if (verbosity < 2)
88: dst.print('.');
89: else {
90: dst.print(" name_and_type: ");
91: dst.printOptionalIndex(nameAndType);
92: dst.print('<');
93: }
94: nameAndType.print(dst, 0);
95: if (verbosity == 2)
96: dst.print('>');
97: }
98: }
|