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

Thu, 09 Dec 2010 18:33:32 -0800

author
jjg
date
Thu, 09 Dec 2010 18:33:32 -0800
changeset 784
4dd1c0176d81
parent 776
3c32c90031fd
child 795
7b99f98b3035
permissions
-rw-r--r--

7005856: avoid name clash for langtools when building on MacOS
Reviewed-by: mcimadamore

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

mercurial