Source code: com/chaoswg/xtc4y/classdesc/AttributeInfo.java
1 //$Header: /cvsroot/xtc4y/xtc4y/src/com/chaoswg/xtc4y/classdesc/AttributeInfo.java,v 1.3 2003/09/08 21:06:42 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/09/08 21:06:42 $, by $Author: toggm $ *
9 * Version: $Revision: 1.3 $ *
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.ByteArrayOutputStream;
23 import java.io.DataInputStream;
24 import java.io.DataOutputStream;
25 import java.io.IOException;
26
27 /**
28 * This class describes the information for an attribute
29 * @author Mike Toggweiler
30 **/
31 public class AttributeInfo {
32 private Attribute attribute;
33
34 /**
35 * Creates a AttributeInfo and initializes it from a DataInputStream
36 * @param dis the DataInputStream to read from
37 * @param cp the constant pool to resolve indices
38 **/
39 protected AttributeInfo(DataInputStream dis, ConstantPool cp)
40 throws IOException {
41 short nameIndex = (short)dis.readUnsignedShort();
42 UTF8CPEntry utf = (UTF8CPEntry)cp.getCPEntryAt(nameIndex);
43 String name = utf.getString();
44 int aCount = dis.readInt();
45
46 if (name.equals(ConstantValueAttribute.NAME)) {
47 attribute = new ConstantValueAttribute(dis, cp, aCount);
48 }
49 else if (name.equals(CodeAttribute.NAME)) {
50 attribute = new CodeAttribute(dis, cp, aCount);
51 }
52 else if (name.equals(ExceptionsAttribute.NAME)) {
53 attribute = new ExceptionsAttribute(dis, cp, aCount);
54 }
55 else if (name.equals(InnerClassesAttribute.NAME)) {
56 attribute = new InnerClassesAttribute(dis, cp, aCount);
57 }
58 else if (name.equals(SyntheticAttribute.NAME)) {
59 attribute = new SyntheticAttribute(dis, cp, aCount);
60 }
61 else if (name.equals(SourceFileAttribute.NAME)) {
62 attribute = new SourceFileAttribute(dis, cp, aCount);
63 }
64 else if (name.equals(LineNumberTableAttribute.NAME)) {
65 attribute = new LineNumberTableAttribute(dis, cp, aCount);
66 }
67 else if (name.equals(LocalVariableTableAttribute.NAME)) {
68 attribute = new LocalVariableTableAttribute(dis, cp, aCount);
69 }
70 else if (name.equals(DeprecatedAttribute.NAME)) {
71 attribute = new DeprecatedAttribute(dis, cp, aCount);
72 }
73 else {
74 //default attribute
75 attribute = new DefaultAttribute(dis, cp, aCount, name);
76 }
77 }
78
79 /**
80 * Construct a new attribute initializes with the
81 * appropriate attribute.
82 * @param attribute the attribute
83 **/
84 public AttributeInfo(Attribute attribute) {
85 this.attribute = attribute;
86 }
87
88 /**
89 * Write the constant pool information into a DataOutputStream
90 * @param dos the DataOutputStream to write on
91 * @param cp the constant pool to store variables
92 **/
93 protected final void write(DataOutputStream dos, ConstantPool cp)
94 throws IOException {
95 //register name
96 dos.writeShort(cp.addCPEntry(new UTF8CPEntry(attribute.getName())));
97
98 ByteArrayOutputStream baos = new ByteArrayOutputStream();
99 DataOutputStream dos2 = new DataOutputStream(baos);
100
101 //write the attribute information onto a temporary dataoutputstream
102 attribute.writeAttribute(dos2, cp);
103
104 byte[] info = baos.toByteArray();
105
106 //write attribute count
107 dos.writeInt(info.length);
108 //write the attribute information
109 dos.write(info);
110
111 dos2.close();
112 baos.close();
113 }
114
115 /**
116 * @return the attribute encapsulated into this AttributeInfo
117 **/
118 public Attribute getAttribute() {
119 return attribute;
120 }
121
122 /**
123 * Set a new attribute to this attributeinfo
124 **/
125 public void setAttribute(Attribute attr) {
126 attribute = attr;
127 }
128
129 /**
130 * Write in a readable way
131 **/
132 public String toString() {
133 return attribute.toString();
134 }
135 }