/Users/lyon/j4p/src/classUtils/dumper/MethodInfo.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 describes a Method as it is stored 
11    * in the class file. 
12    * The attribute associated with method is the 
13    * code that actually implements 
14    * the method. Since we don't need to manipulate 
15    * the byte codes directly 
16    * we leave them as an opaque chunk in the 
17    * attributes[] array. References 
18    * in the code are all references into the 
19    * constant table so when we are 
20    * modifing a class to use a different object we 
21    * needn't get into the code 
22    * level. 
23    * 
24    * @version 1.4, 16 Aug 1995 
25    */ 
26    
27    
28   public class MethodInfo { 
29    
30       short accessFlags; 
31    
32       ConstantPoolInfo name; 
33    
34       ConstantPoolInfo signature; 
35    
36       AttributeInfo attributes[]; 
37    
38    
39       /** 
40        * Read a method_info from the data stream. 
41        */ 
42       public boolean read(DataInputStream di, 
43                           ConstantPoolInfo pool[]) 
44               throws IOException { 
45           int count; 
46           accessFlags = di.readShort(); 
47           name = pool[di.readShort()]; 
48           signature = pool[di.readShort()]; 
49           count = di.readShort(); 
50           if (count != 0) { 
51               attributes = 
52               new AttributeInfo[count]; 
53               for (int i = 0; i < count; i++) { 
54                   attributes[i] = 
55                   new AttributeInfo(); // "code" 
56                   if (!attributes[i].read(di, 
57                                           pool)) { 
58                       return (false); 
59                   } 
60               } 
61           } 
62           return (true); 
63       } 
64    
65    
66       /** 
67        * Write out a method_info, do constant table 
68        * fixups on the write. 
69        */ 
70       public void write(DataOutputStream dos, 
71                         ConstantPoolInfo pool[]) 
72               throws IOException, Exception { 
73           dos.writeShort(accessFlags); 
74           dos.writeShort( 
75                   ConstantPoolInfo.indexOf(name, 
76                                            pool)); 
77           dos.writeShort( 
78                   ConstantPoolInfo.indexOf( 
79                           signature, pool)); 
80           if (attributes == null) { 
81               dos.writeShort(0); 
82           } else { 
83               dos.writeShort(attributes.length); 
84               for (int i = 0; 
85                    i < attributes.length; 
86                    i++) 
87                   attributes[i].write(dos, pool); 
88           } 
89       } 
90    
91    
92       /** 
93        * print out the method, much as you would see 
94        * it in the source 
95        * file. The string ClassName is substituted 
96        * for &LTinit&GT when 
97        * printing. 
98        */ 
99       public String toString(String className) { 
100          StringBuffer x = new StringBuffer(); 
101          boolean isArray = false; 
102          String paramSig; 
103          String returnSig; 
104          int ndx = 0; 
105          StringBuffer parameterList = new StringBuffer(); 
106          char initialParameter = 'a'; 
107          StringBuffer varName = new StringBuffer(); 
108          String s = signature.strValue; 
109          paramSig = 
110          s.substring(s.indexOf('(') + 1, 
111                      s.indexOf(')')); 
112          returnSig = 
113          s.substring(s.indexOf(')') + 1); 
114          x.append( 
115                  ClassFile.accessString( 
116                          accessFlags)); 
117   
118          /* catch constructors */ 
119   
120          if ((className != null) && 
121              (name.toString().startsWith("<init>"))) 
122              parameterList.append(className); 
123          else 
124              parameterList.append(name.toString()); 
125          parameterList.append("("); 
126          if ((paramSig.length() > 0) && 
127              paramSig.charAt(0) != 'V') { 
128              while (paramSig.length() > 0) { 
129                  varName.setLength(0); 
130                  varName.append(initialParameter); 
131                  initialParameter++; 
132                  parameterList.append( 
133                          ClassFile.typeString( 
134                                  paramSig, 
135                                  varName.toString())); 
136                  paramSig = 
137                  ClassFile.nextSig(paramSig); 
138                  if (paramSig.length() > 0) 
139                      parameterList.append(", "); 
140              } 
141          } 
142          parameterList.append(")"); 
143          x.append( 
144                  ClassFile.typeString(returnSig, 
145                                       parameterList.toString())); 
146          x.append(";"); 
147          return (x.toString()); 
148      } 
149   
150   
151      /** 
152       * Generic toString method, init method is 
153       * unchanged. 
154       */ 
155      public String toString() { 
156          return (toString((String) null)); 
157      } 
158   
159  } 
160   
161