Source code: com/chaoswg/xtc4y/classdesc/NameDescriptorInfo.java
1 //$Header: /cvsroot/xtc4y/xtc4y/src/com/chaoswg/xtc4y/classdesc/NameDescriptorInfo.java,v 1.2 2003/08/25 10:50:14 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/25 10:50:14 $, by $Author: toggm $ *
9 * Version: $Revision: 1.2 $ *
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.util.Vector;
21
22 import java.io.DataInputStream;
23 import java.io.DataOutputStream;
24 import java.io.IOException;
25
26 /**
27 * This class describes the information for a filed
28 * @author Mike Toggweiler
29 **/
30 public abstract class NameDescriptorInfo {
31 private String name;
32 private String descriptor;
33 private Vector attributes;
34 private AccessFlags access;
35
36 /**
37 * Creates a NameDescriptorInfo and initializes it from a DataInputStream
38 * @param dis the DataInputStream to read from
39 * @param cp the constant pool to resolve indices
40 **/
41 protected NameDescriptorInfo(DataInputStream dis, ConstantPool cp)
42 throws IOException {
43 short accessFlag = (short)dis.readUnsignedShort();
44 access = getAccessFlags(accessFlag);
45 //name_index
46 short nameIndex = (short)dis.readUnsignedShort();
47 UTF8CPEntry utf = (UTF8CPEntry)cp.getCPEntryAt(nameIndex);
48 name = utf.getString();
49 //desciptor_index
50 short descIndex = (short)dis.readUnsignedShort();
51 utf = (UTF8CPEntry)cp.getCPEntryAt(descIndex);
52 descriptor = utf.getString();
53 //attributes_count
54 int aCount = (short)dis.readUnsignedShort();
55 attributes = new Vector();
56 //attributes[attributes_count]
57 for (int i=0; i<aCount; ++i) {
58 attributes.add(new AttributeInfo(dis, cp));
59 }
60 }
61
62 /**
63 * Construct a new field with a given the name and the
64 * descriptor
65 * @param access the access flags
66 * @param name the field name
67 * @param descriptor the field descriptor
68 **/
69 public NameDescriptorInfo(AccessFlags access, String name,
70 String descriptor) {
71 this.access = access;
72 this.name = name;
73 this.descriptor = descriptor;
74 attributes = new Vector();
75 }
76
77 /**
78 * @param access the access flag
79 * @return a concrete implementation of the abstract AccessFlags class
80 **/
81 protected abstract AccessFlags getAccessFlags(short access);
82
83 /**
84 * Write the constant pool information into a DataOutputStream
85 * @param dos the DataOutputStream to write on
86 * @param cp the constant pool to store variables
87 **/
88 protected void write(DataOutputStream dos, ConstantPool cp)
89 throws IOException {
90 //write accessflag
91 access.write(dos);
92 //register name
93 dos.writeShort(cp.addCPEntry(new UTF8CPEntry(name)));
94 //register descriptor
95 dos.writeShort(cp.addCPEntry(new UTF8CPEntry(descriptor)));
96 //write attribute count
97 dos.writeShort(attributes.size());
98 //write all attributes
99 for (int i=0; i<attributes.size(); ++i) {
100 ((AttributeInfo)attributes.elementAt(i)).write(dos, cp);
101 }
102 }
103
104 /**
105 * @return the name
106 **/
107 public String getName() {
108 return name;
109 }
110
111 /**
112 * @return the descriptor
113 **/
114 public String getDescriptor() {
115 return descriptor;
116 }
117
118 /**
119 * Set a new name for this field
120 * @param name the new name
121 **/
122 public void setName(String name) {
123 this.name = name;
124 }
125
126 /**
127 * Set the new field descriptor
128 * @param descriptor the new descriptor
129 **/
130 public void setDescriptor(String descriptor) {
131 this.descriptor = descriptor;
132 }
133
134 /**
135 * Add an attribute to this class
136 * @param attribute the attribute to add
137 **/
138 public void addAttribute(AttributeInfo attribute) {
139 attributes.add(attribute);
140 }
141
142 /**
143 * @return an array of Attributes currently defined in this class
144 **/
145 public AttributeInfo[] getAttributes() {
146 return
147 ((AttributeInfo[])
148 attributes.toArray(new AttributeInfo[attributes.size()]));
149 }
150
151 /**
152 * Remove an attribute from the current class
153 * @param attribute the AttributeInfo to remove
154 **/
155 public void removeAttribute(AttributeInfo attribute) {
156 attributes.remove(attribute);
157 }
158
159 /**
160 * @return the currently associated access flags
161 **/
162 public AccessFlags getAccessFlags() {
163 return this.access;
164 }
165
166 /**
167 * Write in a readable way
168 **/
169 public String toString() {
170 String ret = name+":"+descriptor+":"+access+":\n";
171 ret += "Attributes:\n";
172 for (int i=0; i<attributes.size(); ++i) {
173 ret += " " + i + " " + attributes.elementAt(i) + "\n";
174 }
175 return ret;
176 }
177 }