KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > bytecode > CpoolRef


1 // Copyright (c) 1997 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.bytecode;
5 import java.io.*;
6
7 /** A CONSTANT_{Field,Method,InterfaceMethod}Ref entry in the constant pool. */
8
9 public class
10 CpoolRef extends CpoolEntry
11 {
12   CpoolClass clas;
13   CpoolNameAndType nameAndType;
14
15   /**
16    * The specific kind of Ref constant:
17    * CONSTANT_Fieldref (9), CONSTANT_Methodref (10), or
18    * CONSTANT_InterfaceMethodref (11).
19    */

20   int tag;
21   public int getTag() { return tag; }
22
23   public final CpoolClass getCpoolClass()
24   {
25     return clas;
26   }
27
28   public final CpoolNameAndType getNameAndType()
29   {
30     return nameAndType;
31   }
32
33   CpoolRef (int tag) { this.tag = tag; }
34
35   CpoolRef (ConstantPool cpool, int hash, int tag,
36         CpoolClass clas, CpoolNameAndType nameAndType)
37   {
38     super (cpool, hash);
39     this.tag = tag;
40     this.clas = clas;
41     this.nameAndType = nameAndType;
42   }
43
44   final static int hashCode (CpoolClass clas, CpoolNameAndType nameAndType)
45   {
46     return clas.hashCode() ^ nameAndType.hashCode();
47   }
48
49   public int hashCode ()
50   {
51     if (hash == 0)
52       hash = hashCode(clas, nameAndType);
53     return hash;
54   }
55
56   void write (DataOutputStream dstr) throws java.io.IOException JavaDoc
57   {
58     dstr.writeByte (tag);
59     dstr.writeShort (clas.index);
60     dstr.writeShort (nameAndType.index);
61   }
62
63   public void print (ClassTypeWriter dst, int verbosity)
64   {
65     String JavaDoc str;
66     switch (tag)
67       {
68       case 9: str = "Field"; break;
69       case 10: str = "Method"; break;
70       case 11: str = "InterfaceMethod"; break;
71       default: str = "<Unknown>Ref"; break;
72       }
73     if (verbosity > 0)
74       {
75     dst.print(str);
76     if (verbosity == 2)
77       {
78         dst.print(" class: ");
79         dst.printOptionalIndex(clas);
80       }
81     else
82       dst.print( ' ');
83       }
84     clas.print(dst, 0);
85     if (verbosity < 2)
86       dst.print('.');
87     else
88       {
89     dst.print(" name_and_type: ");
90     dst.printOptionalIndex(nameAndType);
91     dst.print('<');
92       }
93     nameAndType.print(dst, 0);
94     if (verbosity == 2)
95       dst.print('>');
96   }
97 }
98
99
Popular Tags