Tue, 13 Oct 2009 15:26:30 -0700
6891079: Compiler allows invalid binary literals 0b and oBL
Reviewed-by: darcy
jjg@308 | 1 | |
jjg@46 | 2 | /* |
jjg@46 | 3 | * Copyright 2008 Sun Microsystems, Inc. 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 |
jjg@46 | 8 | * published by the Free Software Foundation. Sun designates this |
jjg@46 | 9 | * particular file as subject to the "Classpath" exception as provided |
jjg@46 | 10 | * by Sun 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 | * |
jjg@46 | 22 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
jjg@46 | 23 | * CA 95054 USA or visit www.sun.com if you need additional information or |
jjg@46 | 24 | * have any 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 visitModule(Module_attribute attr, ClassOutputStream out) { |
jjg@46 | 453 | out.writeShort(attr.module_name); |
jjg@46 | 454 | return null; |
jjg@46 | 455 | } |
jjg@46 | 456 | |
jjg@46 | 457 | public Void visitModuleExportTable(ModuleExportTable_attribute attr, ClassOutputStream out) { |
jjg@46 | 458 | out.writeShort(attr.export_type_table.length); |
jjg@46 | 459 | for (int i: attr.export_type_table) |
jjg@46 | 460 | out.writeShort(i); |
jjg@46 | 461 | return null; |
jjg@46 | 462 | } |
jjg@46 | 463 | |
jjg@46 | 464 | public Void visitModuleMemberTable(ModuleMemberTable_attribute attr, ClassOutputStream out) { |
jjg@46 | 465 | out.writeShort(attr.package_member_table.length); |
jjg@46 | 466 | for (int i: attr.package_member_table) |
jjg@46 | 467 | out.writeShort(i); |
jjg@46 | 468 | return null; |
jjg@46 | 469 | } |
jjg@46 | 470 | |
jjg@46 | 471 | public Void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, ClassOutputStream out) { |
jjg@46 | 472 | annotationWriter.write(attr.annotations, out); |
jjg@46 | 473 | return null; |
jjg@46 | 474 | } |
jjg@46 | 475 | |
jjg@46 | 476 | public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, ClassOutputStream out) { |
jjg@46 | 477 | annotationWriter.write(attr.annotations, out); |
jjg@46 | 478 | return null; |
jjg@46 | 479 | } |
jjg@46 | 480 | |
jjg@308 | 481 | public Void visitRuntimeVisibleTypeAnnotations(RuntimeVisibleTypeAnnotations_attribute attr, ClassOutputStream out) { |
jjg@308 | 482 | annotationWriter.write(attr.annotations, out); |
jjg@308 | 483 | return null; |
jjg@308 | 484 | } |
jjg@308 | 485 | |
jjg@308 | 486 | public Void visitRuntimeInvisibleTypeAnnotations(RuntimeInvisibleTypeAnnotations_attribute attr, ClassOutputStream out) { |
jjg@308 | 487 | annotationWriter.write(attr.annotations, out); |
jjg@308 | 488 | return null; |
jjg@308 | 489 | } |
jjg@308 | 490 | |
jjg@46 | 491 | public Void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, ClassOutputStream out) { |
jjg@46 | 492 | out.writeByte(attr.parameter_annotations.length); |
jjg@46 | 493 | for (Annotation[] annos: attr.parameter_annotations) |
jjg@46 | 494 | annotationWriter.write(annos, out); |
jjg@46 | 495 | return null; |
jjg@46 | 496 | } |
jjg@46 | 497 | |
jjg@46 | 498 | public Void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, ClassOutputStream out) { |
jjg@46 | 499 | out.writeByte(attr.parameter_annotations.length); |
jjg@46 | 500 | for (Annotation[] annos: attr.parameter_annotations) |
jjg@46 | 501 | annotationWriter.write(annos, out); |
jjg@46 | 502 | return null; |
jjg@46 | 503 | } |
jjg@46 | 504 | |
jjg@46 | 505 | public Void visitSignature(Signature_attribute attr, ClassOutputStream out) { |
jjg@46 | 506 | out.writeShort(attr.signature_index); |
jjg@46 | 507 | return null; |
jjg@46 | 508 | } |
jjg@46 | 509 | |
jjg@46 | 510 | public Void visitSourceDebugExtension(SourceDebugExtension_attribute attr, ClassOutputStream out) { |
jjg@46 | 511 | out.write(attr.debug_extension, 0, attr.debug_extension.length); |
jjg@46 | 512 | return null; |
jjg@46 | 513 | } |
jjg@46 | 514 | |
jjg@46 | 515 | public Void visitSourceFile(SourceFile_attribute attr, ClassOutputStream out) { |
jjg@46 | 516 | out.writeShort(attr.sourcefile_index); |
jjg@46 | 517 | return null; |
jjg@46 | 518 | } |
jjg@46 | 519 | |
jjg@46 | 520 | public Void visitSourceID(SourceID_attribute attr, ClassOutputStream out) { |
jjg@46 | 521 | out.writeShort(attr.sourceID_index); |
jjg@46 | 522 | return null; |
jjg@46 | 523 | } |
jjg@46 | 524 | |
jjg@46 | 525 | public Void visitStackMap(StackMap_attribute attr, ClassOutputStream out) { |
jjg@46 | 526 | if (stackMapWriter == null) |
jjg@46 | 527 | stackMapWriter = new StackMapTableWriter(); |
jjg@46 | 528 | |
jjg@46 | 529 | out.writeShort(attr.entries.length); |
jjg@46 | 530 | for (stack_map_frame f: attr.entries) |
jjg@46 | 531 | stackMapWriter.write(f, out); |
jjg@46 | 532 | return null; |
jjg@46 | 533 | } |
jjg@46 | 534 | |
jjg@46 | 535 | public Void visitStackMapTable(StackMapTable_attribute attr, ClassOutputStream out) { |
jjg@46 | 536 | if (stackMapWriter == null) |
jjg@46 | 537 | stackMapWriter = new StackMapTableWriter(); |
jjg@46 | 538 | |
jjg@46 | 539 | out.writeShort(attr.entries.length); |
jjg@46 | 540 | for (stack_map_frame f: attr.entries) |
jjg@46 | 541 | stackMapWriter.write(f, out); |
jjg@46 | 542 | return null; |
jjg@46 | 543 | } |
jjg@46 | 544 | |
jjg@46 | 545 | public Void visitSynthetic(Synthetic_attribute attr, ClassOutputStream out) { |
jjg@46 | 546 | return null; |
jjg@46 | 547 | } |
jjg@46 | 548 | |
jjg@46 | 549 | protected void writeAccessFlags(AccessFlags flags, ClassOutputStream p) { |
jjg@46 | 550 | sharedOut.writeShort(flags.flags); |
jjg@46 | 551 | } |
jjg@46 | 552 | |
jjg@46 | 553 | protected StackMapTableWriter stackMapWriter; |
jjg@46 | 554 | } |
jjg@46 | 555 | |
jjg@46 | 556 | /** |
jjg@46 | 557 | * Writer for the frames of StackMap and StackMapTable attributes. |
jjg@46 | 558 | */ |
jjg@46 | 559 | protected static class StackMapTableWriter |
jjg@46 | 560 | implements stack_map_frame.Visitor<Void,ClassOutputStream> { |
jjg@46 | 561 | |
jjg@46 | 562 | public void write(stack_map_frame frame, ClassOutputStream out) { |
jjg@46 | 563 | out.write(frame.frame_type); |
jjg@46 | 564 | frame.accept(this, out); |
jjg@46 | 565 | } |
jjg@46 | 566 | |
jjg@46 | 567 | public Void visit_same_frame(same_frame frame, ClassOutputStream p) { |
jjg@46 | 568 | return null; |
jjg@46 | 569 | } |
jjg@46 | 570 | |
jjg@46 | 571 | public Void visit_same_locals_1_stack_item_frame(same_locals_1_stack_item_frame frame, ClassOutputStream out) { |
jjg@46 | 572 | writeVerificationTypeInfo(frame.stack[0], out); |
jjg@46 | 573 | return null; |
jjg@46 | 574 | } |
jjg@46 | 575 | |
jjg@46 | 576 | public Void visit_same_locals_1_stack_item_frame_extended(same_locals_1_stack_item_frame_extended frame, ClassOutputStream out) { |
jjg@46 | 577 | out.writeShort(frame.offset_delta); |
jjg@46 | 578 | writeVerificationTypeInfo(frame.stack[0], out); |
jjg@46 | 579 | return null; |
jjg@46 | 580 | } |
jjg@46 | 581 | |
jjg@46 | 582 | public Void visit_chop_frame(chop_frame frame, ClassOutputStream out) { |
jjg@46 | 583 | out.writeShort(frame.offset_delta); |
jjg@46 | 584 | return null; |
jjg@46 | 585 | } |
jjg@46 | 586 | |
jjg@46 | 587 | public Void visit_same_frame_extended(same_frame_extended frame, ClassOutputStream out) { |
jjg@46 | 588 | out.writeShort(frame.offset_delta); |
jjg@46 | 589 | return null; |
jjg@46 | 590 | } |
jjg@46 | 591 | |
jjg@46 | 592 | public Void visit_append_frame(append_frame frame, ClassOutputStream out) { |
jjg@46 | 593 | out.writeShort(frame.offset_delta); |
jjg@46 | 594 | for (verification_type_info l: frame.locals) |
jjg@46 | 595 | writeVerificationTypeInfo(l, out); |
jjg@46 | 596 | return null; |
jjg@46 | 597 | } |
jjg@46 | 598 | |
jjg@46 | 599 | public Void visit_full_frame(full_frame frame, ClassOutputStream out) { |
jjg@46 | 600 | out.writeShort(frame.offset_delta); |
jjg@46 | 601 | out.writeShort(frame.locals.length); |
jjg@46 | 602 | for (verification_type_info l: frame.locals) |
jjg@46 | 603 | writeVerificationTypeInfo(l, out); |
jjg@46 | 604 | out.writeShort(frame.stack.length); |
jjg@46 | 605 | for (verification_type_info s: frame.stack) |
jjg@46 | 606 | writeVerificationTypeInfo(s, out); |
jjg@46 | 607 | return null; |
jjg@46 | 608 | } |
jjg@46 | 609 | |
jjg@46 | 610 | protected void writeVerificationTypeInfo(verification_type_info info, |
jjg@46 | 611 | ClassOutputStream out) { |
jjg@46 | 612 | out.write(info.tag); |
jjg@46 | 613 | switch (info.tag) { |
jjg@46 | 614 | case ITEM_Top: |
jjg@46 | 615 | case ITEM_Integer: |
jjg@46 | 616 | case ITEM_Float: |
jjg@46 | 617 | case ITEM_Long: |
jjg@46 | 618 | case ITEM_Double: |
jjg@46 | 619 | case ITEM_Null: |
jjg@46 | 620 | case ITEM_UninitializedThis: |
jjg@46 | 621 | break; |
jjg@46 | 622 | |
jjg@46 | 623 | case ITEM_Object: |
jjg@46 | 624 | Object_variable_info o = (Object_variable_info) info; |
jjg@46 | 625 | out.writeShort(o.cpool_index); |
jjg@46 | 626 | break; |
jjg@46 | 627 | |
jjg@46 | 628 | case ITEM_Uninitialized: |
jjg@46 | 629 | Uninitialized_variable_info u = (Uninitialized_variable_info) info; |
jjg@46 | 630 | out.writeShort(u.offset); |
jjg@46 | 631 | break; |
jjg@46 | 632 | |
jjg@46 | 633 | default: |
jjg@46 | 634 | throw new Error(); |
jjg@46 | 635 | } |
jjg@46 | 636 | } |
jjg@46 | 637 | } |
jjg@46 | 638 | |
jjg@46 | 639 | /** |
jjg@46 | 640 | * Writer for annotations and the values they contain. |
jjg@46 | 641 | */ |
jjg@46 | 642 | protected static class AnnotationWriter |
jjg@46 | 643 | implements Annotation.element_value.Visitor<Void,ClassOutputStream> { |
jjg@46 | 644 | public void write(Annotation[] annos, ClassOutputStream out) { |
jjg@46 | 645 | out.writeShort(annos.length); |
jjg@46 | 646 | for (Annotation anno: annos) |
jjg@46 | 647 | write(anno, out); |
jjg@46 | 648 | } |
jjg@46 | 649 | |
jjg@308 | 650 | public void write(ExtendedAnnotation[] annos, ClassOutputStream out) { |
jjg@308 | 651 | out.writeShort(annos.length); |
jjg@308 | 652 | for (ExtendedAnnotation anno: annos) |
jjg@308 | 653 | write(anno, out); |
jjg@308 | 654 | } |
jjg@308 | 655 | |
jjg@46 | 656 | public void write(Annotation anno, ClassOutputStream out) { |
jjg@46 | 657 | out.writeShort(anno.type_index); |
jjg@46 | 658 | out.writeShort(anno.element_value_pairs.length); |
jjg@46 | 659 | for (element_value_pair p: anno.element_value_pairs) |
jjg@46 | 660 | write(p, out); |
jjg@46 | 661 | } |
jjg@46 | 662 | |
jjg@308 | 663 | public void write(ExtendedAnnotation anno, ClassOutputStream out) { |
jjg@308 | 664 | write(anno.annotation, out); |
jjg@308 | 665 | write(anno.position, out); |
jjg@308 | 666 | } |
jjg@308 | 667 | |
jjg@46 | 668 | public void write(element_value_pair pair, ClassOutputStream out) { |
jjg@46 | 669 | out.writeShort(pair.element_name_index); |
jjg@46 | 670 | write(pair.value, out); |
jjg@46 | 671 | } |
jjg@46 | 672 | |
jjg@46 | 673 | public void write(element_value ev, ClassOutputStream out) { |
jjg@46 | 674 | out.writeByte(ev.tag); |
jjg@46 | 675 | ev.accept(this, out); |
jjg@46 | 676 | } |
jjg@46 | 677 | |
jjg@46 | 678 | public Void visitPrimitive(Primitive_element_value ev, ClassOutputStream out) { |
jjg@46 | 679 | out.writeShort(ev.const_value_index); |
jjg@46 | 680 | return null; |
jjg@46 | 681 | } |
jjg@46 | 682 | |
jjg@46 | 683 | public Void visitEnum(Enum_element_value ev, ClassOutputStream out) { |
jjg@46 | 684 | out.writeShort(ev.type_name_index); |
jjg@46 | 685 | out.writeShort(ev.const_name_index); |
jjg@46 | 686 | return null; |
jjg@46 | 687 | } |
jjg@46 | 688 | |
jjg@46 | 689 | public Void visitClass(Class_element_value ev, ClassOutputStream out) { |
jjg@46 | 690 | out.writeShort(ev.class_info_index); |
jjg@46 | 691 | return null; |
jjg@46 | 692 | } |
jjg@46 | 693 | |
jjg@46 | 694 | public Void visitAnnotation(Annotation_element_value ev, ClassOutputStream out) { |
jjg@46 | 695 | write(ev.annotation_value, out); |
jjg@46 | 696 | return null; |
jjg@46 | 697 | } |
jjg@46 | 698 | |
jjg@46 | 699 | public Void visitArray(Array_element_value ev, ClassOutputStream out) { |
jjg@46 | 700 | out.writeShort(ev.num_values); |
jjg@46 | 701 | for (element_value v: ev.values) |
jjg@46 | 702 | write(v, out); |
jjg@46 | 703 | return null; |
jjg@46 | 704 | } |
jjg@308 | 705 | |
jjg@308 | 706 | private void write(ExtendedAnnotation.Position p, ClassOutputStream out) { |
jjg@308 | 707 | out.writeByte(p.type.targetTypeValue()); |
jjg@308 | 708 | switch (p.type) { |
jjg@308 | 709 | // type case |
jjg@308 | 710 | case TYPECAST: |
jjg@308 | 711 | case TYPECAST_GENERIC_OR_ARRAY: |
jjg@308 | 712 | // object creation |
jjg@308 | 713 | case INSTANCEOF: |
jjg@308 | 714 | case INSTANCEOF_GENERIC_OR_ARRAY: |
jjg@308 | 715 | // new expression |
jjg@308 | 716 | case NEW: |
jjg@308 | 717 | case NEW_GENERIC_OR_ARRAY: |
jjg@308 | 718 | case NEW_TYPE_ARGUMENT: |
jjg@308 | 719 | case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY: |
jjg@308 | 720 | out.writeShort(p.offset); |
jjg@308 | 721 | break; |
jjg@308 | 722 | // local variable |
jjg@308 | 723 | case LOCAL_VARIABLE: |
jjg@308 | 724 | case LOCAL_VARIABLE_GENERIC_OR_ARRAY: |
jjg@308 | 725 | int table_length = p.lvarOffset.length; |
jjg@308 | 726 | out.writeShort(table_length); |
jjg@308 | 727 | for (int i = 0; i < table_length; ++i) { |
jjg@308 | 728 | out.writeShort(1); // for table length |
jjg@308 | 729 | out.writeShort(p.lvarOffset[i]); |
jjg@308 | 730 | out.writeShort(p.lvarLength[i]); |
jjg@308 | 731 | out.writeShort(p.lvarIndex[i]); |
jjg@308 | 732 | } |
jjg@308 | 733 | break; |
jjg@308 | 734 | // method receiver |
jjg@308 | 735 | case METHOD_RECEIVER: |
jjg@308 | 736 | // Do nothing |
jjg@308 | 737 | break; |
jjg@308 | 738 | // type parameters |
jjg@308 | 739 | case CLASS_TYPE_PARAMETER: |
jjg@308 | 740 | case METHOD_TYPE_PARAMETER: |
jjg@308 | 741 | out.writeByte(p.parameter_index); |
jjg@308 | 742 | break; |
jjg@308 | 743 | // type parameters bounds |
jjg@308 | 744 | case CLASS_TYPE_PARAMETER_BOUND: |
jjg@308 | 745 | case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY: |
jjg@308 | 746 | case METHOD_TYPE_PARAMETER_BOUND: |
jjg@308 | 747 | case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY: |
jjg@308 | 748 | out.writeByte(p.parameter_index); |
jjg@308 | 749 | out.writeByte(p.bound_index); |
jjg@308 | 750 | break; |
jjg@308 | 751 | // wildcards |
jjg@308 | 752 | case WILDCARD_BOUND: |
jjg@308 | 753 | case WILDCARD_BOUND_GENERIC_OR_ARRAY: |
jjg@308 | 754 | write(p.wildcard_position, out); |
jjg@308 | 755 | break; |
jjg@308 | 756 | // Class extends and implements clauses |
jjg@308 | 757 | case CLASS_EXTENDS: |
jjg@308 | 758 | case CLASS_EXTENDS_GENERIC_OR_ARRAY: |
jjg@308 | 759 | out.writeByte(p.type_index); |
jjg@308 | 760 | break; |
jjg@308 | 761 | // throws |
jjg@308 | 762 | case THROWS: |
jjg@308 | 763 | out.writeByte(p.type_index); |
jjg@308 | 764 | break; |
jjg@308 | 765 | case CLASS_LITERAL: |
jjg@308 | 766 | out.writeShort(p.offset); |
jjg@308 | 767 | break; |
jjg@308 | 768 | // method parameter: not specified |
jjg@308 | 769 | case METHOD_PARAMETER_GENERIC_OR_ARRAY: |
jjg@308 | 770 | out.writeByte(p.parameter_index); |
jjg@308 | 771 | break; |
jjg@308 | 772 | // method type argument: wasn't specified |
jjg@308 | 773 | case METHOD_TYPE_ARGUMENT: |
jjg@308 | 774 | case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY: |
jjg@308 | 775 | out.writeShort(p.offset); |
jjg@308 | 776 | out.writeByte(p.type_index); |
jjg@308 | 777 | break; |
jjg@308 | 778 | // We don't need to worry abut these |
jjg@308 | 779 | case METHOD_RETURN_GENERIC_OR_ARRAY: |
jjg@308 | 780 | case FIELD_GENERIC_OR_ARRAY: |
jjg@308 | 781 | break; |
jjg@308 | 782 | case UNKNOWN: |
jjg@308 | 783 | break; |
jjg@308 | 784 | default: |
jjg@308 | 785 | throw new AssertionError("unknown type: " + p); |
jjg@308 | 786 | } |
jjg@308 | 787 | |
jjg@308 | 788 | // Append location data for generics/arrays. |
jjg@308 | 789 | if (p.type.hasLocation()) { |
jjg@308 | 790 | out.writeShort(p.location.size()); |
jjg@308 | 791 | for (int i : p.location) |
jjg@308 | 792 | out.writeByte((byte)i); |
jjg@308 | 793 | } |
jjg@308 | 794 | } |
jjg@46 | 795 | } |
jjg@46 | 796 | } |