/Users/lyon/j4p/src/classUtils/dumper/FieldInfo.java

1    package classUtils.dumper; 
2     
3     
4    import java.io.DataInputStream; 
5    import java.io.DataOutputStream; 
6    import java.io.IOException; 
7     
8     
9    /** 
10    * This class defines a FieldInfo in the class 
11    * file. Fields are used to 
12    * describe instance variables in a class. The 
13    * toString() method is 
14    * augmented by a version that takes an array of 
15    * ConstantPoolInfo 
16    * objects (a constant pool). When a constant pool 
17    * is available the 
18    * toString() method generates a declaration for 
19    * the field as it would 
20    * appear in Java source. 
21    * 
22    * @version 1.3, 16 Aug 1995 
23    */ 
24    
25    
26   public class FieldInfo { 
27    
28       short accessFlags; 
29    
30       ConstantPoolInfo name; 
31    
32       ConstantPoolInfo signature; 
33    
34       AttributeInfo attributes[]; 
35    
36    
37       public boolean read(DataInputStream di, 
38                           ConstantPoolInfo pool[]) 
39               throws IOException { 
40           int count; 
41           accessFlags = di.readShort(); 
42           name = pool[di.readShort()]; 
43           signature = pool[di.readShort()]; 
44           count = di.readShort(); 
45           if (count != 0) { 
46               attributes = 
47               new AttributeInfo[count]; 
48               for (int i = 0; i < count; i++) { 
49                   attributes[i] = 
50                   new AttributeInfo(); 
51                   if (!attributes[i].read(di, 
52                                           pool)) 
53                       return (false); 
54               } 
55           } 
56           return (true); 
57       } 
58    
59    
60       public void write(DataOutputStream dos, 
61                         ConstantPoolInfo pool[]) 
62               throws IOException, Exception { 
63           dos.writeShort(accessFlags); 
64           dos.writeShort( 
65                   ConstantPoolInfo.indexOf(name, 
66                                            pool)); 
67           dos.writeShort( 
68                   ConstantPoolInfo.indexOf( 
69                           signature, pool)); 
70           if (attributes == null) { 
71               dos.writeShort(0); 
72           } else { 
73               dos.writeShort(attributes.length); 
74               for (int i = 0; 
75                    i < attributes.length; 
76                    i++) { 
77                   attributes[i].write(dos, pool); 
78               } 
79           } 
80       } 
81    
82    
83       public String toString() { 
84           StringBuffer x = new StringBuffer(); 
85           x.append( 
86                   ClassFile.accessString( 
87                           accessFlags)); 
88           x.append( 
89                   ClassFile.typeString( 
90                           signature.toString(), 
91                           name.toString())); 
92           if (attributes != null) { 
93               x.append( 
94                       " = " + 
95                       attributes[0].toString()); 
96           } 
97           return (x.toString()); 
98       } 
99    
100   
101      public String toString( 
102              ConstantPoolInfo pool[]) { 
103          StringBuffer x = new StringBuffer(); 
104          String mytype; 
105          x.append( 
106                  ClassFile.accessString( 
107                          accessFlags)); 
108          mytype = 
109          ClassFile.typeString( 
110                  signature.toString(), 
111                  name.toString()); 
112          x.append(mytype); 
113          if (attributes != null) { 
114              if (mytype.startsWith("boolean")) { 
115                  x.append( 
116                          " " + 
117                          attributes[0].toBoolean( 
118                                  pool)); 
119              } else 
120                  x.append( 
121                          " " + 
122                          attributes[0].toString( 
123                                  pool)); 
124          } 
125          return (x.toString()); 
126      } 
127   
128  } 
129   
130