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