Thu, 25 Sep 2014 17:09:23 -0700
8058708: java.lang.AssertionError compiling source code
Reviewed-by: jjg
duke@1 | 1 | /* |
jjg@1521 | 2 | * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.jvm; |
duke@1 | 27 | |
duke@1 | 28 | import java.io.*; |
mcimadamore@1336 | 29 | import java.util.LinkedHashMap; |
mcimadamore@1336 | 30 | import java.util.Map; |
duke@1 | 31 | import java.util.Set; |
duke@1 | 32 | import java.util.HashSet; |
duke@1 | 33 | |
duke@1 | 34 | import javax.tools.JavaFileManager; |
duke@1 | 35 | import javax.tools.FileObject; |
duke@1 | 36 | import javax.tools.JavaFileObject; |
duke@1 | 37 | |
duke@1 | 38 | import com.sun.tools.javac.code.*; |
jjg@657 | 39 | import com.sun.tools.javac.code.Attribute.RetentionPolicy; |
duke@1 | 40 | import com.sun.tools.javac.code.Symbol.*; |
duke@1 | 41 | import com.sun.tools.javac.code.Type.*; |
vromero@1452 | 42 | import com.sun.tools.javac.code.Types.UniqueType; |
jjg@415 | 43 | import com.sun.tools.javac.file.BaseFileObject; |
vromero@1452 | 44 | import com.sun.tools.javac.jvm.Pool.DynamicMethod; |
vromero@1452 | 45 | import com.sun.tools.javac.jvm.Pool.Method; |
vromero@1452 | 46 | import com.sun.tools.javac.jvm.Pool.MethodHandle; |
vromero@1452 | 47 | import com.sun.tools.javac.jvm.Pool.Variable; |
duke@1 | 48 | import com.sun.tools.javac.util.*; |
duke@1 | 49 | |
duke@1 | 50 | import static com.sun.tools.javac.code.Flags.*; |
duke@1 | 51 | import static com.sun.tools.javac.code.Kinds.*; |
jjg@1374 | 52 | import static com.sun.tools.javac.code.TypeTag.*; |
duke@1 | 53 | import static com.sun.tools.javac.jvm.UninitializedType.*; |
jjg@1157 | 54 | import static com.sun.tools.javac.main.Option.*; |
duke@1 | 55 | import static javax.tools.StandardLocation.CLASS_OUTPUT; |
duke@1 | 56 | |
duke@1 | 57 | /** This class provides operations to map an internal symbol table graph |
duke@1 | 58 | * rooted in a ClassSymbol into a classfile. |
duke@1 | 59 | * |
jjg@581 | 60 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 61 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 62 | * This code and its internal interfaces are subject to change or |
duke@1 | 63 | * deletion without notice.</b> |
duke@1 | 64 | */ |
duke@1 | 65 | public class ClassWriter extends ClassFile { |
duke@1 | 66 | protected static final Context.Key<ClassWriter> classWriterKey = |
duke@1 | 67 | new Context.Key<ClassWriter>(); |
duke@1 | 68 | |
duke@1 | 69 | private final Options options; |
duke@1 | 70 | |
duke@1 | 71 | /** Switch: verbose output. |
duke@1 | 72 | */ |
duke@1 | 73 | private boolean verbose; |
duke@1 | 74 | |
jjg@1521 | 75 | /** Switch: scramble private field names. |
duke@1 | 76 | */ |
duke@1 | 77 | private boolean scramble; |
duke@1 | 78 | |
jjg@1521 | 79 | /** Switch: scramble all field names. |
duke@1 | 80 | */ |
duke@1 | 81 | private boolean scrambleAll; |
duke@1 | 82 | |
duke@1 | 83 | /** Switch: retrofit mode. |
duke@1 | 84 | */ |
duke@1 | 85 | private boolean retrofit; |
duke@1 | 86 | |
duke@1 | 87 | /** Switch: emit source file attribute. |
duke@1 | 88 | */ |
duke@1 | 89 | private boolean emitSourceFile; |
duke@1 | 90 | |
duke@1 | 91 | /** Switch: generate CharacterRangeTable attribute. |
duke@1 | 92 | */ |
duke@1 | 93 | private boolean genCrt; |
duke@1 | 94 | |
jjg@1521 | 95 | /** Switch: describe the generated stackmap. |
duke@1 | 96 | */ |
duke@1 | 97 | boolean debugstackmap; |
duke@1 | 98 | |
duke@1 | 99 | /** |
duke@1 | 100 | * Target class version. |
duke@1 | 101 | */ |
duke@1 | 102 | private Target target; |
duke@1 | 103 | |
duke@1 | 104 | /** |
duke@1 | 105 | * Source language version. |
duke@1 | 106 | */ |
duke@1 | 107 | private Source source; |
duke@1 | 108 | |
duke@1 | 109 | /** Type utilities. */ |
duke@1 | 110 | private Types types; |
duke@1 | 111 | |
duke@1 | 112 | /** The initial sizes of the data and constant pool buffers. |
jjg@1521 | 113 | * Sizes are increased when buffers get full. |
duke@1 | 114 | */ |
duke@1 | 115 | static final int DATA_BUF_SIZE = 0x0fff0; |
duke@1 | 116 | static final int POOL_BUF_SIZE = 0x1fff0; |
duke@1 | 117 | |
duke@1 | 118 | /** An output buffer for member info. |
duke@1 | 119 | */ |
duke@1 | 120 | ByteBuffer databuf = new ByteBuffer(DATA_BUF_SIZE); |
duke@1 | 121 | |
duke@1 | 122 | /** An output buffer for the constant pool. |
duke@1 | 123 | */ |
duke@1 | 124 | ByteBuffer poolbuf = new ByteBuffer(POOL_BUF_SIZE); |
duke@1 | 125 | |
duke@1 | 126 | /** The constant pool. |
duke@1 | 127 | */ |
duke@1 | 128 | Pool pool; |
duke@1 | 129 | |
duke@1 | 130 | /** The inner classes to be written, as a set. |
duke@1 | 131 | */ |
duke@1 | 132 | Set<ClassSymbol> innerClasses; |
duke@1 | 133 | |
duke@1 | 134 | /** The inner classes to be written, as a queue where |
duke@1 | 135 | * enclosing classes come first. |
duke@1 | 136 | */ |
duke@1 | 137 | ListBuffer<ClassSymbol> innerClassesQueue; |
duke@1 | 138 | |
mcimadamore@1336 | 139 | /** The bootstrap methods to be written in the corresponding class attribute |
mcimadamore@1336 | 140 | * (one for each invokedynamic) |
mcimadamore@1336 | 141 | */ |
vromero@1452 | 142 | Map<DynamicMethod, MethodHandle> bootstrapMethods; |
mcimadamore@1336 | 143 | |
duke@1 | 144 | /** The log to use for verbose output. |
duke@1 | 145 | */ |
duke@1 | 146 | private final Log log; |
duke@1 | 147 | |
duke@1 | 148 | /** The name table. */ |
jjg@113 | 149 | private final Names names; |
duke@1 | 150 | |
duke@1 | 151 | /** Access to files. */ |
duke@1 | 152 | private final JavaFileManager fileManager; |
duke@1 | 153 | |
rfield@1587 | 154 | /** Sole signature generator */ |
rfield@1587 | 155 | private final CWSignatureGenerator signatureGen; |
rfield@1587 | 156 | |
duke@1 | 157 | /** The tags and constants used in compressed stackmap. */ |
duke@1 | 158 | static final int SAME_FRAME_SIZE = 64; |
duke@1 | 159 | static final int SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247; |
duke@1 | 160 | static final int SAME_FRAME_EXTENDED = 251; |
duke@1 | 161 | static final int FULL_FRAME = 255; |
duke@1 | 162 | static final int MAX_LOCAL_LENGTH_DIFF = 4; |
duke@1 | 163 | |
duke@1 | 164 | /** Get the ClassWriter instance for this context. */ |
duke@1 | 165 | public static ClassWriter instance(Context context) { |
duke@1 | 166 | ClassWriter instance = context.get(classWriterKey); |
duke@1 | 167 | if (instance == null) |
duke@1 | 168 | instance = new ClassWriter(context); |
duke@1 | 169 | return instance; |
duke@1 | 170 | } |
duke@1 | 171 | |
duke@1 | 172 | /** Construct a class writer, given an options table. |
duke@1 | 173 | */ |
ksrini@1309 | 174 | protected ClassWriter(Context context) { |
duke@1 | 175 | context.put(classWriterKey, this); |
duke@1 | 176 | |
duke@1 | 177 | log = Log.instance(context); |
jjg@113 | 178 | names = Names.instance(context); |
duke@1 | 179 | options = Options.instance(context); |
duke@1 | 180 | target = Target.instance(context); |
duke@1 | 181 | source = Source.instance(context); |
duke@1 | 182 | types = Types.instance(context); |
duke@1 | 183 | fileManager = context.get(JavaFileManager.class); |
rfield@1587 | 184 | signatureGen = new CWSignatureGenerator(types); |
duke@1 | 185 | |
jjg@700 | 186 | verbose = options.isSet(VERBOSE); |
jjg@700 | 187 | scramble = options.isSet("-scramble"); |
jjg@700 | 188 | scrambleAll = options.isSet("-scrambleAll"); |
jjg@700 | 189 | retrofit = options.isSet("-retrofit"); |
jjg@700 | 190 | genCrt = options.isSet(XJCOV); |
jjg@700 | 191 | debugstackmap = options.isSet("debugstackmap"); |
duke@1 | 192 | |
jjg@700 | 193 | emitSourceFile = options.isUnset(G_CUSTOM) || |
jjg@700 | 194 | options.isSet(G_CUSTOM, "source"); |
duke@1 | 195 | |
duke@1 | 196 | String dumpModFlags = options.get("dumpmodifiers"); |
duke@1 | 197 | dumpClassModifiers = |
duke@1 | 198 | (dumpModFlags != null && dumpModFlags.indexOf('c') != -1); |
duke@1 | 199 | dumpFieldModifiers = |
duke@1 | 200 | (dumpModFlags != null && dumpModFlags.indexOf('f') != -1); |
duke@1 | 201 | dumpInnerClassModifiers = |
duke@1 | 202 | (dumpModFlags != null && dumpModFlags.indexOf('i') != -1); |
duke@1 | 203 | dumpMethodModifiers = |
duke@1 | 204 | (dumpModFlags != null && dumpModFlags.indexOf('m') != -1); |
duke@1 | 205 | } |
duke@1 | 206 | |
duke@1 | 207 | /****************************************************************** |
duke@1 | 208 | * Diagnostics: dump generated class names and modifiers |
duke@1 | 209 | ******************************************************************/ |
duke@1 | 210 | |
duke@1 | 211 | /** Value of option 'dumpmodifiers' is a string |
duke@1 | 212 | * indicating which modifiers should be dumped for debugging: |
duke@1 | 213 | * 'c' -- classes |
duke@1 | 214 | * 'f' -- fields |
duke@1 | 215 | * 'i' -- innerclass attributes |
duke@1 | 216 | * 'm' -- methods |
duke@1 | 217 | * For example, to dump everything: |
duke@1 | 218 | * javac -XDdumpmodifiers=cifm MyProg.java |
duke@1 | 219 | */ |
duke@1 | 220 | private final boolean dumpClassModifiers; // -XDdumpmodifiers=c |
duke@1 | 221 | private final boolean dumpFieldModifiers; // -XDdumpmodifiers=f |
duke@1 | 222 | private final boolean dumpInnerClassModifiers; // -XDdumpmodifiers=i |
duke@1 | 223 | private final boolean dumpMethodModifiers; // -XDdumpmodifiers=m |
duke@1 | 224 | |
duke@1 | 225 | |
duke@1 | 226 | /** Return flags as a string, separated by " ". |
duke@1 | 227 | */ |
duke@1 | 228 | public static String flagNames(long flags) { |
jjg@816 | 229 | StringBuilder sbuf = new StringBuilder(); |
duke@1 | 230 | int i = 0; |
duke@1 | 231 | long f = flags & StandardFlags; |
duke@1 | 232 | while (f != 0) { |
jjg@816 | 233 | if ((f & 1) != 0) { |
jjg@816 | 234 | sbuf.append(" "); |
jjg@816 | 235 | sbuf.append(flagName[i]); |
jjg@816 | 236 | } |
duke@1 | 237 | f = f >> 1; |
duke@1 | 238 | i++; |
duke@1 | 239 | } |
duke@1 | 240 | return sbuf.toString(); |
duke@1 | 241 | } |
duke@1 | 242 | //where |
duke@1 | 243 | private final static String[] flagName = { |
duke@1 | 244 | "PUBLIC", "PRIVATE", "PROTECTED", "STATIC", "FINAL", |
duke@1 | 245 | "SUPER", "VOLATILE", "TRANSIENT", "NATIVE", "INTERFACE", |
duke@1 | 246 | "ABSTRACT", "STRICTFP"}; |
duke@1 | 247 | |
duke@1 | 248 | /****************************************************************** |
duke@1 | 249 | * Output routines |
duke@1 | 250 | ******************************************************************/ |
duke@1 | 251 | |
duke@1 | 252 | /** Write a character into given byte buffer; |
duke@1 | 253 | * byte buffer will not be grown. |
duke@1 | 254 | */ |
duke@1 | 255 | void putChar(ByteBuffer buf, int op, int x) { |
duke@1 | 256 | buf.elems[op ] = (byte)((x >> 8) & 0xFF); |
duke@1 | 257 | buf.elems[op+1] = (byte)((x ) & 0xFF); |
duke@1 | 258 | } |
duke@1 | 259 | |
duke@1 | 260 | /** Write an integer into given byte buffer; |
duke@1 | 261 | * byte buffer will not be grown. |
duke@1 | 262 | */ |
duke@1 | 263 | void putInt(ByteBuffer buf, int adr, int x) { |
duke@1 | 264 | buf.elems[adr ] = (byte)((x >> 24) & 0xFF); |
duke@1 | 265 | buf.elems[adr+1] = (byte)((x >> 16) & 0xFF); |
duke@1 | 266 | buf.elems[adr+2] = (byte)((x >> 8) & 0xFF); |
duke@1 | 267 | buf.elems[adr+3] = (byte)((x ) & 0xFF); |
duke@1 | 268 | } |
duke@1 | 269 | |
rfield@1587 | 270 | /** |
rfield@1587 | 271 | * Signature Generation |
rfield@1587 | 272 | */ |
rfield@1587 | 273 | private class CWSignatureGenerator extends Types.SignatureGenerator { |
duke@1 | 274 | |
rfield@1587 | 275 | /** |
rfield@1587 | 276 | * An output buffer for type signatures. |
rfield@1587 | 277 | */ |
rfield@1587 | 278 | ByteBuffer sigbuf = new ByteBuffer(); |
rfield@1587 | 279 | |
rfield@1587 | 280 | CWSignatureGenerator(Types types) { |
rfield@1587 | 281 | super(types); |
rfield@1587 | 282 | } |
rfield@1587 | 283 | |
rfield@1587 | 284 | /** |
rfield@1587 | 285 | * Assemble signature of given type in string buffer. |
rfield@1587 | 286 | * Check for uninitialized types before calling the general case. |
rfield@1587 | 287 | */ |
rfield@1587 | 288 | @Override |
rfield@1587 | 289 | public void assembleSig(Type type) { |
rfield@1587 | 290 | type = type.unannotatedType(); |
rfield@1587 | 291 | switch (type.getTag()) { |
rfield@1587 | 292 | case UNINITIALIZED_THIS: |
rfield@1587 | 293 | case UNINITIALIZED_OBJECT: |
rfield@1587 | 294 | // we don't yet have a spec for uninitialized types in the |
rfield@1587 | 295 | // local variable table |
rfield@1587 | 296 | assembleSig(types.erasure(((UninitializedType)type).qtype)); |
rfield@1587 | 297 | break; |
rfield@1587 | 298 | default: |
rfield@1587 | 299 | super.assembleSig(type); |
duke@1 | 300 | } |
duke@1 | 301 | } |
rfield@1587 | 302 | |
rfield@1587 | 303 | @Override |
rfield@1587 | 304 | protected void append(char ch) { |
rfield@1587 | 305 | sigbuf.appendByte(ch); |
rfield@1587 | 306 | } |
rfield@1587 | 307 | |
rfield@1587 | 308 | @Override |
rfield@1587 | 309 | protected void append(byte[] ba) { |
rfield@1587 | 310 | sigbuf.appendBytes(ba); |
rfield@1587 | 311 | } |
rfield@1587 | 312 | |
rfield@1587 | 313 | @Override |
rfield@1587 | 314 | protected void append(Name name) { |
rfield@1587 | 315 | sigbuf.appendName(name); |
rfield@1587 | 316 | } |
rfield@1587 | 317 | |
rfield@1587 | 318 | @Override |
rfield@1587 | 319 | protected void classReference(ClassSymbol c) { |
rfield@1587 | 320 | enterInner(c); |
rfield@1587 | 321 | } |
rfield@1587 | 322 | |
rfield@1587 | 323 | private void reset() { |
rfield@1587 | 324 | sigbuf.reset(); |
rfield@1587 | 325 | } |
rfield@1587 | 326 | |
rfield@1587 | 327 | private Name toName() { |
rfield@1587 | 328 | return sigbuf.toName(names); |
rfield@1587 | 329 | } |
rfield@1587 | 330 | |
rfield@1587 | 331 | private boolean isEmpty() { |
rfield@1587 | 332 | return sigbuf.length == 0; |
duke@1 | 333 | } |
duke@1 | 334 | } |
duke@1 | 335 | |
rfield@1587 | 336 | /** |
rfield@1587 | 337 | * Return signature of given type |
duke@1 | 338 | */ |
duke@1 | 339 | Name typeSig(Type type) { |
rfield@1587 | 340 | Assert.check(signatureGen.isEmpty()); |
duke@1 | 341 | //- System.out.println(" ? " + type); |
rfield@1587 | 342 | signatureGen.assembleSig(type); |
rfield@1587 | 343 | Name n = signatureGen.toName(); |
rfield@1587 | 344 | signatureGen.reset(); |
duke@1 | 345 | //- System.out.println(" " + n); |
duke@1 | 346 | return n; |
duke@1 | 347 | } |
duke@1 | 348 | |
duke@1 | 349 | /** Given a type t, return the extended class name of its erasure in |
duke@1 | 350 | * external representation. |
duke@1 | 351 | */ |
duke@1 | 352 | public Name xClassName(Type t) { |
jjg@1374 | 353 | if (t.hasTag(CLASS)) { |
duke@1 | 354 | return names.fromUtf(externalize(t.tsym.flatName())); |
jjg@1374 | 355 | } else if (t.hasTag(ARRAY)) { |
duke@1 | 356 | return typeSig(types.erasure(t)); |
duke@1 | 357 | } else { |
duke@1 | 358 | throw new AssertionError("xClassName"); |
duke@1 | 359 | } |
duke@1 | 360 | } |
duke@1 | 361 | |
duke@1 | 362 | /****************************************************************** |
duke@1 | 363 | * Writing the Constant Pool |
duke@1 | 364 | ******************************************************************/ |
duke@1 | 365 | |
duke@1 | 366 | /** Thrown when the constant pool is over full. |
duke@1 | 367 | */ |
duke@1 | 368 | public static class PoolOverflow extends Exception { |
duke@1 | 369 | private static final long serialVersionUID = 0; |
duke@1 | 370 | public PoolOverflow() {} |
duke@1 | 371 | } |
duke@1 | 372 | public static class StringOverflow extends Exception { |
duke@1 | 373 | private static final long serialVersionUID = 0; |
duke@1 | 374 | public final String value; |
duke@1 | 375 | public StringOverflow(String s) { |
duke@1 | 376 | value = s; |
duke@1 | 377 | } |
duke@1 | 378 | } |
duke@1 | 379 | |
duke@1 | 380 | /** Write constant pool to pool buffer. |
duke@1 | 381 | * Note: during writing, constant pool |
duke@1 | 382 | * might grow since some parts of constants still need to be entered. |
duke@1 | 383 | */ |
duke@1 | 384 | void writePool(Pool pool) throws PoolOverflow, StringOverflow { |
duke@1 | 385 | int poolCountIdx = poolbuf.length; |
duke@1 | 386 | poolbuf.appendChar(0); |
duke@1 | 387 | int i = 1; |
duke@1 | 388 | while (i < pool.pp) { |
duke@1 | 389 | Object value = pool.pool[i]; |
jjg@816 | 390 | Assert.checkNonNull(value); |
vromero@1541 | 391 | if (value instanceof Method || value instanceof Variable) |
vromero@1541 | 392 | value = ((DelegatedSymbol)value).getUnderlyingSymbol(); |
duke@1 | 393 | |
duke@1 | 394 | if (value instanceof MethodSymbol) { |
duke@1 | 395 | MethodSymbol m = (MethodSymbol)value; |
mcimadamore@1336 | 396 | if (!m.isDynamic()) { |
mcimadamore@1336 | 397 | poolbuf.appendByte((m.owner.flags() & INTERFACE) != 0 |
mcimadamore@1336 | 398 | ? CONSTANT_InterfaceMethodref |
mcimadamore@1336 | 399 | : CONSTANT_Methodref); |
mcimadamore@1336 | 400 | poolbuf.appendChar(pool.put(m.owner)); |
mcimadamore@1336 | 401 | poolbuf.appendChar(pool.put(nameType(m))); |
mcimadamore@1336 | 402 | } else { |
mcimadamore@1336 | 403 | //invokedynamic |
mcimadamore@1336 | 404 | DynamicMethodSymbol dynSym = (DynamicMethodSymbol)m; |
vromero@1452 | 405 | MethodHandle handle = new MethodHandle(dynSym.bsmKind, dynSym.bsm, types); |
vromero@1452 | 406 | DynamicMethod dynMeth = new DynamicMethod(dynSym, types); |
vromero@1452 | 407 | bootstrapMethods.put(dynMeth, handle); |
mcimadamore@1336 | 408 | //init cp entries |
mcimadamore@1336 | 409 | pool.put(names.BootstrapMethods); |
mcimadamore@1336 | 410 | pool.put(handle); |
mcimadamore@1336 | 411 | for (Object staticArg : dynSym.staticArgs) { |
mcimadamore@1336 | 412 | pool.put(staticArg); |
mcimadamore@1336 | 413 | } |
mcimadamore@1336 | 414 | poolbuf.appendByte(CONSTANT_InvokeDynamic); |
mcimadamore@1336 | 415 | poolbuf.appendChar(bootstrapMethods.size() - 1); |
mcimadamore@1336 | 416 | poolbuf.appendChar(pool.put(nameType(dynSym))); |
mcimadamore@1336 | 417 | } |
duke@1 | 418 | } else if (value instanceof VarSymbol) { |
duke@1 | 419 | VarSymbol v = (VarSymbol)value; |
duke@1 | 420 | poolbuf.appendByte(CONSTANT_Fieldref); |
duke@1 | 421 | poolbuf.appendChar(pool.put(v.owner)); |
duke@1 | 422 | poolbuf.appendChar(pool.put(nameType(v))); |
duke@1 | 423 | } else if (value instanceof Name) { |
duke@1 | 424 | poolbuf.appendByte(CONSTANT_Utf8); |
duke@1 | 425 | byte[] bs = ((Name)value).toUtf(); |
duke@1 | 426 | poolbuf.appendChar(bs.length); |
duke@1 | 427 | poolbuf.appendBytes(bs, 0, bs.length); |
duke@1 | 428 | if (bs.length > Pool.MAX_STRING_LENGTH) |
duke@1 | 429 | throw new StringOverflow(value.toString()); |
duke@1 | 430 | } else if (value instanceof ClassSymbol) { |
duke@1 | 431 | ClassSymbol c = (ClassSymbol)value; |
duke@1 | 432 | if (c.owner.kind == TYP) pool.put(c.owner); |
duke@1 | 433 | poolbuf.appendByte(CONSTANT_Class); |
jjg@1374 | 434 | if (c.type.hasTag(ARRAY)) { |
duke@1 | 435 | poolbuf.appendChar(pool.put(typeSig(c.type))); |
duke@1 | 436 | } else { |
duke@1 | 437 | poolbuf.appendChar(pool.put(names.fromUtf(externalize(c.flatname)))); |
duke@1 | 438 | enterInner(c); |
duke@1 | 439 | } |
duke@1 | 440 | } else if (value instanceof NameAndType) { |
duke@1 | 441 | NameAndType nt = (NameAndType)value; |
duke@1 | 442 | poolbuf.appendByte(CONSTANT_NameandType); |
duke@1 | 443 | poolbuf.appendChar(pool.put(nt.name)); |
vromero@1452 | 444 | poolbuf.appendChar(pool.put(typeSig(nt.uniqueType.type))); |
duke@1 | 445 | } else if (value instanceof Integer) { |
duke@1 | 446 | poolbuf.appendByte(CONSTANT_Integer); |
duke@1 | 447 | poolbuf.appendInt(((Integer)value).intValue()); |
duke@1 | 448 | } else if (value instanceof Long) { |
duke@1 | 449 | poolbuf.appendByte(CONSTANT_Long); |
duke@1 | 450 | poolbuf.appendLong(((Long)value).longValue()); |
duke@1 | 451 | i++; |
duke@1 | 452 | } else if (value instanceof Float) { |
duke@1 | 453 | poolbuf.appendByte(CONSTANT_Float); |
duke@1 | 454 | poolbuf.appendFloat(((Float)value).floatValue()); |
duke@1 | 455 | } else if (value instanceof Double) { |
duke@1 | 456 | poolbuf.appendByte(CONSTANT_Double); |
duke@1 | 457 | poolbuf.appendDouble(((Double)value).doubleValue()); |
duke@1 | 458 | i++; |
duke@1 | 459 | } else if (value instanceof String) { |
duke@1 | 460 | poolbuf.appendByte(CONSTANT_String); |
duke@1 | 461 | poolbuf.appendChar(pool.put(names.fromString((String)value))); |
vromero@1452 | 462 | } else if (value instanceof UniqueType) { |
vromero@1452 | 463 | Type type = ((UniqueType)value).type; |
vromero@1452 | 464 | if (type instanceof MethodType) { |
vromero@1452 | 465 | poolbuf.appendByte(CONSTANT_MethodType); |
vromero@1452 | 466 | poolbuf.appendChar(pool.put(typeSig((MethodType)type))); |
vromero@1452 | 467 | } else { |
vromero@1452 | 468 | if (type.hasTag(CLASS)) enterInner((ClassSymbol)type.tsym); |
vromero@1452 | 469 | poolbuf.appendByte(CONSTANT_Class); |
vromero@1452 | 470 | poolbuf.appendChar(pool.put(xClassName(type))); |
vromero@1452 | 471 | } |
vromero@1452 | 472 | } else if (value instanceof MethodHandle) { |
vromero@1452 | 473 | MethodHandle ref = (MethodHandle)value; |
mcimadamore@1336 | 474 | poolbuf.appendByte(CONSTANT_MethodHandle); |
mcimadamore@1336 | 475 | poolbuf.appendByte(ref.refKind); |
mcimadamore@1336 | 476 | poolbuf.appendChar(pool.put(ref.refSym)); |
duke@1 | 477 | } else { |
jjg@816 | 478 | Assert.error("writePool " + value); |
duke@1 | 479 | } |
duke@1 | 480 | i++; |
duke@1 | 481 | } |
duke@1 | 482 | if (pool.pp > Pool.MAX_ENTRIES) |
duke@1 | 483 | throw new PoolOverflow(); |
duke@1 | 484 | putChar(poolbuf, poolCountIdx, pool.pp); |
duke@1 | 485 | } |
duke@1 | 486 | |
duke@1 | 487 | /** Given a field, return its name. |
duke@1 | 488 | */ |
duke@1 | 489 | Name fieldName(Symbol sym) { |
duke@1 | 490 | if (scramble && (sym.flags() & PRIVATE) != 0 || |
duke@1 | 491 | scrambleAll && (sym.flags() & (PROTECTED | PUBLIC)) == 0) |
jjg@113 | 492 | return names.fromString("_$" + sym.name.getIndex()); |
duke@1 | 493 | else |
duke@1 | 494 | return sym.name; |
duke@1 | 495 | } |
duke@1 | 496 | |
duke@1 | 497 | /** Given a symbol, return its name-and-type. |
duke@1 | 498 | */ |
duke@1 | 499 | NameAndType nameType(Symbol sym) { |
duke@1 | 500 | return new NameAndType(fieldName(sym), |
duke@1 | 501 | retrofit |
duke@1 | 502 | ? sym.erasure(types) |
vromero@1452 | 503 | : sym.externalType(types), types); |
duke@1 | 504 | // if we retrofit, then the NameAndType has been read in as is |
duke@1 | 505 | // and no change is necessary. If we compile normally, the |
duke@1 | 506 | // NameAndType is generated from a symbol reference, and the |
duke@1 | 507 | // adjustment of adding an additional this$n parameter needs to be made. |
duke@1 | 508 | } |
duke@1 | 509 | |
duke@1 | 510 | /****************************************************************** |
duke@1 | 511 | * Writing Attributes |
duke@1 | 512 | ******************************************************************/ |
duke@1 | 513 | |
duke@1 | 514 | /** Write header for an attribute to data buffer and return |
duke@1 | 515 | * position past attribute length index. |
duke@1 | 516 | */ |
duke@1 | 517 | int writeAttr(Name attrName) { |
duke@1 | 518 | databuf.appendChar(pool.put(attrName)); |
duke@1 | 519 | databuf.appendInt(0); |
duke@1 | 520 | return databuf.length; |
duke@1 | 521 | } |
duke@1 | 522 | |
duke@1 | 523 | /** Fill in attribute length. |
duke@1 | 524 | */ |
duke@1 | 525 | void endAttr(int index) { |
duke@1 | 526 | putInt(databuf, index - 4, databuf.length - index); |
duke@1 | 527 | } |
duke@1 | 528 | |
duke@1 | 529 | /** Leave space for attribute count and return index for |
duke@1 | 530 | * number of attributes field. |
duke@1 | 531 | */ |
duke@1 | 532 | int beginAttrs() { |
duke@1 | 533 | databuf.appendChar(0); |
duke@1 | 534 | return databuf.length; |
duke@1 | 535 | } |
duke@1 | 536 | |
duke@1 | 537 | /** Fill in number of attributes. |
duke@1 | 538 | */ |
duke@1 | 539 | void endAttrs(int index, int count) { |
duke@1 | 540 | putChar(databuf, index - 2, count); |
duke@1 | 541 | } |
duke@1 | 542 | |
duke@1 | 543 | /** Write the EnclosingMethod attribute if needed. |
duke@1 | 544 | * Returns the number of attributes written (0 or 1). |
duke@1 | 545 | */ |
duke@1 | 546 | int writeEnclosingMethodAttribute(ClassSymbol c) { |
ksrini@1309 | 547 | if (!target.hasEnclosingMethodAttribute()) |
ksrini@1309 | 548 | return 0; |
ksrini@1309 | 549 | return writeEnclosingMethodAttribute(names.EnclosingMethod, c); |
ksrini@1309 | 550 | } |
ksrini@1309 | 551 | |
ksrini@1309 | 552 | /** Write the EnclosingMethod attribute with a specified name. |
ksrini@1309 | 553 | * Returns the number of attributes written (0 or 1). |
ksrini@1309 | 554 | */ |
ksrini@1309 | 555 | protected int writeEnclosingMethodAttribute(Name attributeName, ClassSymbol c) { |
ksrini@1309 | 556 | if (c.owner.kind != MTH && // neither a local class |
duke@1 | 557 | c.name != names.empty) // nor anonymous |
duke@1 | 558 | return 0; |
duke@1 | 559 | |
ksrini@1309 | 560 | int alenIdx = writeAttr(attributeName); |
duke@1 | 561 | ClassSymbol enclClass = c.owner.enclClass(); |
duke@1 | 562 | MethodSymbol enclMethod = |
duke@1 | 563 | (c.owner.type == null // local to init block |
duke@1 | 564 | || c.owner.kind != MTH) // or member init |
duke@1 | 565 | ? null |
duke@1 | 566 | : (MethodSymbol)c.owner; |
duke@1 | 567 | databuf.appendChar(pool.put(enclClass)); |
duke@1 | 568 | databuf.appendChar(enclMethod == null ? 0 : pool.put(nameType(c.owner))); |
duke@1 | 569 | endAttr(alenIdx); |
duke@1 | 570 | return 1; |
duke@1 | 571 | } |
duke@1 | 572 | |
duke@1 | 573 | /** Write flag attributes; return number of attributes written. |
duke@1 | 574 | */ |
duke@1 | 575 | int writeFlagAttrs(long flags) { |
duke@1 | 576 | int acount = 0; |
duke@1 | 577 | if ((flags & DEPRECATED) != 0) { |
duke@1 | 578 | int alenIdx = writeAttr(names.Deprecated); |
duke@1 | 579 | endAttr(alenIdx); |
duke@1 | 580 | acount++; |
duke@1 | 581 | } |
duke@1 | 582 | if ((flags & ENUM) != 0 && !target.useEnumFlag()) { |
duke@1 | 583 | int alenIdx = writeAttr(names.Enum); |
duke@1 | 584 | endAttr(alenIdx); |
duke@1 | 585 | acount++; |
duke@1 | 586 | } |
duke@1 | 587 | if ((flags & SYNTHETIC) != 0 && !target.useSyntheticFlag()) { |
duke@1 | 588 | int alenIdx = writeAttr(names.Synthetic); |
duke@1 | 589 | endAttr(alenIdx); |
duke@1 | 590 | acount++; |
duke@1 | 591 | } |
duke@1 | 592 | if ((flags & BRIDGE) != 0 && !target.useBridgeFlag()) { |
duke@1 | 593 | int alenIdx = writeAttr(names.Bridge); |
duke@1 | 594 | endAttr(alenIdx); |
duke@1 | 595 | acount++; |
duke@1 | 596 | } |
duke@1 | 597 | if ((flags & VARARGS) != 0 && !target.useVarargsFlag()) { |
duke@1 | 598 | int alenIdx = writeAttr(names.Varargs); |
duke@1 | 599 | endAttr(alenIdx); |
duke@1 | 600 | acount++; |
duke@1 | 601 | } |
duke@1 | 602 | if ((flags & ANNOTATION) != 0 && !target.useAnnotationFlag()) { |
duke@1 | 603 | int alenIdx = writeAttr(names.Annotation); |
duke@1 | 604 | endAttr(alenIdx); |
duke@1 | 605 | acount++; |
duke@1 | 606 | } |
duke@1 | 607 | return acount; |
duke@1 | 608 | } |
duke@1 | 609 | |
duke@1 | 610 | /** Write member (field or method) attributes; |
duke@1 | 611 | * return number of attributes written. |
duke@1 | 612 | */ |
duke@1 | 613 | int writeMemberAttrs(Symbol sym) { |
duke@1 | 614 | int acount = writeFlagAttrs(sym.flags()); |
duke@1 | 615 | long flags = sym.flags(); |
duke@1 | 616 | if (source.allowGenerics() && |
duke@1 | 617 | (flags & (SYNTHETIC|BRIDGE)) != SYNTHETIC && |
duke@1 | 618 | (flags & ANONCONSTR) == 0 && |
duke@1 | 619 | (!types.isSameType(sym.type, sym.erasure(types)) || |
rfield@1587 | 620 | signatureGen.hasTypeVar(sym.type.getThrownTypes()))) { |
duke@1 | 621 | // note that a local class with captured variables |
duke@1 | 622 | // will get a signature attribute |
duke@1 | 623 | int alenIdx = writeAttr(names.Signature); |
duke@1 | 624 | databuf.appendChar(pool.put(typeSig(sym.type))); |
duke@1 | 625 | endAttr(alenIdx); |
duke@1 | 626 | acount++; |
duke@1 | 627 | } |
jfranck@1464 | 628 | acount += writeJavaAnnotations(sym.getRawAttributes()); |
jjg@1755 | 629 | acount += writeTypeAnnotations(sym.getRawTypeAttributes(), false); |
duke@1 | 630 | return acount; |
duke@1 | 631 | } |
duke@1 | 632 | |
jjg@1473 | 633 | /** |
jjg@1473 | 634 | * Write method parameter names attribute. |
jjg@1473 | 635 | */ |
jjg@1473 | 636 | int writeMethodParametersAttr(MethodSymbol m) { |
mcimadamore@1565 | 637 | MethodType ty = m.externalType(types).asMethodType(); |
mcimadamore@1565 | 638 | final int allparams = ty.argtypes.size(); |
mcimadamore@1565 | 639 | if (m.params != null && allparams != 0) { |
mcimadamore@1565 | 640 | final int attrIndex = writeAttr(names.MethodParameters); |
mcimadamore@1565 | 641 | databuf.appendByte(allparams); |
mcimadamore@1565 | 642 | // Write extra parameters first |
mcimadamore@1565 | 643 | for (VarSymbol s : m.extraParams) { |
mcimadamore@1565 | 644 | final int flags = |
mcimadamore@1565 | 645 | ((int) s.flags() & (FINAL | SYNTHETIC | MANDATED)) | |
mcimadamore@1565 | 646 | ((int) m.flags() & SYNTHETIC); |
mcimadamore@1565 | 647 | databuf.appendChar(pool.put(s.name)); |
ksrini@1602 | 648 | databuf.appendChar(flags); |
mcimadamore@1565 | 649 | } |
mcimadamore@1565 | 650 | // Now write the real parameters |
jjg@1473 | 651 | for (VarSymbol s : m.params) { |
mcimadamore@1565 | 652 | final int flags = |
mcimadamore@1565 | 653 | ((int) s.flags() & (FINAL | SYNTHETIC | MANDATED)) | |
mcimadamore@1565 | 654 | ((int) m.flags() & SYNTHETIC); |
jjg@1473 | 655 | databuf.appendChar(pool.put(s.name)); |
ksrini@1592 | 656 | databuf.appendChar(flags); |
jjg@1473 | 657 | } |
emc@1975 | 658 | // Now write the captured locals |
emc@1975 | 659 | for (VarSymbol s : m.capturedLocals) { |
emc@1975 | 660 | final int flags = |
emc@1975 | 661 | ((int) s.flags() & (FINAL | SYNTHETIC | MANDATED)) | |
emc@1975 | 662 | ((int) m.flags() & SYNTHETIC); |
emc@1975 | 663 | databuf.appendChar(pool.put(s.name)); |
emc@1975 | 664 | databuf.appendChar(flags); |
emc@1975 | 665 | } |
jjg@1473 | 666 | endAttr(attrIndex); |
jjg@1473 | 667 | return 1; |
jjg@1473 | 668 | } else |
jjg@1473 | 669 | return 0; |
jjg@1473 | 670 | } |
jjg@1473 | 671 | |
jjg@1473 | 672 | |
duke@1 | 673 | /** Write method parameter annotations; |
duke@1 | 674 | * return number of attributes written. |
duke@1 | 675 | */ |
duke@1 | 676 | int writeParameterAttrs(MethodSymbol m) { |
duke@1 | 677 | boolean hasVisible = false; |
duke@1 | 678 | boolean hasInvisible = false; |
vromero@1792 | 679 | if (m.params != null) { |
vromero@1792 | 680 | for (VarSymbol s : m.params) { |
vromero@1792 | 681 | for (Attribute.Compound a : s.getRawAttributes()) { |
vromero@1792 | 682 | switch (types.getRetention(a)) { |
vromero@1792 | 683 | case SOURCE: break; |
vromero@1792 | 684 | case CLASS: hasInvisible = true; break; |
vromero@1792 | 685 | case RUNTIME: hasVisible = true; break; |
vromero@1792 | 686 | default: ;// /* fail soft */ throw new AssertionError(vis); |
vromero@1792 | 687 | } |
duke@1 | 688 | } |
duke@1 | 689 | } |
duke@1 | 690 | } |
duke@1 | 691 | |
duke@1 | 692 | int attrCount = 0; |
duke@1 | 693 | if (hasVisible) { |
duke@1 | 694 | int attrIndex = writeAttr(names.RuntimeVisibleParameterAnnotations); |
duke@1 | 695 | databuf.appendByte(m.params.length()); |
duke@1 | 696 | for (VarSymbol s : m.params) { |
duke@1 | 697 | ListBuffer<Attribute.Compound> buf = new ListBuffer<Attribute.Compound>(); |
jfranck@1464 | 698 | for (Attribute.Compound a : s.getRawAttributes()) |
jjg@657 | 699 | if (types.getRetention(a) == RetentionPolicy.RUNTIME) |
duke@1 | 700 | buf.append(a); |
duke@1 | 701 | databuf.appendChar(buf.length()); |
duke@1 | 702 | for (Attribute.Compound a : buf) |
duke@1 | 703 | writeCompoundAttribute(a); |
duke@1 | 704 | } |
duke@1 | 705 | endAttr(attrIndex); |
duke@1 | 706 | attrCount++; |
duke@1 | 707 | } |
duke@1 | 708 | if (hasInvisible) { |
duke@1 | 709 | int attrIndex = writeAttr(names.RuntimeInvisibleParameterAnnotations); |
duke@1 | 710 | databuf.appendByte(m.params.length()); |
duke@1 | 711 | for (VarSymbol s : m.params) { |
duke@1 | 712 | ListBuffer<Attribute.Compound> buf = new ListBuffer<Attribute.Compound>(); |
jfranck@1464 | 713 | for (Attribute.Compound a : s.getRawAttributes()) |
jjg@657 | 714 | if (types.getRetention(a) == RetentionPolicy.CLASS) |
duke@1 | 715 | buf.append(a); |
duke@1 | 716 | databuf.appendChar(buf.length()); |
duke@1 | 717 | for (Attribute.Compound a : buf) |
duke@1 | 718 | writeCompoundAttribute(a); |
duke@1 | 719 | } |
duke@1 | 720 | endAttr(attrIndex); |
duke@1 | 721 | attrCount++; |
duke@1 | 722 | } |
duke@1 | 723 | return attrCount; |
duke@1 | 724 | } |
duke@1 | 725 | |
duke@1 | 726 | /********************************************************************** |
duke@1 | 727 | * Writing Java-language annotations (aka metadata, attributes) |
duke@1 | 728 | **********************************************************************/ |
duke@1 | 729 | |
duke@1 | 730 | /** Write Java-language annotations; return number of JVM |
duke@1 | 731 | * attributes written (zero or one). |
duke@1 | 732 | */ |
duke@1 | 733 | int writeJavaAnnotations(List<Attribute.Compound> attrs) { |
duke@1 | 734 | if (attrs.isEmpty()) return 0; |
duke@1 | 735 | ListBuffer<Attribute.Compound> visibles = new ListBuffer<Attribute.Compound>(); |
duke@1 | 736 | ListBuffer<Attribute.Compound> invisibles = new ListBuffer<Attribute.Compound>(); |
duke@1 | 737 | for (Attribute.Compound a : attrs) { |
jjg@657 | 738 | switch (types.getRetention(a)) { |
duke@1 | 739 | case SOURCE: break; |
duke@1 | 740 | case CLASS: invisibles.append(a); break; |
duke@1 | 741 | case RUNTIME: visibles.append(a); break; |
duke@1 | 742 | default: ;// /* fail soft */ throw new AssertionError(vis); |
duke@1 | 743 | } |
duke@1 | 744 | } |
duke@1 | 745 | |
duke@1 | 746 | int attrCount = 0; |
duke@1 | 747 | if (visibles.length() != 0) { |
duke@1 | 748 | int attrIndex = writeAttr(names.RuntimeVisibleAnnotations); |
duke@1 | 749 | databuf.appendChar(visibles.length()); |
duke@1 | 750 | for (Attribute.Compound a : visibles) |
duke@1 | 751 | writeCompoundAttribute(a); |
duke@1 | 752 | endAttr(attrIndex); |
duke@1 | 753 | attrCount++; |
duke@1 | 754 | } |
duke@1 | 755 | if (invisibles.length() != 0) { |
duke@1 | 756 | int attrIndex = writeAttr(names.RuntimeInvisibleAnnotations); |
duke@1 | 757 | databuf.appendChar(invisibles.length()); |
duke@1 | 758 | for (Attribute.Compound a : invisibles) |
duke@1 | 759 | writeCompoundAttribute(a); |
duke@1 | 760 | endAttr(attrIndex); |
duke@1 | 761 | attrCount++; |
duke@1 | 762 | } |
duke@1 | 763 | return attrCount; |
duke@1 | 764 | } |
duke@1 | 765 | |
jjg@1755 | 766 | int writeTypeAnnotations(List<Attribute.TypeCompound> typeAnnos, boolean inCode) { |
jjg@1521 | 767 | if (typeAnnos.isEmpty()) return 0; |
jjg@1521 | 768 | |
alundblad@2047 | 769 | ListBuffer<Attribute.TypeCompound> visibles = new ListBuffer<>(); |
alundblad@2047 | 770 | ListBuffer<Attribute.TypeCompound> invisibles = new ListBuffer<>(); |
jjg@1521 | 771 | |
jjg@1521 | 772 | for (Attribute.TypeCompound tc : typeAnnos) { |
jjg@1755 | 773 | if (tc.hasUnknownPosition()) { |
jjg@1755 | 774 | boolean fixed = tc.tryFixPosition(); |
jjg@1755 | 775 | |
jjg@1755 | 776 | // Could we fix it? |
jjg@1755 | 777 | if (!fixed) { |
jjg@1521 | 778 | // This happens for nested types like @A Outer. @B Inner. |
jjg@1521 | 779 | // For method parameters we get the annotation twice! Once with |
jjg@1521 | 780 | // a valid position, once unknown. |
jjg@1521 | 781 | // TODO: find a cleaner solution. |
jjg@1755 | 782 | PrintWriter pw = log.getWriter(Log.WriterKind.ERROR); |
jjg@1755 | 783 | pw.println("ClassWriter: Position UNKNOWN in type annotation: " + tc); |
jjg@1521 | 784 | continue; |
jjg@1521 | 785 | } |
jjg@1521 | 786 | } |
jjg@1755 | 787 | |
jjg@1755 | 788 | if (tc.position.type.isLocal() != inCode) |
jjg@1755 | 789 | continue; |
jjg@1521 | 790 | if (!tc.position.emitToClassfile()) |
jjg@1521 | 791 | continue; |
jjg@1521 | 792 | switch (types.getRetention(tc)) { |
jjg@1521 | 793 | case SOURCE: break; |
jjg@1521 | 794 | case CLASS: invisibles.append(tc); break; |
jjg@1521 | 795 | case RUNTIME: visibles.append(tc); break; |
jjg@1521 | 796 | default: ;// /* fail soft */ throw new AssertionError(vis); |
jjg@1521 | 797 | } |
jjg@1521 | 798 | } |
jjg@1521 | 799 | |
jjg@1521 | 800 | int attrCount = 0; |
jjg@1521 | 801 | if (visibles.length() != 0) { |
jjg@1521 | 802 | int attrIndex = writeAttr(names.RuntimeVisibleTypeAnnotations); |
jjg@1521 | 803 | databuf.appendChar(visibles.length()); |
jjg@1521 | 804 | for (Attribute.TypeCompound p : visibles) |
jjg@1521 | 805 | writeTypeAnnotation(p); |
jjg@1521 | 806 | endAttr(attrIndex); |
jjg@1521 | 807 | attrCount++; |
jjg@1521 | 808 | } |
jjg@1521 | 809 | |
jjg@1521 | 810 | if (invisibles.length() != 0) { |
jjg@1521 | 811 | int attrIndex = writeAttr(names.RuntimeInvisibleTypeAnnotations); |
jjg@1521 | 812 | databuf.appendChar(invisibles.length()); |
jjg@1521 | 813 | for (Attribute.TypeCompound p : invisibles) |
jjg@1521 | 814 | writeTypeAnnotation(p); |
jjg@1521 | 815 | endAttr(attrIndex); |
jjg@1521 | 816 | attrCount++; |
jjg@1521 | 817 | } |
jjg@1521 | 818 | |
jjg@1521 | 819 | return attrCount; |
jjg@1521 | 820 | } |
jjg@1521 | 821 | |
duke@1 | 822 | /** A visitor to write an attribute including its leading |
duke@1 | 823 | * single-character marker. |
duke@1 | 824 | */ |
duke@1 | 825 | class AttributeWriter implements Attribute.Visitor { |
duke@1 | 826 | public void visitConstant(Attribute.Constant _value) { |
duke@1 | 827 | Object value = _value.value; |
jjg@1374 | 828 | switch (_value.type.getTag()) { |
duke@1 | 829 | case BYTE: |
duke@1 | 830 | databuf.appendByte('B'); |
duke@1 | 831 | break; |
duke@1 | 832 | case CHAR: |
duke@1 | 833 | databuf.appendByte('C'); |
duke@1 | 834 | break; |
duke@1 | 835 | case SHORT: |
duke@1 | 836 | databuf.appendByte('S'); |
duke@1 | 837 | break; |
duke@1 | 838 | case INT: |
duke@1 | 839 | databuf.appendByte('I'); |
duke@1 | 840 | break; |
duke@1 | 841 | case LONG: |
duke@1 | 842 | databuf.appendByte('J'); |
duke@1 | 843 | break; |
duke@1 | 844 | case FLOAT: |
duke@1 | 845 | databuf.appendByte('F'); |
duke@1 | 846 | break; |
duke@1 | 847 | case DOUBLE: |
duke@1 | 848 | databuf.appendByte('D'); |
duke@1 | 849 | break; |
duke@1 | 850 | case BOOLEAN: |
duke@1 | 851 | databuf.appendByte('Z'); |
duke@1 | 852 | break; |
duke@1 | 853 | case CLASS: |
jjg@816 | 854 | Assert.check(value instanceof String); |
duke@1 | 855 | databuf.appendByte('s'); |
duke@1 | 856 | value = names.fromString(value.toString()); // CONSTANT_Utf8 |
duke@1 | 857 | break; |
duke@1 | 858 | default: |
duke@1 | 859 | throw new AssertionError(_value.type); |
duke@1 | 860 | } |
duke@1 | 861 | databuf.appendChar(pool.put(value)); |
duke@1 | 862 | } |
duke@1 | 863 | public void visitEnum(Attribute.Enum e) { |
duke@1 | 864 | databuf.appendByte('e'); |
duke@1 | 865 | databuf.appendChar(pool.put(typeSig(e.value.type))); |
duke@1 | 866 | databuf.appendChar(pool.put(e.value.name)); |
duke@1 | 867 | } |
duke@1 | 868 | public void visitClass(Attribute.Class clazz) { |
duke@1 | 869 | databuf.appendByte('c'); |
jfranck@1313 | 870 | databuf.appendChar(pool.put(typeSig(clazz.classType))); |
duke@1 | 871 | } |
duke@1 | 872 | public void visitCompound(Attribute.Compound compound) { |
duke@1 | 873 | databuf.appendByte('@'); |
duke@1 | 874 | writeCompoundAttribute(compound); |
duke@1 | 875 | } |
duke@1 | 876 | public void visitError(Attribute.Error x) { |
duke@1 | 877 | throw new AssertionError(x); |
duke@1 | 878 | } |
duke@1 | 879 | public void visitArray(Attribute.Array array) { |
duke@1 | 880 | databuf.appendByte('['); |
duke@1 | 881 | databuf.appendChar(array.values.length); |
duke@1 | 882 | for (Attribute a : array.values) { |
duke@1 | 883 | a.accept(this); |
duke@1 | 884 | } |
duke@1 | 885 | } |
duke@1 | 886 | } |
duke@1 | 887 | AttributeWriter awriter = new AttributeWriter(); |
duke@1 | 888 | |
duke@1 | 889 | /** Write a compound attribute excluding the '@' marker. */ |
duke@1 | 890 | void writeCompoundAttribute(Attribute.Compound c) { |
duke@1 | 891 | databuf.appendChar(pool.put(typeSig(c.type))); |
duke@1 | 892 | databuf.appendChar(c.values.length()); |
duke@1 | 893 | for (Pair<Symbol.MethodSymbol,Attribute> p : c.values) { |
duke@1 | 894 | databuf.appendChar(pool.put(p.fst.name)); |
duke@1 | 895 | p.snd.accept(awriter); |
duke@1 | 896 | } |
duke@1 | 897 | } |
jjg@1521 | 898 | |
jjg@1521 | 899 | void writeTypeAnnotation(Attribute.TypeCompound c) { |
jjg@1521 | 900 | writePosition(c.position); |
jjg@1521 | 901 | writeCompoundAttribute(c); |
jjg@1521 | 902 | } |
jjg@1521 | 903 | |
jjg@1521 | 904 | void writePosition(TypeAnnotationPosition p) { |
jjg@1521 | 905 | databuf.appendByte(p.type.targetTypeValue()); // TargetType tag is a byte |
jjg@1521 | 906 | switch (p.type) { |
jjg@1521 | 907 | // instanceof |
jjg@1521 | 908 | case INSTANCEOF: |
jjg@1521 | 909 | // new expression |
jjg@1521 | 910 | case NEW: |
jjg@1563 | 911 | // constructor/method reference receiver |
jjg@1563 | 912 | case CONSTRUCTOR_REFERENCE: |
jjg@1563 | 913 | case METHOD_REFERENCE: |
jjg@1521 | 914 | databuf.appendChar(p.offset); |
jjg@1521 | 915 | break; |
jjg@1521 | 916 | // local variable |
jjg@1521 | 917 | case LOCAL_VARIABLE: |
jjg@1521 | 918 | // resource variable |
jjg@1521 | 919 | case RESOURCE_VARIABLE: |
jjg@1521 | 920 | databuf.appendChar(p.lvarOffset.length); // for table length |
jjg@1521 | 921 | for (int i = 0; i < p.lvarOffset.length; ++i) { |
jjg@1521 | 922 | databuf.appendChar(p.lvarOffset[i]); |
jjg@1521 | 923 | databuf.appendChar(p.lvarLength[i]); |
jjg@1521 | 924 | databuf.appendChar(p.lvarIndex[i]); |
jjg@1521 | 925 | } |
jjg@1521 | 926 | break; |
jjg@1521 | 927 | // exception parameter |
jjg@1521 | 928 | case EXCEPTION_PARAMETER: |
jjg@1755 | 929 | databuf.appendChar(p.exception_index); |
jjg@1521 | 930 | break; |
jjg@1521 | 931 | // method receiver |
jjg@1521 | 932 | case METHOD_RECEIVER: |
jjg@1521 | 933 | // Do nothing |
jjg@1521 | 934 | break; |
jjg@1521 | 935 | // type parameter |
jjg@1521 | 936 | case CLASS_TYPE_PARAMETER: |
jjg@1521 | 937 | case METHOD_TYPE_PARAMETER: |
jjg@1521 | 938 | databuf.appendByte(p.parameter_index); |
jjg@1521 | 939 | break; |
jjg@1521 | 940 | // type parameter bound |
jjg@1521 | 941 | case CLASS_TYPE_PARAMETER_BOUND: |
jjg@1521 | 942 | case METHOD_TYPE_PARAMETER_BOUND: |
jjg@1521 | 943 | databuf.appendByte(p.parameter_index); |
jjg@1521 | 944 | databuf.appendByte(p.bound_index); |
jjg@1521 | 945 | break; |
jjg@1521 | 946 | // class extends or implements clause |
jjg@1521 | 947 | case CLASS_EXTENDS: |
jjg@1521 | 948 | databuf.appendChar(p.type_index); |
jjg@1521 | 949 | break; |
jjg@1521 | 950 | // throws |
jjg@1521 | 951 | case THROWS: |
jjg@1521 | 952 | databuf.appendChar(p.type_index); |
jjg@1521 | 953 | break; |
jjg@1521 | 954 | // method parameter |
jjg@1521 | 955 | case METHOD_FORMAL_PARAMETER: |
jjg@1521 | 956 | databuf.appendByte(p.parameter_index); |
jjg@1521 | 957 | break; |
jjg@1563 | 958 | // type cast |
jjg@1563 | 959 | case CAST: |
jjg@1521 | 960 | // method/constructor/reference type argument |
jjg@1521 | 961 | case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT: |
jjg@1521 | 962 | case METHOD_INVOCATION_TYPE_ARGUMENT: |
jjg@1563 | 963 | case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT: |
jjg@1521 | 964 | case METHOD_REFERENCE_TYPE_ARGUMENT: |
jjg@1521 | 965 | databuf.appendChar(p.offset); |
jjg@1521 | 966 | databuf.appendByte(p.type_index); |
jjg@1521 | 967 | break; |
jjg@1521 | 968 | // We don't need to worry about these |
jjg@1521 | 969 | case METHOD_RETURN: |
jjg@1521 | 970 | case FIELD: |
jjg@1521 | 971 | break; |
jjg@1521 | 972 | case UNKNOWN: |
jjg@1521 | 973 | throw new AssertionError("jvm.ClassWriter: UNKNOWN target type should never occur!"); |
jjg@1521 | 974 | default: |
jjg@1521 | 975 | throw new AssertionError("jvm.ClassWriter: Unknown target type for position: " + p); |
jjg@1521 | 976 | } |
jjg@1521 | 977 | |
jjg@1521 | 978 | { // Append location data for generics/arrays. |
jjg@1521 | 979 | databuf.appendByte(p.location.size()); |
jjg@1521 | 980 | java.util.List<Integer> loc = TypeAnnotationPosition.getBinaryFromTypePath(p.location); |
jjg@1521 | 981 | for (int i : loc) |
jjg@1521 | 982 | databuf.appendByte((byte)i); |
jjg@1521 | 983 | } |
jjg@1521 | 984 | } |
jjg@1521 | 985 | |
duke@1 | 986 | /********************************************************************** |
duke@1 | 987 | * Writing Objects |
duke@1 | 988 | **********************************************************************/ |
duke@1 | 989 | |
duke@1 | 990 | /** Enter an inner class into the `innerClasses' set/queue. |
duke@1 | 991 | */ |
duke@1 | 992 | void enterInner(ClassSymbol c) { |
mcimadamore@747 | 993 | if (c.type.isCompound()) { |
mcimadamore@747 | 994 | throw new AssertionError("Unexpected intersection type: " + c.type); |
mcimadamore@747 | 995 | } |
duke@1 | 996 | try { |
duke@1 | 997 | c.complete(); |
duke@1 | 998 | } catch (CompletionFailure ex) { |
duke@1 | 999 | System.err.println("error: " + c + ": " + ex.getMessage()); |
duke@1 | 1000 | throw ex; |
duke@1 | 1001 | } |
jjg@1374 | 1002 | if (!c.type.hasTag(CLASS)) return; // arrays |
duke@1 | 1003 | if (pool != null && // pool might be null if called from xClassName |
mcimadamore@1086 | 1004 | c.owner.enclClass() != null && |
duke@1 | 1005 | (innerClasses == null || !innerClasses.contains(c))) { |
duke@1 | 1006 | // log.errWriter.println("enter inner " + c);//DEBUG |
mcimadamore@1086 | 1007 | enterInner(c.owner.enclClass()); |
duke@1 | 1008 | pool.put(c); |
vromero@1698 | 1009 | if (c.name != names.empty) |
vromero@1698 | 1010 | pool.put(c.name); |
duke@1 | 1011 | if (innerClasses == null) { |
duke@1 | 1012 | innerClasses = new HashSet<ClassSymbol>(); |
duke@1 | 1013 | innerClassesQueue = new ListBuffer<ClassSymbol>(); |
duke@1 | 1014 | pool.put(names.InnerClasses); |
duke@1 | 1015 | } |
duke@1 | 1016 | innerClasses.add(c); |
duke@1 | 1017 | innerClassesQueue.append(c); |
duke@1 | 1018 | } |
duke@1 | 1019 | } |
duke@1 | 1020 | |
duke@1 | 1021 | /** Write "inner classes" attribute. |
duke@1 | 1022 | */ |
duke@1 | 1023 | void writeInnerClasses() { |
duke@1 | 1024 | int alenIdx = writeAttr(names.InnerClasses); |
duke@1 | 1025 | databuf.appendChar(innerClassesQueue.length()); |
duke@1 | 1026 | for (List<ClassSymbol> l = innerClassesQueue.toList(); |
duke@1 | 1027 | l.nonEmpty(); |
duke@1 | 1028 | l = l.tail) { |
duke@1 | 1029 | ClassSymbol inner = l.head; |
duke@1 | 1030 | char flags = (char) adjustFlags(inner.flags_field); |
duke@1 | 1031 | if ((flags & INTERFACE) != 0) flags |= ABSTRACT; // Interfaces are always ABSTRACT |
duke@1 | 1032 | if (inner.name.isEmpty()) flags &= ~FINAL; // Anonymous class: unset FINAL flag |
vromero@1801 | 1033 | flags &= ~STRICTFP; //inner classes should not have the strictfp flag set. |
duke@1 | 1034 | if (dumpInnerClassModifiers) { |
jjg@1135 | 1035 | PrintWriter pw = log.getWriter(Log.WriterKind.ERROR); |
jjg@1135 | 1036 | pw.println("INNERCLASS " + inner.name); |
jjg@1135 | 1037 | pw.println("---" + flagNames(flags)); |
duke@1 | 1038 | } |
duke@1 | 1039 | databuf.appendChar(pool.get(inner)); |
duke@1 | 1040 | databuf.appendChar( |
jlahoda@2304 | 1041 | inner.owner.kind == TYP && !inner.name.isEmpty() ? pool.get(inner.owner) : 0); |
duke@1 | 1042 | databuf.appendChar( |
jjg@113 | 1043 | !inner.name.isEmpty() ? pool.get(inner.name) : 0); |
duke@1 | 1044 | databuf.appendChar(flags); |
duke@1 | 1045 | } |
duke@1 | 1046 | endAttr(alenIdx); |
duke@1 | 1047 | } |
duke@1 | 1048 | |
mcimadamore@1336 | 1049 | /** Write "bootstrapMethods" attribute. |
mcimadamore@1336 | 1050 | */ |
mcimadamore@1336 | 1051 | void writeBootstrapMethods() { |
mcimadamore@1336 | 1052 | int alenIdx = writeAttr(names.BootstrapMethods); |
mcimadamore@1336 | 1053 | databuf.appendChar(bootstrapMethods.size()); |
vromero@1452 | 1054 | for (Map.Entry<DynamicMethod, MethodHandle> entry : bootstrapMethods.entrySet()) { |
vromero@1452 | 1055 | DynamicMethod dmeth = entry.getKey(); |
vromero@1452 | 1056 | DynamicMethodSymbol dsym = (DynamicMethodSymbol)dmeth.baseSymbol(); |
mcimadamore@1336 | 1057 | //write BSM handle |
mcimadamore@1336 | 1058 | databuf.appendChar(pool.get(entry.getValue())); |
mcimadamore@1336 | 1059 | //write static args length |
mcimadamore@1336 | 1060 | databuf.appendChar(dsym.staticArgs.length); |
mcimadamore@1336 | 1061 | //write static args array |
vromero@1452 | 1062 | Object[] uniqueArgs = dmeth.uniqueStaticArgs; |
vromero@1452 | 1063 | for (Object o : uniqueArgs) { |
mcimadamore@1336 | 1064 | databuf.appendChar(pool.get(o)); |
mcimadamore@1336 | 1065 | } |
mcimadamore@1336 | 1066 | } |
mcimadamore@1336 | 1067 | endAttr(alenIdx); |
mcimadamore@1336 | 1068 | } |
mcimadamore@1336 | 1069 | |
duke@1 | 1070 | /** Write field symbol, entering all references into constant pool. |
duke@1 | 1071 | */ |
duke@1 | 1072 | void writeField(VarSymbol v) { |
duke@1 | 1073 | int flags = adjustFlags(v.flags()); |
duke@1 | 1074 | databuf.appendChar(flags); |
duke@1 | 1075 | if (dumpFieldModifiers) { |
jjg@1135 | 1076 | PrintWriter pw = log.getWriter(Log.WriterKind.ERROR); |
jjg@1135 | 1077 | pw.println("FIELD " + fieldName(v)); |
jjg@1135 | 1078 | pw.println("---" + flagNames(v.flags())); |
duke@1 | 1079 | } |
duke@1 | 1080 | databuf.appendChar(pool.put(fieldName(v))); |
duke@1 | 1081 | databuf.appendChar(pool.put(typeSig(v.erasure(types)))); |
duke@1 | 1082 | int acountIdx = beginAttrs(); |
duke@1 | 1083 | int acount = 0; |
duke@1 | 1084 | if (v.getConstValue() != null) { |
duke@1 | 1085 | int alenIdx = writeAttr(names.ConstantValue); |
duke@1 | 1086 | databuf.appendChar(pool.put(v.getConstValue())); |
duke@1 | 1087 | endAttr(alenIdx); |
duke@1 | 1088 | acount++; |
duke@1 | 1089 | } |
duke@1 | 1090 | acount += writeMemberAttrs(v); |
duke@1 | 1091 | endAttrs(acountIdx, acount); |
duke@1 | 1092 | } |
duke@1 | 1093 | |
duke@1 | 1094 | /** Write method symbol, entering all references into constant pool. |
duke@1 | 1095 | */ |
duke@1 | 1096 | void writeMethod(MethodSymbol m) { |
duke@1 | 1097 | int flags = adjustFlags(m.flags()); |
duke@1 | 1098 | databuf.appendChar(flags); |
duke@1 | 1099 | if (dumpMethodModifiers) { |
jjg@1135 | 1100 | PrintWriter pw = log.getWriter(Log.WriterKind.ERROR); |
jjg@1135 | 1101 | pw.println("METHOD " + fieldName(m)); |
jjg@1135 | 1102 | pw.println("---" + flagNames(m.flags())); |
duke@1 | 1103 | } |
duke@1 | 1104 | databuf.appendChar(pool.put(fieldName(m))); |
duke@1 | 1105 | databuf.appendChar(pool.put(typeSig(m.externalType(types)))); |
duke@1 | 1106 | int acountIdx = beginAttrs(); |
duke@1 | 1107 | int acount = 0; |
duke@1 | 1108 | if (m.code != null) { |
duke@1 | 1109 | int alenIdx = writeAttr(names.Code); |
duke@1 | 1110 | writeCode(m.code); |
duke@1 | 1111 | m.code = null; // to conserve space |
duke@1 | 1112 | endAttr(alenIdx); |
duke@1 | 1113 | acount++; |
duke@1 | 1114 | } |
duke@1 | 1115 | List<Type> thrown = m.erasure(types).getThrownTypes(); |
duke@1 | 1116 | if (thrown.nonEmpty()) { |
duke@1 | 1117 | int alenIdx = writeAttr(names.Exceptions); |
duke@1 | 1118 | databuf.appendChar(thrown.length()); |
duke@1 | 1119 | for (List<Type> l = thrown; l.nonEmpty(); l = l.tail) |
duke@1 | 1120 | databuf.appendChar(pool.put(l.head.tsym)); |
duke@1 | 1121 | endAttr(alenIdx); |
duke@1 | 1122 | acount++; |
duke@1 | 1123 | } |
duke@1 | 1124 | if (m.defaultValue != null) { |
duke@1 | 1125 | int alenIdx = writeAttr(names.AnnotationDefault); |
duke@1 | 1126 | m.defaultValue.accept(awriter); |
duke@1 | 1127 | endAttr(alenIdx); |
duke@1 | 1128 | acount++; |
duke@1 | 1129 | } |
jjg@1473 | 1130 | if (options.isSet(PARAMETERS)) |
jjg@1473 | 1131 | acount += writeMethodParametersAttr(m); |
duke@1 | 1132 | acount += writeMemberAttrs(m); |
duke@1 | 1133 | acount += writeParameterAttrs(m); |
duke@1 | 1134 | endAttrs(acountIdx, acount); |
duke@1 | 1135 | } |
duke@1 | 1136 | |
duke@1 | 1137 | /** Write code attribute of method. |
duke@1 | 1138 | */ |
duke@1 | 1139 | void writeCode(Code code) { |
duke@1 | 1140 | databuf.appendChar(code.max_stack); |
duke@1 | 1141 | databuf.appendChar(code.max_locals); |
duke@1 | 1142 | databuf.appendInt(code.cp); |
duke@1 | 1143 | databuf.appendBytes(code.code, 0, code.cp); |
duke@1 | 1144 | databuf.appendChar(code.catchInfo.length()); |
duke@1 | 1145 | for (List<char[]> l = code.catchInfo.toList(); |
duke@1 | 1146 | l.nonEmpty(); |
duke@1 | 1147 | l = l.tail) { |
duke@1 | 1148 | for (int i = 0; i < l.head.length; i++) |
duke@1 | 1149 | databuf.appendChar(l.head[i]); |
duke@1 | 1150 | } |
duke@1 | 1151 | int acountIdx = beginAttrs(); |
duke@1 | 1152 | int acount = 0; |
duke@1 | 1153 | |
duke@1 | 1154 | if (code.lineInfo.nonEmpty()) { |
duke@1 | 1155 | int alenIdx = writeAttr(names.LineNumberTable); |
duke@1 | 1156 | databuf.appendChar(code.lineInfo.length()); |
duke@1 | 1157 | for (List<char[]> l = code.lineInfo.reverse(); |
duke@1 | 1158 | l.nonEmpty(); |
duke@1 | 1159 | l = l.tail) |
duke@1 | 1160 | for (int i = 0; i < l.head.length; i++) |
duke@1 | 1161 | databuf.appendChar(l.head[i]); |
duke@1 | 1162 | endAttr(alenIdx); |
duke@1 | 1163 | acount++; |
duke@1 | 1164 | } |
duke@1 | 1165 | |
duke@1 | 1166 | if (genCrt && (code.crt != null)) { |
duke@1 | 1167 | CRTable crt = code.crt; |
duke@1 | 1168 | int alenIdx = writeAttr(names.CharacterRangeTable); |
duke@1 | 1169 | int crtIdx = beginAttrs(); |
duke@1 | 1170 | int crtEntries = crt.writeCRT(databuf, code.lineMap, log); |
duke@1 | 1171 | endAttrs(crtIdx, crtEntries); |
duke@1 | 1172 | endAttr(alenIdx); |
duke@1 | 1173 | acount++; |
duke@1 | 1174 | } |
duke@1 | 1175 | |
duke@1 | 1176 | // counter for number of generic local variables |
vromero@2199 | 1177 | if (code.varDebugInfo && code.varBufferSize > 0) { |
vromero@2199 | 1178 | int nGenericVars = 0; |
duke@1 | 1179 | int alenIdx = writeAttr(names.LocalVariableTable); |
vromero@2027 | 1180 | databuf.appendChar(code.getLVTSize()); |
duke@1 | 1181 | for (int i=0; i<code.varBufferSize; i++) { |
duke@1 | 1182 | Code.LocalVar var = code.varBuffer[i]; |
duke@1 | 1183 | |
vromero@2027 | 1184 | for (Code.LocalVar.Range r: var.aliveRanges) { |
vromero@2027 | 1185 | // write variable info |
vromero@2027 | 1186 | Assert.check(r.start_pc >= 0 |
vromero@2027 | 1187 | && r.start_pc <= code.cp); |
vromero@2027 | 1188 | databuf.appendChar(r.start_pc); |
vromero@2027 | 1189 | Assert.check(r.length >= 0 |
vromero@2027 | 1190 | && (r.start_pc + r.length) <= code.cp); |
vromero@2027 | 1191 | databuf.appendChar(r.length); |
vromero@2027 | 1192 | VarSymbol sym = var.sym; |
vromero@2027 | 1193 | databuf.appendChar(pool.put(sym.name)); |
vromero@2027 | 1194 | Type vartype = sym.erasure(types); |
vromero@2027 | 1195 | databuf.appendChar(pool.put(typeSig(vartype))); |
vromero@2027 | 1196 | databuf.appendChar(var.reg); |
vromero@2199 | 1197 | if (needsLocalVariableTypeEntry(var.sym.type)) { |
vromero@2027 | 1198 | nGenericVars++; |
vromero@2199 | 1199 | } |
vromero@2027 | 1200 | } |
duke@1 | 1201 | } |
duke@1 | 1202 | endAttr(alenIdx); |
duke@1 | 1203 | acount++; |
duke@1 | 1204 | |
vromero@2199 | 1205 | if (nGenericVars > 0) { |
vromero@2199 | 1206 | alenIdx = writeAttr(names.LocalVariableTypeTable); |
vromero@2199 | 1207 | databuf.appendChar(nGenericVars); |
vromero@2199 | 1208 | int count = 0; |
duke@1 | 1209 | |
vromero@2199 | 1210 | for (int i=0; i<code.varBufferSize; i++) { |
vromero@2199 | 1211 | Code.LocalVar var = code.varBuffer[i]; |
vromero@2199 | 1212 | VarSymbol sym = var.sym; |
vromero@2199 | 1213 | if (!needsLocalVariableTypeEntry(sym.type)) |
vromero@2199 | 1214 | continue; |
vromero@2199 | 1215 | for (Code.LocalVar.Range r : var.aliveRanges) { |
vromero@2199 | 1216 | // write variable info |
vromero@2199 | 1217 | databuf.appendChar(r.start_pc); |
vromero@2199 | 1218 | databuf.appendChar(r.length); |
vromero@2199 | 1219 | databuf.appendChar(pool.put(sym.name)); |
vromero@2199 | 1220 | databuf.appendChar(pool.put(typeSig(sym.type))); |
vromero@2199 | 1221 | databuf.appendChar(var.reg); |
vromero@2199 | 1222 | count++; |
vromero@2199 | 1223 | } |
vromero@2027 | 1224 | } |
vromero@2199 | 1225 | Assert.check(count == nGenericVars); |
vromero@2199 | 1226 | endAttr(alenIdx); |
vromero@2199 | 1227 | acount++; |
duke@1 | 1228 | } |
duke@1 | 1229 | } |
duke@1 | 1230 | |
duke@1 | 1231 | if (code.stackMapBufferSize > 0) { |
duke@1 | 1232 | if (debugstackmap) System.out.println("Stack map for " + code.meth); |
duke@1 | 1233 | int alenIdx = writeAttr(code.stackMap.getAttributeName(names)); |
duke@1 | 1234 | writeStackMap(code); |
duke@1 | 1235 | endAttr(alenIdx); |
duke@1 | 1236 | acount++; |
duke@1 | 1237 | } |
jjg@1755 | 1238 | |
jjg@1755 | 1239 | acount += writeTypeAnnotations(code.meth.getRawTypeAttributes(), true); |
jjg@1755 | 1240 | |
duke@1 | 1241 | endAttrs(acountIdx, acount); |
duke@1 | 1242 | } |
mcimadamore@781 | 1243 | //where |
mcimadamore@781 | 1244 | private boolean needsLocalVariableTypeEntry(Type t) { |
mcimadamore@781 | 1245 | //a local variable needs a type-entry if its type T is generic |
mcimadamore@781 | 1246 | //(i.e. |T| != T) and if it's not an intersection type (not supported |
mcimadamore@781 | 1247 | //in signature attribute grammar) |
mcimadamore@781 | 1248 | return (!types.isSameType(t, types.erasure(t)) && |
mcimadamore@781 | 1249 | !t.isCompound()); |
mcimadamore@781 | 1250 | } |
duke@1 | 1251 | |
duke@1 | 1252 | void writeStackMap(Code code) { |
duke@1 | 1253 | int nframes = code.stackMapBufferSize; |
duke@1 | 1254 | if (debugstackmap) System.out.println(" nframes = " + nframes); |
duke@1 | 1255 | databuf.appendChar(nframes); |
duke@1 | 1256 | |
duke@1 | 1257 | switch (code.stackMap) { |
duke@1 | 1258 | case CLDC: |
duke@1 | 1259 | for (int i=0; i<nframes; i++) { |
duke@1 | 1260 | if (debugstackmap) System.out.print(" " + i + ":"); |
duke@1 | 1261 | Code.StackMapFrame frame = code.stackMapBuffer[i]; |
duke@1 | 1262 | |
duke@1 | 1263 | // output PC |
duke@1 | 1264 | if (debugstackmap) System.out.print(" pc=" + frame.pc); |
duke@1 | 1265 | databuf.appendChar(frame.pc); |
duke@1 | 1266 | |
duke@1 | 1267 | // output locals |
duke@1 | 1268 | int localCount = 0; |
duke@1 | 1269 | for (int j=0; j<frame.locals.length; |
duke@1 | 1270 | j += (target.generateEmptyAfterBig() ? 1 : Code.width(frame.locals[j]))) { |
duke@1 | 1271 | localCount++; |
duke@1 | 1272 | } |
duke@1 | 1273 | if (debugstackmap) System.out.print(" nlocals=" + |
duke@1 | 1274 | localCount); |
duke@1 | 1275 | databuf.appendChar(localCount); |
duke@1 | 1276 | for (int j=0; j<frame.locals.length; |
duke@1 | 1277 | j += (target.generateEmptyAfterBig() ? 1 : Code.width(frame.locals[j]))) { |
duke@1 | 1278 | if (debugstackmap) System.out.print(" local[" + j + "]="); |
duke@1 | 1279 | writeStackMapType(frame.locals[j]); |
duke@1 | 1280 | } |
duke@1 | 1281 | |
duke@1 | 1282 | // output stack |
duke@1 | 1283 | int stackCount = 0; |
duke@1 | 1284 | for (int j=0; j<frame.stack.length; |
duke@1 | 1285 | j += (target.generateEmptyAfterBig() ? 1 : Code.width(frame.stack[j]))) { |
duke@1 | 1286 | stackCount++; |
duke@1 | 1287 | } |
duke@1 | 1288 | if (debugstackmap) System.out.print(" nstack=" + |
duke@1 | 1289 | stackCount); |
duke@1 | 1290 | databuf.appendChar(stackCount); |
duke@1 | 1291 | for (int j=0; j<frame.stack.length; |
duke@1 | 1292 | j += (target.generateEmptyAfterBig() ? 1 : Code.width(frame.stack[j]))) { |
duke@1 | 1293 | if (debugstackmap) System.out.print(" stack[" + j + "]="); |
duke@1 | 1294 | writeStackMapType(frame.stack[j]); |
duke@1 | 1295 | } |
duke@1 | 1296 | if (debugstackmap) System.out.println(); |
duke@1 | 1297 | } |
duke@1 | 1298 | break; |
duke@1 | 1299 | case JSR202: { |
jjg@816 | 1300 | Assert.checkNull(code.stackMapBuffer); |
duke@1 | 1301 | for (int i=0; i<nframes; i++) { |
duke@1 | 1302 | if (debugstackmap) System.out.print(" " + i + ":"); |
duke@1 | 1303 | StackMapTableFrame frame = code.stackMapTableBuffer[i]; |
duke@1 | 1304 | frame.write(this); |
duke@1 | 1305 | if (debugstackmap) System.out.println(); |
duke@1 | 1306 | } |
duke@1 | 1307 | break; |
duke@1 | 1308 | } |
duke@1 | 1309 | default: |
duke@1 | 1310 | throw new AssertionError("Unexpected stackmap format value"); |
duke@1 | 1311 | } |
duke@1 | 1312 | } |
duke@1 | 1313 | |
duke@1 | 1314 | //where |
duke@1 | 1315 | void writeStackMapType(Type t) { |
duke@1 | 1316 | if (t == null) { |
duke@1 | 1317 | if (debugstackmap) System.out.print("empty"); |
duke@1 | 1318 | databuf.appendByte(0); |
duke@1 | 1319 | } |
jjg@1374 | 1320 | else switch(t.getTag()) { |
duke@1 | 1321 | case BYTE: |
duke@1 | 1322 | case CHAR: |
duke@1 | 1323 | case SHORT: |
duke@1 | 1324 | case INT: |
duke@1 | 1325 | case BOOLEAN: |
duke@1 | 1326 | if (debugstackmap) System.out.print("int"); |
duke@1 | 1327 | databuf.appendByte(1); |
duke@1 | 1328 | break; |
duke@1 | 1329 | case FLOAT: |
duke@1 | 1330 | if (debugstackmap) System.out.print("float"); |
duke@1 | 1331 | databuf.appendByte(2); |
duke@1 | 1332 | break; |
duke@1 | 1333 | case DOUBLE: |
duke@1 | 1334 | if (debugstackmap) System.out.print("double"); |
duke@1 | 1335 | databuf.appendByte(3); |
duke@1 | 1336 | break; |
duke@1 | 1337 | case LONG: |
duke@1 | 1338 | if (debugstackmap) System.out.print("long"); |
duke@1 | 1339 | databuf.appendByte(4); |
duke@1 | 1340 | break; |
duke@1 | 1341 | case BOT: // null |
duke@1 | 1342 | if (debugstackmap) System.out.print("null"); |
duke@1 | 1343 | databuf.appendByte(5); |
duke@1 | 1344 | break; |
duke@1 | 1345 | case CLASS: |
duke@1 | 1346 | case ARRAY: |
duke@1 | 1347 | if (debugstackmap) System.out.print("object(" + t + ")"); |
duke@1 | 1348 | databuf.appendByte(7); |
duke@1 | 1349 | databuf.appendChar(pool.put(t)); |
duke@1 | 1350 | break; |
duke@1 | 1351 | case TYPEVAR: |
duke@1 | 1352 | if (debugstackmap) System.out.print("object(" + types.erasure(t).tsym + ")"); |
duke@1 | 1353 | databuf.appendByte(7); |
duke@1 | 1354 | databuf.appendChar(pool.put(types.erasure(t).tsym)); |
duke@1 | 1355 | break; |
duke@1 | 1356 | case UNINITIALIZED_THIS: |
duke@1 | 1357 | if (debugstackmap) System.out.print("uninit_this"); |
duke@1 | 1358 | databuf.appendByte(6); |
duke@1 | 1359 | break; |
duke@1 | 1360 | case UNINITIALIZED_OBJECT: |
duke@1 | 1361 | { UninitializedType uninitType = (UninitializedType)t; |
duke@1 | 1362 | databuf.appendByte(8); |
duke@1 | 1363 | if (debugstackmap) System.out.print("uninit_object@" + uninitType.offset); |
duke@1 | 1364 | databuf.appendChar(uninitType.offset); |
duke@1 | 1365 | } |
duke@1 | 1366 | break; |
duke@1 | 1367 | default: |
duke@1 | 1368 | throw new AssertionError(); |
duke@1 | 1369 | } |
duke@1 | 1370 | } |
duke@1 | 1371 | |
duke@1 | 1372 | /** An entry in the JSR202 StackMapTable */ |
duke@1 | 1373 | abstract static class StackMapTableFrame { |
duke@1 | 1374 | abstract int getFrameType(); |
duke@1 | 1375 | |
duke@1 | 1376 | void write(ClassWriter writer) { |
duke@1 | 1377 | int frameType = getFrameType(); |
duke@1 | 1378 | writer.databuf.appendByte(frameType); |
duke@1 | 1379 | if (writer.debugstackmap) System.out.print(" frame_type=" + frameType); |
duke@1 | 1380 | } |
duke@1 | 1381 | |
duke@1 | 1382 | static class SameFrame extends StackMapTableFrame { |
duke@1 | 1383 | final int offsetDelta; |
duke@1 | 1384 | SameFrame(int offsetDelta) { |
duke@1 | 1385 | this.offsetDelta = offsetDelta; |
duke@1 | 1386 | } |
duke@1 | 1387 | int getFrameType() { |
duke@1 | 1388 | return (offsetDelta < SAME_FRAME_SIZE) ? offsetDelta : SAME_FRAME_EXTENDED; |
duke@1 | 1389 | } |
duke@1 | 1390 | @Override |
duke@1 | 1391 | void write(ClassWriter writer) { |
duke@1 | 1392 | super.write(writer); |
duke@1 | 1393 | if (getFrameType() == SAME_FRAME_EXTENDED) { |
duke@1 | 1394 | writer.databuf.appendChar(offsetDelta); |
duke@1 | 1395 | if (writer.debugstackmap){ |
duke@1 | 1396 | System.out.print(" offset_delta=" + offsetDelta); |
duke@1 | 1397 | } |
duke@1 | 1398 | } |
duke@1 | 1399 | } |
duke@1 | 1400 | } |
duke@1 | 1401 | |
duke@1 | 1402 | static class SameLocals1StackItemFrame extends StackMapTableFrame { |
duke@1 | 1403 | final int offsetDelta; |
duke@1 | 1404 | final Type stack; |
duke@1 | 1405 | SameLocals1StackItemFrame(int offsetDelta, Type stack) { |
duke@1 | 1406 | this.offsetDelta = offsetDelta; |
duke@1 | 1407 | this.stack = stack; |
duke@1 | 1408 | } |
duke@1 | 1409 | int getFrameType() { |
duke@1 | 1410 | return (offsetDelta < SAME_FRAME_SIZE) ? |
duke@1 | 1411 | (SAME_FRAME_SIZE + offsetDelta) : |
duke@1 | 1412 | SAME_LOCALS_1_STACK_ITEM_EXTENDED; |
duke@1 | 1413 | } |
duke@1 | 1414 | @Override |
duke@1 | 1415 | void write(ClassWriter writer) { |
duke@1 | 1416 | super.write(writer); |
duke@1 | 1417 | if (getFrameType() == SAME_LOCALS_1_STACK_ITEM_EXTENDED) { |
duke@1 | 1418 | writer.databuf.appendChar(offsetDelta); |
duke@1 | 1419 | if (writer.debugstackmap) { |
duke@1 | 1420 | System.out.print(" offset_delta=" + offsetDelta); |
duke@1 | 1421 | } |
duke@1 | 1422 | } |
duke@1 | 1423 | if (writer.debugstackmap) { |
duke@1 | 1424 | System.out.print(" stack[" + 0 + "]="); |
duke@1 | 1425 | } |
duke@1 | 1426 | writer.writeStackMapType(stack); |
duke@1 | 1427 | } |
duke@1 | 1428 | } |
duke@1 | 1429 | |
duke@1 | 1430 | static class ChopFrame extends StackMapTableFrame { |
duke@1 | 1431 | final int frameType; |
duke@1 | 1432 | final int offsetDelta; |
duke@1 | 1433 | ChopFrame(int frameType, int offsetDelta) { |
duke@1 | 1434 | this.frameType = frameType; |
duke@1 | 1435 | this.offsetDelta = offsetDelta; |
duke@1 | 1436 | } |
duke@1 | 1437 | int getFrameType() { return frameType; } |
duke@1 | 1438 | @Override |
duke@1 | 1439 | void write(ClassWriter writer) { |
duke@1 | 1440 | super.write(writer); |
duke@1 | 1441 | writer.databuf.appendChar(offsetDelta); |
duke@1 | 1442 | if (writer.debugstackmap) { |
duke@1 | 1443 | System.out.print(" offset_delta=" + offsetDelta); |
duke@1 | 1444 | } |
duke@1 | 1445 | } |
duke@1 | 1446 | } |
duke@1 | 1447 | |
duke@1 | 1448 | static class AppendFrame extends StackMapTableFrame { |
duke@1 | 1449 | final int frameType; |
duke@1 | 1450 | final int offsetDelta; |
duke@1 | 1451 | final Type[] locals; |
duke@1 | 1452 | AppendFrame(int frameType, int offsetDelta, Type[] locals) { |
duke@1 | 1453 | this.frameType = frameType; |
duke@1 | 1454 | this.offsetDelta = offsetDelta; |
duke@1 | 1455 | this.locals = locals; |
duke@1 | 1456 | } |
duke@1 | 1457 | int getFrameType() { return frameType; } |
duke@1 | 1458 | @Override |
duke@1 | 1459 | void write(ClassWriter writer) { |
duke@1 | 1460 | super.write(writer); |
duke@1 | 1461 | writer.databuf.appendChar(offsetDelta); |
duke@1 | 1462 | if (writer.debugstackmap) { |
duke@1 | 1463 | System.out.print(" offset_delta=" + offsetDelta); |
duke@1 | 1464 | } |
duke@1 | 1465 | for (int i=0; i<locals.length; i++) { |
duke@1 | 1466 | if (writer.debugstackmap) System.out.print(" locals[" + i + "]="); |
duke@1 | 1467 | writer.writeStackMapType(locals[i]); |
duke@1 | 1468 | } |
duke@1 | 1469 | } |
duke@1 | 1470 | } |
duke@1 | 1471 | |
duke@1 | 1472 | static class FullFrame extends StackMapTableFrame { |
duke@1 | 1473 | final int offsetDelta; |
duke@1 | 1474 | final Type[] locals; |
duke@1 | 1475 | final Type[] stack; |
duke@1 | 1476 | FullFrame(int offsetDelta, Type[] locals, Type[] stack) { |
duke@1 | 1477 | this.offsetDelta = offsetDelta; |
duke@1 | 1478 | this.locals = locals; |
duke@1 | 1479 | this.stack = stack; |
duke@1 | 1480 | } |
duke@1 | 1481 | int getFrameType() { return FULL_FRAME; } |
duke@1 | 1482 | @Override |
duke@1 | 1483 | void write(ClassWriter writer) { |
duke@1 | 1484 | super.write(writer); |
duke@1 | 1485 | writer.databuf.appendChar(offsetDelta); |
duke@1 | 1486 | writer.databuf.appendChar(locals.length); |
duke@1 | 1487 | if (writer.debugstackmap) { |
duke@1 | 1488 | System.out.print(" offset_delta=" + offsetDelta); |
duke@1 | 1489 | System.out.print(" nlocals=" + locals.length); |
duke@1 | 1490 | } |
duke@1 | 1491 | for (int i=0; i<locals.length; i++) { |
duke@1 | 1492 | if (writer.debugstackmap) System.out.print(" locals[" + i + "]="); |
duke@1 | 1493 | writer.writeStackMapType(locals[i]); |
duke@1 | 1494 | } |
duke@1 | 1495 | |
duke@1 | 1496 | writer.databuf.appendChar(stack.length); |
duke@1 | 1497 | if (writer.debugstackmap) { System.out.print(" nstack=" + stack.length); } |
duke@1 | 1498 | for (int i=0; i<stack.length; i++) { |
duke@1 | 1499 | if (writer.debugstackmap) System.out.print(" stack[" + i + "]="); |
duke@1 | 1500 | writer.writeStackMapType(stack[i]); |
duke@1 | 1501 | } |
duke@1 | 1502 | } |
duke@1 | 1503 | } |
duke@1 | 1504 | |
duke@1 | 1505 | /** Compare this frame with the previous frame and produce |
duke@1 | 1506 | * an entry of compressed stack map frame. */ |
duke@1 | 1507 | static StackMapTableFrame getInstance(Code.StackMapFrame this_frame, |
duke@1 | 1508 | int prev_pc, |
duke@1 | 1509 | Type[] prev_locals, |
duke@1 | 1510 | Types types) { |
duke@1 | 1511 | Type[] locals = this_frame.locals; |
duke@1 | 1512 | Type[] stack = this_frame.stack; |
duke@1 | 1513 | int offset_delta = this_frame.pc - prev_pc - 1; |
duke@1 | 1514 | if (stack.length == 1) { |
duke@1 | 1515 | if (locals.length == prev_locals.length |
duke@1 | 1516 | && compare(prev_locals, locals, types) == 0) { |
duke@1 | 1517 | return new SameLocals1StackItemFrame(offset_delta, stack[0]); |
duke@1 | 1518 | } |
duke@1 | 1519 | } else if (stack.length == 0) { |
duke@1 | 1520 | int diff_length = compare(prev_locals, locals, types); |
duke@1 | 1521 | if (diff_length == 0) { |
duke@1 | 1522 | return new SameFrame(offset_delta); |
duke@1 | 1523 | } else if (-MAX_LOCAL_LENGTH_DIFF < diff_length && diff_length < 0) { |
duke@1 | 1524 | // APPEND |
duke@1 | 1525 | Type[] local_diff = new Type[-diff_length]; |
duke@1 | 1526 | for (int i=prev_locals.length, j=0; i<locals.length; i++,j++) { |
duke@1 | 1527 | local_diff[j] = locals[i]; |
duke@1 | 1528 | } |
duke@1 | 1529 | return new AppendFrame(SAME_FRAME_EXTENDED - diff_length, |
duke@1 | 1530 | offset_delta, |
duke@1 | 1531 | local_diff); |
duke@1 | 1532 | } else if (0 < diff_length && diff_length < MAX_LOCAL_LENGTH_DIFF) { |
duke@1 | 1533 | // CHOP |
duke@1 | 1534 | return new ChopFrame(SAME_FRAME_EXTENDED - diff_length, |
duke@1 | 1535 | offset_delta); |
duke@1 | 1536 | } |
duke@1 | 1537 | } |
duke@1 | 1538 | // FULL_FRAME |
duke@1 | 1539 | return new FullFrame(offset_delta, locals, stack); |
duke@1 | 1540 | } |
duke@1 | 1541 | |
duke@1 | 1542 | static boolean isInt(Type t) { |
jjg@1374 | 1543 | return (t.getTag().isStrictSubRangeOf(INT) || t.hasTag(BOOLEAN)); |
duke@1 | 1544 | } |
duke@1 | 1545 | |
duke@1 | 1546 | static boolean isSameType(Type t1, Type t2, Types types) { |
duke@1 | 1547 | if (t1 == null) { return t2 == null; } |
duke@1 | 1548 | if (t2 == null) { return false; } |
duke@1 | 1549 | |
duke@1 | 1550 | if (isInt(t1) && isInt(t2)) { return true; } |
duke@1 | 1551 | |
jjg@1374 | 1552 | if (t1.hasTag(UNINITIALIZED_THIS)) { |
jjg@1374 | 1553 | return t2.hasTag(UNINITIALIZED_THIS); |
jjg@1374 | 1554 | } else if (t1.hasTag(UNINITIALIZED_OBJECT)) { |
jjg@1374 | 1555 | if (t2.hasTag(UNINITIALIZED_OBJECT)) { |
duke@1 | 1556 | return ((UninitializedType)t1).offset == ((UninitializedType)t2).offset; |
duke@1 | 1557 | } else { |
duke@1 | 1558 | return false; |
duke@1 | 1559 | } |
jjg@1374 | 1560 | } else if (t2.hasTag(UNINITIALIZED_THIS) || t2.hasTag(UNINITIALIZED_OBJECT)) { |
duke@1 | 1561 | return false; |
duke@1 | 1562 | } |
duke@1 | 1563 | |
duke@1 | 1564 | return types.isSameType(t1, t2); |
duke@1 | 1565 | } |
duke@1 | 1566 | |
duke@1 | 1567 | static int compare(Type[] arr1, Type[] arr2, Types types) { |
duke@1 | 1568 | int diff_length = arr1.length - arr2.length; |
duke@1 | 1569 | if (diff_length > MAX_LOCAL_LENGTH_DIFF || diff_length < -MAX_LOCAL_LENGTH_DIFF) { |
duke@1 | 1570 | return Integer.MAX_VALUE; |
duke@1 | 1571 | } |
duke@1 | 1572 | int len = (diff_length > 0) ? arr2.length : arr1.length; |
duke@1 | 1573 | for (int i=0; i<len; i++) { |
duke@1 | 1574 | if (!isSameType(arr1[i], arr2[i], types)) { |
duke@1 | 1575 | return Integer.MAX_VALUE; |
duke@1 | 1576 | } |
duke@1 | 1577 | } |
duke@1 | 1578 | return diff_length; |
duke@1 | 1579 | } |
duke@1 | 1580 | } |
duke@1 | 1581 | |
duke@1 | 1582 | void writeFields(Scope.Entry e) { |
duke@1 | 1583 | // process them in reverse sibling order; |
duke@1 | 1584 | // i.e., process them in declaration order. |
duke@1 | 1585 | List<VarSymbol> vars = List.nil(); |
duke@1 | 1586 | for (Scope.Entry i = e; i != null; i = i.sibling) { |
duke@1 | 1587 | if (i.sym.kind == VAR) vars = vars.prepend((VarSymbol)i.sym); |
duke@1 | 1588 | } |
duke@1 | 1589 | while (vars.nonEmpty()) { |
duke@1 | 1590 | writeField(vars.head); |
duke@1 | 1591 | vars = vars.tail; |
duke@1 | 1592 | } |
duke@1 | 1593 | } |
duke@1 | 1594 | |
duke@1 | 1595 | void writeMethods(Scope.Entry e) { |
duke@1 | 1596 | List<MethodSymbol> methods = List.nil(); |
duke@1 | 1597 | for (Scope.Entry i = e; i != null; i = i.sibling) { |
duke@1 | 1598 | if (i.sym.kind == MTH && (i.sym.flags() & HYPOTHETICAL) == 0) |
duke@1 | 1599 | methods = methods.prepend((MethodSymbol)i.sym); |
duke@1 | 1600 | } |
duke@1 | 1601 | while (methods.nonEmpty()) { |
duke@1 | 1602 | writeMethod(methods.head); |
duke@1 | 1603 | methods = methods.tail; |
duke@1 | 1604 | } |
duke@1 | 1605 | } |
duke@1 | 1606 | |
duke@1 | 1607 | /** Emit a class file for a given class. |
duke@1 | 1608 | * @param c The class from which a class file is generated. |
duke@1 | 1609 | */ |
duke@1 | 1610 | public JavaFileObject writeClass(ClassSymbol c) |
duke@1 | 1611 | throws IOException, PoolOverflow, StringOverflow |
duke@1 | 1612 | { |
duke@1 | 1613 | JavaFileObject outFile |
duke@1 | 1614 | = fileManager.getJavaFileForOutput(CLASS_OUTPUT, |
duke@1 | 1615 | c.flatname.toString(), |
duke@1 | 1616 | JavaFileObject.Kind.CLASS, |
duke@1 | 1617 | c.sourcefile); |
duke@1 | 1618 | OutputStream out = outFile.openOutputStream(); |
duke@1 | 1619 | try { |
duke@1 | 1620 | writeClassFile(out, c); |
duke@1 | 1621 | if (verbose) |
jjg@909 | 1622 | log.printVerbose("wrote.file", outFile); |
duke@1 | 1623 | out.close(); |
duke@1 | 1624 | out = null; |
duke@1 | 1625 | } finally { |
duke@1 | 1626 | if (out != null) { |
jjg@1755 | 1627 | // if we are propagating an exception, delete the file |
duke@1 | 1628 | out.close(); |
duke@1 | 1629 | outFile.delete(); |
duke@1 | 1630 | outFile = null; |
duke@1 | 1631 | } |
duke@1 | 1632 | } |
duke@1 | 1633 | return outFile; // may be null if write failed |
duke@1 | 1634 | } |
duke@1 | 1635 | |
duke@1 | 1636 | /** Write class `c' to outstream `out'. |
duke@1 | 1637 | */ |
duke@1 | 1638 | public void writeClassFile(OutputStream out, ClassSymbol c) |
duke@1 | 1639 | throws IOException, PoolOverflow, StringOverflow { |
jjg@816 | 1640 | Assert.check((c.flags() & COMPOUND) == 0); |
duke@1 | 1641 | databuf.reset(); |
duke@1 | 1642 | poolbuf.reset(); |
rfield@1587 | 1643 | signatureGen.reset(); |
duke@1 | 1644 | pool = c.pool; |
duke@1 | 1645 | innerClasses = null; |
duke@1 | 1646 | innerClassesQueue = null; |
vromero@1452 | 1647 | bootstrapMethods = new LinkedHashMap<DynamicMethod, MethodHandle>(); |
duke@1 | 1648 | |
duke@1 | 1649 | Type supertype = types.supertype(c.type); |
duke@1 | 1650 | List<Type> interfaces = types.interfaces(c.type); |
duke@1 | 1651 | List<Type> typarams = c.type.getTypeArguments(); |
duke@1 | 1652 | |
mcimadamore@1393 | 1653 | int flags = adjustFlags(c.flags() & ~DEFAULT); |
duke@1 | 1654 | if ((flags & PROTECTED) != 0) flags |= PUBLIC; |
duke@1 | 1655 | flags = flags & ClassFlags & ~STRICTFP; |
duke@1 | 1656 | if ((flags & INTERFACE) == 0) flags |= ACC_SUPER; |
duke@1 | 1657 | if (c.isInner() && c.name.isEmpty()) flags &= ~FINAL; |
duke@1 | 1658 | if (dumpClassModifiers) { |
jjg@1135 | 1659 | PrintWriter pw = log.getWriter(Log.WriterKind.ERROR); |
jjg@1135 | 1660 | pw.println(); |
jjg@1135 | 1661 | pw.println("CLASSFILE " + c.getQualifiedName()); |
jjg@1135 | 1662 | pw.println("---" + flagNames(flags)); |
duke@1 | 1663 | } |
duke@1 | 1664 | databuf.appendChar(flags); |
duke@1 | 1665 | |
duke@1 | 1666 | databuf.appendChar(pool.put(c)); |
jjg@1374 | 1667 | databuf.appendChar(supertype.hasTag(CLASS) ? pool.put(supertype.tsym) : 0); |
duke@1 | 1668 | databuf.appendChar(interfaces.length()); |
duke@1 | 1669 | for (List<Type> l = interfaces; l.nonEmpty(); l = l.tail) |
duke@1 | 1670 | databuf.appendChar(pool.put(l.head.tsym)); |
duke@1 | 1671 | int fieldsCount = 0; |
duke@1 | 1672 | int methodsCount = 0; |
duke@1 | 1673 | for (Scope.Entry e = c.members().elems; e != null; e = e.sibling) { |
duke@1 | 1674 | switch (e.sym.kind) { |
duke@1 | 1675 | case VAR: fieldsCount++; break; |
duke@1 | 1676 | case MTH: if ((e.sym.flags() & HYPOTHETICAL) == 0) methodsCount++; |
duke@1 | 1677 | break; |
duke@1 | 1678 | case TYP: enterInner((ClassSymbol)e.sym); break; |
jjg@816 | 1679 | default : Assert.error(); |
duke@1 | 1680 | } |
duke@1 | 1681 | } |
mcimadamore@1086 | 1682 | |
mcimadamore@1086 | 1683 | if (c.trans_local != null) { |
mcimadamore@1086 | 1684 | for (ClassSymbol local : c.trans_local) { |
mcimadamore@1086 | 1685 | enterInner(local); |
mcimadamore@1086 | 1686 | } |
mcimadamore@1086 | 1687 | } |
mcimadamore@1086 | 1688 | |
duke@1 | 1689 | databuf.appendChar(fieldsCount); |
duke@1 | 1690 | writeFields(c.members().elems); |
duke@1 | 1691 | databuf.appendChar(methodsCount); |
duke@1 | 1692 | writeMethods(c.members().elems); |
duke@1 | 1693 | |
duke@1 | 1694 | int acountIdx = beginAttrs(); |
duke@1 | 1695 | int acount = 0; |
duke@1 | 1696 | |
duke@1 | 1697 | boolean sigReq = |
mcimadamore@297 | 1698 | typarams.length() != 0 || supertype.allparams().length() != 0; |
duke@1 | 1699 | for (List<Type> l = interfaces; !sigReq && l.nonEmpty(); l = l.tail) |
mcimadamore@297 | 1700 | sigReq = l.head.allparams().length() != 0; |
duke@1 | 1701 | if (sigReq) { |
jjg@816 | 1702 | Assert.check(source.allowGenerics()); |
duke@1 | 1703 | int alenIdx = writeAttr(names.Signature); |
rfield@1587 | 1704 | if (typarams.length() != 0) signatureGen.assembleParamsSig(typarams); |
rfield@1587 | 1705 | signatureGen.assembleSig(supertype); |
duke@1 | 1706 | for (List<Type> l = interfaces; l.nonEmpty(); l = l.tail) |
rfield@1587 | 1707 | signatureGen.assembleSig(l.head); |
rfield@1587 | 1708 | databuf.appendChar(pool.put(signatureGen.toName())); |
rfield@1587 | 1709 | signatureGen.reset(); |
duke@1 | 1710 | endAttr(alenIdx); |
duke@1 | 1711 | acount++; |
duke@1 | 1712 | } |
duke@1 | 1713 | |
duke@1 | 1714 | if (c.sourcefile != null && emitSourceFile) { |
duke@1 | 1715 | int alenIdx = writeAttr(names.SourceFile); |
duke@1 | 1716 | // WHM 6/29/1999: Strip file path prefix. We do it here at |
duke@1 | 1717 | // the last possible moment because the sourcefile may be used |
duke@1 | 1718 | // elsewhere in error diagnostics. Fixes 4241573. |
duke@1 | 1719 | //databuf.appendChar(c.pool.put(c.sourcefile)); |
jjg@415 | 1720 | String simpleName = BaseFileObject.getSimpleName(c.sourcefile); |
jjg@415 | 1721 | databuf.appendChar(c.pool.put(names.fromString(simpleName))); |
duke@1 | 1722 | endAttr(alenIdx); |
duke@1 | 1723 | acount++; |
duke@1 | 1724 | } |
duke@1 | 1725 | |
duke@1 | 1726 | if (genCrt) { |
duke@1 | 1727 | // Append SourceID attribute |
duke@1 | 1728 | int alenIdx = writeAttr(names.SourceID); |
duke@1 | 1729 | databuf.appendChar(c.pool.put(names.fromString(Long.toString(getLastModified(c.sourcefile))))); |
duke@1 | 1730 | endAttr(alenIdx); |
duke@1 | 1731 | acount++; |
duke@1 | 1732 | // Append CompilationID attribute |
duke@1 | 1733 | alenIdx = writeAttr(names.CompilationID); |
duke@1 | 1734 | databuf.appendChar(c.pool.put(names.fromString(Long.toString(System.currentTimeMillis())))); |
duke@1 | 1735 | endAttr(alenIdx); |
duke@1 | 1736 | acount++; |
duke@1 | 1737 | } |
duke@1 | 1738 | |
duke@1 | 1739 | acount += writeFlagAttrs(c.flags()); |
jfranck@1464 | 1740 | acount += writeJavaAnnotations(c.getRawAttributes()); |
jjg@1755 | 1741 | acount += writeTypeAnnotations(c.getRawTypeAttributes(), false); |
duke@1 | 1742 | acount += writeEnclosingMethodAttribute(c); |
ksrini@1309 | 1743 | acount += writeExtraClassAttributes(c); |
duke@1 | 1744 | |
duke@1 | 1745 | poolbuf.appendInt(JAVA_MAGIC); |
duke@1 | 1746 | poolbuf.appendChar(target.minorVersion); |
duke@1 | 1747 | poolbuf.appendChar(target.majorVersion); |
duke@1 | 1748 | |
duke@1 | 1749 | writePool(c.pool); |
duke@1 | 1750 | |
duke@1 | 1751 | if (innerClasses != null) { |
duke@1 | 1752 | writeInnerClasses(); |
duke@1 | 1753 | acount++; |
duke@1 | 1754 | } |
mcimadamore@1336 | 1755 | |
mcimadamore@1336 | 1756 | if (!bootstrapMethods.isEmpty()) { |
mcimadamore@1336 | 1757 | writeBootstrapMethods(); |
mcimadamore@1336 | 1758 | acount++; |
mcimadamore@1336 | 1759 | } |
mcimadamore@1336 | 1760 | |
duke@1 | 1761 | endAttrs(acountIdx, acount); |
duke@1 | 1762 | |
duke@1 | 1763 | poolbuf.appendBytes(databuf.elems, 0, databuf.length); |
duke@1 | 1764 | out.write(poolbuf.elems, 0, poolbuf.length); |
duke@1 | 1765 | |
duke@1 | 1766 | pool = c.pool = null; // to conserve space |
duke@1 | 1767 | } |
duke@1 | 1768 | |
ksrini@1309 | 1769 | /**Allows subclasses to write additional class attributes |
ksrini@1309 | 1770 | * |
ksrini@1309 | 1771 | * @return the number of attributes written |
ksrini@1309 | 1772 | */ |
ksrini@1309 | 1773 | protected int writeExtraClassAttributes(ClassSymbol c) { |
ksrini@1309 | 1774 | return 0; |
ksrini@1309 | 1775 | } |
ksrini@1309 | 1776 | |
duke@1 | 1777 | int adjustFlags(final long flags) { |
duke@1 | 1778 | int result = (int)flags; |
duke@1 | 1779 | if ((flags & SYNTHETIC) != 0 && !target.useSyntheticFlag()) |
duke@1 | 1780 | result &= ~SYNTHETIC; |
duke@1 | 1781 | if ((flags & ENUM) != 0 && !target.useEnumFlag()) |
duke@1 | 1782 | result &= ~ENUM; |
duke@1 | 1783 | if ((flags & ANNOTATION) != 0 && !target.useAnnotationFlag()) |
duke@1 | 1784 | result &= ~ANNOTATION; |
duke@1 | 1785 | |
duke@1 | 1786 | if ((flags & BRIDGE) != 0 && target.useBridgeFlag()) |
duke@1 | 1787 | result |= ACC_BRIDGE; |
duke@1 | 1788 | if ((flags & VARARGS) != 0 && target.useVarargsFlag()) |
duke@1 | 1789 | result |= ACC_VARARGS; |
mcimadamore@1393 | 1790 | if ((flags & DEFAULT) != 0) |
mcimadamore@1393 | 1791 | result &= ~ABSTRACT; |
duke@1 | 1792 | return result; |
duke@1 | 1793 | } |
duke@1 | 1794 | |
duke@1 | 1795 | long getLastModified(FileObject filename) { |
duke@1 | 1796 | long mod = 0; |
duke@1 | 1797 | try { |
duke@1 | 1798 | mod = filename.getLastModified(); |
duke@1 | 1799 | } catch (SecurityException e) { |
duke@1 | 1800 | throw new AssertionError("CRT: couldn't get source file modification date: " + e.getMessage()); |
duke@1 | 1801 | } |
duke@1 | 1802 | return mod; |
duke@1 | 1803 | } |
duke@1 | 1804 | } |