src/share/classes/com/sun/tools/classfile/ClassWriter.java

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 485
b0a68258360a
child 581
f2fdd52e4e87
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

jjg@308 1
jjg@46 2 /*
ohair@554 3 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
jjg@46 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@46 5 *
jjg@46 6 * This code is free software; you can redistribute it and/or modify it
jjg@46 7 * under the terms of the GNU General Public License version 2 only, as
ohair@554 8 * published by the Free Software Foundation. Oracle designates this
jjg@46 9 * particular file as subject to the "Classpath" exception as provided
ohair@554 10 * by Oracle in the LICENSE file that accompanied this code.
jjg@46 11 *
jjg@46 12 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@46 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@46 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@46 15 * version 2 for more details (a copy is included in the LICENSE file that
jjg@46 16 * accompanied this code).
jjg@46 17 *
jjg@46 18 * You should have received a copy of the GNU General Public License version
jjg@46 19 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@46 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@46 21 *
ohair@554 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 23 * or visit www.oracle.com if you need additional information or have any
ohair@554 24 * questions.
jjg@46 25 */
jjg@46 26
jjg@46 27 package com.sun.tools.classfile;
jjg@46 28
jjg@46 29 import java.io.ByteArrayOutputStream;
jjg@46 30 import java.io.DataOutputStream;
jjg@46 31 import java.io.File;
jjg@46 32 import java.io.FileOutputStream;
jjg@46 33 import java.io.IOException;
jjg@46 34 import java.io.OutputStream;
jjg@46 35
jjg@46 36 import static com.sun.tools.classfile.Annotation.*;
jjg@46 37 import static com.sun.tools.classfile.ConstantPool.*;
jjg@46 38 import static com.sun.tools.classfile.StackMapTable_attribute.*;
jjg@46 39 import static com.sun.tools.classfile.StackMapTable_attribute.verification_type_info.*;
jjg@46 40
jjg@46 41 /**
jjg@46 42 * Write a ClassFile data structure to a file or stream.
jjg@46 43 *
jjg@46 44 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
jjg@46 45 * you write code that depends on this, you do so at your own risk.
jjg@46 46 * This code and its internal interfaces are subject to change or
jjg@46 47 * deletion without notice.</b>
jjg@46 48 */
jjg@46 49 public class ClassWriter {
jjg@46 50 public ClassWriter() {
jjg@46 51 attributeWriter = new AttributeWriter();
jjg@46 52 constantPoolWriter = new ConstantPoolWriter();
jjg@46 53 out = new ClassOutputStream();
jjg@46 54 }
jjg@46 55
jjg@46 56 /**
jjg@46 57 * Write a ClassFile data structure to a file.
jjg@46 58 */
jjg@46 59 public void write(ClassFile classFile, File f) throws IOException {
jjg@46 60 FileOutputStream f_out = new FileOutputStream(f);
jjg@46 61 try {
jjg@46 62 write(classFile, f_out);
jjg@46 63 } finally {
jjg@46 64 f_out.close();
jjg@46 65 }
jjg@46 66 }
jjg@46 67
jjg@46 68 /**
jjg@46 69 * Write a ClassFile data structure to a stream.
jjg@46 70 */
jjg@46 71 public void write(ClassFile classFile, OutputStream s) throws IOException {
jjg@46 72 this.classFile = classFile;
jjg@46 73 out.reset();
jjg@46 74 write();
jjg@46 75 out.writeTo(s);
jjg@46 76 }
jjg@46 77
jjg@46 78 protected void write() throws IOException {
jjg@46 79 writeHeader();
jjg@46 80 writeConstantPool();
jjg@46 81 writeAccessFlags(classFile.access_flags);
jjg@46 82 writeClassInfo();
jjg@46 83 writeFields();
jjg@46 84 writeMethods();
jjg@46 85 writeAttributes(classFile.attributes);
jjg@46 86 }
jjg@46 87
jjg@46 88 protected void writeHeader() {
jjg@46 89 out.writeInt(classFile.magic);
jjg@46 90 out.writeShort(classFile.minor_version);
jjg@46 91 out.writeShort(classFile.major_version);
jjg@46 92 }
jjg@46 93
jjg@46 94 protected void writeAccessFlags(AccessFlags flags) {
jjg@46 95 out.writeShort(flags.flags);
jjg@46 96 }
jjg@46 97
jjg@46 98 protected void writeAttributes(Attributes attributes) {
jjg@46 99 int size = attributes.size();
jjg@46 100 out.writeShort(size);
jjg@46 101 for (Attribute attr: attributes)
jjg@46 102 attributeWriter.write(attr, out);
jjg@46 103 }
jjg@46 104
jjg@46 105 protected void writeClassInfo() {
jjg@46 106 out.writeShort(classFile.this_class);
jjg@46 107 out.writeShort(classFile.super_class);
jjg@46 108 int[] interfaces = classFile.interfaces;
jjg@46 109 out.writeShort(interfaces.length);
jjg@46 110 for (int i: interfaces)
jjg@46 111 out.writeShort(i);
jjg@46 112 }
jjg@46 113
jjg@46 114 protected void writeDescriptor(Descriptor d) {
jjg@46 115 out.writeShort(d.index);
jjg@46 116 }
jjg@46 117
jjg@46 118 protected void writeConstantPool() {
jjg@46 119 ConstantPool pool = classFile.constant_pool;
jjg@46 120 int size = pool.size();
jjg@46 121 out.writeShort(size);
jjg@282 122 for (CPInfo cpInfo: pool.entries())
jjg@282 123 constantPoolWriter.write(cpInfo, out);
jjg@46 124 }
jjg@46 125
jjg@46 126 protected void writeFields() throws IOException {
jjg@46 127 Field[] fields = classFile.fields;
jjg@46 128 out.writeShort(fields.length);
jjg@46 129 for (Field f: fields)
jjg@46 130 writeField(f);
jjg@46 131 }
jjg@46 132
jjg@46 133 protected void writeField(Field f) throws IOException {
jjg@46 134 writeAccessFlags(f.access_flags);
jjg@46 135 out.writeShort(f.name_index);
jjg@46 136 writeDescriptor(f.descriptor);
jjg@46 137 writeAttributes(f.attributes);
jjg@46 138 }
jjg@46 139
jjg@46 140 protected void writeMethods() throws IOException {
jjg@46 141 Method[] methods = classFile.methods;
jjg@46 142 out.writeShort(methods.length);
jjg@46 143 for (Method m: methods) {
jjg@46 144 writeMethod(m);
jjg@46 145 }
jjg@46 146 }
jjg@46 147
jjg@46 148 protected void writeMethod(Method m) throws IOException {
jjg@46 149 writeAccessFlags(m.access_flags);
jjg@46 150 out.writeShort(m.name_index);
jjg@46 151 writeDescriptor(m.descriptor);
jjg@46 152 writeAttributes(m.attributes);
jjg@46 153 }
jjg@46 154
jjg@46 155 protected ClassFile classFile;
jjg@46 156 protected ClassOutputStream out;
jjg@46 157 protected AttributeWriter attributeWriter;
jjg@46 158 protected ConstantPoolWriter constantPoolWriter;
jjg@46 159
jjg@46 160 /**
jjg@46 161 * Subtype of ByteArrayOutputStream with the convenience methods of
jjg@46 162 * a DataOutputStream. Since ByteArrayOutputStream does not throw
jjg@46 163 * IOException, there are no exceptions from the additional
jjg@46 164 * convenience methods either,
jjg@46 165 */
jjg@46 166 protected static class ClassOutputStream extends ByteArrayOutputStream {
jjg@46 167 public ClassOutputStream() {
jjg@46 168 d = new DataOutputStream(this);
jjg@46 169 }
jjg@46 170
jjg@46 171 public void writeByte(int value) {
jjg@46 172 try {
jjg@46 173 d.writeByte(value);
jjg@46 174 } catch (IOException ignore) {
jjg@46 175 }
jjg@46 176 }
jjg@46 177
jjg@46 178 public void writeShort(int value) {
jjg@46 179 try {
jjg@46 180 d.writeShort(value);
jjg@46 181 } catch (IOException ignore) {
jjg@46 182 }
jjg@46 183 }
jjg@46 184
jjg@46 185 public void writeInt(int value) {
jjg@46 186 try {
jjg@46 187 d.writeInt(value);
jjg@46 188 } catch (IOException ignore) {
jjg@46 189 }
jjg@46 190 }
jjg@46 191
jjg@46 192 public void writeLong(long value) {
jjg@46 193 try {
jjg@46 194 d.writeLong(value);
jjg@46 195 } catch (IOException ignore) {
jjg@46 196 }
jjg@46 197 }
jjg@46 198
jjg@46 199 public void writeFloat(float value) {
jjg@46 200 try {
jjg@46 201 d.writeFloat(value);
jjg@46 202 } catch (IOException ignore) {
jjg@46 203 }
jjg@46 204 }
jjg@46 205
jjg@46 206 public void writeDouble(double value) {
jjg@46 207 try {
jjg@46 208 d.writeDouble(value);
jjg@46 209 } catch (IOException ignore) {
jjg@46 210 }
jjg@46 211 }
jjg@46 212
jjg@46 213 public void writeUTF(String value) {
jjg@46 214 try {
jjg@46 215 d.writeUTF(value);
jjg@46 216 } catch (IOException ignore) {
jjg@46 217 }
jjg@46 218 }
jjg@46 219
jjg@46 220 public void writeTo(ClassOutputStream s) {
jjg@46 221 try {
jjg@46 222 super.writeTo(s);
jjg@46 223 } catch (IOException ignore) {
jjg@46 224 }
jjg@46 225 }
jjg@46 226
jjg@46 227 private DataOutputStream d;
jjg@46 228 }
jjg@46 229
jjg@46 230 /**
jjg@46 231 * Writer for the entries in the constant pool.
jjg@46 232 */
jjg@46 233 protected static class ConstantPoolWriter
jjg@46 234 implements ConstantPool.Visitor<Integer,ClassOutputStream> {
jjg@46 235 protected int write(CPInfo info, ClassOutputStream out) {
jjg@46 236 out.writeByte(info.getTag());
jjg@46 237 return info.accept(this, out);
jjg@46 238 }
jjg@46 239
jjg@46 240 public Integer visitClass(CONSTANT_Class_info info, ClassOutputStream out) {
jjg@46 241 out.writeShort(info.name_index);
jjg@46 242 return 1;
jjg@46 243 }
jjg@46 244
jjg@46 245 public Integer visitDouble(CONSTANT_Double_info info, ClassOutputStream out) {
jjg@46 246 out.writeDouble(info.value);
jjg@46 247 return 2;
jjg@46 248 }
jjg@46 249
jjg@46 250 public Integer visitFieldref(CONSTANT_Fieldref_info info, ClassOutputStream out) {
jjg@46 251 writeRef(info, out);
jjg@46 252 return 1;
jjg@46 253 }
jjg@46 254
jjg@46 255 public Integer visitFloat(CONSTANT_Float_info info, ClassOutputStream out) {
jjg@46 256 out.writeFloat(info.value);
jjg@46 257 return 1;
jjg@46 258 }
jjg@46 259
jjg@46 260 public Integer visitInteger(CONSTANT_Integer_info info, ClassOutputStream out) {
jjg@46 261 out.writeInt(info.value);
jjg@46 262 return 1;
jjg@46 263 }
jjg@46 264
jjg@46 265 public Integer visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, ClassOutputStream out) {
jjg@46 266 writeRef(info, out);
jjg@46 267 return 1;
jjg@46 268 }
jjg@46 269
jjg@46 270 public Integer visitLong(CONSTANT_Long_info info, ClassOutputStream out) {
jjg@46 271 out.writeLong(info.value);
jjg@46 272 return 2;
jjg@46 273 }
jjg@46 274
jjg@46 275 public Integer visitNameAndType(CONSTANT_NameAndType_info info, ClassOutputStream out) {
jjg@46 276 out.writeShort(info.name_index);
jjg@46 277 out.writeShort(info.type_index);
jjg@46 278 return 1;
jjg@46 279 }
jjg@46 280
jjg@46 281 public Integer visitMethodref(CONSTANT_Methodref_info info, ClassOutputStream out) {
jjg@46 282 return writeRef(info, out);
jjg@46 283 }
jjg@46 284
jjg@46 285 public Integer visitString(CONSTANT_String_info info, ClassOutputStream out) {
jjg@46 286 out.writeShort(info.string_index);
jjg@46 287 return 1;
jjg@46 288 }
jjg@46 289
jjg@46 290 public Integer visitUtf8(CONSTANT_Utf8_info info, ClassOutputStream out) {
jjg@46 291 out.writeUTF(info.value);
jjg@46 292 return 1;
jjg@46 293 }
jjg@46 294
jjg@46 295 protected Integer writeRef(CPRefInfo info, ClassOutputStream out) {
jjg@46 296 out.writeShort(info.class_index);
jjg@46 297 out.writeShort(info.name_and_type_index);
jjg@46 298 return 1;
jjg@46 299 }
jjg@46 300 }
jjg@46 301
jjg@46 302 /**
jjg@46 303 * Writer for the different types of attribute.
jjg@46 304 */
jjg@46 305 protected static class AttributeWriter implements Attribute.Visitor<Void,ClassOutputStream> {
jjg@46 306 public void write(Attributes attributes, ClassOutputStream out) {
jjg@46 307 int size = attributes.size();
jjg@46 308 out.writeShort(size);
jjg@46 309 for (Attribute a: attributes)
jjg@46 310 write(a, out);
jjg@46 311 }
jjg@46 312
jjg@46 313 // Note: due to the use of shared resources, this method is not reentrant.
jjg@46 314 public void write(Attribute attr, ClassOutputStream out) {
jjg@46 315 out.writeShort(attr.attribute_name_index);
jjg@46 316 sharedOut.reset();
jjg@46 317 attr.accept(this, sharedOut);
jjg@46 318 out.writeInt(sharedOut.size());
jjg@46 319 sharedOut.writeTo(out);
jjg@46 320 }
jjg@46 321
jjg@46 322 protected ClassOutputStream sharedOut = new ClassOutputStream();
jjg@46 323 protected AnnotationWriter annotationWriter = new AnnotationWriter();
jjg@46 324
jjg@46 325 public Void visitDefault(DefaultAttribute attr, ClassOutputStream out) {
jjg@46 326 out.write(attr.info, 0, attr.info.length);
jjg@46 327 return null;
jjg@46 328 }
jjg@46 329
jjg@46 330 public Void visitAnnotationDefault(AnnotationDefault_attribute attr, ClassOutputStream out) {
jjg@46 331 annotationWriter.write(attr.default_value, out);
jjg@46 332 return null;
jjg@46 333 }
jjg@46 334
jjg@46 335 public Void visitCharacterRangeTable(CharacterRangeTable_attribute attr, ClassOutputStream out) {
jjg@46 336 out.writeShort(attr.character_range_table.length);
jjg@46 337 for (CharacterRangeTable_attribute.Entry e: attr.character_range_table)
jjg@46 338 writeCharacterRangeTableEntry(e, out);
jjg@46 339 return null;
jjg@46 340 }
jjg@46 341
jjg@46 342 protected void writeCharacterRangeTableEntry(CharacterRangeTable_attribute.Entry entry, ClassOutputStream out) {
jjg@46 343 out.writeShort(entry.start_pc);
jjg@46 344 out.writeShort(entry.end_pc);
jjg@46 345 out.writeInt(entry.character_range_start);
jjg@46 346 out.writeInt(entry.character_range_end);
jjg@46 347 out.writeShort(entry.flags);
jjg@46 348 }
jjg@46 349
jjg@46 350 public Void visitCode(Code_attribute attr, ClassOutputStream out) {
jjg@46 351 out.writeShort(attr.max_stack);
jjg@46 352 out.writeShort(attr.max_locals);
jjg@46 353 out.writeInt(attr.code.length);
jjg@46 354 out.write(attr.code, 0, attr.code.length);
jjg@46 355 out.writeShort(attr.exception_table.length);
jjg@46 356 for (Code_attribute.Exception_data e: attr.exception_table)
jjg@46 357 writeExceptionTableEntry(e, out);
jjg@46 358 new AttributeWriter().write(attr.attributes, out);
jjg@46 359 return null;
jjg@46 360 }
jjg@46 361
jjg@46 362 protected void writeExceptionTableEntry(Code_attribute.Exception_data exception_data, ClassOutputStream out) {
jjg@46 363 out.writeShort(exception_data.start_pc);
jjg@46 364 out.writeShort(exception_data.end_pc);
jjg@46 365 out.writeShort(exception_data.handler_pc);
jjg@46 366 out.writeShort(exception_data.catch_type);
jjg@46 367 }
jjg@46 368
jjg@46 369 public Void visitCompilationID(CompilationID_attribute attr, ClassOutputStream out) {
jjg@46 370 out.writeShort(attr.compilationID_index);
jjg@46 371 return null;
jjg@46 372 }
jjg@46 373
jjg@46 374 public Void visitConstantValue(ConstantValue_attribute attr, ClassOutputStream out) {
jjg@46 375 out.writeShort(attr.constantvalue_index);
jjg@46 376 return null;
jjg@46 377 }
jjg@46 378
jjg@46 379 public Void visitDeprecated(Deprecated_attribute attr, ClassOutputStream out) {
jjg@46 380 return null;
jjg@46 381 }
jjg@46 382
jjg@46 383 public Void visitEnclosingMethod(EnclosingMethod_attribute attr, ClassOutputStream out) {
jjg@46 384 out.writeShort(attr.class_index);
jjg@46 385 out.writeShort(attr.method_index);
jjg@46 386 return null;
jjg@46 387 }
jjg@46 388
jjg@46 389 public Void visitExceptions(Exceptions_attribute attr, ClassOutputStream out) {
jjg@46 390 out.writeShort(attr.exception_index_table.length);
jjg@46 391 for (int i: attr.exception_index_table)
jjg@46 392 out.writeShort(i);
jjg@46 393 return null;
jjg@46 394 }
jjg@46 395
jjg@46 396 public Void visitInnerClasses(InnerClasses_attribute attr, ClassOutputStream out) {
jjg@46 397 out.writeShort(attr.classes.length);
jjg@46 398 for (InnerClasses_attribute.Info info: attr.classes)
jjg@46 399 writeInnerClassesInfo(info, out);
jjg@46 400 return null;
jjg@46 401 }
jjg@46 402
jjg@46 403 protected void writeInnerClassesInfo(InnerClasses_attribute.Info info, ClassOutputStream out) {
jjg@46 404 out.writeShort(info.inner_class_info_index);
jjg@46 405 out.writeShort(info.outer_class_info_index);
jjg@46 406 out.writeShort(info.inner_name_index);
jjg@46 407 writeAccessFlags(info.inner_class_access_flags, out);
jjg@46 408 }
jjg@46 409
jjg@46 410 public Void visitLineNumberTable(LineNumberTable_attribute attr, ClassOutputStream out) {
jjg@46 411 out.writeShort(attr.line_number_table.length);
jjg@46 412 for (LineNumberTable_attribute.Entry e: attr.line_number_table)
jjg@46 413 writeLineNumberTableEntry(e, out);
jjg@46 414 return null;
jjg@46 415 }
jjg@46 416
jjg@46 417 protected void writeLineNumberTableEntry(LineNumberTable_attribute.Entry entry, ClassOutputStream out) {
jjg@46 418 out.writeShort(entry.start_pc);
jjg@46 419 out.writeShort(entry.line_number);
jjg@46 420 }
jjg@46 421
jjg@46 422 public Void visitLocalVariableTable(LocalVariableTable_attribute attr, ClassOutputStream out) {
jjg@46 423 out.writeShort(attr.local_variable_table.length);
jjg@46 424 for (LocalVariableTable_attribute.Entry e: attr.local_variable_table)
jjg@46 425 writeLocalVariableTableEntry(e, out);
jjg@46 426 return null;
jjg@46 427 }
jjg@46 428
jjg@46 429 protected void writeLocalVariableTableEntry(LocalVariableTable_attribute.Entry entry, ClassOutputStream out) {
jjg@46 430 out.writeShort(entry.start_pc);
jjg@46 431 out.writeShort(entry.length);
jjg@46 432 out.writeShort(entry.name_index);
jjg@46 433 out.writeShort(entry.descriptor_index);
jjg@46 434 out.writeShort(entry.index);
jjg@46 435 }
jjg@46 436
jjg@46 437 public Void visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, ClassOutputStream out) {
jjg@352 438 out.writeShort(attr.local_variable_table.length);
jjg@46 439 for (LocalVariableTypeTable_attribute.Entry e: attr.local_variable_table)
jjg@46 440 writeLocalVariableTypeTableEntry(e, out);
jjg@46 441 return null;
jjg@46 442 }
jjg@46 443
jjg@46 444 protected void writeLocalVariableTypeTableEntry(LocalVariableTypeTable_attribute.Entry entry, ClassOutputStream out) {
jjg@46 445 out.writeShort(entry.start_pc);
jjg@46 446 out.writeShort(entry.length);
jjg@46 447 out.writeShort(entry.name_index);
jjg@46 448 out.writeShort(entry.signature_index);
jjg@46 449 out.writeShort(entry.index);
jjg@46 450 }
jjg@46 451
jjg@46 452 public Void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, ClassOutputStream out) {
jjg@46 453 annotationWriter.write(attr.annotations, out);
jjg@46 454 return null;
jjg@46 455 }
jjg@46 456
jjg@46 457 public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, ClassOutputStream out) {
jjg@46 458 annotationWriter.write(attr.annotations, out);
jjg@46 459 return null;
jjg@46 460 }
jjg@46 461
jjg@308 462 public Void visitRuntimeVisibleTypeAnnotations(RuntimeVisibleTypeAnnotations_attribute attr, ClassOutputStream out) {
jjg@308 463 annotationWriter.write(attr.annotations, out);
jjg@308 464 return null;
jjg@308 465 }
jjg@308 466
jjg@308 467 public Void visitRuntimeInvisibleTypeAnnotations(RuntimeInvisibleTypeAnnotations_attribute attr, ClassOutputStream out) {
jjg@308 468 annotationWriter.write(attr.annotations, out);
jjg@308 469 return null;
jjg@308 470 }
jjg@308 471
jjg@46 472 public Void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, ClassOutputStream out) {
jjg@46 473 out.writeByte(attr.parameter_annotations.length);
jjg@46 474 for (Annotation[] annos: attr.parameter_annotations)
jjg@46 475 annotationWriter.write(annos, out);
jjg@46 476 return null;
jjg@46 477 }
jjg@46 478
jjg@46 479 public Void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, ClassOutputStream out) {
jjg@46 480 out.writeByte(attr.parameter_annotations.length);
jjg@46 481 for (Annotation[] annos: attr.parameter_annotations)
jjg@46 482 annotationWriter.write(annos, out);
jjg@46 483 return null;
jjg@46 484 }
jjg@46 485
jjg@46 486 public Void visitSignature(Signature_attribute attr, ClassOutputStream out) {
jjg@46 487 out.writeShort(attr.signature_index);
jjg@46 488 return null;
jjg@46 489 }
jjg@46 490
jjg@46 491 public Void visitSourceDebugExtension(SourceDebugExtension_attribute attr, ClassOutputStream out) {
jjg@46 492 out.write(attr.debug_extension, 0, attr.debug_extension.length);
jjg@46 493 return null;
jjg@46 494 }
jjg@46 495
jjg@46 496 public Void visitSourceFile(SourceFile_attribute attr, ClassOutputStream out) {
jjg@46 497 out.writeShort(attr.sourcefile_index);
jjg@46 498 return null;
jjg@46 499 }
jjg@46 500
jjg@46 501 public Void visitSourceID(SourceID_attribute attr, ClassOutputStream out) {
jjg@46 502 out.writeShort(attr.sourceID_index);
jjg@46 503 return null;
jjg@46 504 }
jjg@46 505
jjg@46 506 public Void visitStackMap(StackMap_attribute attr, ClassOutputStream out) {
jjg@46 507 if (stackMapWriter == null)
jjg@46 508 stackMapWriter = new StackMapTableWriter();
jjg@46 509
jjg@46 510 out.writeShort(attr.entries.length);
jjg@46 511 for (stack_map_frame f: attr.entries)
jjg@46 512 stackMapWriter.write(f, out);
jjg@46 513 return null;
jjg@46 514 }
jjg@46 515
jjg@46 516 public Void visitStackMapTable(StackMapTable_attribute attr, ClassOutputStream out) {
jjg@46 517 if (stackMapWriter == null)
jjg@46 518 stackMapWriter = new StackMapTableWriter();
jjg@46 519
jjg@46 520 out.writeShort(attr.entries.length);
jjg@46 521 for (stack_map_frame f: attr.entries)
jjg@46 522 stackMapWriter.write(f, out);
jjg@46 523 return null;
jjg@46 524 }
jjg@46 525
jjg@46 526 public Void visitSynthetic(Synthetic_attribute attr, ClassOutputStream out) {
jjg@46 527 return null;
jjg@46 528 }
jjg@46 529
jjg@46 530 protected void writeAccessFlags(AccessFlags flags, ClassOutputStream p) {
jjg@46 531 sharedOut.writeShort(flags.flags);
jjg@46 532 }
jjg@46 533
jjg@46 534 protected StackMapTableWriter stackMapWriter;
jjg@46 535 }
jjg@46 536
jjg@46 537 /**
jjg@46 538 * Writer for the frames of StackMap and StackMapTable attributes.
jjg@46 539 */
jjg@46 540 protected static class StackMapTableWriter
jjg@46 541 implements stack_map_frame.Visitor<Void,ClassOutputStream> {
jjg@46 542
jjg@46 543 public void write(stack_map_frame frame, ClassOutputStream out) {
jjg@46 544 out.write(frame.frame_type);
jjg@46 545 frame.accept(this, out);
jjg@46 546 }
jjg@46 547
jjg@46 548 public Void visit_same_frame(same_frame frame, ClassOutputStream p) {
jjg@46 549 return null;
jjg@46 550 }
jjg@46 551
jjg@46 552 public Void visit_same_locals_1_stack_item_frame(same_locals_1_stack_item_frame frame, ClassOutputStream out) {
jjg@46 553 writeVerificationTypeInfo(frame.stack[0], out);
jjg@46 554 return null;
jjg@46 555 }
jjg@46 556
jjg@46 557 public Void visit_same_locals_1_stack_item_frame_extended(same_locals_1_stack_item_frame_extended frame, ClassOutputStream out) {
jjg@46 558 out.writeShort(frame.offset_delta);
jjg@46 559 writeVerificationTypeInfo(frame.stack[0], out);
jjg@46 560 return null;
jjg@46 561 }
jjg@46 562
jjg@46 563 public Void visit_chop_frame(chop_frame frame, ClassOutputStream out) {
jjg@46 564 out.writeShort(frame.offset_delta);
jjg@46 565 return null;
jjg@46 566 }
jjg@46 567
jjg@46 568 public Void visit_same_frame_extended(same_frame_extended frame, ClassOutputStream out) {
jjg@46 569 out.writeShort(frame.offset_delta);
jjg@46 570 return null;
jjg@46 571 }
jjg@46 572
jjg@46 573 public Void visit_append_frame(append_frame frame, ClassOutputStream out) {
jjg@46 574 out.writeShort(frame.offset_delta);
jjg@46 575 for (verification_type_info l: frame.locals)
jjg@46 576 writeVerificationTypeInfo(l, out);
jjg@46 577 return null;
jjg@46 578 }
jjg@46 579
jjg@46 580 public Void visit_full_frame(full_frame frame, ClassOutputStream out) {
jjg@46 581 out.writeShort(frame.offset_delta);
jjg@46 582 out.writeShort(frame.locals.length);
jjg@46 583 for (verification_type_info l: frame.locals)
jjg@46 584 writeVerificationTypeInfo(l, out);
jjg@46 585 out.writeShort(frame.stack.length);
jjg@46 586 for (verification_type_info s: frame.stack)
jjg@46 587 writeVerificationTypeInfo(s, out);
jjg@46 588 return null;
jjg@46 589 }
jjg@46 590
jjg@46 591 protected void writeVerificationTypeInfo(verification_type_info info,
jjg@46 592 ClassOutputStream out) {
jjg@46 593 out.write(info.tag);
jjg@46 594 switch (info.tag) {
jjg@46 595 case ITEM_Top:
jjg@46 596 case ITEM_Integer:
jjg@46 597 case ITEM_Float:
jjg@46 598 case ITEM_Long:
jjg@46 599 case ITEM_Double:
jjg@46 600 case ITEM_Null:
jjg@46 601 case ITEM_UninitializedThis:
jjg@46 602 break;
jjg@46 603
jjg@46 604 case ITEM_Object:
jjg@46 605 Object_variable_info o = (Object_variable_info) info;
jjg@46 606 out.writeShort(o.cpool_index);
jjg@46 607 break;
jjg@46 608
jjg@46 609 case ITEM_Uninitialized:
jjg@46 610 Uninitialized_variable_info u = (Uninitialized_variable_info) info;
jjg@46 611 out.writeShort(u.offset);
jjg@46 612 break;
jjg@46 613
jjg@46 614 default:
jjg@46 615 throw new Error();
jjg@46 616 }
jjg@46 617 }
jjg@46 618 }
jjg@46 619
jjg@46 620 /**
jjg@46 621 * Writer for annotations and the values they contain.
jjg@46 622 */
jjg@46 623 protected static class AnnotationWriter
jjg@46 624 implements Annotation.element_value.Visitor<Void,ClassOutputStream> {
jjg@46 625 public void write(Annotation[] annos, ClassOutputStream out) {
jjg@46 626 out.writeShort(annos.length);
jjg@46 627 for (Annotation anno: annos)
jjg@46 628 write(anno, out);
jjg@46 629 }
jjg@46 630
jjg@308 631 public void write(ExtendedAnnotation[] annos, ClassOutputStream out) {
jjg@308 632 out.writeShort(annos.length);
jjg@308 633 for (ExtendedAnnotation anno: annos)
jjg@308 634 write(anno, out);
jjg@308 635 }
jjg@308 636
jjg@46 637 public void write(Annotation anno, ClassOutputStream out) {
jjg@46 638 out.writeShort(anno.type_index);
jjg@46 639 out.writeShort(anno.element_value_pairs.length);
jjg@46 640 for (element_value_pair p: anno.element_value_pairs)
jjg@46 641 write(p, out);
jjg@46 642 }
jjg@46 643
jjg@308 644 public void write(ExtendedAnnotation anno, ClassOutputStream out) {
jjg@308 645 write(anno.annotation, out);
jjg@308 646 write(anno.position, out);
jjg@308 647 }
jjg@308 648
jjg@46 649 public void write(element_value_pair pair, ClassOutputStream out) {
jjg@46 650 out.writeShort(pair.element_name_index);
jjg@46 651 write(pair.value, out);
jjg@46 652 }
jjg@46 653
jjg@46 654 public void write(element_value ev, ClassOutputStream out) {
jjg@46 655 out.writeByte(ev.tag);
jjg@46 656 ev.accept(this, out);
jjg@46 657 }
jjg@46 658
jjg@46 659 public Void visitPrimitive(Primitive_element_value ev, ClassOutputStream out) {
jjg@46 660 out.writeShort(ev.const_value_index);
jjg@46 661 return null;
jjg@46 662 }
jjg@46 663
jjg@46 664 public Void visitEnum(Enum_element_value ev, ClassOutputStream out) {
jjg@46 665 out.writeShort(ev.type_name_index);
jjg@46 666 out.writeShort(ev.const_name_index);
jjg@46 667 return null;
jjg@46 668 }
jjg@46 669
jjg@46 670 public Void visitClass(Class_element_value ev, ClassOutputStream out) {
jjg@46 671 out.writeShort(ev.class_info_index);
jjg@46 672 return null;
jjg@46 673 }
jjg@46 674
jjg@46 675 public Void visitAnnotation(Annotation_element_value ev, ClassOutputStream out) {
jjg@46 676 write(ev.annotation_value, out);
jjg@46 677 return null;
jjg@46 678 }
jjg@46 679
jjg@46 680 public Void visitArray(Array_element_value ev, ClassOutputStream out) {
jjg@46 681 out.writeShort(ev.num_values);
jjg@46 682 for (element_value v: ev.values)
jjg@46 683 write(v, out);
jjg@46 684 return null;
jjg@46 685 }
jjg@308 686
jjg@308 687 private void write(ExtendedAnnotation.Position p, ClassOutputStream out) {
jjg@308 688 out.writeByte(p.type.targetTypeValue());
jjg@308 689 switch (p.type) {
jjg@308 690 // type case
jjg@308 691 case TYPECAST:
jjg@308 692 case TYPECAST_GENERIC_OR_ARRAY:
jjg@308 693 // object creation
jjg@308 694 case INSTANCEOF:
jjg@308 695 case INSTANCEOF_GENERIC_OR_ARRAY:
jjg@308 696 // new expression
jjg@308 697 case NEW:
jjg@308 698 case NEW_GENERIC_OR_ARRAY:
jjg@308 699 case NEW_TYPE_ARGUMENT:
jjg@308 700 case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@308 701 out.writeShort(p.offset);
jjg@308 702 break;
jjg@308 703 // local variable
jjg@308 704 case LOCAL_VARIABLE:
jjg@308 705 case LOCAL_VARIABLE_GENERIC_OR_ARRAY:
jjg@308 706 int table_length = p.lvarOffset.length;
jjg@308 707 out.writeShort(table_length);
jjg@308 708 for (int i = 0; i < table_length; ++i) {
jjg@308 709 out.writeShort(1); // for table length
jjg@308 710 out.writeShort(p.lvarOffset[i]);
jjg@308 711 out.writeShort(p.lvarLength[i]);
jjg@308 712 out.writeShort(p.lvarIndex[i]);
jjg@308 713 }
jjg@308 714 break;
jjg@308 715 // method receiver
jjg@308 716 case METHOD_RECEIVER:
jjg@308 717 // Do nothing
jjg@308 718 break;
jjg@308 719 // type parameters
jjg@308 720 case CLASS_TYPE_PARAMETER:
jjg@308 721 case METHOD_TYPE_PARAMETER:
jjg@308 722 out.writeByte(p.parameter_index);
jjg@308 723 break;
jjg@308 724 // type parameters bounds
jjg@308 725 case CLASS_TYPE_PARAMETER_BOUND:
jjg@308 726 case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
jjg@308 727 case METHOD_TYPE_PARAMETER_BOUND:
jjg@308 728 case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
jjg@308 729 out.writeByte(p.parameter_index);
jjg@308 730 out.writeByte(p.bound_index);
jjg@308 731 break;
jjg@308 732 // wildcards
jjg@308 733 case WILDCARD_BOUND:
jjg@308 734 case WILDCARD_BOUND_GENERIC_OR_ARRAY:
jjg@308 735 write(p.wildcard_position, out);
jjg@308 736 break;
jjg@308 737 // Class extends and implements clauses
jjg@308 738 case CLASS_EXTENDS:
jjg@308 739 case CLASS_EXTENDS_GENERIC_OR_ARRAY:
jjg@308 740 out.writeByte(p.type_index);
jjg@308 741 break;
jjg@308 742 // throws
jjg@308 743 case THROWS:
jjg@308 744 out.writeByte(p.type_index);
jjg@308 745 break;
jjg@308 746 case CLASS_LITERAL:
jjg@485 747 case CLASS_LITERAL_GENERIC_OR_ARRAY:
jjg@308 748 out.writeShort(p.offset);
jjg@308 749 break;
jjg@308 750 // method parameter: not specified
jjg@308 751 case METHOD_PARAMETER_GENERIC_OR_ARRAY:
jjg@308 752 out.writeByte(p.parameter_index);
jjg@308 753 break;
jjg@308 754 // method type argument: wasn't specified
jjg@308 755 case METHOD_TYPE_ARGUMENT:
jjg@308 756 case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@308 757 out.writeShort(p.offset);
jjg@308 758 out.writeByte(p.type_index);
jjg@308 759 break;
jjg@308 760 // We don't need to worry abut these
jjg@308 761 case METHOD_RETURN_GENERIC_OR_ARRAY:
jjg@308 762 case FIELD_GENERIC_OR_ARRAY:
jjg@308 763 break;
jjg@308 764 case UNKNOWN:
jjg@308 765 break;
jjg@308 766 default:
jjg@308 767 throw new AssertionError("unknown type: " + p);
jjg@308 768 }
jjg@308 769
jjg@308 770 // Append location data for generics/arrays.
jjg@308 771 if (p.type.hasLocation()) {
jjg@308 772 out.writeShort(p.location.size());
jjg@308 773 for (int i : p.location)
jjg@308 774 out.writeByte((byte)i);
jjg@308 775 }
jjg@308 776 }
jjg@46 777 }
jjg@46 778 }

mercurial