test/tools/javac/lambdaShapes/org/openjdk/tests/separate/ClassFile.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1448
7d34e91f66bb
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package org.openjdk.tests.separate;
aoqi@0 27
aoqi@0 28 import java.io.*;
aoqi@0 29 import java.util.*;
aoqi@0 30
aoqi@0 31 class CfInputStream extends ByteArrayInputStream {
aoqi@0 32 private int ct;
aoqi@0 33 public CfInputStream(byte[] input) {
aoqi@0 34 super(input);
aoqi@0 35 }
aoqi@0 36
aoqi@0 37 byte u1() { return (byte)read(); }
aoqi@0 38 short u2() {
aoqi@0 39 int b0 = read() << 8;
aoqi@0 40 int b1 = read();
aoqi@0 41 return (short)(b0 | b1);
aoqi@0 42 }
aoqi@0 43 int u4() {
aoqi@0 44 int b0 = read() << 24;
aoqi@0 45 int b1 = read() << 16;
aoqi@0 46 int b2 = read() << 8;
aoqi@0 47 int b3 = read();
aoqi@0 48 return b0 | b1 | b2 | b3;
aoqi@0 49 }
aoqi@0 50 byte[] array(int count) {
aoqi@0 51 byte[] ret = new byte[count];
aoqi@0 52 read(ret, 0, count);
aoqi@0 53 return ret;
aoqi@0 54 }
aoqi@0 55 };
aoqi@0 56
aoqi@0 57 class CfOutputStream extends ByteArrayOutputStream {
aoqi@0 58 void u1(byte b) { write((int)b); }
aoqi@0 59 void u2(short s) {
aoqi@0 60 write((s >> 8) & 0xff);
aoqi@0 61 write(s & 0xff);
aoqi@0 62 }
aoqi@0 63 void u4(int i) {
aoqi@0 64 write((i >> 24) & 0xff);
aoqi@0 65 write((i >> 16) & 0xff);
aoqi@0 66 write((i >> 8) & 0xff);
aoqi@0 67 write(i & 0xff);
aoqi@0 68 }
aoqi@0 69 void array(byte[] a) {
aoqi@0 70 write(a, 0, a.length);
aoqi@0 71 }
aoqi@0 72
aoqi@0 73 public byte[] toByteArray() { return super.toByteArray(); }
aoqi@0 74 };
aoqi@0 75
aoqi@0 76 // A quick and dirty class file parser and representation
aoqi@0 77 public class ClassFile {
aoqi@0 78
aoqi@0 79 int magic;
aoqi@0 80 short minor_version;
aoqi@0 81 short major_version;
aoqi@0 82 ArrayList<CpEntry> constant_pool;
aoqi@0 83 short access_flags;
aoqi@0 84 short this_class;
aoqi@0 85 short super_class;
aoqi@0 86 ArrayList<Interface> interfaces;
aoqi@0 87 ArrayList<Field> fields;
aoqi@0 88 ArrayList<Method> methods;
aoqi@0 89 ArrayList<Attribute> attributes;
aoqi@0 90
aoqi@0 91 ClassFile(byte[] cf) {
aoqi@0 92 CfInputStream in = new CfInputStream(cf);
aoqi@0 93
aoqi@0 94 magic = in.u4();
aoqi@0 95 minor_version = in.u2();
aoqi@0 96 major_version = in.u2();
aoqi@0 97
aoqi@0 98 short cpCount = in.u2();
aoqi@0 99 constant_pool = new ArrayList<>();
aoqi@0 100 constant_pool.add(new CpNull());
aoqi@0 101 for (int i = 1; i < cpCount; ++i) {
aoqi@0 102 constant_pool.add(CpEntry.newCpEntry(in));
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 access_flags = in.u2();
aoqi@0 106 this_class = in.u2();
aoqi@0 107 super_class = in.u2();
aoqi@0 108
aoqi@0 109 short ifaceCount = in.u2();
aoqi@0 110 interfaces = new ArrayList<>();
aoqi@0 111 for (int i = 0; i < ifaceCount; ++i) {
aoqi@0 112 interfaces.add(new Interface(in));
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 short fieldCount = in.u2();
aoqi@0 116 fields = new ArrayList<>();
aoqi@0 117 for (int i = 0; i < fieldCount; ++i) {
aoqi@0 118 fields.add(new Field(in));
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 short methodCount = in.u2();
aoqi@0 122 methods = new ArrayList<>();
aoqi@0 123 for (int i = 0; i < methodCount; ++i) {
aoqi@0 124 methods.add(new Method(in));
aoqi@0 125 }
aoqi@0 126
aoqi@0 127 short attributeCount = in.u2();
aoqi@0 128 attributes = new ArrayList<>();
aoqi@0 129 for (int i = 0; i < attributeCount; ++i) {
aoqi@0 130 attributes.add(new Attribute(in));
aoqi@0 131 }
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 byte[] toByteArray() {
aoqi@0 135 CfOutputStream out = new CfOutputStream();
aoqi@0 136
aoqi@0 137 out.u4(magic);
aoqi@0 138 out.u2(minor_version);
aoqi@0 139 out.u2(major_version);
aoqi@0 140
aoqi@0 141 out.u2((short)(constant_pool.size()));
aoqi@0 142 for (CpEntry cp : constant_pool) {
aoqi@0 143 cp.write(out);
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 out.u2(access_flags);
aoqi@0 147 out.u2(this_class);
aoqi@0 148 out.u2(super_class);
aoqi@0 149
aoqi@0 150 out.u2((short)interfaces.size());
aoqi@0 151 for (Interface iface : interfaces) {
aoqi@0 152 iface.write(out);
aoqi@0 153 }
aoqi@0 154
aoqi@0 155 out.u2((short)fields.size());
aoqi@0 156 for (Field field : fields) {
aoqi@0 157 field.write(out);
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 out.u2((short)methods.size());
aoqi@0 161 for (Method method : methods) {
aoqi@0 162 method.write(out);
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 out.u2((short)attributes.size());
aoqi@0 166 for (Attribute attribute : attributes) {
aoqi@0 167 attribute.write(out);
aoqi@0 168 }
aoqi@0 169
aoqi@0 170 return out.toByteArray();
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 static abstract class CpEntry {
aoqi@0 174 byte tag;
aoqi@0 175
aoqi@0 176 CpEntry(byte t) { tag = t; }
aoqi@0 177 void write(CfOutputStream out) {
aoqi@0 178 out.u1(tag);
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 static CpEntry newCpEntry(CfInputStream in) {
aoqi@0 182 byte tag = in.u1();
aoqi@0 183 switch (tag) {
aoqi@0 184 case CpUtf8.TAG: return new CpUtf8(in);
aoqi@0 185 case CpInteger.TAG: return new CpInteger(in);
aoqi@0 186 case CpFloat.TAG: return new CpFloat(in);
aoqi@0 187 case CpLong.TAG: return new CpLong(in);
aoqi@0 188 case CpDouble.TAG: return new CpDouble(in);
aoqi@0 189 case CpClass.TAG: return new CpClass(in);
aoqi@0 190 case CpString.TAG: return new CpString(in);
aoqi@0 191 case CpFieldRef.TAG: return new CpFieldRef(in);
aoqi@0 192 case CpMethodRef.TAG: return new CpMethodRef(in);
aoqi@0 193 case CpInterfaceMethodRef.TAG:
aoqi@0 194 return new CpInterfaceMethodRef(in);
aoqi@0 195 case CpNameAndType.TAG: return new CpNameAndType(in);
aoqi@0 196 case CpMethodHandle.TAG: return new CpMethodHandle(in);
aoqi@0 197 case CpMethodType.TAG: return new CpMethodType(in);
aoqi@0 198 case CpInvokeDynamic.TAG: return new CpInvokeDynamic(in);
aoqi@0 199 default: throw new RuntimeException("Bad cp entry tag: " + tag);
aoqi@0 200 }
aoqi@0 201 }
aoqi@0 202 }
aoqi@0 203
aoqi@0 204 static class CpNull extends CpEntry {
aoqi@0 205 CpNull() { super((byte)0); }
aoqi@0 206 CpNull(CfInputStream in) { super((byte)0); }
aoqi@0 207 void write(CfOutputStream out) {}
aoqi@0 208 }
aoqi@0 209
aoqi@0 210 static class CpUtf8 extends CpEntry {
aoqi@0 211 static final byte TAG = 1;
aoqi@0 212 byte[] bytes;
aoqi@0 213
aoqi@0 214 CpUtf8() { super(TAG); }
aoqi@0 215 CpUtf8(CfInputStream in) {
aoqi@0 216 this();
aoqi@0 217 short length = in.u2();
aoqi@0 218 bytes = in.array(length);
aoqi@0 219 }
aoqi@0 220 void write(CfOutputStream out) {
aoqi@0 221 super.write(out);
aoqi@0 222 out.u2((short)bytes.length);
aoqi@0 223 out.array(bytes);
aoqi@0 224 }
aoqi@0 225 }
aoqi@0 226
aoqi@0 227 static class CpU4Constant extends CpEntry {
aoqi@0 228 byte[] bytes;
aoqi@0 229
aoqi@0 230 CpU4Constant(byte tag) { super(tag); }
aoqi@0 231 CpU4Constant(byte tag, CfInputStream in) {
aoqi@0 232 this(tag);
aoqi@0 233 bytes = in.array(4);
aoqi@0 234 }
aoqi@0 235 void write(CfOutputStream out) { super.write(out); out.array(bytes); }
aoqi@0 236 }
aoqi@0 237 static class CpInteger extends CpU4Constant {
aoqi@0 238 static final byte TAG = 3;
aoqi@0 239 CpInteger() { super(TAG); }
aoqi@0 240 CpInteger(CfInputStream in) { super(TAG, in); }
aoqi@0 241 }
aoqi@0 242 static class CpFloat extends CpU4Constant {
aoqi@0 243 static final byte TAG = 4;
aoqi@0 244 CpFloat() { super(TAG); }
aoqi@0 245 CpFloat(CfInputStream in) { super(TAG, in); }
aoqi@0 246 }
aoqi@0 247
aoqi@0 248 static class CpU8Constant extends CpEntry {
aoqi@0 249 byte[] bytes;
aoqi@0 250
aoqi@0 251 CpU8Constant(byte tag) { super(tag); }
aoqi@0 252 CpU8Constant(byte tag, CfInputStream in) {
aoqi@0 253 this(tag);
aoqi@0 254 bytes = in.array(8);
aoqi@0 255 }
aoqi@0 256 void write(CfOutputStream out) { super.write(out); out.array(bytes); }
aoqi@0 257 }
aoqi@0 258 static class CpLong extends CpU8Constant {
aoqi@0 259 static final byte TAG = 5;
aoqi@0 260 CpLong() { super(TAG); }
aoqi@0 261 CpLong(CfInputStream in) { super(TAG, in); }
aoqi@0 262 }
aoqi@0 263 static class CpDouble extends CpU8Constant {
aoqi@0 264 static final byte TAG = 6;
aoqi@0 265 CpDouble() { super(TAG); }
aoqi@0 266 CpDouble(CfInputStream in) { super(TAG, in); }
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 static class CpClass extends CpEntry {
aoqi@0 270 static final byte TAG = 7;
aoqi@0 271 short name_index;
aoqi@0 272
aoqi@0 273 CpClass() { super(TAG); }
aoqi@0 274 CpClass(CfInputStream in) { super(TAG); name_index = in.u2(); }
aoqi@0 275 void write(CfOutputStream out) {
aoqi@0 276 super.write(out);
aoqi@0 277 out.u2(name_index);
aoqi@0 278 }
aoqi@0 279 }
aoqi@0 280
aoqi@0 281 static class CpString extends CpEntry {
aoqi@0 282 static final byte TAG = 8;
aoqi@0 283 short string_index;
aoqi@0 284
aoqi@0 285 CpString() { super(TAG); }
aoqi@0 286 CpString(CfInputStream in) { super(TAG); string_index = in.u2(); }
aoqi@0 287 void write(CfOutputStream out) {
aoqi@0 288 super.write(out);
aoqi@0 289 out.u2(string_index);
aoqi@0 290 }
aoqi@0 291 }
aoqi@0 292
aoqi@0 293 static class CpRef extends CpEntry {
aoqi@0 294 short class_index;
aoqi@0 295 short name_and_type_index;
aoqi@0 296
aoqi@0 297 CpRef(byte tag) { super(tag); }
aoqi@0 298 CpRef(byte tag, CfInputStream in) {
aoqi@0 299 this(tag);
aoqi@0 300 class_index = in.u2();
aoqi@0 301 name_and_type_index = in.u2();
aoqi@0 302 }
aoqi@0 303 void write(CfOutputStream out) {
aoqi@0 304 super.write(out);
aoqi@0 305 out.u2(class_index);
aoqi@0 306 out.u2(name_and_type_index);
aoqi@0 307 }
aoqi@0 308 }
aoqi@0 309 static class CpFieldRef extends CpRef {
aoqi@0 310 static final byte TAG = 9;
aoqi@0 311 CpFieldRef() { super(TAG); }
aoqi@0 312 CpFieldRef(CfInputStream in) { super(TAG, in); }
aoqi@0 313 }
aoqi@0 314 static class CpMethodRef extends CpRef {
aoqi@0 315 static final byte TAG = 10;
aoqi@0 316 CpMethodRef() { super(TAG); }
aoqi@0 317 CpMethodRef(CfInputStream in) { super(TAG, in); }
aoqi@0 318 }
aoqi@0 319 static class CpInterfaceMethodRef extends CpRef {
aoqi@0 320 static final byte TAG = 11;
aoqi@0 321 CpInterfaceMethodRef() { super(TAG); }
aoqi@0 322 CpInterfaceMethodRef(CfInputStream in) { super(TAG, in); }
aoqi@0 323 }
aoqi@0 324
aoqi@0 325 static class CpNameAndType extends CpEntry {
aoqi@0 326 static final byte TAG = 12;
aoqi@0 327 short name_index;
aoqi@0 328 short descriptor_index;
aoqi@0 329
aoqi@0 330 CpNameAndType() { super(TAG); }
aoqi@0 331 CpNameAndType(CfInputStream in) {
aoqi@0 332 this();
aoqi@0 333 name_index = in.u2();
aoqi@0 334 descriptor_index = in.u2();
aoqi@0 335 }
aoqi@0 336 void write(CfOutputStream out) {
aoqi@0 337 super.write(out);
aoqi@0 338 out.u2(name_index);
aoqi@0 339 out.u2(descriptor_index);
aoqi@0 340 }
aoqi@0 341 }
aoqi@0 342
aoqi@0 343 static class CpMethodHandle extends CpEntry {
aoqi@0 344 static final byte TAG = 15;
aoqi@0 345 byte reference_kind;
aoqi@0 346 short reference_index;
aoqi@0 347
aoqi@0 348 CpMethodHandle() { super(TAG); }
aoqi@0 349 CpMethodHandle(CfInputStream in) {
aoqi@0 350 this();
aoqi@0 351 reference_kind = in.u1();
aoqi@0 352 reference_index = in.u2();
aoqi@0 353 }
aoqi@0 354 void write(CfOutputStream out) {
aoqi@0 355 super.write(out);
aoqi@0 356 out.u1(reference_kind);
aoqi@0 357 out.u2(reference_index);
aoqi@0 358 }
aoqi@0 359 }
aoqi@0 360
aoqi@0 361 static class CpMethodType extends CpEntry {
aoqi@0 362 static final byte TAG = 16;
aoqi@0 363 short descriptor_index;
aoqi@0 364
aoqi@0 365 CpMethodType() { super(TAG); }
aoqi@0 366 CpMethodType(CfInputStream in) {
aoqi@0 367 this();
aoqi@0 368 descriptor_index = in.u2();
aoqi@0 369 }
aoqi@0 370 void write(CfOutputStream out) {
aoqi@0 371 super.write(out);
aoqi@0 372 out.u2(descriptor_index);
aoqi@0 373 }
aoqi@0 374 }
aoqi@0 375
aoqi@0 376 static class CpInvokeDynamic extends CpEntry {
aoqi@0 377 static final byte TAG = 18;
aoqi@0 378 short bootstrap_index;
aoqi@0 379 short name_and_type_index;
aoqi@0 380
aoqi@0 381 CpInvokeDynamic() { super(TAG); }
aoqi@0 382 CpInvokeDynamic(CfInputStream in) {
aoqi@0 383 this();
aoqi@0 384 bootstrap_index = in.u2();
aoqi@0 385 name_and_type_index = in.u2();
aoqi@0 386 }
aoqi@0 387 void write(CfOutputStream out) {
aoqi@0 388 super.write(out);
aoqi@0 389 out.u2(bootstrap_index);
aoqi@0 390 out.u2(name_and_type_index);
aoqi@0 391 }
aoqi@0 392 }
aoqi@0 393
aoqi@0 394 static class Interface {
aoqi@0 395 short index;
aoqi@0 396
aoqi@0 397 Interface() {}
aoqi@0 398 Interface(CfInputStream in) { index = in.u2(); }
aoqi@0 399 void write(CfOutputStream out) { out.u2(index); }
aoqi@0 400 }
aoqi@0 401
aoqi@0 402 static class FieldOrMethod {
aoqi@0 403 short access_flags;
aoqi@0 404 short name_index;
aoqi@0 405 short descriptor_index;
aoqi@0 406 ArrayList<Attribute> attributes;
aoqi@0 407
aoqi@0 408 FieldOrMethod() { attributes = new ArrayList<>(); }
aoqi@0 409 FieldOrMethod(CfInputStream in) {
aoqi@0 410 access_flags = in.u2();
aoqi@0 411 name_index = in.u2();
aoqi@0 412 descriptor_index = in.u2();
aoqi@0 413
aoqi@0 414 short attrCount = in.u2();
aoqi@0 415 attributes = new ArrayList<>();
aoqi@0 416 for (int i = 0; i < attrCount; ++i) {
aoqi@0 417 attributes.add(new Attribute(in));
aoqi@0 418 }
aoqi@0 419 }
aoqi@0 420 void write(CfOutputStream out) {
aoqi@0 421 out.u2(access_flags);
aoqi@0 422 out.u2(name_index);
aoqi@0 423 out.u2(descriptor_index);
aoqi@0 424 out.u2((short)attributes.size());
aoqi@0 425 for (Attribute attribute : attributes) { attribute.write(out); }
aoqi@0 426 }
aoqi@0 427 }
aoqi@0 428
aoqi@0 429 static class Field extends FieldOrMethod {
aoqi@0 430 Field() {}
aoqi@0 431 Field(CfInputStream in) { super(in); }
aoqi@0 432 }
aoqi@0 433 static class Method extends FieldOrMethod {
aoqi@0 434 Method() {}
aoqi@0 435 Method(CfInputStream in) { super(in); }
aoqi@0 436 }
aoqi@0 437
aoqi@0 438 static class Attribute {
aoqi@0 439 short attribute_name_index;
aoqi@0 440 byte[] info;
aoqi@0 441
aoqi@0 442 Attribute() { info = new byte[0]; }
aoqi@0 443 Attribute(CfInputStream in) {
aoqi@0 444 attribute_name_index = in.u2();
aoqi@0 445 int length = in.u4();
aoqi@0 446 info = in.array(length);
aoqi@0 447 }
aoqi@0 448 void write(CfOutputStream out) {
aoqi@0 449 out.u2(attribute_name_index);
aoqi@0 450 out.u4(info.length);
aoqi@0 451 out.array(info);
aoqi@0 452 }
aoqi@0 453 }
aoqi@0 454 }

mercurial