src/share/classes/com/sun/tools/javac/jvm/ClassReader.java

Tue, 28 Jul 2009 12:12:59 -0700

author
xdono
date
Tue, 28 Jul 2009 12:12:59 -0700
changeset 323
14b1a8ede954
parent 308
03944ee4fac4
child 400
35e29f51a7c3
permissions
-rw-r--r--

6862919: Update copyright year
Summary: Update copyright for files that have been modified in 2009, up to 07/09
Reviewed-by: tbell, ohair

duke@1 1 /*
xdono@54 2 * Copyright 1999-2008 Sun Microsystems, Inc. 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
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun 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 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any 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.*;
duke@1 29 import java.net.URI;
duke@1 30 import java.nio.CharBuffer;
duke@1 31 import java.util.EnumSet;
duke@1 32 import java.util.HashMap;
duke@1 33 import java.util.Map;
duke@1 34 import java.util.Set;
duke@1 35 import javax.lang.model.SourceVersion;
duke@1 36 import javax.tools.JavaFileObject;
duke@1 37 import javax.tools.JavaFileManager;
jjg@256 38 import javax.tools.JavaFileManager.Location;
duke@1 39 import javax.tools.StandardJavaFileManager;
duke@1 40
jjg@256 41 import static javax.tools.StandardLocation.*;
jjg@256 42
duke@1 43 import com.sun.tools.javac.comp.Annotate;
duke@1 44 import com.sun.tools.javac.code.*;
duke@1 45 import com.sun.tools.javac.code.Type.*;
duke@1 46 import com.sun.tools.javac.code.Symbol.*;
duke@1 47 import com.sun.tools.javac.code.Symtab;
jjg@50 48 import com.sun.tools.javac.file.BaseFileObject;
duke@1 49 import com.sun.tools.javac.util.*;
duke@1 50
duke@1 51 import static com.sun.tools.javac.code.Flags.*;
duke@1 52 import static com.sun.tools.javac.code.Kinds.*;
duke@1 53 import static com.sun.tools.javac.code.TypeTags.*;
jjg@256 54 import static com.sun.tools.javac.jvm.ClassFile.*;
jjg@256 55 import static com.sun.tools.javac.jvm.ClassFile.Version.*;
duke@1 56
duke@1 57 /** This class provides operations to read a classfile into an internal
duke@1 58 * representation. The internal representation is anchored in a
duke@1 59 * ClassSymbol which contains in its scope symbol representations
duke@1 60 * for all other definitions in the classfile. Top-level Classes themselves
duke@1 61 * appear as members of the scopes of PackageSymbols.
duke@1 62 *
duke@1 63 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 64 * you write code that depends on this, you do so at your own risk.
duke@1 65 * This code and its internal interfaces are subject to change or
duke@1 66 * deletion without notice.</b>
duke@1 67 */
jjg@256 68 public class ClassReader implements Completer {
duke@1 69 /** The context key for the class reader. */
duke@1 70 protected static final Context.Key<ClassReader> classReaderKey =
duke@1 71 new Context.Key<ClassReader>();
duke@1 72
duke@1 73 Annotate annotate;
duke@1 74
duke@1 75 /** Switch: verbose output.
duke@1 76 */
duke@1 77 boolean verbose;
duke@1 78
duke@1 79 /** Switch: check class file for correct minor version, unrecognized
duke@1 80 * attributes.
duke@1 81 */
duke@1 82 boolean checkClassFile;
duke@1 83
duke@1 84 /** Switch: read constant pool and code sections. This switch is initially
duke@1 85 * set to false but can be turned on from outside.
duke@1 86 */
duke@1 87 public boolean readAllOfClassFile = false;
duke@1 88
duke@1 89 /** Switch: read GJ signature information.
duke@1 90 */
duke@1 91 boolean allowGenerics;
duke@1 92
duke@1 93 /** Switch: read varargs attribute.
duke@1 94 */
duke@1 95 boolean allowVarargs;
duke@1 96
duke@1 97 /** Switch: allow annotations.
duke@1 98 */
duke@1 99 boolean allowAnnotations;
duke@1 100
duke@1 101 /** Switch: preserve parameter names from the variable table.
duke@1 102 */
duke@1 103 public boolean saveParameterNames;
duke@1 104
duke@1 105 /**
duke@1 106 * Switch: cache completion failures unless -XDdev is used
duke@1 107 */
duke@1 108 private boolean cacheCompletionFailure;
duke@1 109
duke@1 110 /**
duke@1 111 * Switch: prefer source files instead of newer when both source
duke@1 112 * and class are available
duke@1 113 **/
duke@1 114 public boolean preferSource;
duke@1 115
duke@1 116 /** The log to use for verbose output
duke@1 117 */
duke@1 118 final Log log;
duke@1 119
duke@1 120 /** The symbol table. */
duke@1 121 Symtab syms;
duke@1 122
duke@1 123 Types types;
duke@1 124
duke@1 125 /** The name table. */
jjg@113 126 final Names names;
duke@1 127
duke@1 128 /** Force a completion failure on this name
duke@1 129 */
duke@1 130 final Name completionFailureName;
duke@1 131
duke@1 132 /** Access to files
duke@1 133 */
duke@1 134 private final JavaFileManager fileManager;
duke@1 135
jjg@12 136 /** Factory for diagnostics
jjg@12 137 */
jjg@12 138 JCDiagnostic.Factory diagFactory;
jjg@12 139
duke@1 140 /** Can be reassigned from outside:
duke@1 141 * the completer to be used for ".java" files. If this remains unassigned
duke@1 142 * ".java" files will not be loaded.
duke@1 143 */
duke@1 144 public SourceCompleter sourceCompleter = null;
duke@1 145
duke@1 146 /** A hashtable containing the encountered top-level and member classes,
duke@1 147 * indexed by flat names. The table does not contain local classes.
duke@1 148 */
duke@1 149 private Map<Name,ClassSymbol> classes;
duke@1 150
duke@1 151 /** A hashtable containing the encountered packages.
duke@1 152 */
duke@1 153 private Map<Name, PackageSymbol> packages;
duke@1 154
duke@1 155 /** The current scope where type variables are entered.
duke@1 156 */
duke@1 157 protected Scope typevars;
duke@1 158
duke@1 159 /** The path name of the class file currently being read.
duke@1 160 */
duke@1 161 protected JavaFileObject currentClassFile = null;
duke@1 162
duke@1 163 /** The class or method currently being read.
duke@1 164 */
duke@1 165 protected Symbol currentOwner = null;
duke@1 166
duke@1 167 /** The buffer containing the currently read class file.
duke@1 168 */
duke@1 169 byte[] buf = new byte[0x0fff0];
duke@1 170
duke@1 171 /** The current input pointer.
duke@1 172 */
duke@1 173 int bp;
duke@1 174
duke@1 175 /** The objects of the constant pool.
duke@1 176 */
duke@1 177 Object[] poolObj;
duke@1 178
duke@1 179 /** For every constant pool entry, an index into buf where the
duke@1 180 * defining section of the entry is found.
duke@1 181 */
duke@1 182 int[] poolIdx;
duke@1 183
jjg@256 184 /** The major version number of the class file being read. */
jjg@256 185 int majorVersion;
jjg@256 186 /** The minor version number of the class file being read. */
jjg@256 187 int minorVersion;
jjg@256 188
jjg@308 189 /** Switch: debug output for JSR 308-related operations.
jjg@308 190 */
jjg@308 191 boolean debugJSR308;
jjg@308 192
duke@1 193 /** Get the ClassReader instance for this invocation. */
duke@1 194 public static ClassReader instance(Context context) {
duke@1 195 ClassReader instance = context.get(classReaderKey);
duke@1 196 if (instance == null)
duke@1 197 instance = new ClassReader(context, true);
duke@1 198 return instance;
duke@1 199 }
duke@1 200
duke@1 201 /** Initialize classes and packages, treating this as the definitive classreader. */
duke@1 202 public void init(Symtab syms) {
duke@1 203 init(syms, true);
duke@1 204 }
duke@1 205
duke@1 206 /** Initialize classes and packages, optionally treating this as
duke@1 207 * the definitive classreader.
duke@1 208 */
duke@1 209 private void init(Symtab syms, boolean definitive) {
duke@1 210 if (classes != null) return;
duke@1 211
duke@1 212 if (definitive) {
duke@1 213 assert packages == null || packages == syms.packages;
duke@1 214 packages = syms.packages;
duke@1 215 assert classes == null || classes == syms.classes;
duke@1 216 classes = syms.classes;
duke@1 217 } else {
duke@1 218 packages = new HashMap<Name, PackageSymbol>();
duke@1 219 classes = new HashMap<Name, ClassSymbol>();
duke@1 220 }
duke@1 221
duke@1 222 packages.put(names.empty, syms.rootPackage);
duke@1 223 syms.rootPackage.completer = this;
duke@1 224 syms.unnamedPackage.completer = this;
duke@1 225 }
duke@1 226
duke@1 227 /** Construct a new class reader, optionally treated as the
duke@1 228 * definitive classreader for this invocation.
duke@1 229 */
duke@1 230 protected ClassReader(Context context, boolean definitive) {
duke@1 231 if (definitive) context.put(classReaderKey, this);
duke@1 232
jjg@113 233 names = Names.instance(context);
duke@1 234 syms = Symtab.instance(context);
duke@1 235 types = Types.instance(context);
duke@1 236 fileManager = context.get(JavaFileManager.class);
duke@1 237 if (fileManager == null)
duke@1 238 throw new AssertionError("FileManager initialization error");
jjg@12 239 diagFactory = JCDiagnostic.Factory.instance(context);
duke@1 240
duke@1 241 init(syms, definitive);
duke@1 242 log = Log.instance(context);
duke@1 243
duke@1 244 Options options = Options.instance(context);
duke@1 245 annotate = Annotate.instance(context);
duke@1 246 verbose = options.get("-verbose") != null;
duke@1 247 checkClassFile = options.get("-checkclassfile") != null;
duke@1 248 Source source = Source.instance(context);
duke@1 249 allowGenerics = source.allowGenerics();
duke@1 250 allowVarargs = source.allowVarargs();
duke@1 251 allowAnnotations = source.allowAnnotations();
duke@1 252 saveParameterNames = options.get("save-parameter-names") != null;
duke@1 253 cacheCompletionFailure = options.get("dev") == null;
duke@1 254 preferSource = "source".equals(options.get("-Xprefer"));
duke@1 255
duke@1 256 completionFailureName =
duke@1 257 (options.get("failcomplete") != null)
duke@1 258 ? names.fromString(options.get("failcomplete"))
duke@1 259 : null;
duke@1 260
duke@1 261 typevars = new Scope(syms.noSymbol);
jjg@308 262 debugJSR308 = options.get("TA:reader") != null;
jjg@256 263
jjg@256 264 initAttributeReaders();
duke@1 265 }
duke@1 266
duke@1 267 /** Add member to class unless it is synthetic.
duke@1 268 */
duke@1 269 private void enterMember(ClassSymbol c, Symbol sym) {
duke@1 270 if ((sym.flags_field & (SYNTHETIC|BRIDGE)) != SYNTHETIC)
duke@1 271 c.members_field.enter(sym);
duke@1 272 }
duke@1 273
duke@1 274 /************************************************************************
duke@1 275 * Error Diagnoses
duke@1 276 ***********************************************************************/
duke@1 277
jjg@12 278
jjg@12 279 public class BadClassFile extends CompletionFailure {
duke@1 280 private static final long serialVersionUID = 0;
duke@1 281
jjg@12 282 public BadClassFile(TypeSymbol sym, JavaFileObject file, JCDiagnostic diag) {
jjg@12 283 super(sym, createBadClassFileDiagnostic(file, diag));
duke@1 284 }
duke@1 285 }
jjg@12 286 // where
jjg@12 287 private JCDiagnostic createBadClassFileDiagnostic(JavaFileObject file, JCDiagnostic diag) {
jjg@12 288 String key = (file.getKind() == JavaFileObject.Kind.SOURCE
jjg@12 289 ? "bad.source.file.header" : "bad.class.file.header");
jjg@12 290 return diagFactory.fragment(key, file, diag);
jjg@12 291 }
duke@1 292
duke@1 293 public BadClassFile badClassFile(String key, Object... args) {
duke@1 294 return new BadClassFile (
duke@1 295 currentOwner.enclClass(),
duke@1 296 currentClassFile,
jjg@12 297 diagFactory.fragment(key, args));
duke@1 298 }
duke@1 299
duke@1 300 /************************************************************************
duke@1 301 * Buffer Access
duke@1 302 ***********************************************************************/
duke@1 303
duke@1 304 /** Read a character.
duke@1 305 */
duke@1 306 char nextChar() {
duke@1 307 return (char)(((buf[bp++] & 0xFF) << 8) + (buf[bp++] & 0xFF));
duke@1 308 }
duke@1 309
jjg@308 310 /** Read a byte.
jjg@308 311 */
jjg@308 312 byte nextByte() {
jjg@308 313 return buf[bp++];
jjg@308 314 }
jjg@308 315
duke@1 316 /** Read an integer.
duke@1 317 */
duke@1 318 int nextInt() {
duke@1 319 return
duke@1 320 ((buf[bp++] & 0xFF) << 24) +
duke@1 321 ((buf[bp++] & 0xFF) << 16) +
duke@1 322 ((buf[bp++] & 0xFF) << 8) +
duke@1 323 (buf[bp++] & 0xFF);
duke@1 324 }
duke@1 325
duke@1 326 /** Extract a character at position bp from buf.
duke@1 327 */
duke@1 328 char getChar(int bp) {
duke@1 329 return
duke@1 330 (char)(((buf[bp] & 0xFF) << 8) + (buf[bp+1] & 0xFF));
duke@1 331 }
duke@1 332
duke@1 333 /** Extract an integer at position bp from buf.
duke@1 334 */
duke@1 335 int getInt(int bp) {
duke@1 336 return
duke@1 337 ((buf[bp] & 0xFF) << 24) +
duke@1 338 ((buf[bp+1] & 0xFF) << 16) +
duke@1 339 ((buf[bp+2] & 0xFF) << 8) +
duke@1 340 (buf[bp+3] & 0xFF);
duke@1 341 }
duke@1 342
duke@1 343
duke@1 344 /** Extract a long integer at position bp from buf.
duke@1 345 */
duke@1 346 long getLong(int bp) {
duke@1 347 DataInputStream bufin =
duke@1 348 new DataInputStream(new ByteArrayInputStream(buf, bp, 8));
duke@1 349 try {
duke@1 350 return bufin.readLong();
duke@1 351 } catch (IOException e) {
duke@1 352 throw new AssertionError(e);
duke@1 353 }
duke@1 354 }
duke@1 355
duke@1 356 /** Extract a float at position bp from buf.
duke@1 357 */
duke@1 358 float getFloat(int bp) {
duke@1 359 DataInputStream bufin =
duke@1 360 new DataInputStream(new ByteArrayInputStream(buf, bp, 4));
duke@1 361 try {
duke@1 362 return bufin.readFloat();
duke@1 363 } catch (IOException e) {
duke@1 364 throw new AssertionError(e);
duke@1 365 }
duke@1 366 }
duke@1 367
duke@1 368 /** Extract a double at position bp from buf.
duke@1 369 */
duke@1 370 double getDouble(int bp) {
duke@1 371 DataInputStream bufin =
duke@1 372 new DataInputStream(new ByteArrayInputStream(buf, bp, 8));
duke@1 373 try {
duke@1 374 return bufin.readDouble();
duke@1 375 } catch (IOException e) {
duke@1 376 throw new AssertionError(e);
duke@1 377 }
duke@1 378 }
duke@1 379
duke@1 380 /************************************************************************
duke@1 381 * Constant Pool Access
duke@1 382 ***********************************************************************/
duke@1 383
duke@1 384 /** Index all constant pool entries, writing their start addresses into
duke@1 385 * poolIdx.
duke@1 386 */
duke@1 387 void indexPool() {
duke@1 388 poolIdx = new int[nextChar()];
duke@1 389 poolObj = new Object[poolIdx.length];
duke@1 390 int i = 1;
duke@1 391 while (i < poolIdx.length) {
duke@1 392 poolIdx[i++] = bp;
duke@1 393 byte tag = buf[bp++];
duke@1 394 switch (tag) {
duke@1 395 case CONSTANT_Utf8: case CONSTANT_Unicode: {
duke@1 396 int len = nextChar();
duke@1 397 bp = bp + len;
duke@1 398 break;
duke@1 399 }
duke@1 400 case CONSTANT_Class:
duke@1 401 case CONSTANT_String:
duke@1 402 bp = bp + 2;
duke@1 403 break;
duke@1 404 case CONSTANT_Fieldref:
duke@1 405 case CONSTANT_Methodref:
duke@1 406 case CONSTANT_InterfaceMethodref:
duke@1 407 case CONSTANT_NameandType:
duke@1 408 case CONSTANT_Integer:
duke@1 409 case CONSTANT_Float:
duke@1 410 bp = bp + 4;
duke@1 411 break;
duke@1 412 case CONSTANT_Long:
duke@1 413 case CONSTANT_Double:
duke@1 414 bp = bp + 8;
duke@1 415 i++;
duke@1 416 break;
duke@1 417 default:
duke@1 418 throw badClassFile("bad.const.pool.tag.at",
duke@1 419 Byte.toString(tag),
duke@1 420 Integer.toString(bp -1));
duke@1 421 }
duke@1 422 }
duke@1 423 }
duke@1 424
duke@1 425 /** Read constant pool entry at start address i, use pool as a cache.
duke@1 426 */
duke@1 427 Object readPool(int i) {
duke@1 428 Object result = poolObj[i];
duke@1 429 if (result != null) return result;
duke@1 430
duke@1 431 int index = poolIdx[i];
duke@1 432 if (index == 0) return null;
duke@1 433
duke@1 434 byte tag = buf[index];
duke@1 435 switch (tag) {
duke@1 436 case CONSTANT_Utf8:
duke@1 437 poolObj[i] = names.fromUtf(buf, index + 3, getChar(index + 1));
duke@1 438 break;
duke@1 439 case CONSTANT_Unicode:
duke@1 440 throw badClassFile("unicode.str.not.supported");
duke@1 441 case CONSTANT_Class:
duke@1 442 poolObj[i] = readClassOrType(getChar(index + 1));
duke@1 443 break;
duke@1 444 case CONSTANT_String:
duke@1 445 // FIXME: (footprint) do not use toString here
duke@1 446 poolObj[i] = readName(getChar(index + 1)).toString();
duke@1 447 break;
duke@1 448 case CONSTANT_Fieldref: {
duke@1 449 ClassSymbol owner = readClassSymbol(getChar(index + 1));
duke@1 450 NameAndType nt = (NameAndType)readPool(getChar(index + 3));
duke@1 451 poolObj[i] = new VarSymbol(0, nt.name, nt.type, owner);
duke@1 452 break;
duke@1 453 }
duke@1 454 case CONSTANT_Methodref:
duke@1 455 case CONSTANT_InterfaceMethodref: {
duke@1 456 ClassSymbol owner = readClassSymbol(getChar(index + 1));
duke@1 457 NameAndType nt = (NameAndType)readPool(getChar(index + 3));
duke@1 458 poolObj[i] = new MethodSymbol(0, nt.name, nt.type, owner);
duke@1 459 break;
duke@1 460 }
duke@1 461 case CONSTANT_NameandType:
duke@1 462 poolObj[i] = new NameAndType(
duke@1 463 readName(getChar(index + 1)),
duke@1 464 readType(getChar(index + 3)));
duke@1 465 break;
duke@1 466 case CONSTANT_Integer:
duke@1 467 poolObj[i] = getInt(index + 1);
duke@1 468 break;
duke@1 469 case CONSTANT_Float:
duke@1 470 poolObj[i] = new Float(getFloat(index + 1));
duke@1 471 break;
duke@1 472 case CONSTANT_Long:
duke@1 473 poolObj[i] = new Long(getLong(index + 1));
duke@1 474 break;
duke@1 475 case CONSTANT_Double:
duke@1 476 poolObj[i] = new Double(getDouble(index + 1));
duke@1 477 break;
duke@1 478 default:
duke@1 479 throw badClassFile("bad.const.pool.tag", Byte.toString(tag));
duke@1 480 }
duke@1 481 return poolObj[i];
duke@1 482 }
duke@1 483
duke@1 484 /** Read signature and convert to type.
duke@1 485 */
duke@1 486 Type readType(int i) {
duke@1 487 int index = poolIdx[i];
duke@1 488 return sigToType(buf, index + 3, getChar(index + 1));
duke@1 489 }
duke@1 490
duke@1 491 /** If name is an array type or class signature, return the
duke@1 492 * corresponding type; otherwise return a ClassSymbol with given name.
duke@1 493 */
duke@1 494 Object readClassOrType(int i) {
duke@1 495 int index = poolIdx[i];
duke@1 496 int len = getChar(index + 1);
duke@1 497 int start = index + 3;
duke@1 498 assert buf[start] == '[' || buf[start + len - 1] != ';';
duke@1 499 // by the above assertion, the following test can be
duke@1 500 // simplified to (buf[start] == '[')
duke@1 501 return (buf[start] == '[' || buf[start + len - 1] == ';')
duke@1 502 ? (Object)sigToType(buf, start, len)
duke@1 503 : (Object)enterClass(names.fromUtf(internalize(buf, start,
duke@1 504 len)));
duke@1 505 }
duke@1 506
duke@1 507 /** Read signature and convert to type parameters.
duke@1 508 */
duke@1 509 List<Type> readTypeParams(int i) {
duke@1 510 int index = poolIdx[i];
duke@1 511 return sigToTypeParams(buf, index + 3, getChar(index + 1));
duke@1 512 }
duke@1 513
duke@1 514 /** Read class entry.
duke@1 515 */
duke@1 516 ClassSymbol readClassSymbol(int i) {
duke@1 517 return (ClassSymbol) (readPool(i));
duke@1 518 }
duke@1 519
duke@1 520 /** Read name.
duke@1 521 */
duke@1 522 Name readName(int i) {
duke@1 523 return (Name) (readPool(i));
duke@1 524 }
duke@1 525
duke@1 526 /************************************************************************
duke@1 527 * Reading Types
duke@1 528 ***********************************************************************/
duke@1 529
duke@1 530 /** The unread portion of the currently read type is
duke@1 531 * signature[sigp..siglimit-1].
duke@1 532 */
duke@1 533 byte[] signature;
duke@1 534 int sigp;
duke@1 535 int siglimit;
duke@1 536 boolean sigEnterPhase = false;
duke@1 537
duke@1 538 /** Convert signature to type, where signature is a byte array segment.
duke@1 539 */
duke@1 540 Type sigToType(byte[] sig, int offset, int len) {
duke@1 541 signature = sig;
duke@1 542 sigp = offset;
duke@1 543 siglimit = offset + len;
duke@1 544 return sigToType();
duke@1 545 }
duke@1 546
duke@1 547 /** Convert signature to type, where signature is implicit.
duke@1 548 */
duke@1 549 Type sigToType() {
duke@1 550 switch ((char) signature[sigp]) {
duke@1 551 case 'T':
duke@1 552 sigp++;
duke@1 553 int start = sigp;
duke@1 554 while (signature[sigp] != ';') sigp++;
duke@1 555 sigp++;
duke@1 556 return sigEnterPhase
duke@1 557 ? Type.noType
duke@1 558 : findTypeVar(names.fromUtf(signature, start, sigp - 1 - start));
duke@1 559 case '+': {
duke@1 560 sigp++;
duke@1 561 Type t = sigToType();
duke@1 562 return new WildcardType(t, BoundKind.EXTENDS,
duke@1 563 syms.boundClass);
duke@1 564 }
duke@1 565 case '*':
duke@1 566 sigp++;
duke@1 567 return new WildcardType(syms.objectType, BoundKind.UNBOUND,
duke@1 568 syms.boundClass);
duke@1 569 case '-': {
duke@1 570 sigp++;
duke@1 571 Type t = sigToType();
duke@1 572 return new WildcardType(t, BoundKind.SUPER,
duke@1 573 syms.boundClass);
duke@1 574 }
duke@1 575 case 'B':
duke@1 576 sigp++;
duke@1 577 return syms.byteType;
duke@1 578 case 'C':
duke@1 579 sigp++;
duke@1 580 return syms.charType;
duke@1 581 case 'D':
duke@1 582 sigp++;
duke@1 583 return syms.doubleType;
duke@1 584 case 'F':
duke@1 585 sigp++;
duke@1 586 return syms.floatType;
duke@1 587 case 'I':
duke@1 588 sigp++;
duke@1 589 return syms.intType;
duke@1 590 case 'J':
duke@1 591 sigp++;
duke@1 592 return syms.longType;
duke@1 593 case 'L':
duke@1 594 {
duke@1 595 // int oldsigp = sigp;
duke@1 596 Type t = classSigToType();
duke@1 597 if (sigp < siglimit && signature[sigp] == '.')
duke@1 598 throw badClassFile("deprecated inner class signature syntax " +
duke@1 599 "(please recompile from source)");
duke@1 600 /*
duke@1 601 System.err.println(" decoded " +
duke@1 602 new String(signature, oldsigp, sigp-oldsigp) +
duke@1 603 " => " + t + " outer " + t.outer());
duke@1 604 */
duke@1 605 return t;
duke@1 606 }
duke@1 607 case 'S':
duke@1 608 sigp++;
duke@1 609 return syms.shortType;
duke@1 610 case 'V':
duke@1 611 sigp++;
duke@1 612 return syms.voidType;
duke@1 613 case 'Z':
duke@1 614 sigp++;
duke@1 615 return syms.booleanType;
duke@1 616 case '[':
duke@1 617 sigp++;
duke@1 618 return new ArrayType(sigToType(), syms.arrayClass);
duke@1 619 case '(':
duke@1 620 sigp++;
duke@1 621 List<Type> argtypes = sigToTypes(')');
duke@1 622 Type restype = sigToType();
duke@1 623 List<Type> thrown = List.nil();
duke@1 624 while (signature[sigp] == '^') {
duke@1 625 sigp++;
duke@1 626 thrown = thrown.prepend(sigToType());
duke@1 627 }
duke@1 628 return new MethodType(argtypes,
duke@1 629 restype,
duke@1 630 thrown.reverse(),
duke@1 631 syms.methodClass);
duke@1 632 case '<':
duke@1 633 typevars = typevars.dup(currentOwner);
duke@1 634 Type poly = new ForAll(sigToTypeParams(), sigToType());
duke@1 635 typevars = typevars.leave();
duke@1 636 return poly;
duke@1 637 default:
duke@1 638 throw badClassFile("bad.signature",
duke@1 639 Convert.utf2string(signature, sigp, 10));
duke@1 640 }
duke@1 641 }
duke@1 642
duke@1 643 byte[] signatureBuffer = new byte[0];
duke@1 644 int sbp = 0;
duke@1 645 /** Convert class signature to type, where signature is implicit.
duke@1 646 */
duke@1 647 Type classSigToType() {
duke@1 648 if (signature[sigp] != 'L')
duke@1 649 throw badClassFile("bad.class.signature",
duke@1 650 Convert.utf2string(signature, sigp, 10));
duke@1 651 sigp++;
duke@1 652 Type outer = Type.noType;
duke@1 653 int startSbp = sbp;
duke@1 654
duke@1 655 while (true) {
duke@1 656 final byte c = signature[sigp++];
duke@1 657 switch (c) {
duke@1 658
duke@1 659 case ';': { // end
duke@1 660 ClassSymbol t = enterClass(names.fromUtf(signatureBuffer,
duke@1 661 startSbp,
duke@1 662 sbp - startSbp));
duke@1 663 if (outer == Type.noType)
duke@1 664 outer = t.erasure(types);
duke@1 665 else
duke@1 666 outer = new ClassType(outer, List.<Type>nil(), t);
duke@1 667 sbp = startSbp;
duke@1 668 return outer;
duke@1 669 }
duke@1 670
duke@1 671 case '<': // generic arguments
duke@1 672 ClassSymbol t = enterClass(names.fromUtf(signatureBuffer,
duke@1 673 startSbp,
duke@1 674 sbp - startSbp));
duke@1 675 outer = new ClassType(outer, sigToTypes('>'), t) {
duke@1 676 boolean completed = false;
jjg@256 677 @Override
duke@1 678 public Type getEnclosingType() {
duke@1 679 if (!completed) {
duke@1 680 completed = true;
duke@1 681 tsym.complete();
duke@1 682 Type enclosingType = tsym.type.getEnclosingType();
duke@1 683 if (enclosingType != Type.noType) {
duke@1 684 List<Type> typeArgs =
duke@1 685 super.getEnclosingType().allparams();
duke@1 686 List<Type> typeParams =
duke@1 687 enclosingType.allparams();
duke@1 688 if (typeParams.length() != typeArgs.length()) {
duke@1 689 // no "rare" types
duke@1 690 super.setEnclosingType(types.erasure(enclosingType));
duke@1 691 } else {
duke@1 692 super.setEnclosingType(types.subst(enclosingType,
duke@1 693 typeParams,
duke@1 694 typeArgs));
duke@1 695 }
duke@1 696 } else {
duke@1 697 super.setEnclosingType(Type.noType);
duke@1 698 }
duke@1 699 }
duke@1 700 return super.getEnclosingType();
duke@1 701 }
jjg@256 702 @Override
duke@1 703 public void setEnclosingType(Type outer) {
duke@1 704 throw new UnsupportedOperationException();
duke@1 705 }
duke@1 706 };
duke@1 707 switch (signature[sigp++]) {
duke@1 708 case ';':
duke@1 709 if (sigp < signature.length && signature[sigp] == '.') {
duke@1 710 // support old-style GJC signatures
duke@1 711 // The signature produced was
duke@1 712 // Lfoo/Outer<Lfoo/X;>;.Lfoo/Outer$Inner<Lfoo/Y;>;
duke@1 713 // rather than say
duke@1 714 // Lfoo/Outer<Lfoo/X;>.Inner<Lfoo/Y;>;
duke@1 715 // so we skip past ".Lfoo/Outer$"
duke@1 716 sigp += (sbp - startSbp) + // "foo/Outer"
duke@1 717 3; // ".L" and "$"
duke@1 718 signatureBuffer[sbp++] = (byte)'$';
duke@1 719 break;
duke@1 720 } else {
duke@1 721 sbp = startSbp;
duke@1 722 return outer;
duke@1 723 }
duke@1 724 case '.':
duke@1 725 signatureBuffer[sbp++] = (byte)'$';
duke@1 726 break;
duke@1 727 default:
duke@1 728 throw new AssertionError(signature[sigp-1]);
duke@1 729 }
duke@1 730 continue;
duke@1 731
duke@1 732 case '.':
duke@1 733 signatureBuffer[sbp++] = (byte)'$';
duke@1 734 continue;
duke@1 735 case '/':
duke@1 736 signatureBuffer[sbp++] = (byte)'.';
duke@1 737 continue;
duke@1 738 default:
duke@1 739 signatureBuffer[sbp++] = c;
duke@1 740 continue;
duke@1 741 }
duke@1 742 }
duke@1 743 }
duke@1 744
duke@1 745 /** Convert (implicit) signature to list of types
duke@1 746 * until `terminator' is encountered.
duke@1 747 */
duke@1 748 List<Type> sigToTypes(char terminator) {
duke@1 749 List<Type> head = List.of(null);
duke@1 750 List<Type> tail = head;
duke@1 751 while (signature[sigp] != terminator)
duke@1 752 tail = tail.setTail(List.of(sigToType()));
duke@1 753 sigp++;
duke@1 754 return head.tail;
duke@1 755 }
duke@1 756
duke@1 757 /** Convert signature to type parameters, where signature is a byte
duke@1 758 * array segment.
duke@1 759 */
duke@1 760 List<Type> sigToTypeParams(byte[] sig, int offset, int len) {
duke@1 761 signature = sig;
duke@1 762 sigp = offset;
duke@1 763 siglimit = offset + len;
duke@1 764 return sigToTypeParams();
duke@1 765 }
duke@1 766
duke@1 767 /** Convert signature to type parameters, where signature is implicit.
duke@1 768 */
duke@1 769 List<Type> sigToTypeParams() {
duke@1 770 List<Type> tvars = List.nil();
duke@1 771 if (signature[sigp] == '<') {
duke@1 772 sigp++;
duke@1 773 int start = sigp;
duke@1 774 sigEnterPhase = true;
duke@1 775 while (signature[sigp] != '>')
duke@1 776 tvars = tvars.prepend(sigToTypeParam());
duke@1 777 sigEnterPhase = false;
duke@1 778 sigp = start;
duke@1 779 while (signature[sigp] != '>')
duke@1 780 sigToTypeParam();
duke@1 781 sigp++;
duke@1 782 }
duke@1 783 return tvars.reverse();
duke@1 784 }
duke@1 785
duke@1 786 /** Convert (implicit) signature to type parameter.
duke@1 787 */
duke@1 788 Type sigToTypeParam() {
duke@1 789 int start = sigp;
duke@1 790 while (signature[sigp] != ':') sigp++;
duke@1 791 Name name = names.fromUtf(signature, start, sigp - start);
duke@1 792 TypeVar tvar;
duke@1 793 if (sigEnterPhase) {
duke@1 794 tvar = new TypeVar(name, currentOwner, syms.botType);
duke@1 795 typevars.enter(tvar.tsym);
duke@1 796 } else {
duke@1 797 tvar = (TypeVar)findTypeVar(name);
duke@1 798 }
duke@1 799 List<Type> bounds = List.nil();
duke@1 800 Type st = null;
duke@1 801 if (signature[sigp] == ':' && signature[sigp+1] == ':') {
duke@1 802 sigp++;
duke@1 803 st = syms.objectType;
duke@1 804 }
duke@1 805 while (signature[sigp] == ':') {
duke@1 806 sigp++;
duke@1 807 bounds = bounds.prepend(sigToType());
duke@1 808 }
duke@1 809 if (!sigEnterPhase) {
duke@1 810 types.setBounds(tvar, bounds.reverse(), st);
duke@1 811 }
duke@1 812 return tvar;
duke@1 813 }
duke@1 814
duke@1 815 /** Find type variable with given name in `typevars' scope.
duke@1 816 */
duke@1 817 Type findTypeVar(Name name) {
duke@1 818 Scope.Entry e = typevars.lookup(name);
duke@1 819 if (e.scope != null) {
duke@1 820 return e.sym.type;
duke@1 821 } else {
duke@1 822 if (readingClassAttr) {
duke@1 823 // While reading the class attribute, the supertypes
duke@1 824 // might refer to a type variable from an enclosing element
duke@1 825 // (method or class).
duke@1 826 // If the type variable is defined in the enclosing class,
duke@1 827 // we can actually find it in
duke@1 828 // currentOwner.owner.type.getTypeArguments()
duke@1 829 // However, until we have read the enclosing method attribute
duke@1 830 // we don't know for sure if this owner is correct. It could
duke@1 831 // be a method and there is no way to tell before reading the
duke@1 832 // enclosing method attribute.
duke@1 833 TypeVar t = new TypeVar(name, currentOwner, syms.botType);
duke@1 834 missingTypeVariables = missingTypeVariables.prepend(t);
duke@1 835 // System.err.println("Missing type var " + name);
duke@1 836 return t;
duke@1 837 }
duke@1 838 throw badClassFile("undecl.type.var", name);
duke@1 839 }
duke@1 840 }
duke@1 841
duke@1 842 /************************************************************************
duke@1 843 * Reading Attributes
duke@1 844 ***********************************************************************/
duke@1 845
jjg@256 846 protected enum AttributeKind { CLASS, MEMBER };
jjg@256 847 protected abstract class AttributeReader {
jjg@256 848 AttributeReader(Name name, Version version, Set<AttributeKind> kinds) {
jjg@256 849 this.name = name;
jjg@256 850 this.version = version;
jjg@256 851 this.kinds = kinds;
jjg@256 852 }
jjg@256 853
jjg@256 854 boolean accepts(AttributeKind kind) {
jjg@256 855 return kinds.contains(kind) && majorVersion >= version.major;
jjg@256 856 }
jjg@256 857
jjg@256 858 abstract void read(Symbol sym, int attrLen);
jjg@256 859
jjg@256 860 final Name name;
jjg@256 861 final Version version;
jjg@256 862 final Set<AttributeKind> kinds;
jjg@256 863 }
jjg@256 864
jjg@256 865 protected Set<AttributeKind> CLASS_ATTRIBUTE =
jjg@256 866 EnumSet.of(AttributeKind.CLASS);
jjg@256 867 protected Set<AttributeKind> MEMBER_ATTRIBUTE =
jjg@256 868 EnumSet.of(AttributeKind.MEMBER);
jjg@256 869 protected Set<AttributeKind> CLASS_OR_MEMBER_ATTRIBUTE =
jjg@256 870 EnumSet.of(AttributeKind.CLASS, AttributeKind.MEMBER);
jjg@256 871
jjg@256 872 protected Map<Name, AttributeReader> attributeReaders = new HashMap<Name, AttributeReader>();
jjg@256 873
jjg@256 874 protected void initAttributeReaders() {
jjg@256 875 AttributeReader[] readers = {
jjg@256 876 // v45.3 attributes
jjg@256 877
jjg@256 878 new AttributeReader(names.Code, V45_3, MEMBER_ATTRIBUTE) {
jjg@256 879 void read(Symbol sym, int attrLen) {
jjg@256 880 if (readAllOfClassFile || saveParameterNames)
jjg@256 881 ((MethodSymbol)sym).code = readCode(sym);
jjg@256 882 else
jjg@256 883 bp = bp + attrLen;
jjg@256 884 }
jjg@256 885 },
jjg@256 886
jjg@256 887 new AttributeReader(names.ConstantValue, V45_3, MEMBER_ATTRIBUTE) {
jjg@256 888 void read(Symbol sym, int attrLen) {
jjg@256 889 Object v = readPool(nextChar());
jjg@256 890 // Ignore ConstantValue attribute if field not final.
jjg@256 891 if ((sym.flags() & FINAL) != 0)
jjg@256 892 ((VarSymbol) sym).setData(v);
jjg@256 893 }
jjg@256 894 },
jjg@256 895
jjg@256 896 new AttributeReader(names.Deprecated, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@256 897 void read(Symbol sym, int attrLen) {
jjg@256 898 sym.flags_field |= DEPRECATED;
jjg@256 899 }
jjg@256 900 },
jjg@256 901
jjg@256 902 new AttributeReader(names.Exceptions, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@256 903 void read(Symbol sym, int attrLen) {
jjg@256 904 int nexceptions = nextChar();
jjg@256 905 List<Type> thrown = List.nil();
jjg@256 906 for (int j = 0; j < nexceptions; j++)
jjg@256 907 thrown = thrown.prepend(readClassSymbol(nextChar()).type);
jjg@256 908 if (sym.type.getThrownTypes().isEmpty())
jjg@256 909 sym.type.asMethodType().thrown = thrown.reverse();
jjg@256 910 }
jjg@256 911 },
jjg@256 912
jjg@256 913 new AttributeReader(names.InnerClasses, V45_3, CLASS_ATTRIBUTE) {
jjg@256 914 void read(Symbol sym, int attrLen) {
jjg@256 915 ClassSymbol c = (ClassSymbol) sym;
jjg@256 916 readInnerClasses(c);
jjg@256 917 }
jjg@256 918 },
jjg@256 919
jjg@256 920 new AttributeReader(names.LocalVariableTable, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@256 921 void read(Symbol sym, int attrLen) {
jjg@256 922 int newbp = bp + attrLen;
jjg@256 923 if (saveParameterNames) {
jjg@256 924 // pick up parameter names from the variable table
jjg@256 925 List<Name> parameterNames = List.nil();
jjg@256 926 int firstParam = ((sym.flags() & STATIC) == 0) ? 1 : 0;
jjg@256 927 int endParam = firstParam + Code.width(sym.type.getParameterTypes());
jjg@256 928 int numEntries = nextChar();
jjg@256 929 for (int i=0; i<numEntries; i++) {
jjg@256 930 int start_pc = nextChar();
jjg@256 931 int length = nextChar();
jjg@256 932 int nameIndex = nextChar();
jjg@256 933 int sigIndex = nextChar();
jjg@256 934 int register = nextChar();
jjg@256 935 if (start_pc == 0 &&
jjg@256 936 firstParam <= register &&
jjg@256 937 register < endParam) {
jjg@256 938 int index = firstParam;
jjg@256 939 for (Type t : sym.type.getParameterTypes()) {
jjg@256 940 if (index == register) {
jjg@256 941 parameterNames = parameterNames.prepend(readName(nameIndex));
jjg@256 942 break;
jjg@256 943 }
jjg@256 944 index += Code.width(t);
jjg@256 945 }
jjg@256 946 }
jjg@256 947 }
jjg@256 948 parameterNames = parameterNames.reverse();
jjg@256 949 ((MethodSymbol)sym).savedParameterNames = parameterNames;
jjg@256 950 }
jjg@256 951 bp = newbp;
jjg@256 952 }
jjg@256 953 },
jjg@256 954
jjg@256 955 new AttributeReader(names.SourceFile, V45_3, CLASS_ATTRIBUTE) {
jjg@256 956 void read(Symbol sym, int attrLen) {
jjg@256 957 ClassSymbol c = (ClassSymbol) sym;
jjg@256 958 Name n = readName(nextChar());
jjg@256 959 c.sourcefile = new SourceFileObject(n, c.flatname);
jjg@256 960 }
jjg@256 961 },
jjg@256 962
jjg@256 963 new AttributeReader(names.Synthetic, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@256 964 void read(Symbol sym, int attrLen) {
jjg@256 965 // bridge methods are visible when generics not enabled
jjg@256 966 if (allowGenerics || (sym.flags_field & BRIDGE) == 0)
jjg@256 967 sym.flags_field |= SYNTHETIC;
jjg@256 968 }
jjg@256 969 },
jjg@256 970
jjg@256 971 // standard v49 attributes
jjg@256 972
jjg@256 973 new AttributeReader(names.EnclosingMethod, V49, CLASS_ATTRIBUTE) {
jjg@256 974 void read(Symbol sym, int attrLen) {
jjg@256 975 int newbp = bp + attrLen;
jjg@256 976 readEnclosingMethodAttr(sym);
jjg@256 977 bp = newbp;
jjg@256 978 }
jjg@256 979 },
jjg@256 980
jjg@256 981 new AttributeReader(names.Signature, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@256 982 @Override
jjg@256 983 boolean accepts(AttributeKind kind) {
jjg@256 984 return super.accepts(kind) && allowGenerics;
jjg@256 985 }
jjg@256 986
jjg@256 987 void read(Symbol sym, int attrLen) {
jjg@256 988 if (sym.kind == TYP) {
jjg@256 989 ClassSymbol c = (ClassSymbol) sym;
jjg@256 990 readingClassAttr = true;
jjg@256 991 try {
jjg@256 992 ClassType ct1 = (ClassType)c.type;
jjg@256 993 assert c == currentOwner;
jjg@256 994 ct1.typarams_field = readTypeParams(nextChar());
jjg@256 995 ct1.supertype_field = sigToType();
jjg@256 996 ListBuffer<Type> is = new ListBuffer<Type>();
jjg@256 997 while (sigp != siglimit) is.append(sigToType());
jjg@256 998 ct1.interfaces_field = is.toList();
jjg@256 999 } finally {
jjg@256 1000 readingClassAttr = false;
jjg@256 1001 }
jjg@256 1002 } else {
jjg@256 1003 List<Type> thrown = sym.type.getThrownTypes();
jjg@256 1004 sym.type = readType(nextChar());
jjg@256 1005 //- System.err.println(" # " + sym.type);
jjg@256 1006 if (sym.kind == MTH && sym.type.getThrownTypes().isEmpty())
jjg@256 1007 sym.type.asMethodType().thrown = thrown;
jjg@256 1008
jjg@256 1009 }
jjg@256 1010 }
jjg@256 1011 },
jjg@256 1012
jjg@256 1013 // v49 annotation attributes
jjg@256 1014
jjg@256 1015 new AttributeReader(names.AnnotationDefault, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@256 1016 void read(Symbol sym, int attrLen) {
jjg@256 1017 attachAnnotationDefault(sym);
jjg@256 1018 }
jjg@256 1019 },
jjg@256 1020
jjg@256 1021 new AttributeReader(names.RuntimeInvisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@256 1022 void read(Symbol sym, int attrLen) {
jjg@256 1023 attachAnnotations(sym);
jjg@256 1024 }
jjg@256 1025 },
jjg@256 1026
jjg@256 1027 new AttributeReader(names.RuntimeInvisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@256 1028 void read(Symbol sym, int attrLen) {
jjg@256 1029 attachParameterAnnotations(sym);
jjg@256 1030 }
jjg@256 1031 },
jjg@256 1032
jjg@256 1033 new AttributeReader(names.RuntimeVisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@256 1034 void read(Symbol sym, int attrLen) {
jjg@256 1035 attachAnnotations(sym);
jjg@256 1036 }
jjg@256 1037 },
jjg@256 1038
jjg@256 1039 new AttributeReader(names.RuntimeVisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@256 1040 void read(Symbol sym, int attrLen) {
jjg@256 1041 attachParameterAnnotations(sym);
jjg@256 1042 }
jjg@256 1043 },
jjg@256 1044
jjg@256 1045 // additional "legacy" v49 attributes, superceded by flags
jjg@256 1046
jjg@256 1047 new AttributeReader(names.Annotation, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@256 1048 void read(Symbol sym, int attrLen) {
jjg@256 1049 if (allowAnnotations)
jjg@256 1050 sym.flags_field |= ANNOTATION;
jjg@256 1051 }
jjg@256 1052 },
jjg@256 1053
jjg@256 1054 new AttributeReader(names.Bridge, V49, MEMBER_ATTRIBUTE) {
jjg@256 1055 void read(Symbol sym, int attrLen) {
jjg@256 1056 sym.flags_field |= BRIDGE;
jjg@256 1057 if (!allowGenerics)
jjg@256 1058 sym.flags_field &= ~SYNTHETIC;
jjg@256 1059 }
jjg@256 1060 },
jjg@256 1061
jjg@256 1062 new AttributeReader(names.Enum, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@256 1063 void read(Symbol sym, int attrLen) {
jjg@256 1064 sym.flags_field |= ENUM;
jjg@256 1065 }
jjg@256 1066 },
jjg@256 1067
jjg@256 1068 new AttributeReader(names.Varargs, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@256 1069 void read(Symbol sym, int attrLen) {
jjg@256 1070 if (allowVarargs)
jjg@256 1071 sym.flags_field |= VARARGS;
jjg@256 1072 }
jjg@308 1073 },
jjg@308 1074
jjg@308 1075 // v51 attributes
jjg@308 1076 new AttributeReader(names.RuntimeVisibleTypeAnnotations, V51, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@308 1077 void read(Symbol sym, int attrLen) {
jjg@308 1078 attachTypeAnnotations(sym);
jjg@308 1079 }
jjg@308 1080 },
jjg@308 1081
jjg@308 1082 new AttributeReader(names.RuntimeInvisibleTypeAnnotations, V51, CLASS_OR_MEMBER_ATTRIBUTE) {
jjg@308 1083 void read(Symbol sym, int attrLen) {
jjg@308 1084 attachTypeAnnotations(sym);
jjg@308 1085 }
jjg@308 1086 },
jjg@308 1087
jjg@256 1088
jjg@256 1089 // The following attributes for a Code attribute are not currently handled
jjg@256 1090 // StackMapTable
jjg@256 1091 // SourceDebugExtension
jjg@256 1092 // LineNumberTable
jjg@256 1093 // LocalVariableTypeTable
jjg@256 1094 };
jjg@256 1095
jjg@256 1096 for (AttributeReader r: readers)
jjg@256 1097 attributeReaders.put(r.name, r);
jjg@256 1098 }
jjg@256 1099
duke@1 1100 /** Report unrecognized attribute.
duke@1 1101 */
duke@1 1102 void unrecognized(Name attrName) {
duke@1 1103 if (checkClassFile)
duke@1 1104 printCCF("ccf.unrecognized.attribute", attrName);
duke@1 1105 }
duke@1 1106
jjg@256 1107
duke@1 1108
duke@1 1109 void readEnclosingMethodAttr(Symbol sym) {
duke@1 1110 // sym is a nested class with an "Enclosing Method" attribute
duke@1 1111 // remove sym from it's current owners scope and place it in
duke@1 1112 // the scope specified by the attribute
duke@1 1113 sym.owner.members().remove(sym);
duke@1 1114 ClassSymbol self = (ClassSymbol)sym;
duke@1 1115 ClassSymbol c = readClassSymbol(nextChar());
duke@1 1116 NameAndType nt = (NameAndType)readPool(nextChar());
duke@1 1117
duke@1 1118 MethodSymbol m = findMethod(nt, c.members_field, self.flags());
duke@1 1119 if (nt != null && m == null)
duke@1 1120 throw badClassFile("bad.enclosing.method", self);
duke@1 1121
duke@1 1122 self.name = simpleBinaryName(self.flatname, c.flatname) ;
duke@1 1123 self.owner = m != null ? m : c;
jjg@113 1124 if (self.name.isEmpty())
duke@1 1125 self.fullname = null;
duke@1 1126 else
duke@1 1127 self.fullname = ClassSymbol.formFullName(self.name, self.owner);
duke@1 1128
duke@1 1129 if (m != null) {
duke@1 1130 ((ClassType)sym.type).setEnclosingType(m.type);
duke@1 1131 } else if ((self.flags_field & STATIC) == 0) {
duke@1 1132 ((ClassType)sym.type).setEnclosingType(c.type);
duke@1 1133 } else {
duke@1 1134 ((ClassType)sym.type).setEnclosingType(Type.noType);
duke@1 1135 }
duke@1 1136 enterTypevars(self);
duke@1 1137 if (!missingTypeVariables.isEmpty()) {
duke@1 1138 ListBuffer<Type> typeVars = new ListBuffer<Type>();
duke@1 1139 for (Type typevar : missingTypeVariables) {
duke@1 1140 typeVars.append(findTypeVar(typevar.tsym.name));
duke@1 1141 }
duke@1 1142 foundTypeVariables = typeVars.toList();
duke@1 1143 } else {
duke@1 1144 foundTypeVariables = List.nil();
duke@1 1145 }
duke@1 1146 }
duke@1 1147
duke@1 1148 // See java.lang.Class
duke@1 1149 private Name simpleBinaryName(Name self, Name enclosing) {
duke@1 1150 String simpleBinaryName = self.toString().substring(enclosing.toString().length());
duke@1 1151 if (simpleBinaryName.length() < 1 || simpleBinaryName.charAt(0) != '$')
duke@1 1152 throw badClassFile("bad.enclosing.method", self);
duke@1 1153 int index = 1;
duke@1 1154 while (index < simpleBinaryName.length() &&
duke@1 1155 isAsciiDigit(simpleBinaryName.charAt(index)))
duke@1 1156 index++;
duke@1 1157 return names.fromString(simpleBinaryName.substring(index));
duke@1 1158 }
duke@1 1159
duke@1 1160 private MethodSymbol findMethod(NameAndType nt, Scope scope, long flags) {
duke@1 1161 if (nt == null)
duke@1 1162 return null;
duke@1 1163
duke@1 1164 MethodType type = nt.type.asMethodType();
duke@1 1165
duke@1 1166 for (Scope.Entry e = scope.lookup(nt.name); e.scope != null; e = e.next())
duke@1 1167 if (e.sym.kind == MTH && isSameBinaryType(e.sym.type.asMethodType(), type))
duke@1 1168 return (MethodSymbol)e.sym;
duke@1 1169
duke@1 1170 if (nt.name != names.init)
duke@1 1171 // not a constructor
duke@1 1172 return null;
duke@1 1173 if ((flags & INTERFACE) != 0)
duke@1 1174 // no enclosing instance
duke@1 1175 return null;
duke@1 1176 if (nt.type.getParameterTypes().isEmpty())
duke@1 1177 // no parameters
duke@1 1178 return null;
duke@1 1179
duke@1 1180 // A constructor of an inner class.
duke@1 1181 // Remove the first argument (the enclosing instance)
duke@1 1182 nt.type = new MethodType(nt.type.getParameterTypes().tail,
duke@1 1183 nt.type.getReturnType(),
duke@1 1184 nt.type.getThrownTypes(),
duke@1 1185 syms.methodClass);
duke@1 1186 // Try searching again
duke@1 1187 return findMethod(nt, scope, flags);
duke@1 1188 }
duke@1 1189
duke@1 1190 /** Similar to Types.isSameType but avoids completion */
duke@1 1191 private boolean isSameBinaryType(MethodType mt1, MethodType mt2) {
duke@1 1192 List<Type> types1 = types.erasure(mt1.getParameterTypes())
duke@1 1193 .prepend(types.erasure(mt1.getReturnType()));
duke@1 1194 List<Type> types2 = mt2.getParameterTypes().prepend(mt2.getReturnType());
duke@1 1195 while (!types1.isEmpty() && !types2.isEmpty()) {
duke@1 1196 if (types1.head.tsym != types2.head.tsym)
duke@1 1197 return false;
duke@1 1198 types1 = types1.tail;
duke@1 1199 types2 = types2.tail;
duke@1 1200 }
duke@1 1201 return types1.isEmpty() && types2.isEmpty();
duke@1 1202 }
duke@1 1203
duke@1 1204 /**
duke@1 1205 * Character.isDigit answers <tt>true</tt> to some non-ascii
duke@1 1206 * digits. This one does not. <b>copied from java.lang.Class</b>
duke@1 1207 */
duke@1 1208 private static boolean isAsciiDigit(char c) {
duke@1 1209 return '0' <= c && c <= '9';
duke@1 1210 }
duke@1 1211
duke@1 1212 /** Read member attributes.
duke@1 1213 */
duke@1 1214 void readMemberAttrs(Symbol sym) {
jjg@256 1215 readAttrs(sym, AttributeKind.MEMBER);
jjg@256 1216 }
jjg@256 1217
jjg@256 1218 void readAttrs(Symbol sym, AttributeKind kind) {
duke@1 1219 char ac = nextChar();
duke@1 1220 for (int i = 0; i < ac; i++) {
duke@1 1221 Name attrName = readName(nextChar());
duke@1 1222 int attrLen = nextInt();
jjg@256 1223 AttributeReader r = attributeReaders.get(attrName);
jjg@256 1224 if (r != null && r.accepts(kind))
jjg@256 1225 r.read(sym, attrLen);
jjg@256 1226 else {
jjg@256 1227 unrecognized(attrName);
jjg@256 1228 bp = bp + attrLen;
jjg@256 1229 }
duke@1 1230 }
duke@1 1231 }
duke@1 1232
duke@1 1233 private boolean readingClassAttr = false;
duke@1 1234 private List<Type> missingTypeVariables = List.nil();
duke@1 1235 private List<Type> foundTypeVariables = List.nil();
duke@1 1236
duke@1 1237 /** Read class attributes.
duke@1 1238 */
duke@1 1239 void readClassAttrs(ClassSymbol c) {
jjg@256 1240 readAttrs(c, AttributeKind.CLASS);
duke@1 1241 }
duke@1 1242
duke@1 1243 /** Read code block.
duke@1 1244 */
duke@1 1245 Code readCode(Symbol owner) {
duke@1 1246 nextChar(); // max_stack
duke@1 1247 nextChar(); // max_locals
duke@1 1248 final int code_length = nextInt();
duke@1 1249 bp += code_length;
duke@1 1250 final char exception_table_length = nextChar();
duke@1 1251 bp += exception_table_length * 8;
duke@1 1252 readMemberAttrs(owner);
duke@1 1253 return null;
duke@1 1254 }
duke@1 1255
duke@1 1256 /************************************************************************
duke@1 1257 * Reading Java-language annotations
duke@1 1258 ***********************************************************************/
duke@1 1259
duke@1 1260 /** Attach annotations.
duke@1 1261 */
duke@1 1262 void attachAnnotations(final Symbol sym) {
duke@1 1263 int numAttributes = nextChar();
duke@1 1264 if (numAttributes != 0) {
duke@1 1265 ListBuffer<CompoundAnnotationProxy> proxies =
duke@1 1266 new ListBuffer<CompoundAnnotationProxy>();
duke@1 1267 for (int i = 0; i<numAttributes; i++) {
duke@1 1268 CompoundAnnotationProxy proxy = readCompoundAnnotation();
duke@1 1269 if (proxy.type.tsym == syms.proprietaryType.tsym)
duke@1 1270 sym.flags_field |= PROPRIETARY;
duke@1 1271 else
duke@1 1272 proxies.append(proxy);
duke@1 1273 }
duke@1 1274 annotate.later(new AnnotationCompleter(sym, proxies.toList()));
duke@1 1275 }
duke@1 1276 }
duke@1 1277
duke@1 1278 /** Attach parameter annotations.
duke@1 1279 */
duke@1 1280 void attachParameterAnnotations(final Symbol method) {
duke@1 1281 final MethodSymbol meth = (MethodSymbol)method;
duke@1 1282 int numParameters = buf[bp++] & 0xFF;
duke@1 1283 List<VarSymbol> parameters = meth.params();
duke@1 1284 int pnum = 0;
duke@1 1285 while (parameters.tail != null) {
duke@1 1286 attachAnnotations(parameters.head);
duke@1 1287 parameters = parameters.tail;
duke@1 1288 pnum++;
duke@1 1289 }
duke@1 1290 if (pnum != numParameters) {
duke@1 1291 throw badClassFile("bad.runtime.invisible.param.annotations", meth);
duke@1 1292 }
duke@1 1293 }
duke@1 1294
jjg@308 1295 void attachTypeAnnotations(final Symbol sym) {
jjg@308 1296 int numAttributes = nextChar();
jjg@308 1297 if (numAttributes != 0) {
jjg@308 1298 ListBuffer<TypeAnnotationProxy> proxies =
jjg@308 1299 ListBuffer.lb();
jjg@308 1300 for (int i = 0; i < numAttributes; i++)
jjg@308 1301 proxies.append(readTypeAnnotation());
jjg@308 1302 annotate.later(new TypeAnnotationCompleter(sym, proxies.toList()));
jjg@308 1303 }
jjg@308 1304 }
jjg@308 1305
duke@1 1306 /** Attach the default value for an annotation element.
duke@1 1307 */
duke@1 1308 void attachAnnotationDefault(final Symbol sym) {
duke@1 1309 final MethodSymbol meth = (MethodSymbol)sym; // only on methods
duke@1 1310 final Attribute value = readAttributeValue();
duke@1 1311 annotate.later(new AnnotationDefaultCompleter(meth, value));
duke@1 1312 }
duke@1 1313
duke@1 1314 Type readTypeOrClassSymbol(int i) {
duke@1 1315 // support preliminary jsr175-format class files
duke@1 1316 if (buf[poolIdx[i]] == CONSTANT_Class)
duke@1 1317 return readClassSymbol(i).type;
duke@1 1318 return readType(i);
duke@1 1319 }
duke@1 1320 Type readEnumType(int i) {
duke@1 1321 // support preliminary jsr175-format class files
duke@1 1322 int index = poolIdx[i];
duke@1 1323 int length = getChar(index + 1);
duke@1 1324 if (buf[index + length + 2] != ';')
duke@1 1325 return enterClass(readName(i)).type;
duke@1 1326 return readType(i);
duke@1 1327 }
duke@1 1328
duke@1 1329 CompoundAnnotationProxy readCompoundAnnotation() {
duke@1 1330 Type t = readTypeOrClassSymbol(nextChar());
duke@1 1331 int numFields = nextChar();
duke@1 1332 ListBuffer<Pair<Name,Attribute>> pairs =
duke@1 1333 new ListBuffer<Pair<Name,Attribute>>();
duke@1 1334 for (int i=0; i<numFields; i++) {
duke@1 1335 Name name = readName(nextChar());
duke@1 1336 Attribute value = readAttributeValue();
duke@1 1337 pairs.append(new Pair<Name,Attribute>(name, value));
duke@1 1338 }
duke@1 1339 return new CompoundAnnotationProxy(t, pairs.toList());
duke@1 1340 }
duke@1 1341
jjg@308 1342 TypeAnnotationProxy readTypeAnnotation() {
jjg@308 1343 CompoundAnnotationProxy proxy = readCompoundAnnotation();
jjg@308 1344 TypeAnnotationPosition position = readPosition();
jjg@308 1345
jjg@308 1346 if (debugJSR308)
jjg@308 1347 System.out.println("TA: reading: " + proxy + " @ " + position
jjg@308 1348 + " in " + log.currentSourceFile());
jjg@308 1349
jjg@308 1350 return new TypeAnnotationProxy(proxy, position);
jjg@308 1351 }
jjg@308 1352
jjg@308 1353 TypeAnnotationPosition readPosition() {
jjg@308 1354 byte tag = nextByte();
jjg@308 1355
jjg@308 1356 if (!TargetType.isValidTargetTypeValue(tag))
jjg@308 1357 throw this.badClassFile("bad.type.annotation.value", tag);
jjg@308 1358
jjg@308 1359 TypeAnnotationPosition position = new TypeAnnotationPosition();
jjg@308 1360 TargetType type = TargetType.fromTargetTypeValue(tag);
jjg@308 1361
jjg@308 1362 position.type = type;
jjg@308 1363
jjg@308 1364 switch (type) {
jjg@308 1365 // type case
jjg@308 1366 case TYPECAST:
jjg@308 1367 case TYPECAST_GENERIC_OR_ARRAY:
jjg@308 1368 // object creation
jjg@308 1369 case INSTANCEOF:
jjg@308 1370 case INSTANCEOF_GENERIC_OR_ARRAY:
jjg@308 1371 // new expression
jjg@308 1372 case NEW:
jjg@308 1373 case NEW_GENERIC_OR_ARRAY:
jjg@308 1374 position.offset = nextChar();
jjg@308 1375 break;
jjg@308 1376 // local variable
jjg@308 1377 case LOCAL_VARIABLE:
jjg@308 1378 case LOCAL_VARIABLE_GENERIC_OR_ARRAY:
jjg@308 1379 int table_length = nextChar();
jjg@308 1380 position.lvarOffset = new int[table_length];
jjg@308 1381 position.lvarLength = new int[table_length];
jjg@308 1382 position.lvarIndex = new int[table_length];
jjg@308 1383
jjg@308 1384 for (int i = 0; i < table_length; ++i) {
jjg@308 1385 position.lvarOffset[i] = nextChar();
jjg@308 1386 position.lvarLength[i] = nextChar();
jjg@308 1387 position.lvarIndex[i] = nextChar();
jjg@308 1388 }
jjg@308 1389 break;
jjg@308 1390 // method receiver
jjg@308 1391 case METHOD_RECEIVER:
jjg@308 1392 // Do nothing
jjg@308 1393 break;
jjg@308 1394 // type parameters
jjg@308 1395 case CLASS_TYPE_PARAMETER:
jjg@308 1396 case METHOD_TYPE_PARAMETER:
jjg@308 1397 position.parameter_index = nextByte();
jjg@308 1398 break;
jjg@308 1399 // type parameter bounds
jjg@308 1400 case CLASS_TYPE_PARAMETER_BOUND:
jjg@308 1401 case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
jjg@308 1402 case METHOD_TYPE_PARAMETER_BOUND:
jjg@308 1403 case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
jjg@308 1404 position.parameter_index = nextByte();
jjg@308 1405 position.bound_index = nextByte();
jjg@308 1406 break;
jjg@308 1407 // wildcard
jjg@308 1408 case WILDCARD_BOUND:
jjg@308 1409 case WILDCARD_BOUND_GENERIC_OR_ARRAY:
jjg@308 1410 position.wildcard_position = readPosition();
jjg@308 1411 break;
jjg@308 1412 // Class extends and implements clauses
jjg@308 1413 case CLASS_EXTENDS:
jjg@308 1414 case CLASS_EXTENDS_GENERIC_OR_ARRAY:
jjg@308 1415 position.type_index = nextByte();
jjg@308 1416 break;
jjg@308 1417 // throws
jjg@308 1418 case THROWS:
jjg@308 1419 position.type_index = nextByte();
jjg@308 1420 break;
jjg@308 1421 case CLASS_LITERAL:
jjg@308 1422 case CLASS_LITERAL_GENERIC_OR_ARRAY:
jjg@308 1423 position.offset = nextChar();
jjg@308 1424 break;
jjg@308 1425 // method parameter: not specified
jjg@308 1426 case METHOD_PARAMETER_GENERIC_OR_ARRAY:
jjg@308 1427 position.parameter_index = nextByte();
jjg@308 1428 break;
jjg@308 1429 // method type argument: wasn't specified
jjg@308 1430 case NEW_TYPE_ARGUMENT:
jjg@308 1431 case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@308 1432 case METHOD_TYPE_ARGUMENT:
jjg@308 1433 case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@308 1434 position.offset = nextChar();
jjg@308 1435 position.type_index = nextByte();
jjg@308 1436 break;
jjg@308 1437 // We don't need to worry abut these
jjg@308 1438 case METHOD_RETURN_GENERIC_OR_ARRAY:
jjg@308 1439 case FIELD_GENERIC_OR_ARRAY:
jjg@308 1440 break;
jjg@308 1441 case UNKNOWN:
jjg@308 1442 break;
jjg@308 1443 default:
jjg@308 1444 throw new AssertionError("unknown type: " + position);
jjg@308 1445 }
jjg@308 1446
jjg@308 1447 if (type.hasLocation()) {
jjg@308 1448 int len = nextChar();
jjg@308 1449 ListBuffer<Integer> loc = ListBuffer.lb();
jjg@308 1450 for (int i = 0; i < len; i++)
jjg@308 1451 loc = loc.append((int)nextByte());
jjg@308 1452 position.location = loc.toList();
jjg@308 1453 }
jjg@308 1454
jjg@308 1455 return position;
jjg@308 1456 }
duke@1 1457 Attribute readAttributeValue() {
duke@1 1458 char c = (char) buf[bp++];
duke@1 1459 switch (c) {
duke@1 1460 case 'B':
duke@1 1461 return new Attribute.Constant(syms.byteType, readPool(nextChar()));
duke@1 1462 case 'C':
duke@1 1463 return new Attribute.Constant(syms.charType, readPool(nextChar()));
duke@1 1464 case 'D':
duke@1 1465 return new Attribute.Constant(syms.doubleType, readPool(nextChar()));
duke@1 1466 case 'F':
duke@1 1467 return new Attribute.Constant(syms.floatType, readPool(nextChar()));
duke@1 1468 case 'I':
duke@1 1469 return new Attribute.Constant(syms.intType, readPool(nextChar()));
duke@1 1470 case 'J':
duke@1 1471 return new Attribute.Constant(syms.longType, readPool(nextChar()));
duke@1 1472 case 'S':
duke@1 1473 return new Attribute.Constant(syms.shortType, readPool(nextChar()));
duke@1 1474 case 'Z':
duke@1 1475 return new Attribute.Constant(syms.booleanType, readPool(nextChar()));
duke@1 1476 case 's':
duke@1 1477 return new Attribute.Constant(syms.stringType, readPool(nextChar()).toString());
duke@1 1478 case 'e':
duke@1 1479 return new EnumAttributeProxy(readEnumType(nextChar()), readName(nextChar()));
duke@1 1480 case 'c':
duke@1 1481 return new Attribute.Class(types, readTypeOrClassSymbol(nextChar()));
duke@1 1482 case '[': {
duke@1 1483 int n = nextChar();
duke@1 1484 ListBuffer<Attribute> l = new ListBuffer<Attribute>();
duke@1 1485 for (int i=0; i<n; i++)
duke@1 1486 l.append(readAttributeValue());
duke@1 1487 return new ArrayAttributeProxy(l.toList());
duke@1 1488 }
duke@1 1489 case '@':
duke@1 1490 return readCompoundAnnotation();
duke@1 1491 default:
duke@1 1492 throw new AssertionError("unknown annotation tag '" + c + "'");
duke@1 1493 }
duke@1 1494 }
duke@1 1495
duke@1 1496 interface ProxyVisitor extends Attribute.Visitor {
duke@1 1497 void visitEnumAttributeProxy(EnumAttributeProxy proxy);
duke@1 1498 void visitArrayAttributeProxy(ArrayAttributeProxy proxy);
duke@1 1499 void visitCompoundAnnotationProxy(CompoundAnnotationProxy proxy);
duke@1 1500 }
duke@1 1501
duke@1 1502 static class EnumAttributeProxy extends Attribute {
duke@1 1503 Type enumType;
duke@1 1504 Name enumerator;
duke@1 1505 public EnumAttributeProxy(Type enumType, Name enumerator) {
duke@1 1506 super(null);
duke@1 1507 this.enumType = enumType;
duke@1 1508 this.enumerator = enumerator;
duke@1 1509 }
duke@1 1510 public void accept(Visitor v) { ((ProxyVisitor)v).visitEnumAttributeProxy(this); }
jjg@256 1511 @Override
duke@1 1512 public String toString() {
duke@1 1513 return "/*proxy enum*/" + enumType + "." + enumerator;
duke@1 1514 }
duke@1 1515 }
duke@1 1516
duke@1 1517 static class ArrayAttributeProxy extends Attribute {
duke@1 1518 List<Attribute> values;
duke@1 1519 ArrayAttributeProxy(List<Attribute> values) {
duke@1 1520 super(null);
duke@1 1521 this.values = values;
duke@1 1522 }
duke@1 1523 public void accept(Visitor v) { ((ProxyVisitor)v).visitArrayAttributeProxy(this); }
jjg@256 1524 @Override
duke@1 1525 public String toString() {
duke@1 1526 return "{" + values + "}";
duke@1 1527 }
duke@1 1528 }
duke@1 1529
duke@1 1530 /** A temporary proxy representing a compound attribute.
duke@1 1531 */
duke@1 1532 static class CompoundAnnotationProxy extends Attribute {
duke@1 1533 final List<Pair<Name,Attribute>> values;
duke@1 1534 public CompoundAnnotationProxy(Type type,
duke@1 1535 List<Pair<Name,Attribute>> values) {
duke@1 1536 super(type);
duke@1 1537 this.values = values;
duke@1 1538 }
duke@1 1539 public void accept(Visitor v) { ((ProxyVisitor)v).visitCompoundAnnotationProxy(this); }
jjg@256 1540 @Override
duke@1 1541 public String toString() {
duke@1 1542 StringBuffer buf = new StringBuffer();
duke@1 1543 buf.append("@");
duke@1 1544 buf.append(type.tsym.getQualifiedName());
duke@1 1545 buf.append("/*proxy*/{");
duke@1 1546 boolean first = true;
duke@1 1547 for (List<Pair<Name,Attribute>> v = values;
duke@1 1548 v.nonEmpty(); v = v.tail) {
duke@1 1549 Pair<Name,Attribute> value = v.head;
duke@1 1550 if (!first) buf.append(",");
duke@1 1551 first = false;
duke@1 1552 buf.append(value.fst);
duke@1 1553 buf.append("=");
duke@1 1554 buf.append(value.snd);
duke@1 1555 }
duke@1 1556 buf.append("}");
duke@1 1557 return buf.toString();
duke@1 1558 }
duke@1 1559 }
duke@1 1560
jjg@308 1561 /** A temporary proxy representing a type annotation.
jjg@308 1562 */
jjg@308 1563 static class TypeAnnotationProxy {
jjg@308 1564 final CompoundAnnotationProxy compound;
jjg@308 1565 final TypeAnnotationPosition position;
jjg@308 1566 public TypeAnnotationProxy(CompoundAnnotationProxy compound,
jjg@308 1567 TypeAnnotationPosition position) {
jjg@308 1568 this.compound = compound;
jjg@308 1569 this.position = position;
jjg@308 1570 }
jjg@308 1571 }
jjg@308 1572
duke@1 1573 class AnnotationDeproxy implements ProxyVisitor {
duke@1 1574 private ClassSymbol requestingOwner = currentOwner.kind == MTH
duke@1 1575 ? currentOwner.enclClass() : (ClassSymbol)currentOwner;
duke@1 1576
duke@1 1577 List<Attribute.Compound> deproxyCompoundList(List<CompoundAnnotationProxy> pl) {
duke@1 1578 // also must fill in types!!!!
duke@1 1579 ListBuffer<Attribute.Compound> buf =
duke@1 1580 new ListBuffer<Attribute.Compound>();
duke@1 1581 for (List<CompoundAnnotationProxy> l = pl; l.nonEmpty(); l=l.tail) {
duke@1 1582 buf.append(deproxyCompound(l.head));
duke@1 1583 }
duke@1 1584 return buf.toList();
duke@1 1585 }
duke@1 1586
duke@1 1587 Attribute.Compound deproxyCompound(CompoundAnnotationProxy a) {
duke@1 1588 ListBuffer<Pair<Symbol.MethodSymbol,Attribute>> buf =
duke@1 1589 new ListBuffer<Pair<Symbol.MethodSymbol,Attribute>>();
duke@1 1590 for (List<Pair<Name,Attribute>> l = a.values;
duke@1 1591 l.nonEmpty();
duke@1 1592 l = l.tail) {
duke@1 1593 MethodSymbol meth = findAccessMethod(a.type, l.head.fst);
duke@1 1594 buf.append(new Pair<Symbol.MethodSymbol,Attribute>
duke@1 1595 (meth, deproxy(meth.type.getReturnType(), l.head.snd)));
duke@1 1596 }
duke@1 1597 return new Attribute.Compound(a.type, buf.toList());
duke@1 1598 }
duke@1 1599
duke@1 1600 MethodSymbol findAccessMethod(Type container, Name name) {
duke@1 1601 CompletionFailure failure = null;
duke@1 1602 try {
duke@1 1603 for (Scope.Entry e = container.tsym.members().lookup(name);
duke@1 1604 e.scope != null;
duke@1 1605 e = e.next()) {
duke@1 1606 Symbol sym = e.sym;
duke@1 1607 if (sym.kind == MTH && sym.type.getParameterTypes().length() == 0)
duke@1 1608 return (MethodSymbol) sym;
duke@1 1609 }
duke@1 1610 } catch (CompletionFailure ex) {
duke@1 1611 failure = ex;
duke@1 1612 }
duke@1 1613 // The method wasn't found: emit a warning and recover
duke@1 1614 JavaFileObject prevSource = log.useSource(requestingOwner.classfile);
duke@1 1615 try {
duke@1 1616 if (failure == null) {
duke@1 1617 log.warning("annotation.method.not.found",
duke@1 1618 container,
duke@1 1619 name);
duke@1 1620 } else {
duke@1 1621 log.warning("annotation.method.not.found.reason",
duke@1 1622 container,
duke@1 1623 name,
mcimadamore@80 1624 failure.getDetailValue());//diagnostic, if present
duke@1 1625 }
duke@1 1626 } finally {
duke@1 1627 log.useSource(prevSource);
duke@1 1628 }
duke@1 1629 // Construct a new method type and symbol. Use bottom
duke@1 1630 // type (typeof null) as return type because this type is
duke@1 1631 // a subtype of all reference types and can be converted
duke@1 1632 // to primitive types by unboxing.
duke@1 1633 MethodType mt = new MethodType(List.<Type>nil(),
duke@1 1634 syms.botType,
duke@1 1635 List.<Type>nil(),
duke@1 1636 syms.methodClass);
duke@1 1637 return new MethodSymbol(PUBLIC | ABSTRACT, name, mt, container.tsym);
duke@1 1638 }
duke@1 1639
duke@1 1640 Attribute result;
duke@1 1641 Type type;
duke@1 1642 Attribute deproxy(Type t, Attribute a) {
duke@1 1643 Type oldType = type;
duke@1 1644 try {
duke@1 1645 type = t;
duke@1 1646 a.accept(this);
duke@1 1647 return result;
duke@1 1648 } finally {
duke@1 1649 type = oldType;
duke@1 1650 }
duke@1 1651 }
duke@1 1652
duke@1 1653 // implement Attribute.Visitor below
duke@1 1654
duke@1 1655 public void visitConstant(Attribute.Constant value) {
duke@1 1656 // assert value.type == type;
duke@1 1657 result = value;
duke@1 1658 }
duke@1 1659
duke@1 1660 public void visitClass(Attribute.Class clazz) {
duke@1 1661 result = clazz;
duke@1 1662 }
duke@1 1663
duke@1 1664 public void visitEnum(Attribute.Enum e) {
duke@1 1665 throw new AssertionError(); // shouldn't happen
duke@1 1666 }
duke@1 1667
duke@1 1668 public void visitCompound(Attribute.Compound compound) {
duke@1 1669 throw new AssertionError(); // shouldn't happen
duke@1 1670 }
duke@1 1671
duke@1 1672 public void visitArray(Attribute.Array array) {
duke@1 1673 throw new AssertionError(); // shouldn't happen
duke@1 1674 }
duke@1 1675
duke@1 1676 public void visitError(Attribute.Error e) {
duke@1 1677 throw new AssertionError(); // shouldn't happen
duke@1 1678 }
duke@1 1679
duke@1 1680 public void visitEnumAttributeProxy(EnumAttributeProxy proxy) {
duke@1 1681 // type.tsym.flatName() should == proxy.enumFlatName
duke@1 1682 TypeSymbol enumTypeSym = proxy.enumType.tsym;
duke@1 1683 VarSymbol enumerator = null;
duke@1 1684 for (Scope.Entry e = enumTypeSym.members().lookup(proxy.enumerator);
duke@1 1685 e.scope != null;
duke@1 1686 e = e.next()) {
duke@1 1687 if (e.sym.kind == VAR) {
duke@1 1688 enumerator = (VarSymbol)e.sym;
duke@1 1689 break;
duke@1 1690 }
duke@1 1691 }
duke@1 1692 if (enumerator == null) {
duke@1 1693 log.error("unknown.enum.constant",
duke@1 1694 currentClassFile, enumTypeSym, proxy.enumerator);
duke@1 1695 result = new Attribute.Error(enumTypeSym.type);
duke@1 1696 } else {
duke@1 1697 result = new Attribute.Enum(enumTypeSym.type, enumerator);
duke@1 1698 }
duke@1 1699 }
duke@1 1700
duke@1 1701 public void visitArrayAttributeProxy(ArrayAttributeProxy proxy) {
duke@1 1702 int length = proxy.values.length();
duke@1 1703 Attribute[] ats = new Attribute[length];
duke@1 1704 Type elemtype = types.elemtype(type);
duke@1 1705 int i = 0;
duke@1 1706 for (List<Attribute> p = proxy.values; p.nonEmpty(); p = p.tail) {
duke@1 1707 ats[i++] = deproxy(elemtype, p.head);
duke@1 1708 }
duke@1 1709 result = new Attribute.Array(type, ats);
duke@1 1710 }
duke@1 1711
duke@1 1712 public void visitCompoundAnnotationProxy(CompoundAnnotationProxy proxy) {
duke@1 1713 result = deproxyCompound(proxy);
duke@1 1714 }
duke@1 1715 }
duke@1 1716
duke@1 1717 class AnnotationDefaultCompleter extends AnnotationDeproxy implements Annotate.Annotator {
duke@1 1718 final MethodSymbol sym;
duke@1 1719 final Attribute value;
duke@1 1720 final JavaFileObject classFile = currentClassFile;
jjg@256 1721 @Override
duke@1 1722 public String toString() {
duke@1 1723 return " ClassReader store default for " + sym.owner + "." + sym + " is " + value;
duke@1 1724 }
duke@1 1725 AnnotationDefaultCompleter(MethodSymbol sym, Attribute value) {
duke@1 1726 this.sym = sym;
duke@1 1727 this.value = value;
duke@1 1728 }
duke@1 1729 // implement Annotate.Annotator.enterAnnotation()
duke@1 1730 public void enterAnnotation() {
duke@1 1731 JavaFileObject previousClassFile = currentClassFile;
duke@1 1732 try {
duke@1 1733 currentClassFile = classFile;
duke@1 1734 sym.defaultValue = deproxy(sym.type.getReturnType(), value);
duke@1 1735 } finally {
duke@1 1736 currentClassFile = previousClassFile;
duke@1 1737 }
duke@1 1738 }
duke@1 1739 }
duke@1 1740
duke@1 1741 class AnnotationCompleter extends AnnotationDeproxy implements Annotate.Annotator {
duke@1 1742 final Symbol sym;
duke@1 1743 final List<CompoundAnnotationProxy> l;
duke@1 1744 final JavaFileObject classFile;
jjg@256 1745 @Override
duke@1 1746 public String toString() {
duke@1 1747 return " ClassReader annotate " + sym.owner + "." + sym + " with " + l;
duke@1 1748 }
duke@1 1749 AnnotationCompleter(Symbol sym, List<CompoundAnnotationProxy> l) {
duke@1 1750 this.sym = sym;
duke@1 1751 this.l = l;
duke@1 1752 this.classFile = currentClassFile;
duke@1 1753 }
duke@1 1754 // implement Annotate.Annotator.enterAnnotation()
duke@1 1755 public void enterAnnotation() {
duke@1 1756 JavaFileObject previousClassFile = currentClassFile;
duke@1 1757 try {
duke@1 1758 currentClassFile = classFile;
duke@1 1759 List<Attribute.Compound> newList = deproxyCompoundList(l);
duke@1 1760 sym.attributes_field = ((sym.attributes_field == null)
duke@1 1761 ? newList
duke@1 1762 : newList.prependList(sym.attributes_field));
duke@1 1763 } finally {
duke@1 1764 currentClassFile = previousClassFile;
duke@1 1765 }
duke@1 1766 }
duke@1 1767 }
duke@1 1768
jjg@308 1769 class TypeAnnotationCompleter extends AnnotationCompleter {
jjg@308 1770
jjg@308 1771 List<TypeAnnotationProxy> proxies;
jjg@308 1772
jjg@308 1773 TypeAnnotationCompleter(Symbol sym,
jjg@308 1774 List<TypeAnnotationProxy> proxies) {
jjg@308 1775 super(sym, List.<CompoundAnnotationProxy>nil());
jjg@308 1776 this.proxies = proxies;
jjg@308 1777 }
jjg@308 1778
jjg@308 1779 List<Attribute.TypeCompound> deproxyTypeCompoundList(List<TypeAnnotationProxy> proxies) {
jjg@308 1780 ListBuffer<Attribute.TypeCompound> buf = ListBuffer.lb();
jjg@308 1781 for (TypeAnnotationProxy proxy: proxies) {
jjg@308 1782 Attribute.Compound compound = deproxyCompound(proxy.compound);
jjg@308 1783 Attribute.TypeCompound typeCompound = new Attribute.TypeCompound(compound, proxy.position);
jjg@308 1784 buf.add(typeCompound);
jjg@308 1785 }
jjg@308 1786 return buf.toList();
jjg@308 1787 }
jjg@308 1788
jjg@308 1789 @Override
jjg@308 1790 public void enterAnnotation() {
jjg@308 1791 JavaFileObject previousClassFile = currentClassFile;
jjg@308 1792 try {
jjg@308 1793 currentClassFile = classFile;
jjg@308 1794 List<Attribute.TypeCompound> newList = deproxyTypeCompoundList(proxies);
jjg@308 1795 if (debugJSR308)
jjg@308 1796 System.out.println("TA: reading: adding " + newList
jjg@308 1797 + " to symbol " + sym + " in " + log.currentSourceFile());
jjg@308 1798 sym.typeAnnotations = ((sym.typeAnnotations == null)
jjg@308 1799 ? newList
jjg@308 1800 : newList.prependList(sym.typeAnnotations));
jjg@308 1801
jjg@308 1802 } finally {
jjg@308 1803 currentClassFile = previousClassFile;
jjg@308 1804 }
jjg@308 1805 }
jjg@308 1806 }
jjg@308 1807
duke@1 1808
duke@1 1809 /************************************************************************
duke@1 1810 * Reading Symbols
duke@1 1811 ***********************************************************************/
duke@1 1812
duke@1 1813 /** Read a field.
duke@1 1814 */
duke@1 1815 VarSymbol readField() {
duke@1 1816 long flags = adjustFieldFlags(nextChar());
duke@1 1817 Name name = readName(nextChar());
duke@1 1818 Type type = readType(nextChar());
duke@1 1819 VarSymbol v = new VarSymbol(flags, name, type, currentOwner);
duke@1 1820 readMemberAttrs(v);
duke@1 1821 return v;
duke@1 1822 }
duke@1 1823
duke@1 1824 /** Read a method.
duke@1 1825 */
duke@1 1826 MethodSymbol readMethod() {
duke@1 1827 long flags = adjustMethodFlags(nextChar());
duke@1 1828 Name name = readName(nextChar());
duke@1 1829 Type type = readType(nextChar());
duke@1 1830 if (name == names.init && currentOwner.hasOuterInstance()) {
duke@1 1831 // Sometimes anonymous classes don't have an outer
duke@1 1832 // instance, however, there is no reliable way to tell so
duke@1 1833 // we never strip this$n
jjg@113 1834 if (!currentOwner.name.isEmpty())
duke@1 1835 type = new MethodType(type.getParameterTypes().tail,
duke@1 1836 type.getReturnType(),
duke@1 1837 type.getThrownTypes(),
duke@1 1838 syms.methodClass);
duke@1 1839 }
duke@1 1840 MethodSymbol m = new MethodSymbol(flags, name, type, currentOwner);
duke@1 1841 Symbol prevOwner = currentOwner;
duke@1 1842 currentOwner = m;
duke@1 1843 try {
duke@1 1844 readMemberAttrs(m);
duke@1 1845 } finally {
duke@1 1846 currentOwner = prevOwner;
duke@1 1847 }
duke@1 1848 return m;
duke@1 1849 }
duke@1 1850
duke@1 1851 /** Skip a field or method
duke@1 1852 */
duke@1 1853 void skipMember() {
duke@1 1854 bp = bp + 6;
duke@1 1855 char ac = nextChar();
duke@1 1856 for (int i = 0; i < ac; i++) {
duke@1 1857 bp = bp + 2;
duke@1 1858 int attrLen = nextInt();
duke@1 1859 bp = bp + attrLen;
duke@1 1860 }
duke@1 1861 }
duke@1 1862
duke@1 1863 /** Enter type variables of this classtype and all enclosing ones in
duke@1 1864 * `typevars'.
duke@1 1865 */
duke@1 1866 protected void enterTypevars(Type t) {
duke@1 1867 if (t.getEnclosingType() != null && t.getEnclosingType().tag == CLASS)
duke@1 1868 enterTypevars(t.getEnclosingType());
duke@1 1869 for (List<Type> xs = t.getTypeArguments(); xs.nonEmpty(); xs = xs.tail)
duke@1 1870 typevars.enter(xs.head.tsym);
duke@1 1871 }
duke@1 1872
duke@1 1873 protected void enterTypevars(Symbol sym) {
duke@1 1874 if (sym.owner.kind == MTH) {
duke@1 1875 enterTypevars(sym.owner);
duke@1 1876 enterTypevars(sym.owner.owner);
duke@1 1877 }
duke@1 1878 enterTypevars(sym.type);
duke@1 1879 }
duke@1 1880
duke@1 1881 /** Read contents of a given class symbol `c'. Both external and internal
duke@1 1882 * versions of an inner class are read.
duke@1 1883 */
duke@1 1884 void readClass(ClassSymbol c) {
duke@1 1885 ClassType ct = (ClassType)c.type;
duke@1 1886
duke@1 1887 // allocate scope for members
duke@1 1888 c.members_field = new Scope(c);
duke@1 1889
duke@1 1890 // prepare type variable table
duke@1 1891 typevars = typevars.dup(currentOwner);
jjg@256 1892 if (ct.getEnclosingType().tag == CLASS)
jjg@256 1893 enterTypevars(ct.getEnclosingType());
duke@1 1894
duke@1 1895 // read flags, or skip if this is an inner class
duke@1 1896 long flags = adjustClassFlags(nextChar());
duke@1 1897 if (c.owner.kind == PCK) c.flags_field = flags;
duke@1 1898
duke@1 1899 // read own class name and check that it matches
duke@1 1900 ClassSymbol self = readClassSymbol(nextChar());
duke@1 1901 if (c != self)
duke@1 1902 throw badClassFile("class.file.wrong.class",
duke@1 1903 self.flatname);
duke@1 1904
duke@1 1905 // class attributes must be read before class
duke@1 1906 // skip ahead to read class attributes
duke@1 1907 int startbp = bp;
duke@1 1908 nextChar();
duke@1 1909 char interfaceCount = nextChar();
duke@1 1910 bp += interfaceCount * 2;
duke@1 1911 char fieldCount = nextChar();
duke@1 1912 for (int i = 0; i < fieldCount; i++) skipMember();
duke@1 1913 char methodCount = nextChar();
duke@1 1914 for (int i = 0; i < methodCount; i++) skipMember();
duke@1 1915 readClassAttrs(c);
duke@1 1916
duke@1 1917 if (readAllOfClassFile) {
duke@1 1918 for (int i = 1; i < poolObj.length; i++) readPool(i);
duke@1 1919 c.pool = new Pool(poolObj.length, poolObj);
duke@1 1920 }
duke@1 1921
duke@1 1922 // reset and read rest of classinfo
duke@1 1923 bp = startbp;
duke@1 1924 int n = nextChar();
duke@1 1925 if (ct.supertype_field == null)
duke@1 1926 ct.supertype_field = (n == 0)
duke@1 1927 ? Type.noType
duke@1 1928 : readClassSymbol(n).erasure(types);
duke@1 1929 n = nextChar();
duke@1 1930 List<Type> is = List.nil();
duke@1 1931 for (int i = 0; i < n; i++) {
duke@1 1932 Type _inter = readClassSymbol(nextChar()).erasure(types);
duke@1 1933 is = is.prepend(_inter);
duke@1 1934 }
duke@1 1935 if (ct.interfaces_field == null)
duke@1 1936 ct.interfaces_field = is.reverse();
duke@1 1937
duke@1 1938 if (fieldCount != nextChar()) assert false;
duke@1 1939 for (int i = 0; i < fieldCount; i++) enterMember(c, readField());
duke@1 1940 if (methodCount != nextChar()) assert false;
duke@1 1941 for (int i = 0; i < methodCount; i++) enterMember(c, readMethod());
duke@1 1942
duke@1 1943 typevars = typevars.leave();
duke@1 1944 }
duke@1 1945
duke@1 1946 /** Read inner class info. For each inner/outer pair allocate a
duke@1 1947 * member class.
duke@1 1948 */
duke@1 1949 void readInnerClasses(ClassSymbol c) {
duke@1 1950 int n = nextChar();
duke@1 1951 for (int i = 0; i < n; i++) {
duke@1 1952 nextChar(); // skip inner class symbol
duke@1 1953 ClassSymbol outer = readClassSymbol(nextChar());
duke@1 1954 Name name = readName(nextChar());
duke@1 1955 if (name == null) name = names.empty;
duke@1 1956 long flags = adjustClassFlags(nextChar());
duke@1 1957 if (outer != null) { // we have a member class
duke@1 1958 if (name == names.empty)
duke@1 1959 name = names.one;
duke@1 1960 ClassSymbol member = enterClass(name, outer);
duke@1 1961 if ((flags & STATIC) == 0) {
duke@1 1962 ((ClassType)member.type).setEnclosingType(outer.type);
duke@1 1963 if (member.erasure_field != null)
duke@1 1964 ((ClassType)member.erasure_field).setEnclosingType(types.erasure(outer.type));
duke@1 1965 }
duke@1 1966 if (c == outer) {
duke@1 1967 member.flags_field = flags;
duke@1 1968 enterMember(c, member);
duke@1 1969 }
duke@1 1970 }
duke@1 1971 }
duke@1 1972 }
duke@1 1973
duke@1 1974 /** Read a class file.
duke@1 1975 */
duke@1 1976 private void readClassFile(ClassSymbol c) throws IOException {
duke@1 1977 int magic = nextInt();
duke@1 1978 if (magic != JAVA_MAGIC)
duke@1 1979 throw badClassFile("illegal.start.of.class.file");
duke@1 1980
jjg@256 1981 minorVersion = nextChar();
jjg@256 1982 majorVersion = nextChar();
duke@1 1983 int maxMajor = Target.MAX().majorVersion;
duke@1 1984 int maxMinor = Target.MAX().minorVersion;
duke@1 1985 if (majorVersion > maxMajor ||
duke@1 1986 majorVersion * 1000 + minorVersion <
duke@1 1987 Target.MIN().majorVersion * 1000 + Target.MIN().minorVersion)
duke@1 1988 {
duke@1 1989 if (majorVersion == (maxMajor + 1))
duke@1 1990 log.warning("big.major.version",
duke@1 1991 currentClassFile,
duke@1 1992 majorVersion,
duke@1 1993 maxMajor);
duke@1 1994 else
duke@1 1995 throw badClassFile("wrong.version",
duke@1 1996 Integer.toString(majorVersion),
duke@1 1997 Integer.toString(minorVersion),
duke@1 1998 Integer.toString(maxMajor),
duke@1 1999 Integer.toString(maxMinor));
duke@1 2000 }
duke@1 2001 else if (checkClassFile &&
duke@1 2002 majorVersion == maxMajor &&
duke@1 2003 minorVersion > maxMinor)
duke@1 2004 {
duke@1 2005 printCCF("found.later.version",
duke@1 2006 Integer.toString(minorVersion));
duke@1 2007 }
duke@1 2008 indexPool();
duke@1 2009 if (signatureBuffer.length < bp) {
duke@1 2010 int ns = Integer.highestOneBit(bp) << 1;
duke@1 2011 signatureBuffer = new byte[ns];
duke@1 2012 }
duke@1 2013 readClass(c);
duke@1 2014 }
duke@1 2015
duke@1 2016 /************************************************************************
duke@1 2017 * Adjusting flags
duke@1 2018 ***********************************************************************/
duke@1 2019
duke@1 2020 long adjustFieldFlags(long flags) {
duke@1 2021 return flags;
duke@1 2022 }
duke@1 2023 long adjustMethodFlags(long flags) {
duke@1 2024 if ((flags & ACC_BRIDGE) != 0) {
duke@1 2025 flags &= ~ACC_BRIDGE;
duke@1 2026 flags |= BRIDGE;
duke@1 2027 if (!allowGenerics)
duke@1 2028 flags &= ~SYNTHETIC;
duke@1 2029 }
duke@1 2030 if ((flags & ACC_VARARGS) != 0) {
duke@1 2031 flags &= ~ACC_VARARGS;
duke@1 2032 flags |= VARARGS;
duke@1 2033 }
duke@1 2034 return flags;
duke@1 2035 }
duke@1 2036 long adjustClassFlags(long flags) {
duke@1 2037 return flags & ~ACC_SUPER; // SUPER and SYNCHRONIZED bits overloaded
duke@1 2038 }
duke@1 2039
duke@1 2040 /************************************************************************
duke@1 2041 * Loading Classes
duke@1 2042 ***********************************************************************/
duke@1 2043
duke@1 2044 /** Define a new class given its name and owner.
duke@1 2045 */
duke@1 2046 public ClassSymbol defineClass(Name name, Symbol owner) {
duke@1 2047 ClassSymbol c = new ClassSymbol(0, name, owner);
duke@1 2048 if (owner.kind == PCK)
duke@1 2049 assert classes.get(c.flatname) == null : c;
duke@1 2050 c.completer = this;
duke@1 2051 return c;
duke@1 2052 }
duke@1 2053
duke@1 2054 /** Create a new toplevel or member class symbol with given name
duke@1 2055 * and owner and enter in `classes' unless already there.
duke@1 2056 */
duke@1 2057 public ClassSymbol enterClass(Name name, TypeSymbol owner) {
duke@1 2058 Name flatname = TypeSymbol.formFlatName(name, owner);
duke@1 2059 ClassSymbol c = classes.get(flatname);
duke@1 2060 if (c == null) {
duke@1 2061 c = defineClass(name, owner);
duke@1 2062 classes.put(flatname, c);
duke@1 2063 } else if ((c.name != name || c.owner != owner) && owner.kind == TYP && c.owner.kind == PCK) {
duke@1 2064 // reassign fields of classes that might have been loaded with
duke@1 2065 // their flat names.
duke@1 2066 c.owner.members().remove(c);
duke@1 2067 c.name = name;
duke@1 2068 c.owner = owner;
duke@1 2069 c.fullname = ClassSymbol.formFullName(name, owner);
duke@1 2070 }
duke@1 2071 return c;
duke@1 2072 }
duke@1 2073
duke@1 2074 /**
duke@1 2075 * Creates a new toplevel class symbol with given flat name and
duke@1 2076 * given class (or source) file.
duke@1 2077 *
duke@1 2078 * @param flatName a fully qualified binary class name
duke@1 2079 * @param classFile the class file or compilation unit defining
duke@1 2080 * the class (may be {@code null})
duke@1 2081 * @return a newly created class symbol
duke@1 2082 * @throws AssertionError if the class symbol already exists
duke@1 2083 */
duke@1 2084 public ClassSymbol enterClass(Name flatName, JavaFileObject classFile) {
duke@1 2085 ClassSymbol cs = classes.get(flatName);
duke@1 2086 if (cs != null) {
duke@1 2087 String msg = Log.format("%s: completer = %s; class file = %s; source file = %s",
duke@1 2088 cs.fullname,
duke@1 2089 cs.completer,
duke@1 2090 cs.classfile,
duke@1 2091 cs.sourcefile);
duke@1 2092 throw new AssertionError(msg);
duke@1 2093 }
duke@1 2094 Name packageName = Convert.packagePart(flatName);
duke@1 2095 PackageSymbol owner = packageName.isEmpty()
duke@1 2096 ? syms.unnamedPackage
duke@1 2097 : enterPackage(packageName);
duke@1 2098 cs = defineClass(Convert.shortName(flatName), owner);
duke@1 2099 cs.classfile = classFile;
duke@1 2100 classes.put(flatName, cs);
duke@1 2101 return cs;
duke@1 2102 }
duke@1 2103
duke@1 2104 /** Create a new member or toplevel class symbol with given flat name
duke@1 2105 * and enter in `classes' unless already there.
duke@1 2106 */
duke@1 2107 public ClassSymbol enterClass(Name flatname) {
duke@1 2108 ClassSymbol c = classes.get(flatname);
duke@1 2109 if (c == null)
duke@1 2110 return enterClass(flatname, (JavaFileObject)null);
duke@1 2111 else
duke@1 2112 return c;
duke@1 2113 }
duke@1 2114
duke@1 2115 private boolean suppressFlush = false;
duke@1 2116
duke@1 2117 /** Completion for classes to be loaded. Before a class is loaded
duke@1 2118 * we make sure its enclosing class (if any) is loaded.
duke@1 2119 */
duke@1 2120 public void complete(Symbol sym) throws CompletionFailure {
duke@1 2121 if (sym.kind == TYP) {
duke@1 2122 ClassSymbol c = (ClassSymbol)sym;
duke@1 2123 c.members_field = new Scope.ErrorScope(c); // make sure it's always defined
jjg@256 2124 boolean saveSuppressFlush = suppressFlush;
jjg@256 2125 suppressFlush = true;
duke@1 2126 try {
duke@1 2127 completeOwners(c.owner);
duke@1 2128 completeEnclosing(c);
duke@1 2129 } finally {
jjg@256 2130 suppressFlush = saveSuppressFlush;
duke@1 2131 }
duke@1 2132 fillIn(c);
duke@1 2133 } else if (sym.kind == PCK) {
duke@1 2134 PackageSymbol p = (PackageSymbol)sym;
duke@1 2135 try {
duke@1 2136 fillIn(p);
duke@1 2137 } catch (IOException ex) {
duke@1 2138 throw new CompletionFailure(sym, ex.getLocalizedMessage()).initCause(ex);
duke@1 2139 }
duke@1 2140 }
duke@1 2141 if (!filling && !suppressFlush)
duke@1 2142 annotate.flush(); // finish attaching annotations
duke@1 2143 }
duke@1 2144
duke@1 2145 /** complete up through the enclosing package. */
duke@1 2146 private void completeOwners(Symbol o) {
duke@1 2147 if (o.kind != PCK) completeOwners(o.owner);
duke@1 2148 o.complete();
duke@1 2149 }
duke@1 2150
duke@1 2151 /**
duke@1 2152 * Tries to complete lexically enclosing classes if c looks like a
duke@1 2153 * nested class. This is similar to completeOwners but handles
duke@1 2154 * the situation when a nested class is accessed directly as it is
duke@1 2155 * possible with the Tree API or javax.lang.model.*.
duke@1 2156 */
duke@1 2157 private void completeEnclosing(ClassSymbol c) {
duke@1 2158 if (c.owner.kind == PCK) {
duke@1 2159 Symbol owner = c.owner;
duke@1 2160 for (Name name : Convert.enclosingCandidates(Convert.shortName(c.name))) {
duke@1 2161 Symbol encl = owner.members().lookup(name).sym;
duke@1 2162 if (encl == null)
duke@1 2163 encl = classes.get(TypeSymbol.formFlatName(name, owner));
duke@1 2164 if (encl != null)
duke@1 2165 encl.complete();
duke@1 2166 }
duke@1 2167 }
duke@1 2168 }
duke@1 2169
duke@1 2170 /** We can only read a single class file at a time; this
duke@1 2171 * flag keeps track of when we are currently reading a class
duke@1 2172 * file.
duke@1 2173 */
duke@1 2174 private boolean filling = false;
duke@1 2175
duke@1 2176 /** Fill in definition of class `c' from corresponding class or
duke@1 2177 * source file.
duke@1 2178 */
duke@1 2179 private void fillIn(ClassSymbol c) {
duke@1 2180 if (completionFailureName == c.fullname) {
duke@1 2181 throw new CompletionFailure(c, "user-selected completion failure by class name");
duke@1 2182 }
duke@1 2183 currentOwner = c;
duke@1 2184 JavaFileObject classfile = c.classfile;
duke@1 2185 if (classfile != null) {
duke@1 2186 JavaFileObject previousClassFile = currentClassFile;
duke@1 2187 try {
duke@1 2188 assert !filling :
duke@1 2189 "Filling " + classfile.toUri() +
duke@1 2190 " during " + previousClassFile;
duke@1 2191 currentClassFile = classfile;
duke@1 2192 if (verbose) {
duke@1 2193 printVerbose("loading", currentClassFile.toString());
duke@1 2194 }
duke@1 2195 if (classfile.getKind() == JavaFileObject.Kind.CLASS) {
duke@1 2196 filling = true;
duke@1 2197 try {
duke@1 2198 bp = 0;
duke@1 2199 buf = readInputStream(buf, classfile.openInputStream());
duke@1 2200 readClassFile(c);
duke@1 2201 if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) {
duke@1 2202 List<Type> missing = missingTypeVariables;
duke@1 2203 List<Type> found = foundTypeVariables;
duke@1 2204 missingTypeVariables = List.nil();
duke@1 2205 foundTypeVariables = List.nil();
duke@1 2206 filling = false;
duke@1 2207 ClassType ct = (ClassType)currentOwner.type;
duke@1 2208 ct.supertype_field =
duke@1 2209 types.subst(ct.supertype_field, missing, found);
duke@1 2210 ct.interfaces_field =
duke@1 2211 types.subst(ct.interfaces_field, missing, found);
duke@1 2212 } else if (missingTypeVariables.isEmpty() !=
duke@1 2213 foundTypeVariables.isEmpty()) {
duke@1 2214 Name name = missingTypeVariables.head.tsym.name;
duke@1 2215 throw badClassFile("undecl.type.var", name);
duke@1 2216 }
duke@1 2217 } finally {
duke@1 2218 missingTypeVariables = List.nil();
duke@1 2219 foundTypeVariables = List.nil();
duke@1 2220 filling = false;
duke@1 2221 }
duke@1 2222 } else {
duke@1 2223 if (sourceCompleter != null) {
duke@1 2224 sourceCompleter.complete(c);
duke@1 2225 } else {
duke@1 2226 throw new IllegalStateException("Source completer required to read "
duke@1 2227 + classfile.toUri());
duke@1 2228 }
duke@1 2229 }
duke@1 2230 return;
duke@1 2231 } catch (IOException ex) {
duke@1 2232 throw badClassFile("unable.to.access.file", ex.getMessage());
duke@1 2233 } finally {
duke@1 2234 currentClassFile = previousClassFile;
duke@1 2235 }
duke@1 2236 } else {
jjg@12 2237 JCDiagnostic diag =
jjg@12 2238 diagFactory.fragment("class.file.not.found", c.flatname);
duke@1 2239 throw
jjg@12 2240 newCompletionFailure(c, diag);
duke@1 2241 }
duke@1 2242 }
duke@1 2243 // where
duke@1 2244 private static byte[] readInputStream(byte[] buf, InputStream s) throws IOException {
duke@1 2245 try {
duke@1 2246 buf = ensureCapacity(buf, s.available());
duke@1 2247 int r = s.read(buf);
duke@1 2248 int bp = 0;
duke@1 2249 while (r != -1) {
duke@1 2250 bp += r;
duke@1 2251 buf = ensureCapacity(buf, bp);
duke@1 2252 r = s.read(buf, bp, buf.length - bp);
duke@1 2253 }
duke@1 2254 return buf;
duke@1 2255 } finally {
duke@1 2256 try {
duke@1 2257 s.close();
duke@1 2258 } catch (IOException e) {
duke@1 2259 /* Ignore any errors, as this stream may have already
duke@1 2260 * thrown a related exception which is the one that
duke@1 2261 * should be reported.
duke@1 2262 */
duke@1 2263 }
duke@1 2264 }
duke@1 2265 }
duke@1 2266 private static byte[] ensureCapacity(byte[] buf, int needed) {
duke@1 2267 if (buf.length < needed) {
duke@1 2268 byte[] old = buf;
duke@1 2269 buf = new byte[Integer.highestOneBit(needed) << 1];
duke@1 2270 System.arraycopy(old, 0, buf, 0, old.length);
duke@1 2271 }
duke@1 2272 return buf;
duke@1 2273 }
duke@1 2274 /** Static factory for CompletionFailure objects.
duke@1 2275 * In practice, only one can be used at a time, so we share one
duke@1 2276 * to reduce the expense of allocating new exception objects.
duke@1 2277 */
jjg@12 2278 private CompletionFailure newCompletionFailure(TypeSymbol c,
jjg@12 2279 JCDiagnostic diag) {
duke@1 2280 if (!cacheCompletionFailure) {
duke@1 2281 // log.warning("proc.messager",
duke@1 2282 // Log.getLocalizedString("class.file.not.found", c.flatname));
duke@1 2283 // c.debug.printStackTrace();
jjg@12 2284 return new CompletionFailure(c, diag);
duke@1 2285 } else {
duke@1 2286 CompletionFailure result = cachedCompletionFailure;
duke@1 2287 result.sym = c;
jjg@12 2288 result.diag = diag;
duke@1 2289 return result;
duke@1 2290 }
duke@1 2291 }
duke@1 2292 private CompletionFailure cachedCompletionFailure =
jjg@12 2293 new CompletionFailure(null, (JCDiagnostic) null);
duke@1 2294 {
duke@1 2295 cachedCompletionFailure.setStackTrace(new StackTraceElement[0]);
duke@1 2296 }
duke@1 2297
duke@1 2298 /** Load a toplevel class with given fully qualified name
duke@1 2299 * The class is entered into `classes' only if load was successful.
duke@1 2300 */
duke@1 2301 public ClassSymbol loadClass(Name flatname) throws CompletionFailure {
duke@1 2302 boolean absent = classes.get(flatname) == null;
duke@1 2303 ClassSymbol c = enterClass(flatname);
duke@1 2304 if (c.members_field == null && c.completer != null) {
duke@1 2305 try {
duke@1 2306 c.complete();
duke@1 2307 } catch (CompletionFailure ex) {
duke@1 2308 if (absent) classes.remove(flatname);
duke@1 2309 throw ex;
duke@1 2310 }
duke@1 2311 }
duke@1 2312 return c;
duke@1 2313 }
duke@1 2314
duke@1 2315 /************************************************************************
duke@1 2316 * Loading Packages
duke@1 2317 ***********************************************************************/
duke@1 2318
duke@1 2319 /** Check to see if a package exists, given its fully qualified name.
duke@1 2320 */
duke@1 2321 public boolean packageExists(Name fullname) {
duke@1 2322 return enterPackage(fullname).exists();
duke@1 2323 }
duke@1 2324
duke@1 2325 /** Make a package, given its fully qualified name.
duke@1 2326 */
duke@1 2327 public PackageSymbol enterPackage(Name fullname) {
duke@1 2328 PackageSymbol p = packages.get(fullname);
duke@1 2329 if (p == null) {
duke@1 2330 assert !fullname.isEmpty() : "rootPackage missing!";
duke@1 2331 p = new PackageSymbol(
duke@1 2332 Convert.shortName(fullname),
duke@1 2333 enterPackage(Convert.packagePart(fullname)));
duke@1 2334 p.completer = this;
duke@1 2335 packages.put(fullname, p);
duke@1 2336 }
duke@1 2337 return p;
duke@1 2338 }
duke@1 2339
duke@1 2340 /** Make a package, given its unqualified name and enclosing package.
duke@1 2341 */
duke@1 2342 public PackageSymbol enterPackage(Name name, PackageSymbol owner) {
duke@1 2343 return enterPackage(TypeSymbol.formFullName(name, owner));
duke@1 2344 }
duke@1 2345
duke@1 2346 /** Include class corresponding to given class file in package,
duke@1 2347 * unless (1) we already have one the same kind (.class or .java), or
duke@1 2348 * (2) we have one of the other kind, and the given class file
duke@1 2349 * is older.
duke@1 2350 */
duke@1 2351 protected void includeClassFile(PackageSymbol p, JavaFileObject file) {
duke@1 2352 if ((p.flags_field & EXISTS) == 0)
duke@1 2353 for (Symbol q = p; q != null && q.kind == PCK; q = q.owner)
duke@1 2354 q.flags_field |= EXISTS;
duke@1 2355 JavaFileObject.Kind kind = file.getKind();
duke@1 2356 int seen;
duke@1 2357 if (kind == JavaFileObject.Kind.CLASS)
duke@1 2358 seen = CLASS_SEEN;
duke@1 2359 else
duke@1 2360 seen = SOURCE_SEEN;
duke@1 2361 String binaryName = fileManager.inferBinaryName(currentLoc, file);
duke@1 2362 int lastDot = binaryName.lastIndexOf(".");
duke@1 2363 Name classname = names.fromString(binaryName.substring(lastDot + 1));
duke@1 2364 boolean isPkgInfo = classname == names.package_info;
duke@1 2365 ClassSymbol c = isPkgInfo
duke@1 2366 ? p.package_info
duke@1 2367 : (ClassSymbol) p.members_field.lookup(classname).sym;
duke@1 2368 if (c == null) {
duke@1 2369 c = enterClass(classname, p);
duke@1 2370 if (c.classfile == null) // only update the file if's it's newly created
duke@1 2371 c.classfile = file;
duke@1 2372 if (isPkgInfo) {
duke@1 2373 p.package_info = c;
duke@1 2374 } else {
duke@1 2375 if (c.owner == p) // it might be an inner class
duke@1 2376 p.members_field.enter(c);
duke@1 2377 }
duke@1 2378 } else if (c.classfile != null && (c.flags_field & seen) == 0) {
duke@1 2379 // if c.classfile == null, we are currently compiling this class
duke@1 2380 // and no further action is necessary.
duke@1 2381 // if (c.flags_field & seen) != 0, we have already encountered
duke@1 2382 // a file of the same kind; again no further action is necessary.
duke@1 2383 if ((c.flags_field & (CLASS_SEEN | SOURCE_SEEN)) != 0)
duke@1 2384 c.classfile = preferredFileObject(file, c.classfile);
duke@1 2385 }
duke@1 2386 c.flags_field |= seen;
duke@1 2387 }
duke@1 2388
duke@1 2389 /** Implement policy to choose to derive information from a source
duke@1 2390 * file or a class file when both are present. May be overridden
duke@1 2391 * by subclasses.
duke@1 2392 */
duke@1 2393 protected JavaFileObject preferredFileObject(JavaFileObject a,
duke@1 2394 JavaFileObject b) {
duke@1 2395
duke@1 2396 if (preferSource)
duke@1 2397 return (a.getKind() == JavaFileObject.Kind.SOURCE) ? a : b;
duke@1 2398 else {
duke@1 2399 long adate = a.getLastModified();
duke@1 2400 long bdate = b.getLastModified();
duke@1 2401 // 6449326: policy for bad lastModifiedTime in ClassReader
duke@1 2402 //assert adate >= 0 && bdate >= 0;
duke@1 2403 return (adate > bdate) ? a : b;
duke@1 2404 }
duke@1 2405 }
duke@1 2406
duke@1 2407 /**
duke@1 2408 * specifies types of files to be read when filling in a package symbol
duke@1 2409 */
duke@1 2410 protected EnumSet<JavaFileObject.Kind> getPackageFileKinds() {
duke@1 2411 return EnumSet.of(JavaFileObject.Kind.CLASS, JavaFileObject.Kind.SOURCE);
duke@1 2412 }
duke@1 2413
duke@1 2414 /**
duke@1 2415 * this is used to support javadoc
duke@1 2416 */
duke@1 2417 protected void extraFileActions(PackageSymbol pack, JavaFileObject fe) {
duke@1 2418 }
duke@1 2419
duke@1 2420 protected Location currentLoc; // FIXME
duke@1 2421
duke@1 2422 private boolean verbosePath = true;
duke@1 2423
duke@1 2424 /** Load directory of package into members scope.
duke@1 2425 */
duke@1 2426 private void fillIn(PackageSymbol p) throws IOException {
duke@1 2427 if (p.members_field == null) p.members_field = new Scope(p);
duke@1 2428 String packageName = p.fullname.toString();
duke@1 2429
duke@1 2430 Set<JavaFileObject.Kind> kinds = getPackageFileKinds();
duke@1 2431
duke@1 2432 fillIn(p, PLATFORM_CLASS_PATH,
duke@1 2433 fileManager.list(PLATFORM_CLASS_PATH,
duke@1 2434 packageName,
duke@1 2435 EnumSet.of(JavaFileObject.Kind.CLASS),
duke@1 2436 false));
duke@1 2437
duke@1 2438 Set<JavaFileObject.Kind> classKinds = EnumSet.copyOf(kinds);
duke@1 2439 classKinds.remove(JavaFileObject.Kind.SOURCE);
duke@1 2440 boolean wantClassFiles = !classKinds.isEmpty();
duke@1 2441
duke@1 2442 Set<JavaFileObject.Kind> sourceKinds = EnumSet.copyOf(kinds);
duke@1 2443 sourceKinds.remove(JavaFileObject.Kind.CLASS);
duke@1 2444 boolean wantSourceFiles = !sourceKinds.isEmpty();
duke@1 2445
duke@1 2446 boolean haveSourcePath = fileManager.hasLocation(SOURCE_PATH);
duke@1 2447
duke@1 2448 if (verbose && verbosePath) {
duke@1 2449 if (fileManager instanceof StandardJavaFileManager) {
duke@1 2450 StandardJavaFileManager fm = (StandardJavaFileManager)fileManager;
duke@1 2451 if (haveSourcePath && wantSourceFiles) {
duke@1 2452 List<File> path = List.nil();
duke@1 2453 for (File file : fm.getLocation(SOURCE_PATH)) {
duke@1 2454 path = path.prepend(file);
duke@1 2455 }
duke@1 2456 printVerbose("sourcepath", path.reverse().toString());
duke@1 2457 } else if (wantSourceFiles) {
duke@1 2458 List<File> path = List.nil();
duke@1 2459 for (File file : fm.getLocation(CLASS_PATH)) {
duke@1 2460 path = path.prepend(file);
duke@1 2461 }
duke@1 2462 printVerbose("sourcepath", path.reverse().toString());
duke@1 2463 }
duke@1 2464 if (wantClassFiles) {
duke@1 2465 List<File> path = List.nil();
duke@1 2466 for (File file : fm.getLocation(PLATFORM_CLASS_PATH)) {
duke@1 2467 path = path.prepend(file);
duke@1 2468 }
duke@1 2469 for (File file : fm.getLocation(CLASS_PATH)) {
duke@1 2470 path = path.prepend(file);
duke@1 2471 }
duke@1 2472 printVerbose("classpath", path.reverse().toString());
duke@1 2473 }
duke@1 2474 }
duke@1 2475 }
duke@1 2476
duke@1 2477 if (wantSourceFiles && !haveSourcePath) {
duke@1 2478 fillIn(p, CLASS_PATH,
duke@1 2479 fileManager.list(CLASS_PATH,
duke@1 2480 packageName,
duke@1 2481 kinds,
duke@1 2482 false));
duke@1 2483 } else {
duke@1 2484 if (wantClassFiles)
duke@1 2485 fillIn(p, CLASS_PATH,
duke@1 2486 fileManager.list(CLASS_PATH,
duke@1 2487 packageName,
duke@1 2488 classKinds,
duke@1 2489 false));
duke@1 2490 if (wantSourceFiles)
duke@1 2491 fillIn(p, SOURCE_PATH,
duke@1 2492 fileManager.list(SOURCE_PATH,
duke@1 2493 packageName,
duke@1 2494 sourceKinds,
duke@1 2495 false));
duke@1 2496 }
duke@1 2497 verbosePath = false;
duke@1 2498 }
duke@1 2499 // where
duke@1 2500 private void fillIn(PackageSymbol p,
duke@1 2501 Location location,
duke@1 2502 Iterable<JavaFileObject> files)
duke@1 2503 {
duke@1 2504 currentLoc = location;
duke@1 2505 for (JavaFileObject fo : files) {
duke@1 2506 switch (fo.getKind()) {
duke@1 2507 case CLASS:
duke@1 2508 case SOURCE: {
duke@1 2509 // TODO pass binaryName to includeClassFile
duke@1 2510 String binaryName = fileManager.inferBinaryName(currentLoc, fo);
duke@1 2511 String simpleName = binaryName.substring(binaryName.lastIndexOf(".") + 1);
duke@1 2512 if (SourceVersion.isIdentifier(simpleName) ||
jrose@267 2513 fo.getKind() == JavaFileObject.Kind.CLASS ||
duke@1 2514 simpleName.equals("package-info"))
duke@1 2515 includeClassFile(p, fo);
duke@1 2516 break;
duke@1 2517 }
duke@1 2518 default:
duke@1 2519 extraFileActions(p, fo);
duke@1 2520 }
duke@1 2521 }
duke@1 2522 }
duke@1 2523
duke@1 2524 /** Output for "-verbose" option.
duke@1 2525 * @param key The key to look up the correct internationalized string.
duke@1 2526 * @param arg An argument for substitution into the output string.
duke@1 2527 */
duke@1 2528 private void printVerbose(String key, CharSequence arg) {
duke@1 2529 Log.printLines(log.noticeWriter, Log.getLocalizedString("verbose." + key, arg));
duke@1 2530 }
duke@1 2531
duke@1 2532 /** Output for "-checkclassfile" option.
duke@1 2533 * @param key The key to look up the correct internationalized string.
duke@1 2534 * @param arg An argument for substitution into the output string.
duke@1 2535 */
duke@1 2536 private void printCCF(String key, Object arg) {
duke@1 2537 Log.printLines(log.noticeWriter, Log.getLocalizedString(key, arg));
duke@1 2538 }
duke@1 2539
duke@1 2540
duke@1 2541 public interface SourceCompleter {
duke@1 2542 void complete(ClassSymbol sym)
duke@1 2543 throws CompletionFailure;
duke@1 2544 }
duke@1 2545
duke@1 2546 /**
duke@1 2547 * A subclass of JavaFileObject for the sourcefile attribute found in a classfile.
duke@1 2548 * The attribute is only the last component of the original filename, so is unlikely
duke@1 2549 * to be valid as is, so operations other than those to access the name throw
duke@1 2550 * UnsupportedOperationException
duke@1 2551 */
duke@1 2552 private static class SourceFileObject extends BaseFileObject {
duke@1 2553
duke@1 2554 /** The file's name.
duke@1 2555 */
duke@1 2556 private Name name;
jjg@57 2557 private Name flatname;
duke@1 2558
jjg@57 2559 public SourceFileObject(Name name, Name flatname) {
jjg@57 2560 super(null); // no file manager; never referenced for this file object
duke@1 2561 this.name = name;
jjg@57 2562 this.flatname = flatname;
duke@1 2563 }
duke@1 2564
duke@1 2565 public InputStream openInputStream() {
duke@1 2566 throw new UnsupportedOperationException();
duke@1 2567 }
duke@1 2568
duke@1 2569 public OutputStream openOutputStream() {
duke@1 2570 throw new UnsupportedOperationException();
duke@1 2571 }
duke@1 2572
duke@1 2573 public Reader openReader() {
duke@1 2574 throw new UnsupportedOperationException();
duke@1 2575 }
duke@1 2576
duke@1 2577 public Writer openWriter() {
duke@1 2578 throw new UnsupportedOperationException();
duke@1 2579 }
duke@1 2580
duke@1 2581 /** @deprecated see bug 6410637 */
duke@1 2582 @Deprecated
duke@1 2583 public String getName() {
duke@1 2584 return name.toString();
duke@1 2585 }
duke@1 2586
duke@1 2587 public long getLastModified() {
duke@1 2588 throw new UnsupportedOperationException();
duke@1 2589 }
duke@1 2590
duke@1 2591 public boolean delete() {
duke@1 2592 throw new UnsupportedOperationException();
duke@1 2593 }
duke@1 2594
duke@1 2595 public CharBuffer getCharContent(boolean ignoreEncodingErrors) {
duke@1 2596 throw new UnsupportedOperationException();
duke@1 2597 }
duke@1 2598
duke@1 2599 @Override
duke@1 2600 public boolean equals(Object other) {
duke@1 2601 if (!(other instanceof SourceFileObject))
duke@1 2602 return false;
duke@1 2603 SourceFileObject o = (SourceFileObject) other;
duke@1 2604 return name.equals(o.name);
duke@1 2605 }
duke@1 2606
duke@1 2607 @Override
duke@1 2608 public int hashCode() {
duke@1 2609 return name.hashCode();
duke@1 2610 }
duke@1 2611
duke@1 2612 public boolean isNameCompatible(String simpleName, JavaFileObject.Kind kind) {
duke@1 2613 return true; // fail-safe mode
duke@1 2614 }
duke@1 2615
duke@1 2616 public URI toUri() {
duke@1 2617 return URI.create(name.toString());
duke@1 2618 }
duke@1 2619
jjg@256 2620 @Override
duke@1 2621 public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
duke@1 2622 throw new UnsupportedOperationException();
duke@1 2623 }
duke@1 2624
jjg@57 2625 @Override
jjg@57 2626 protected String inferBinaryName(Iterable<? extends File> path) {
jjg@57 2627 return flatname.toString();
jjg@57 2628 }
duke@1 2629 }
duke@1 2630 }

mercurial