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