Source code: com/chaoswg/xtc4y/classdesc/NameAndTypeCPEntry.java
1 //$Header: /cvsroot/xtc4y/xtc4y/src/com/chaoswg/xtc4y/classdesc/NameAndTypeCPEntry.java,v 1.1.1.1 2003/08/07 13:40:28 toggm Exp $
2 /******************************************************************************
3 * XTC4y - eXtreme Testing Collection 4 you *
4 * -------------------------------------------------------------------------- *
5 * URL: http://www.chaoswg.com/xtc4y *
6 * Author: Mike Toggweiler (2.dog@gmx.ch) *
7 * *
8 * Last Updated: $Date: 2003/08/07 13:40:28 $, by $Author: toggm $ *
9 * Version: $Revision: 1.1.1.1 $ *
10 * -------------------------------------------------------------------------- *
11 * COPYRIGHT: (c) 2003 by Mike Toggweiler *
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 *****************************************************************************/
18 package com.chaoswg.xtc4y.classdesc;
19
20 import java.io.DataInputStream;
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23
24 /**
25 * this class represent a NameAndType constant pool entry
26 * @author Mike Toggweiler
27 **/
28 public final class NameAndTypeCPEntry extends ConstantPoolEntry {
29 /**
30 * The tag identifying a NameAndType cp entry
31 **/
32 public static final byte TAG = 12;
33 private String name;
34 private String descriptor;
35 private short nameIndex;
36 private short descriptorIndex;
37
38 /**
39 * Creates a nameandtype cp entry
40 * @param nameIndex a valid cp table entry
41 * @param descriptorIndex a valid cp table entry
42 **/
43 public NameAndTypeCPEntry(String name, String descriptor) {
44 super(TAG);
45 this.name = name;
46 this.descriptor = descriptor;
47 }
48
49 /**
50 * Creates a NameAndTypeCPEntry and initializes it from a DataInputStream
51 * @param dis the DataInputStream to read from
52 **/
53 protected NameAndTypeCPEntry(DataInputStream dis)
54 throws IOException {
55 super(TAG);
56 //name_index
57 nameIndex = (short)dis.readUnsignedShort();
58 //descriptor_index
59 descriptorIndex = (short)dis.readUnsignedShort();
60 }
61
62 /**
63 * Resolve the internal stored indices thorugh the Constantpool
64 * @param cp the constantpool to resolve indices
65 **/
66 protected void resolve(ConstantPool cp) {
67 UTF8CPEntry utf = (UTF8CPEntry)cp.getCPEntryAt(nameIndex);
68 name = utf.getString();
69
70 utf = (UTF8CPEntry)cp.getCPEntryAt(descriptorIndex);
71 descriptor = utf.getString();
72 }
73
74 /**
75 * Write the type specific constant pool entry information onto a
76 * DataOutputStream
77 * @param dos the DataOutputStream to write on
78 * @param cp the constantpool entry used to register names
79 **/
80 protected void writeCPEntry(DataOutputStream dos, ConstantPool cp)
81 throws IOException {
82 dos.writeShort(cp.addCPEntry(new UTF8CPEntry(name)));
83 dos.writeShort(cp.addCPEntry(new UTF8CPEntry(descriptor)));
84 }
85
86 /**
87 * @return the name
88 **/
89 public String getName() {
90 return name;
91 }
92
93 /**
94 * @reutrn the descriptor
95 **/
96 public String getDescriptor() {
97 return descriptor;
98 }
99
100 /**
101 * Set the name
102 * @param name the new name value
103 **/
104 public void setName(String name) {
105 this.name = name;
106 }
107
108 /**
109 * Set the descriptor
110 * @param name the new descriptor value
111 **/
112 public void setDescriptor(String desc) {
113 this.descriptor = desc;
114 }
115
116 /**
117 * Checks if another NameAndTypeCPEntry is equal to this.
118 **/
119 public boolean equals(Object obj) {
120 if (!(obj instanceof NameAndTypeCPEntry)) {
121 return false;
122 }
123 NameAndTypeCPEntry other = (NameAndTypeCPEntry)obj;
124 boolean check = (this.name == null && other.name == null ||
125 this.name != null &&
126 this.name.equals(other.name));
127 return (check && (this.descriptor == null && other.descriptor == null
128 || this.descriptor != null &&
129 this.descriptor.equals(other.descriptor)));
130 }
131
132 /**
133 * print in a readable way
134 **/
135 public String toString() {
136 return getClass().getName()+":"+hashCode()+":"+name+":"+descriptor;
137 }
138
139 /**
140 * @return the return type of the descriptor
141 **/
142 public String getReturnType() {
143 return extractReturnType(descriptor);
144 }
145
146 /**
147 * Extrace the return type from the descriptor
148 **/
149 public static String extractReturnType(String descriptor) {
150 return descriptor.substring(descriptor.indexOf(")") + 1);
151 }
152 }