1 4 package gnu.bytecode; 5 import java.io.PrintWriter ; 6 import java.io.PrintStream ; 7 8 12 13 public class ClassTypeWriter extends PrintWriter 14 { 15 ClassType ctype; 16 int flags; 17 boolean printConstants = true; 18 19 public ClassTypeWriter (ClassType ctype, PrintWriter stream, int flags) 20 { 21 super(stream); 22 this.ctype = ctype; 23 this.flags = flags; 24 } 25 26 public ClassTypeWriter (ClassType ctype, PrintStream stream, int flags) 27 { 28 super(stream); 29 this.ctype = ctype; 30 this.flags = flags; 31 } 32 33 public static void print (ClassType ctype, PrintWriter stream, int flags) 34 { 35 ClassTypeWriter writer = new ClassTypeWriter (ctype, stream, flags); 36 writer.print(); 37 writer.flush(); 38 } 39 40 public static void print (ClassType ctype, PrintStream stream, int flags) 41 { 42 ClassTypeWriter writer = new ClassTypeWriter (ctype, stream, flags); 43 writer.print(); 44 writer.flush(); 45 } 46 47 public void print () 48 { 49 if (printConstants) 50 printConstantPool(); 51 printClassInfo(); 52 printFields(); 53 printMethods(); 54 } 55 56 public void printAttributes (AttrContainer container) 57 { 58 for (Attribute attr = container.getAttributes(); 59 attr != null; attr = attr.next) 60 { 61 attr.print(this); 62 } 63 } 64 65 public void printClassInfo () 66 { 67 println(); 68 print("Access flags:"); 69 int modifiers = ctype.getModifiers(); 70 print(Access.toString(modifiers, 'C')); 71 println(); 72 print("This class: "); 73 printOptionalIndex(ctype.thisClassIndex); 74 printConstantTersely(ctype.thisClassIndex, 7); 75 print(" super: "); 76 if (ctype.superClassIndex == -1) 77 print("<unknown>"); 78 else if (ctype.superClassIndex == 0) 79 print("0"); 80 else 81 { 82 printOptionalIndex(ctype.superClassIndex); 83 printConstantTersely(ctype.superClassIndex, ConstantPool.CLASS); 84 } 85 println(); 86 print("Interfaces (count: "); 87 int[] interfaces = ctype.interfaceIndexes; 88 int n_interfaces = interfaces == null ? 0 : interfaces.length; 89 print(n_interfaces); 90 print("):"); 91 println(); 92 for (int i = 0; i < n_interfaces; i++) 93 { 94 print("- Implements: "); 95 int index = interfaces[i]; 96 printOptionalIndex(index); 97 printConstantTersely(index, 7); 98 println(); 99 } 100 } 101 102 public void printFields () 103 { 104 println(); 105 print("Fields (count: "); 106 print(ctype.fields_count); 107 print("):"); 108 println(); 109 int ifield = 0; 110 Field field = ctype.fields; 111 for (; field != null; ifield++, field = field.next) 112 { 113 print("Field name: "); 114 if (field.name_index != 0) 115 printOptionalIndex(field.name_index); 116 print(field.getName()); 117 print(Access.toString(field.flags, 'F')); 118 print(" Signature: "); 119 if (field.signature_index != 0) 120 printOptionalIndex(field.signature_index); 121 printSignature(field.type); 122 println(); 123 printAttributes(field); 124 } 125 } 126 127 public void printMethods() 128 { 129 println(); 130 print("Methods (count: "); 131 print(ctype.methods_count); 132 print("):"); 133 println(); 134 Method method = ctype.methods; 135 for (; method != null; method = method.next) 136 { 137 println(); 138 print("Method name:"); 139 if (method.name_index != 0) 140 printOptionalIndex(method.name_index); 141 print('\"'); 142 print(method.getName()); 143 print('\"'); 144 print(Access.toString(method.access_flags, 'M')); 145 print(" Signature: "); 146 if (method.signature_index != 0) 147 printOptionalIndex(method.signature_index); 148 print('('); 149 for (int i = 0; i < method.arg_types.length; i++) 150 { 151 if (i > 0) 152 print(','); 153 printSignature(method.arg_types[i]); 154 } 155 print(')'); 156 printSignature(method.return_type); 157 println(); 158 printAttributes(method); 159 } 160 } 161 162 final void printConstantTersely(int index, int expected_tag) 163 { 164 CpoolEntry[] pool = ctype.constants.pool; 165 CpoolEntry entry; 166 if (pool == null || index < 0 || index >= pool.length 167 || (entry = pool[index]) == null) 168 print("<invalid constant index>"); 169 else if (entry.getTag() != expected_tag) 170 { 171 print("<unexpected constant type "); 172 entry.print(this, 1); 173 print('>'); 174 } 175 else 176 entry.print(this, 0); 177 } 178 179 180 final void printConstantOperand(int index) 181 { 182 print(' '); 183 if (printConstants) 184 { 185 print('#'); 186 print(index); 187 print('='); 188 } 189 CpoolEntry[] pool = ctype.constants.pool; 190 CpoolEntry entry; 191 if (pool == null || index < 0 || index >= pool.length 192 || (entry = pool[index]) == null) 193 print("<invalid constant index>"); 194 else 195 { 196 print('<'); 197 entry.print(this, 1); 198 print('>'); 199 } 200 } 201 202 public final void printQuotedString (String string) 203 { 204 print('\"'); 205 int len = string.length(); 206 for (int i = 0; i < len; i++) 207 { 208 char ch = string.charAt(i); 209 if (ch == '\"') 210 print("\\\""); 211 else if (ch >= ' ' && ch < 127) 212 print(ch); 213 else if (ch == '\n') 214 print("\\n"); 215 else 216 { 217 print("\\u"); 218 for (int j = 4; --j >= 0; ) 219 print(Character.forDigit((ch >> (j * 4)) & 15, 16)); 220 } 221 } 222 print('\"'); 223 } 224 225 public void printConstantPool () 226 { 227 CpoolEntry[] pool = ctype.constants.pool; 228 int length = ctype.constants.count; 229 for (int i = 1; i <= length; i++) 230 { 231 CpoolEntry entry = pool[i]; 232 if (entry == null) 233 continue; 234 print('#'); 235 print(entry.index); 236 print(": "); 237 entry.print(this, 2); 238 println(); 239 } 240 } 241 242 public final void printOptionalIndex(int index) 243 { 244 if (printConstants) 245 { 246 print(index); 247 print('='); 248 } 249 } 250 251 public final void printOptionalIndex(CpoolEntry entry) 252 { 253 printOptionalIndex(entry.index); 254 } 255 256 void printName(String name) 257 { 258 print(name); 260 } 261 262 266 public final int printSignature(String sig, int pos) 267 { 268 int len = sig.length(); 269 if (pos >= len) 270 { 271 print("<empty signature>"); 272 return pos; 273 } 274 int sig_length = Type.signatureLength(sig, pos); 275 if (sig_length > 0) 276 { 277 String name = Type.signatureToName(sig.substring(pos,pos+sig_length)); 278 if (name != null) 279 { 280 print(name); 281 return pos+sig_length; 282 } 283 } 284 char c = sig.charAt(pos); 285 if (c != '(') 286 { 287 print(c); 288 return pos+1; 289 } 290 int nargs = 0; 291 pos++; 292 print(c); 293 for (;;) 294 { 295 if (pos >= len) 296 { 297 print("<truncated method signature>"); 298 return pos; 299 } 300 c = sig.charAt(pos); 301 if (c == ')') 302 { 303 pos++; 304 print(c); 305 break; 306 } 307 if (nargs++ > 0) 308 print(','); 309 pos = printSignature(sig, pos); 310 } 311 return printSignature(sig, pos); 312 313 370 } 371 372 374 public final void printSignature(String sig) 375 { 376 int pos = printSignature(sig, 0); 377 int len = sig.length(); 378 if (pos < len) 379 { 380 print("<trailing junk:"); 381 print(sig.substring(pos)); 382 print('>'); 383 } 384 } 385 386 public final void printSignature(Type type) 387 { 388 if (type == null) 389 print("<unknown type>"); 390 else 391 printSignature(type.getSignature()); 392 } 393 } 394 | Popular Tags |