KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > structures > constants > ConstantReference


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.structures.constants;
9
10 import org.gjt.jclasslib.structures.CPInfo;
11 import org.gjt.jclasslib.structures.InvalidByteCodeException;
12
13 import java.io.*;
14
15 /**
16     Base class for constant pool data structures which reference class members.
17
18     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
19     @version $Revision: 1.5 $ $Date: 2003/08/18 07:50:25 $
20 */

21 public abstract class ConstantReference extends CPInfo {
22
23     /** Length of the constant pool data structure in bytes. */
24     public static final int SIZE = 4;
25
26     /** <tt>class_index</tt> field. */
27     protected int classIndex;
28     /** <tt>name_and_type_index</tt> field. */
29     protected int nameAndTypeIndex;
30     
31     public String JavaDoc getVerbose() throws InvalidByteCodeException {
32
33         ConstantNameAndTypeInfo nameAndType = getNameAndTypeInfo();
34
35         return classFile.getConstantPoolEntryName(classIndex) + "." +
36                classFile.getConstantPoolEntryName(nameAndType.getNameIndex());
37     }
38
39     /**
40         Get the index of the constant pool entry containing the
41         <tt>CONSTANT_Class_info</tt> of this entry.
42         @return the index
43      */

44     public int getClassIndex() {
45         return classIndex;
46     }
47     
48     /**
49         Set the index of the constant pool entry containing the
50         <tt>CONSTANT_Class_info</tt> of this entry.
51         @param classIndex the index
52      */

53     public void setClassIndex(int classIndex) {
54         this.classIndex = classIndex;
55     }
56     
57     /**
58         Get the index of the constant pool entry containing the
59          <tt>CONSTANT_NameAndType_info</tt> of this entry.
60         @return the index
61      */

62     public int getNameAndTypeIndex() {
63         return nameAndTypeIndex;
64     }
65
66     /**
67         Set the index of the constant pool entry containing the
68          <tt>CONSTANT_NameAndType_info</tt> of this entry.
69         @param nameAndTypeIndex the index
70      */

71     public void setNameAndTypeIndex(int nameAndTypeIndex) {
72         this.nameAndTypeIndex = nameAndTypeIndex;
73     }
74
75     /**
76         Get the class info for this reference.
77         @return the class info.
78         @throws InvalidByteCodeException
79      */

80     public ConstantClassInfo getClassInfo() throws InvalidByteCodeException {
81         return (ConstantClassInfo)getClassFile().getConstantPoolEntry(classIndex, ConstantClassInfo.class);
82     }
83
84     /**
85         Get the name and type info for this reference.
86         @return the name and type info.
87         @throws InvalidByteCodeException
88      */

89     public ConstantNameAndTypeInfo getNameAndTypeInfo() throws InvalidByteCodeException {
90         return (ConstantNameAndTypeInfo)classFile.getConstantPoolEntry(
91                     nameAndTypeIndex,
92                     ConstantNameAndTypeInfo.class);
93     }
94
95     public void read(DataInput in)
96         throws InvalidByteCodeException, IOException {
97             
98         classIndex = in.readUnsignedShort();
99         nameAndTypeIndex = in.readUnsignedShort();
100     }
101     
102     public void write(DataOutput out)
103         throws InvalidByteCodeException, IOException {
104         
105         out.writeShort(classIndex);
106         out.writeShort(nameAndTypeIndex);
107     }
108     
109     public boolean equals(Object JavaDoc object) {
110         if (!(object instanceof ConstantReference)) {
111             return false;
112         }
113         ConstantReference constantReference = (ConstantReference)object;
114         return super.equals(object) &&
115                constantReference.classIndex == classIndex &&
116                constantReference.nameAndTypeIndex == nameAndTypeIndex;
117     }
118
119     public int hashCode() {
120         return super.hashCode() ^ classIndex ^ nameAndTypeIndex;
121     }
122
123 }
124
Popular Tags