< prev index next >

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

Print this page




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.jvm;
  27 
  28 import java.io.*;
  29 import java.net.URI;
  30 import java.net.URISyntaxException;
  31 import java.nio.CharBuffer;
  32 import java.nio.file.ClosedFileSystemException;
  33 import java.util.Arrays;
  34 import java.util.EnumSet;
  35 import java.util.HashMap;
  36 import java.util.HashSet;
  37 import java.util.Map;
  38 import java.util.Set;
  39 import java.util.function.IntFunction;
  40 
  41 import javax.lang.model.element.Modifier;
  42 import javax.lang.model.element.NestingKind;
  43 import javax.tools.JavaFileManager;
  44 import javax.tools.JavaFileObject;
  45 
  46 import com.sun.tools.javac.code.Source.Feature;
  47 import com.sun.tools.javac.comp.Annotate;
  48 import com.sun.tools.javac.comp.Annotate.AnnotationTypeCompleter;
  49 import com.sun.tools.javac.code.*;
  50 import com.sun.tools.javac.code.Directive.*;
  51 import com.sun.tools.javac.code.Lint.LintCategory;
  52 import com.sun.tools.javac.code.Scope.WriteableScope;
  53 import com.sun.tools.javac.code.Symbol.*;
  54 import com.sun.tools.javac.code.Symtab;
  55 import com.sun.tools.javac.code.Type.*;
  56 import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;
  57 import com.sun.tools.javac.file.BaseFileManager;
  58 import com.sun.tools.javac.file.PathFileObject;

  59 import com.sun.tools.javac.jvm.ClassFile.Version;
  60 import com.sun.tools.javac.jvm.PoolConstant.NameAndType;
  61 import com.sun.tools.javac.main.Option;
  62 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  63 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
  64 import com.sun.tools.javac.util.*;
  65 import com.sun.tools.javac.util.DefinedBy.Api;
  66 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  67 
  68 import static com.sun.tools.javac.code.Flags.*;
  69 import static com.sun.tools.javac.code.Kinds.Kind.*;
  70 
  71 import com.sun.tools.javac.code.Scope.LookupKind;
  72 
  73 import static com.sun.tools.javac.code.TypeTag.ARRAY;
  74 import static com.sun.tools.javac.code.TypeTag.CLASS;
  75 import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
  76 import static com.sun.tools.javac.jvm.ClassFile.*;
  77 import static com.sun.tools.javac.jvm.ClassFile.Version.*;
  78 
  79 import static com.sun.tools.javac.main.Option.PARAMETERS;
  80 


  84  *  for all other definitions in the classfile. Top-level Classes themselves
  85  *  appear as members of the scopes of PackageSymbols.
  86  *
  87  *  <p><b>This is NOT part of any supported API.
  88  *  If you write code that depends on this, you do so at your own risk.
  89  *  This code and its internal interfaces are subject to change or
  90  *  deletion without notice.</b>
  91  */
  92 public class ClassReader {
  93     /** The context key for the class reader. */
  94     protected static final Context.Key<ClassReader> classReaderKey = new Context.Key<>();
  95 
  96     public static final int INITIAL_BUFFER_SIZE = 0x0fff0;
  97 
  98     private final Annotate annotate;
  99 
 100     /** Switch: verbose output.
 101      */
 102     boolean verbose;
 103 





 104     /** Switch: allow modules.
 105      */
 106     boolean allowModules;
 107 
 108    /** Lint option: warn about classfile issues
 109      */
 110     boolean lintClassfile;
 111 
 112     /** Switch: preserve parameter names from the variable table.
 113      */
 114     public boolean saveParameterNames;
 115 
 116     /**
 117      * The currently selected profile.
 118      */
 119     public final Profile profile;
 120 
 121     /** The log to use for verbose output
 122      */
 123     final Log log;


 149      */
 150     protected WriteableScope typevars;
 151 
 152     private List<InterimUsesDirective> interimUses = List.nil();
 153     private List<InterimProvidesDirective> interimProvides = List.nil();
 154 
 155     /** The path name of the class file currently being read.
 156      */
 157     protected JavaFileObject currentClassFile = null;
 158 
 159     /** The class or method currently being read.
 160      */
 161     protected Symbol currentOwner = null;
 162 
 163     /** The module containing the class currently being read.
 164      */
 165     protected ModuleSymbol currentModule = null;
 166 
 167     /** The buffer containing the currently read class file.
 168      */
 169     ByteBuffer buf = new ByteBuffer(INITIAL_BUFFER_SIZE);
 170 
 171     /** The current input pointer.
 172      */
 173     protected int bp;
 174 
 175     /** The pool reader.





 176      */
 177     PoolReader poolReader;
 178 
 179     /** The major version number of the class file being read. */
 180     int majorVersion;
 181     /** The minor version number of the class file being read. */
 182     int minorVersion;
 183 
 184     /** A table to hold the constant pool indices for method parameter
 185      * names, as given in LocalVariableTable attributes.
 186      */
 187     int[] parameterNameIndices;
 188 
 189     /**
 190      * A table to hold annotations for method parameters.
 191      */
 192     ParameterAnnotations[] parameterAnnotations;
 193 
 194     /**
 195      * A holder for parameter annotations.
 196      */
 197     static class ParameterAnnotations {


 297             diagFactory,
 298             dcfh);
 299     }
 300 
 301     public ClassFinder.BadEnclosingMethodAttr badEnclosingMethod(Symbol sym) {
 302         return new ClassFinder.BadEnclosingMethodAttr (
 303             currentOwner.enclClass(),
 304             currentClassFile,
 305             diagFactory.fragment(Fragments.BadEnclosingMethod(sym)),
 306             diagFactory,
 307             dcfh);
 308     }
 309 
 310 /************************************************************************
 311  * Buffer Access
 312  ***********************************************************************/
 313 
 314     /** Read a character.
 315      */
 316     char nextChar() {
 317         char res = buf.getChar(bp);
 318         bp += 2;
 319         return res;
 320     }
 321 
 322     /** Read a byte.
 323      */
 324     int nextByte() {
 325         return buf.getByte(bp++) & 0xFF;
 326     }
 327 
 328     /** Read an integer.
 329      */
 330     int nextInt() {
 331         int res = buf.getInt(bp);
 332         bp += 4;
 333         return res;
























































 334     }
 335 
 336 /************************************************************************
 337  * Constant Pool Access
 338  ***********************************************************************/
 339 



















































































































































































































 340     /** Read module_flags.
 341      */
 342     Set<ModuleFlags> readModuleFlags(int flags) {
 343         Set<ModuleFlags> set = EnumSet.noneOf(ModuleFlags.class);
 344         for (ModuleFlags f : ModuleFlags.values()) {
 345             if ((flags & f.value) != 0)
 346                 set.add(f);
 347         }
 348         return set;
 349     }
 350 
 351     /** Read resolution_flags.
 352      */
 353     Set<ModuleResolutionFlags> readModuleResolutionFlags(int flags) {
 354         Set<ModuleResolutionFlags> set = EnumSet.noneOf(ModuleResolutionFlags.class);
 355         for (ModuleResolutionFlags f : ModuleResolutionFlags.values()) {
 356             if ((flags & f.value) != 0)
 357                 set.add(f);
 358         }
 359         return set;


 471                 */
 472                 return t;
 473             }
 474         case 'S':
 475             sigp++;
 476             return syms.shortType;
 477         case 'V':
 478             sigp++;
 479             return syms.voidType;
 480         case 'Z':
 481             sigp++;
 482             return syms.booleanType;
 483         case '[':
 484             sigp++;
 485             return new ArrayType(sigToType(), syms.arrayClass);
 486         case '(':
 487             sigp++;
 488             List<Type> argtypes = sigToTypes(')');
 489             Type restype = sigToType();
 490             List<Type> thrown = List.nil();
 491             while (sigp < siglimit && signature[sigp] == '^') {
 492                 sigp++;
 493                 thrown = thrown.prepend(sigToType());
 494             }
 495             // if there is a typevar in the throws clause we should state it.
 496             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail) {
 497                 if (l.head.hasTag(TYPEVAR)) {
 498                     l.head.tsym.flags_field |= THROWS;
 499                 }
 500             }
 501             return new MethodType(argtypes,
 502                                   restype,
 503                                   thrown.reverse(),
 504                                   syms.methodClass);
 505         case '<':
 506             typevars = typevars.dup(currentOwner);
 507             Type poly = new ForAll(sigToTypeParams(), sigToType());
 508             typevars = typevars.leave();
 509             return poly;
 510         default:
 511             throw badClassFile("bad.signature",


 564                                         // no "rare" types
 565                                         super.setEnclosingType(types.erasure(enclosingType));
 566                                     } else {
 567                                         super.setEnclosingType(types.subst(enclosingType,
 568                                                                            typeParams,
 569                                                                            typeArgs));
 570                                     }
 571                                 } else {
 572                                     super.setEnclosingType(Type.noType);
 573                                 }
 574                             }
 575                             return super.getEnclosingType();
 576                         }
 577                         @Override
 578                         public void setEnclosingType(Type outer) {
 579                             throw new UnsupportedOperationException();
 580                         }
 581                     };
 582                 switch (signature[sigp++]) {
 583                 case ';':
 584                     if (sigp < siglimit && signature[sigp] == '.') {
 585                         // support old-style GJC signatures
 586                         // The signature produced was
 587                         // Lfoo/Outer<Lfoo/X;>;.Lfoo/Outer$Inner<Lfoo/Y;>;
 588                         // rather than say
 589                         // Lfoo/Outer<Lfoo/X;>.Inner<Lfoo/Y;>;
 590                         // so we skip past ".Lfoo/Outer$"
 591                         sigp += (sbp - startSbp) + // "foo/Outer"
 592                             3;  // ".L" and "$"
 593                         signatureBuffer[sbp++] = (byte)'$';
 594                         break;
 595                     } else {
 596                         sbp = startSbp;
 597                         return outer;
 598                     }
 599                 case '.':
 600                     signatureBuffer[sbp++] = (byte)'$';
 601                     break;
 602                 default:
 603                     throw new AssertionError(signature[sigp-1]);
 604                 }


 758         protected final Name name;
 759         protected final ClassFile.Version version;
 760         protected final Set<AttributeKind> kinds;
 761     }
 762 
 763     protected Set<AttributeKind> CLASS_ATTRIBUTE =
 764             EnumSet.of(AttributeKind.CLASS);
 765     protected Set<AttributeKind> MEMBER_ATTRIBUTE =
 766             EnumSet.of(AttributeKind.MEMBER);
 767     protected Set<AttributeKind> CLASS_OR_MEMBER_ATTRIBUTE =
 768             EnumSet.of(AttributeKind.CLASS, AttributeKind.MEMBER);
 769 
 770     protected Map<Name, AttributeReader> attributeReaders = new HashMap<>();
 771 
 772     private void initAttributeReaders() {
 773         AttributeReader[] readers = {
 774             // v45.3 attributes
 775 
 776             new AttributeReader(names.Code, V45_3, MEMBER_ATTRIBUTE) {
 777                 protected void read(Symbol sym, int attrLen) {
 778                     if (saveParameterNames)
 779                         ((MethodSymbol)sym).code = readCode(sym);
 780                     else
 781                         bp = bp + attrLen;
 782                 }
 783             },
 784 
 785             new AttributeReader(names.ConstantValue, V45_3, MEMBER_ATTRIBUTE) {
 786                 protected void read(Symbol sym, int attrLen) {
 787                     Object v = poolReader.getConstant(nextChar());
 788                     // Ignore ConstantValue attribute if field not final.
 789                     if ((sym.flags() & FINAL) == 0) {
 790                         return;
 791                     }
 792                     VarSymbol var = (VarSymbol) sym;
 793                     switch (var.type.getTag()) {
 794                        case BOOLEAN:
 795                        case BYTE:
 796                        case CHAR:
 797                        case SHORT:
 798                        case INT:
 799                            checkType(var, Integer.class, v);
 800                            break;
 801                        case LONG:
 802                            checkType(var, Long.class, v);
 803                            break;
 804                        case FLOAT:
 805                            checkType(var, Float.class, v);
 806                            break;
 807                        case DOUBLE:


 824                 void checkType(Symbol var, Class<?> clazz, Object value) {
 825                     if (!clazz.isInstance(value)) {
 826                         throw badClassFile("bad.constant.value", value, var, clazz.getSimpleName());
 827                     }
 828                 }
 829             },
 830 
 831             new AttributeReader(names.Deprecated, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
 832                 protected void read(Symbol sym, int attrLen) {
 833                     Symbol s = sym.owner.kind == MDL ? sym.owner : sym;
 834 
 835                     s.flags_field |= DEPRECATED;
 836                 }
 837             },
 838 
 839             new AttributeReader(names.Exceptions, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
 840                 protected void read(Symbol sym, int attrLen) {
 841                     int nexceptions = nextChar();
 842                     List<Type> thrown = List.nil();
 843                     for (int j = 0; j < nexceptions; j++)
 844                         thrown = thrown.prepend(poolReader.getClass(nextChar()).type);
 845                     if (sym.type.getThrownTypes().isEmpty())
 846                         sym.type.asMethodType().thrown = thrown.reverse();
 847                 }
 848             },
 849 
 850             new AttributeReader(names.InnerClasses, V45_3, CLASS_ATTRIBUTE) {
 851                 protected void read(Symbol sym, int attrLen) {
 852                     ClassSymbol c = (ClassSymbol) sym;
 853                     if (currentModule.module_info == c) {
 854                         //prevent entering the classes too soon:
 855                         skipInnerClasses();
 856                     } else {
 857                         readInnerClasses(c);
 858                     }
 859                 }
 860             },
 861 
 862             new AttributeReader(names.LocalVariableTable, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
 863                 protected void read(Symbol sym, int attrLen) {
 864                     int newbp = bp + attrLen;


 882                             if (start_pc == 0) {
 883                                 // ensure array large enough
 884                                 if (register >= parameterNameIndices.length) {
 885                                     int newSize =
 886                                             Math.max(register + 1, parameterNameIndices.length + 8);
 887                                     parameterNameIndices =
 888                                             Arrays.copyOf(parameterNameIndices, newSize);
 889                                 }
 890                                 parameterNameIndices[register] = nameIndex;
 891                                 haveParameterNameIndices = true;
 892                             }
 893                         }
 894                     }
 895                     bp = newbp;
 896                 }
 897             },
 898 
 899             new AttributeReader(names.SourceFile, V45_3, CLASS_ATTRIBUTE) {
 900                 protected void read(Symbol sym, int attrLen) {
 901                     ClassSymbol c = (ClassSymbol) sym;
 902                     Name n = poolReader.getName(nextChar());
 903                     c.sourcefile = new SourceFileObject(n);
 904                     // If the class is a toplevel class, originating from a Java source file,
 905                     // but the class name does not match the file name, then it is
 906                     // an auxiliary class.
 907                     String sn = n.toString();
 908                     if (c.owner.kind == PCK &&
 909                         sn.endsWith(".java") &&
 910                         !sn.equals(c.name.toString()+".java")) {
 911                         c.flags_field |= AUXILIARY;
 912                     }
 913                 }
 914             },
 915 
 916             new AttributeReader(names.Synthetic, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
 917                 protected void read(Symbol sym, int attrLen) {
 918                     sym.flags_field |= SYNTHETIC;
 919                 }
 920             },
 921 
 922             // standard v49 attributes
 923 
 924             new AttributeReader(names.EnclosingMethod, V49, CLASS_ATTRIBUTE) {
 925                 protected void read(Symbol sym, int attrLen) {
 926                     int newbp = bp + attrLen;
 927                     readEnclosingMethodAttr(sym);
 928                     bp = newbp;
 929                 }
 930             },
 931 
 932             new AttributeReader(names.Signature, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
 933                 protected void read(Symbol sym, int attrLen) {
 934                     if (sym.kind == TYP) {
 935                         ClassSymbol c = (ClassSymbol) sym;
 936                         readingClassAttr = true;
 937                         try {
 938                             ClassType ct1 = (ClassType)c.type;
 939                             Assert.check(c == currentOwner);
 940                             ct1.typarams_field = poolReader.getName(nextChar())
 941                                     .map(ClassReader.this::sigToTypeParams);
 942                             ct1.supertype_field = sigToType();
 943                             ListBuffer<Type> is = new ListBuffer<>();
 944                             while (sigp != siglimit) is.append(sigToType());
 945                             ct1.interfaces_field = is.toList();
 946                         } finally {
 947                             readingClassAttr = false;
 948                         }
 949                     } else {
 950                         List<Type> thrown = sym.type.getThrownTypes();
 951                         sym.type = poolReader.getType(nextChar());
 952                         //- System.err.println(" # " + sym.type);
 953                         if (sym.kind == MTH && sym.type.getThrownTypes().isEmpty())
 954                             sym.type.asMethodType().thrown = thrown;
 955 
 956                     }
 957                 }
 958             },
 959 
 960             // v49 annotation attributes
 961 
 962             new AttributeReader(names.AnnotationDefault, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
 963                 protected void read(Symbol sym, int attrLen) {
 964                     attachAnnotationDefault(sym);
 965                 }
 966             },
 967 
 968             new AttributeReader(names.RuntimeInvisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
 969                 protected void read(Symbol sym, int attrLen) {
 970                     attachAnnotations(sym);
 971                 }


1052                             }
1053                             parameterNameIndices[index++] = nameIndex;
1054                         }
1055                     }
1056                     bp = newbp;
1057                 }
1058             },
1059 
1060             // standard v53 attributes
1061 
1062             new AttributeReader(names.Module, V53, CLASS_ATTRIBUTE) {
1063                 @Override
1064                 protected boolean accepts(AttributeKind kind) {
1065                     return super.accepts(kind) && allowModules;
1066                 }
1067                 protected void read(Symbol sym, int attrLen) {
1068                     if (sym.kind == TYP && sym.owner.kind == MDL) {
1069                         ModuleSymbol msym = (ModuleSymbol) sym.owner;
1070                         ListBuffer<Directive> directives = new ListBuffer<>();
1071 
1072                         Name moduleName = poolReader.peekModuleName(nextChar(), names::fromUtf);
1073                         if (currentModule.name != moduleName) {
1074                             throw badClassFile("module.name.mismatch", moduleName, currentModule.name);
1075                         }
1076 
1077                         Set<ModuleFlags> moduleFlags = readModuleFlags(nextChar());
1078                         msym.flags.addAll(moduleFlags);
1079                         msym.version = optPoolEntry(nextChar(), poolReader::getName, null);
1080 
1081                         ListBuffer<RequiresDirective> requires = new ListBuffer<>();
1082                         int nrequires = nextChar();
1083                         for (int i = 0; i < nrequires; i++) {
1084                             ModuleSymbol rsym = poolReader.getModule(nextChar());
1085                             Set<RequiresFlag> flags = readRequiresFlags(nextChar());
1086                             if (rsym == syms.java_base && majorVersion >= V54.major) {
1087                                 if (flags.contains(RequiresFlag.TRANSITIVE)) {
1088                                     throw badClassFile("bad.requires.flag", RequiresFlag.TRANSITIVE);
1089                                 }
1090                                 if (flags.contains(RequiresFlag.STATIC_PHASE)) {
1091                                     throw badClassFile("bad.requires.flag", RequiresFlag.STATIC_PHASE);
1092                                 }
1093                             }
1094                             nextChar(); // skip compiled version
1095                             requires.add(new RequiresDirective(rsym, flags));
1096                         }
1097                         msym.requires = requires.toList();
1098                         directives.addAll(msym.requires);
1099 
1100                         ListBuffer<ExportsDirective> exports = new ListBuffer<>();
1101                         int nexports = nextChar();
1102                         for (int i = 0; i < nexports; i++) {
1103                             PackageSymbol p = poolReader.getPackage(nextChar());

1104                             Set<ExportsFlag> flags = readExportsFlags(nextChar());
1105                             int nto = nextChar();
1106                             List<ModuleSymbol> to;
1107                             if (nto == 0) {
1108                                 to = null;
1109                             } else {
1110                                 ListBuffer<ModuleSymbol> lb = new ListBuffer<>();
1111                                 for (int t = 0; t < nto; t++)
1112                                     lb.append(poolReader.getModule(nextChar()));
1113                                 to = lb.toList();
1114                             }
1115                             exports.add(new ExportsDirective(p, to, flags));
1116                         }
1117                         msym.exports = exports.toList();
1118                         directives.addAll(msym.exports);
1119                         ListBuffer<OpensDirective> opens = new ListBuffer<>();
1120                         int nopens = nextChar();
1121                         if (nopens != 0 && msym.flags.contains(ModuleFlags.OPEN)) {
1122                             throw badClassFile("module.non.zero.opens", currentModule.name);
1123                         }
1124                         for (int i = 0; i < nopens; i++) {
1125                             PackageSymbol p = poolReader.getPackage(nextChar());

1126                             Set<OpensFlag> flags = readOpensFlags(nextChar());
1127                             int nto = nextChar();
1128                             List<ModuleSymbol> to;
1129                             if (nto == 0) {
1130                                 to = null;
1131                             } else {
1132                                 ListBuffer<ModuleSymbol> lb = new ListBuffer<>();
1133                                 for (int t = 0; t < nto; t++)
1134                                     lb.append(poolReader.getModule(nextChar()));
1135                                 to = lb.toList();
1136                             }
1137                             opens.add(new OpensDirective(p, to, flags));
1138                         }
1139                         msym.opens = opens.toList();
1140                         directives.addAll(msym.opens);
1141 
1142                         msym.directives = directives.toList();
1143 
1144                         ListBuffer<InterimUsesDirective> uses = new ListBuffer<>();
1145                         int nuses = nextChar();
1146                         for (int i = 0; i < nuses; i++) {
1147                             Name srvc = poolReader.peekClassName(nextChar(), this::classNameMapper);
1148                             uses.add(new InterimUsesDirective(srvc));
1149                         }
1150                         interimUses = uses.toList();
1151 
1152                         ListBuffer<InterimProvidesDirective> provides = new ListBuffer<>();
1153                         int nprovides = nextChar();
1154                         for (int p = 0; p < nprovides; p++) {
1155                             Name srvc = poolReader.peekClassName(nextChar(), this::classNameMapper);
1156                             int nimpls = nextChar();
1157                             ListBuffer<Name> impls = new ListBuffer<>();
1158                             for (int i = 0; i < nimpls; i++) {
1159                                 impls.append(poolReader.peekClassName(nextChar(), this::classNameMapper));
1160                             provides.add(new InterimProvidesDirective(srvc, impls.toList()));
1161                             }
1162                         }
1163                         interimProvides = provides.toList();
1164                     }
1165                 }
1166 
1167                 private Name classNameMapper(byte[] arr, int offset, int length) {
1168                     return names.fromUtf(ClassFile.internalize(arr, offset, length));
1169                 }
1170             },
1171 
1172             new AttributeReader(names.ModuleResolution, V53, CLASS_ATTRIBUTE) {
1173                 @Override
1174                 protected boolean accepts(AttributeKind kind) {
1175                     return super.accepts(kind) && allowModules;
1176                 }
1177                 protected void read(Symbol sym, int attrLen) {
1178                     if (sym.kind == TYP && sym.owner.kind == MDL) {
1179                         ModuleSymbol msym = (ModuleSymbol) sym.owner;
1180                         msym.resolutionFlags.addAll(readModuleResolutionFlags(nextChar()));
1181                     }
1182                 }
1183             },
1184         };
1185 
1186         for (AttributeReader r: readers)
1187             attributeReaders.put(r.name, r);
1188     }
1189 
1190     protected void readEnclosingMethodAttr(Symbol sym) {
1191         // sym is a nested class with an "Enclosing Method" attribute
1192         // remove sym from it's current owners scope and place it in
1193         // the scope specified by the attribute
1194         sym.owner.members().remove(sym);
1195         ClassSymbol self = (ClassSymbol)sym;
1196         ClassSymbol c = poolReader.getClass(nextChar());
1197         NameAndType nt = optPoolEntry(nextChar(), poolReader::getNameAndType, null);
1198 
1199         if (c.members_field == null || c.kind != TYP)
1200             throw badClassFile("bad.enclosing.class", self, c);
1201 
1202         MethodSymbol m = findMethod(nt, c.members_field, self.flags());
1203         if (nt != null && m == null)
1204             throw badEnclosingMethod(self);
1205 
1206         self.name = simpleBinaryName(self.flatname, c.flatname) ;
1207         self.owner = m != null ? m : c;
1208         if (self.name.isEmpty())
1209             self.fullname = names.empty;
1210         else
1211             self.fullname = ClassSymbol.formFullName(self.name, self.owner);
1212 
1213         if (m != null) {
1214             ((ClassType)sym.type).setEnclosingType(m.type);
1215         } else if ((self.flags_field & STATIC) == 0) {
1216             ((ClassType)sym.type).setEnclosingType(c.type);
1217         } else {
1218             ((ClassType)sym.type).setEnclosingType(Type.noType);
1219         }
1220         enterTypevars(self, self.type);
1221         if (!missingTypeVariables.isEmpty()) {
1222             ListBuffer<Type> typeVars =  new ListBuffer<>();
1223             for (Type typevar : missingTypeVariables) {
1224                 typeVars.append(findTypeVar(typevar.tsym.name));
1225             }
1226             foundTypeVariables = typeVars.toList();
1227         } else {
1228             foundTypeVariables = List.nil();
1229         }
1230     }
1231 
1232     // See java.lang.Class
1233     private Name simpleBinaryName(Name self, Name enclosing) {
1234         if (!self.startsWith(enclosing)) {
1235             throw badClassFile("bad.enclosing.method", self);
1236         }
1237 
1238         String simpleBinaryName = self.toString().substring(enclosing.toString().length());
1239         if (simpleBinaryName.length() < 1 || simpleBinaryName.charAt(0) != '$')
1240             throw badClassFile("bad.enclosing.method", self);
1241         int index = 1;
1242         while (index < simpleBinaryName.length() &&
1243                isAsciiDigit(simpleBinaryName.charAt(index)))
1244             index++;
1245         return names.fromString(simpleBinaryName.substring(index));
1246     }
1247 
1248     private MethodSymbol findMethod(NameAndType nt, Scope scope, long flags) {
1249         if (nt == null)
1250             return null;
1251 
1252         MethodType type = nt.type.asMethodType();
1253 
1254         for (Symbol sym : scope.getSymbolsByName(nt.name)) {
1255             if (sym.kind == MTH && isSameBinaryType(sym.type.asMethodType(), type))
1256                 return (MethodSymbol)sym;
1257         }
1258 
1259         if (nt.name != names.init)
1260             // not a constructor
1261             return null;
1262         if ((flags & INTERFACE) != 0)
1263             // no enclosing instance
1264             return null;
1265         if (nt.type.getParameterTypes().isEmpty())
1266             // no parameters
1267             return null;
1268 
1269         // A constructor of an inner class.
1270         // Remove the first argument (the enclosing instance)
1271         nt = new NameAndType(nt.name, new MethodType(nt.type.getParameterTypes().tail,
1272                                  nt.type.getReturnType(),
1273                                  nt.type.getThrownTypes(),
1274                                  syms.methodClass));
1275         // Try searching again
1276         return findMethod(nt, scope, flags);
1277     }
1278 
1279     /** Similar to Types.isSameType but avoids completion */
1280     private boolean isSameBinaryType(MethodType mt1, MethodType mt2) {
1281         List<Type> types1 = types.erasure(mt1.getParameterTypes())
1282             .prepend(types.erasure(mt1.getReturnType()));
1283         List<Type> types2 = mt2.getParameterTypes().prepend(mt2.getReturnType());
1284         while (!types1.isEmpty() && !types2.isEmpty()) {
1285             if (types1.head.tsym != types2.head.tsym)
1286                 return false;
1287             types1 = types1.tail;
1288             types2 = types2.tail;
1289         }
1290         return types1.isEmpty() && types2.isEmpty();
1291     }
1292 
1293     /**
1294      * Character.isDigit answers <tt>true</tt> to some non-ascii
1295      * digits.  This one does not.  <b>copied from java.lang.Class</b>
1296      */
1297     private static boolean isAsciiDigit(char c) {
1298         return '0' <= c && c <= '9';
1299     }
1300 
1301     /** Read member attributes.
1302      */
1303     void readMemberAttrs(Symbol sym) {
1304         readAttrs(sym, AttributeKind.MEMBER);
1305     }
1306 
1307     void readAttrs(Symbol sym, AttributeKind kind) {
1308         char ac = nextChar();
1309         for (int i = 0; i < ac; i++) {
1310             Name attrName = poolReader.getName(nextChar());
1311             int attrLen = nextInt();
1312             AttributeReader r = attributeReaders.get(attrName);
1313             if (r != null && r.accepts(kind))
1314                 r.read(sym, attrLen);
1315             else  {
1316                 bp = bp + attrLen;
1317             }
1318         }
1319     }
1320 
1321     private boolean readingClassAttr = false;
1322     private List<Type> missingTypeVariables = List.nil();
1323     private List<Type> foundTypeVariables = List.nil();
1324 
1325     /** Read class attributes.
1326      */
1327     void readClassAttrs(ClassSymbol c) {
1328         readAttrs(c, AttributeKind.CLASS);
1329     }
1330 


1393                 } else if (proxy.type.tsym == syms.deprecatedType.tsym) {
1394                     sym.flags_field |= (DEPRECATED | DEPRECATED_ANNOTATION);
1395                     for (Pair<Name, Attribute> v : proxy.values) {
1396                         if (v.fst == names.forRemoval && v.snd instanceof Attribute.Constant) {
1397                             Attribute.Constant c = (Attribute.Constant)v.snd;
1398                             if (c.type == syms.booleanType && ((Integer)c.value) != 0) {
1399                                 sym.flags_field |= DEPRECATED_REMOVAL;
1400                             }
1401                         }
1402                     }
1403                 }
1404                 proxies.append(proxy);
1405             }
1406         }
1407         annotate.normal(new AnnotationCompleter(sym, proxies.toList()));
1408     }
1409 
1410     /** Read parameter annotations.
1411      */
1412     void readParameterAnnotations(Symbol meth) {
1413         int numParameters = buf.getByte(bp++) & 0xFF;
1414         if (parameterAnnotations == null) {
1415             parameterAnnotations = new ParameterAnnotations[numParameters];
1416         } else if (parameterAnnotations.length != numParameters) {
1417             throw badClassFile("bad.runtime.invisible.param.annotations", meth);
1418         }
1419         for (int pnum = 0; pnum < numParameters; pnum++) {
1420             if (parameterAnnotations[pnum] == null) {
1421                 parameterAnnotations[pnum] = new ParameterAnnotations();
1422             }
1423             parameterAnnotations[pnum].add(readAnnotations());
1424         }
1425     }
1426 
1427     void attachTypeAnnotations(final Symbol sym) {
1428         int numAttributes = nextChar();
1429         if (numAttributes != 0) {
1430             ListBuffer<TypeAnnotationProxy> proxies = new ListBuffer<>();
1431             for (int i = 0; i < numAttributes; i++)
1432                 proxies.append(readTypeAnnotation());
1433             annotate.normal(new TypeAnnotationCompleter(sym, proxies.toList()));


1437     /** Attach the default value for an annotation element.
1438      */
1439     void attachAnnotationDefault(final Symbol sym) {
1440         final MethodSymbol meth = (MethodSymbol)sym; // only on methods
1441         final Attribute value = readAttributeValue();
1442 
1443         // The default value is set later during annotation. It might
1444         // be the case that the Symbol sym is annotated _after_ the
1445         // repeating instances that depend on this default value,
1446         // because of this we set an interim value that tells us this
1447         // element (most likely) has a default.
1448         //
1449         // Set interim value for now, reset just before we do this
1450         // properly at annotate time.
1451         meth.defaultValue = value;
1452         annotate.normal(new AnnotationDefaultCompleter(meth, value));
1453     }
1454 
1455     Type readTypeOrClassSymbol(int i) {
1456         // support preliminary jsr175-format class files
1457         if (poolReader.hasTag(i, CONSTANT_Class))
1458             return poolReader.getClass(i).type;








1459         return readTypeToProxy(i);
1460     }
1461     Type readTypeToProxy(int i) {
1462         if (currentModule.module_info == currentOwner) {
1463             return new ProxyType(i);

1464         } else {
1465             return poolReader.getType(i);
1466         }
1467     }
1468 
1469     CompoundAnnotationProxy readCompoundAnnotation() {
1470         Type t;
1471         if (currentModule.module_info == currentOwner) {
1472             int cpIndex = nextChar();
1473             t = new ProxyType(cpIndex);
1474         } else {
1475             t = readTypeOrClassSymbol(nextChar());
1476         }
1477         int numFields = nextChar();
1478         ListBuffer<Pair<Name,Attribute>> pairs = new ListBuffer<>();
1479         for (int i=0; i<numFields; i++) {
1480             Name name = poolReader.getName(nextChar());
1481             Attribute value = readAttributeValue();
1482             pairs.append(new Pair<>(name, value));
1483         }
1484         return new CompoundAnnotationProxy(t, pairs.toList());
1485     }
1486 
1487     TypeAnnotationProxy readTypeAnnotation() {
1488         TypeAnnotationPosition position = readPosition();
1489         CompoundAnnotationProxy proxy = readCompoundAnnotation();
1490 
1491         return new TypeAnnotationProxy(proxy, position);
1492     }
1493 
1494     TypeAnnotationPosition readPosition() {
1495         int tag = nextByte(); // TargetType tag is a byte
1496 
1497         if (!TargetType.isValidTargetTypeValue(tag))
1498             throw badClassFile("bad.type.annotation.value", String.format("0x%02X", tag));
1499 
1500         TargetType type = TargetType.fromTargetTypeValue(tag);


1673             return TypeAnnotationPosition.methodReturn(readTypePath());
1674         case FIELD:
1675             return TypeAnnotationPosition.field(readTypePath());
1676         case UNKNOWN:
1677             throw new AssertionError("jvm.ClassReader: UNKNOWN target type should never occur!");
1678         default:
1679             throw new AssertionError("jvm.ClassReader: Unknown target type for position: " + type);
1680         }
1681     }
1682 
1683     List<TypeAnnotationPosition.TypePathEntry> readTypePath() {
1684         int len = nextByte();
1685         ListBuffer<Integer> loc = new ListBuffer<>();
1686         for (int i = 0; i < len * TypeAnnotationPosition.TypePathEntry.bytesPerEntry; ++i)
1687             loc = loc.append(nextByte());
1688 
1689         return TypeAnnotationPosition.getTypePathFromBinary(loc.toList());
1690 
1691     }
1692 
1693     /**
1694      * Helper function to read an optional pool entry (with given function); this is used while parsing
1695      * InnerClasses and EnclosingMethod attributes, as well as when parsing supertype descriptor,
1696      * as per JVMS.
1697      */
1698     <Z> Z optPoolEntry(int index, IntFunction<Z> poolFunc, Z defaultValue) {
1699         return (index == 0) ?
1700                 defaultValue :
1701                 poolFunc.apply(index);
1702     }
1703 
1704     Attribute readAttributeValue() {
1705         char c = (char) buf.getByte(bp++);
1706         switch (c) {
1707         case 'B':
1708             return new Attribute.Constant(syms.byteType, poolReader.getConstant(nextChar()));
1709         case 'C':
1710             return new Attribute.Constant(syms.charType, poolReader.getConstant(nextChar()));
1711         case 'D':
1712             return new Attribute.Constant(syms.doubleType, poolReader.getConstant(nextChar()));
1713         case 'F':
1714             return new Attribute.Constant(syms.floatType, poolReader.getConstant(nextChar()));
1715         case 'I':
1716             return new Attribute.Constant(syms.intType, poolReader.getConstant(nextChar()));
1717         case 'J':
1718             return new Attribute.Constant(syms.longType, poolReader.getConstant(nextChar()));
1719         case 'S':
1720             return new Attribute.Constant(syms.shortType, poolReader.getConstant(nextChar()));
1721         case 'Z':
1722             return new Attribute.Constant(syms.booleanType, poolReader.getConstant(nextChar()));
1723         case 's':
1724             return new Attribute.Constant(syms.stringType, poolReader.getName(nextChar()).toString());
1725         case 'e':
1726             return new EnumAttributeProxy(readTypeToProxy(nextChar()), poolReader.getName(nextChar()));
1727         case 'c':
1728             return new ClassAttributeProxy(readTypeOrClassSymbol(nextChar()));
1729         case '[': {
1730             int n = nextChar();
1731             ListBuffer<Attribute> l = new ListBuffer<>();
1732             for (int i=0; i<n; i++)
1733                 l.append(readAttributeValue());
1734             return new ArrayAttributeProxy(l.toList());
1735         }
1736         case '@':
1737             return readCompoundAnnotation();
1738         default:
1739             throw new AssertionError("unknown annotation tag '" + c + "'");
1740         }
1741     }
1742 
1743     interface ProxyVisitor extends Attribute.Visitor {
1744         void visitEnumAttributeProxy(EnumAttributeProxy proxy);
1745         void visitClassAttributeProxy(ClassAttributeProxy proxy);
1746         void visitArrayAttributeProxy(ArrayAttributeProxy proxy);


2115             JavaFileObject previousClassFile = currentClassFile;
2116             try {
2117                 currentClassFile = classFile;
2118                 List<Attribute.TypeCompound> newList = deproxyTypeCompoundList(proxies);
2119                 sym.setTypeAttributes(newList.prependList(sym.getRawTypeAttributes()));
2120             } finally {
2121                 currentClassFile = previousClassFile;
2122             }
2123         }
2124     }
2125 
2126 
2127 /************************************************************************
2128  * Reading Symbols
2129  ***********************************************************************/
2130 
2131     /** Read a field.
2132      */
2133     VarSymbol readField() {
2134         long flags = adjustFieldFlags(nextChar());
2135         Name name = poolReader.getName(nextChar());
2136         Type type = poolReader.getType(nextChar());
2137         VarSymbol v = new VarSymbol(flags, name, type, currentOwner);
2138         readMemberAttrs(v);
2139         return v;
2140     }
2141 
2142     /** Read a method.
2143      */
2144     MethodSymbol readMethod() {
2145         long flags = adjustMethodFlags(nextChar());
2146         Name name = poolReader.getName(nextChar());
2147         Type type = poolReader.getType(nextChar());
2148         if (currentOwner.isInterface() &&
2149                 (flags & ABSTRACT) == 0 && !name.equals(names.clinit)) {
2150             if (majorVersion > Version.V52.major ||
2151                     (majorVersion == Version.V52.major && minorVersion >= Version.V52.minor)) {
2152                 if ((flags & (STATIC | PRIVATE)) == 0) {
2153                     currentOwner.flags_field |= DEFAULT;
2154                     flags |= DEFAULT | ABSTRACT;
2155                 }
2156             } else {
2157                 //protect against ill-formed classfiles
2158                 throw badClassFile((flags & STATIC) == 0 ? "invalid.default.interface" : "invalid.static.interface",
2159                                    Integer.toString(majorVersion),
2160                                    Integer.toString(minorVersion));
2161             }
2162         }
2163         if (name == names.init && currentOwner.hasOuterInstance()) {
2164             // Sometimes anonymous classes don't have an outer
2165             // instance, however, there is no reliable way to tell so
2166             // we never strip this$n
2167             // ditto for local classes. Local classes that have an enclosing method set


2271                 // we never strip this$n
2272                 if (!currentOwner.name.isEmpty())
2273                     firstParam += 1;
2274             }
2275 
2276             if (sym.type != jvmType) {
2277                 // reading the method attributes has caused the
2278                 // symbol's type to be changed. (i.e. the Signature
2279                 // attribute.)  This may happen if there are hidden
2280                 // (synthetic) parameters in the descriptor, but not
2281                 // in the Signature.  The position of these hidden
2282                 // parameters is unspecified; for now, assume they are
2283                 // at the beginning, and so skip over them. The
2284                 // primary case for this is two hidden parameters
2285                 // passed into Enum constructors.
2286                 int skip = Code.width(jvmType.getParameterTypes())
2287                         - Code.width(sym.type.getParameterTypes());
2288                 firstParam += skip;
2289             }
2290         }
2291         Set<Name> paramNames = new HashSet<>();
2292         ListBuffer<VarSymbol> params = new ListBuffer<>();
2293         int nameIndex = firstParam;
2294         int annotationIndex = 0;
2295         for (Type t: sym.type.getParameterTypes()) {
2296             VarSymbol param = parameter(nameIndex, t, sym, paramNames);


2297             params.append(param);
2298             if (parameterAnnotations != null) {
2299                 ParameterAnnotations annotations = parameterAnnotations[annotationIndex];
2300                 if (annotations != null && annotations.proxies != null
2301                         && !annotations.proxies.isEmpty()) {
2302                     annotate.normal(new AnnotationCompleter(param, annotations.proxies));
2303                 }
2304             }
2305             nameIndex += sawMethodParameters ? 1 : Code.width(t);
2306             annotationIndex++;
2307         }
2308         if (parameterAnnotations != null && parameterAnnotations.length != annotationIndex) {
2309             throw badClassFile("bad.runtime.invisible.param.annotations", sym);
2310         }
2311         Assert.checkNull(sym.params);
2312         sym.params = params.toList();
2313         parameterAnnotations = null;
2314         parameterNameIndices = null;
2315     }
2316 
2317 
2318     // Returns the name for the parameter at position 'index', either using
2319     // names read from the MethodParameters, or by synthesizing a name that
2320     // is not on the 'exclude' list.
2321     private VarSymbol parameter(int index, Type t, MethodSymbol owner, Set<Name> exclude) {
2322         long flags = PARAMETER;
2323         Name argName;
2324         if (parameterNameIndices != null && index < parameterNameIndices.length
2325                 && parameterNameIndices[index] != 0) {
2326             argName = optPoolEntry(parameterNameIndices[index], poolReader::getName, names.empty);
2327             flags |= NAME_FILLED;
2328         } else {
2329             String prefix = "arg";
2330             while (true) {
2331                 argName = names.fromString(prefix + exclude.size());
2332                 if (!exclude.contains(argName))
2333                     break;
2334                 prefix += "$";
2335             }
2336         }
2337         exclude.add(argName);
2338         return new ParamSymbol(flags, argName, t, owner);
2339     }
2340 
2341     /**
2342      * skip n bytes
2343      */
2344     void skipBytes(int n) {
2345         bp = bp + n;
2346     }
2347 
2348     /** Skip a field or method
2349      */
2350     void skipMember() {
2351         bp = bp + 6;
2352         char ac = nextChar();
2353         for (int i = 0; i < ac; i++) {
2354             bp = bp + 2;
2355             int attrLen = nextInt();
2356             bp = bp + attrLen;
2357         }
2358     }


2395      *  versions of an inner class are read.
2396      */
2397     void readClass(ClassSymbol c) {
2398         ClassType ct = (ClassType)c.type;
2399 
2400         // allocate scope for members
2401         c.members_field = WriteableScope.create(c);
2402 
2403         // prepare type variable table
2404         typevars = typevars.dup(currentOwner);
2405         if (ct.getEnclosingType().hasTag(CLASS))
2406             enterTypevars(c.owner, ct.getEnclosingType());
2407 
2408         // read flags, or skip if this is an inner class
2409         long f = nextChar();
2410         long flags = adjustClassFlags(f);
2411         if ((flags & MODULE) == 0) {
2412             if (c.owner.kind == PCK || c.owner.kind == ERR) c.flags_field = flags;
2413             // read own class name and check that it matches
2414             currentModule = c.packge().modle;
2415             ClassSymbol self = poolReader.getClass(nextChar());
2416             if (c != self) {
2417                 throw badClassFile("class.file.wrong.class",
2418                                    self.flatname);
2419             }
2420         } else {
2421             if (majorVersion < Version.V53.major) {
2422                 throw badClassFile("anachronistic.module.info",
2423                         Integer.toString(majorVersion),
2424                         Integer.toString(minorVersion));
2425             }
2426             c.flags_field = flags;
2427             currentModule = (ModuleSymbol) c.owner;
2428             int this_class = nextChar();
2429             // temp, no check on this_class
2430         }
2431 
2432         // class attributes must be read before class
2433         // skip ahead to read class attributes
2434         int startbp = bp;
2435         nextChar();
2436         char interfaceCount = nextChar();
2437         bp += interfaceCount * 2;
2438         char fieldCount = nextChar();
2439         for (int i = 0; i < fieldCount; i++) skipMember();
2440         char methodCount = nextChar();
2441         for (int i = 0; i < methodCount; i++) skipMember();
2442         readClassAttrs(c);
2443 





2444         // reset and read rest of classinfo
2445         bp = startbp;
2446         int n = nextChar();
2447         if ((flags & MODULE) != 0 && n > 0) {
2448             throw badClassFile("module.info.invalid.super.class");
2449         }
2450         if (ct.supertype_field == null)
2451             ct.supertype_field =
2452                     optPoolEntry(n, idx -> poolReader.getClass(idx).erasure(types), Type.noType);

2453         n = nextChar();
2454         List<Type> is = List.nil();
2455         for (int i = 0; i < n; i++) {
2456             Type _inter = poolReader.getClass(nextChar()).erasure(types);
2457             is = is.prepend(_inter);
2458         }
2459         if (ct.interfaces_field == null)
2460             ct.interfaces_field = is.reverse();
2461 
2462         Assert.check(fieldCount == nextChar());
2463         for (int i = 0; i < fieldCount; i++) enterMember(c, readField());
2464         Assert.check(methodCount == nextChar());
2465         for (int i = 0; i < methodCount; i++) enterMember(c, readMethod());
2466 
2467         typevars = typevars.leave();
2468     }
2469 
2470     /** Read inner class info. For each inner/outer pair allocate a
2471      *  member class.
2472      */
2473     void readInnerClasses(ClassSymbol c) {
2474         int n = nextChar();
2475         for (int i = 0; i < n; i++) {
2476             nextChar(); // skip inner class symbol
2477             int outerIdx = nextChar();
2478             int nameIdx = nextChar();
2479             ClassSymbol outer = optPoolEntry(outerIdx, poolReader::getClass, null);
2480             Name name = optPoolEntry(nameIdx, poolReader::getName, names.empty);
2481             if (name == null) name = names.empty;
2482             long flags = adjustClassFlags(nextChar());
2483             if (outer != null) { // we have a member class
2484                 if (name == names.empty)
2485                     name = names.one;
2486                 ClassSymbol member = enterClass(name, outer);
2487                 if ((flags & STATIC) == 0) {
2488                     ((ClassType)member.type).setEnclosingType(outer.type);
2489                     if (member.erasure_field != null)
2490                         ((ClassType)member.erasure_field).setEnclosingType(types.erasure(outer.type));
2491                 }
2492                 if (c == outer) {
2493                     member.flags_field = flags;
2494                     enterMember(c, member);
2495                 }
2496             }
2497         }
2498     }
2499 
2500     /** Read a class definition from the bytes in buf.


2514             if (majorVersion == (maxMajor + 1))
2515                 log.warning(Warnings.BigMajorVersion(currentClassFile,
2516                                                      majorVersion,
2517                                                      maxMajor));
2518             else
2519                 throw badClassFile("wrong.version",
2520                                    Integer.toString(majorVersion),
2521                                    Integer.toString(minorVersion),
2522                                    Integer.toString(maxMajor),
2523                                    Integer.toString(maxMinor));
2524         }
2525 
2526         if (minorVersion == ClassFile.PREVIEW_MINOR_VERSION) {
2527             if (!preview.isEnabled()) {
2528                 log.error(preview.disabledError(currentClassFile, majorVersion));
2529             } else {
2530                 preview.warnPreview(c.classfile, majorVersion);
2531             }
2532         }
2533 
2534         poolReader = new PoolReader(this, names, syms);
2535         bp = poolReader.readPool(buf, bp);
2536         if (signatureBuffer.length < bp) {
2537             int ns = Integer.highestOneBit(bp) << 1;
2538             signatureBuffer = new byte[ns];
2539         }
2540         readClass(c);
2541     }
2542 
2543     public void readClassFile(ClassSymbol c) {
2544         currentOwner = c;
2545         currentClassFile = c.classfile;
2546         warnedAttrs.clear();
2547         filling = true;
2548         target = null;
2549         repeatable = null;
2550         try {
2551             bp = 0;
2552             buf.reset();
2553             buf.appendStream(c.classfile.openInputStream());
2554             readClassBuffer(c);
2555             if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) {
2556                 List<Type> missing = missingTypeVariables;
2557                 List<Type> found = foundTypeVariables;
2558                 missingTypeVariables = List.nil();
2559                 foundTypeVariables = List.nil();
2560                 interimUses = List.nil();
2561                 interimProvides = List.nil();
2562                 filling = false;
2563                 ClassType ct = (ClassType)currentOwner.type;
2564                 ct.supertype_field =
2565                     types.subst(ct.supertype_field, missing, found);
2566                 ct.interfaces_field =
2567                     types.subst(ct.interfaces_field, missing, found);
2568                 ct.typarams_field =
2569                     types.substBounds(ct.typarams_field, missing, found);
2570                 for (List<Type> types = ct.typarams_field; types.nonEmpty(); types = types.tail) {
2571                     types.head.tsym.type = types.head;
2572                 }
2573             } else if (missingTypeVariables.isEmpty() !=


2587                     Assert.check(currentModule.isCompleted());
2588                     currentModule.usesProvidesCompleter =
2589                             new UsesProvidesCompleter(currentModule, interimUses, interimProvides);
2590                 } else {
2591                     currentModule.uses = List.nil();
2592                     currentModule.provides = List.nil();
2593                 }
2594             }
2595         } catch (IOException | ClosedFileSystemException ex) {
2596             throw badClassFile("unable.to.access.file", ex.toString());
2597         } catch (ArrayIndexOutOfBoundsException ex) {
2598             throw badClassFile("bad.class.file", c.flatname);
2599         } finally {
2600             interimUses = List.nil();
2601             interimProvides = List.nil();
2602             missingTypeVariables = List.nil();
2603             foundTypeVariables = List.nil();
2604             filling = false;
2605         }
2606     }





































2607 
2608     /** We can only read a single class file at a time; this
2609      *  flag keeps track of when we are currently reading a class
2610      *  file.
2611      */
2612     public boolean filling = false;
2613 
2614 /************************************************************************
2615  * Adjusting flags
2616  ***********************************************************************/
2617 
2618     long adjustFieldFlags(long flags) {
2619         return flags;
2620     }
2621 
2622     long adjustMethodFlags(long flags) {
2623         if ((flags & ACC_BRIDGE) != 0) {
2624             flags &= ~ACC_BRIDGE;
2625             flags |= BRIDGE;
2626         }


2773                     theTarget = deproxy.deproxyCompound(target);
2774                 }
2775 
2776                 if (repeatable != null) {
2777                     deproxy = new AnnotationDeproxy(proxyOn);
2778                     theRepeatable = deproxy.deproxyCompound(repeatable);
2779                 }
2780             } catch (Exception e) {
2781                 throw new CompletionFailure(sym,
2782                                             () -> ClassReader.this.diagFactory.fragment(Fragments.ExceptionMessage(e.getMessage())),
2783                                             dcfh);
2784             }
2785 
2786             sym.getAnnotationTypeMetadata().setTarget(theTarget);
2787             sym.getAnnotationTypeMetadata().setRepeatable(theRepeatable);
2788         }
2789     }
2790 
2791     private class ProxyType extends Type {
2792 
2793         private final Name name;
2794 
2795         public ProxyType(int index) {
2796             super(syms.noSymbol, TypeMetadata.EMPTY);
2797             this.name = poolReader.getName(index);
2798         }
2799 
2800         @Override
2801         public TypeTag getTag() {
2802             return TypeTag.NONE;
2803         }
2804 
2805         @Override
2806         public Type cloneWithMetadata(TypeMetadata metadata) {
2807             throw new UnsupportedOperationException();
2808         }
2809 
2810         public Type resolve() {
2811             return name.map(ClassReader.this::sigToType);
2812         }
2813 
2814         @Override @DefinedBy(Api.LANGUAGE_MODEL)
2815         public String toString() {
2816             return "<ProxyType>";
2817         }
2818 
2819     }
2820 
2821     private static final class InterimUsesDirective {
2822         public final Name service;
2823 
2824         public InterimUsesDirective(Name service) {
2825             this.service = service;
2826         }
2827 
2828     }
2829 
2830     private static final class InterimProvidesDirective {
2831         public final Name service;




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.jvm;
  27 
  28 import java.io.*;
  29 import java.net.URI;
  30 import java.net.URISyntaxException;
  31 import java.nio.CharBuffer;
  32 import java.nio.file.ClosedFileSystemException;
  33 import java.util.Arrays;
  34 import java.util.EnumSet;
  35 import java.util.HashMap;
  36 import java.util.HashSet;
  37 import java.util.Map;
  38 import java.util.Set;

  39 
  40 import javax.lang.model.element.Modifier;
  41 import javax.lang.model.element.NestingKind;
  42 import javax.tools.JavaFileManager;
  43 import javax.tools.JavaFileObject;
  44 
  45 import com.sun.tools.javac.code.Source.Feature;
  46 import com.sun.tools.javac.comp.Annotate;
  47 import com.sun.tools.javac.comp.Annotate.AnnotationTypeCompleter;
  48 import com.sun.tools.javac.code.*;
  49 import com.sun.tools.javac.code.Directive.*;
  50 import com.sun.tools.javac.code.Lint.LintCategory;
  51 import com.sun.tools.javac.code.Scope.WriteableScope;
  52 import com.sun.tools.javac.code.Symbol.*;
  53 import com.sun.tools.javac.code.Symtab;
  54 import com.sun.tools.javac.code.Type.*;
  55 import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;
  56 import com.sun.tools.javac.file.BaseFileManager;
  57 import com.sun.tools.javac.file.PathFileObject;
  58 import com.sun.tools.javac.jvm.ClassFile.NameAndType;
  59 import com.sun.tools.javac.jvm.ClassFile.Version;

  60 import com.sun.tools.javac.main.Option;
  61 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  62 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
  63 import com.sun.tools.javac.util.*;
  64 import com.sun.tools.javac.util.DefinedBy.Api;
  65 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  66 
  67 import static com.sun.tools.javac.code.Flags.*;
  68 import static com.sun.tools.javac.code.Kinds.Kind.*;
  69 
  70 import com.sun.tools.javac.code.Scope.LookupKind;
  71 
  72 import static com.sun.tools.javac.code.TypeTag.ARRAY;
  73 import static com.sun.tools.javac.code.TypeTag.CLASS;
  74 import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
  75 import static com.sun.tools.javac.jvm.ClassFile.*;
  76 import static com.sun.tools.javac.jvm.ClassFile.Version.*;
  77 
  78 import static com.sun.tools.javac.main.Option.PARAMETERS;
  79 


  83  *  for all other definitions in the classfile. Top-level Classes themselves
  84  *  appear as members of the scopes of PackageSymbols.
  85  *
  86  *  <p><b>This is NOT part of any supported API.
  87  *  If you write code that depends on this, you do so at your own risk.
  88  *  This code and its internal interfaces are subject to change or
  89  *  deletion without notice.</b>
  90  */
  91 public class ClassReader {
  92     /** The context key for the class reader. */
  93     protected static final Context.Key<ClassReader> classReaderKey = new Context.Key<>();
  94 
  95     public static final int INITIAL_BUFFER_SIZE = 0x0fff0;
  96 
  97     private final Annotate annotate;
  98 
  99     /** Switch: verbose output.
 100      */
 101     boolean verbose;
 102 
 103     /** Switch: read constant pool and code sections. This switch is initially
 104      *  set to false but can be turned on from outside.
 105      */
 106     public boolean readAllOfClassFile = false;
 107 
 108     /** Switch: allow modules.
 109      */
 110     boolean allowModules;
 111 
 112    /** Lint option: warn about classfile issues
 113      */
 114     boolean lintClassfile;
 115 
 116     /** Switch: preserve parameter names from the variable table.
 117      */
 118     public boolean saveParameterNames;
 119 
 120     /**
 121      * The currently selected profile.
 122      */
 123     public final Profile profile;
 124 
 125     /** The log to use for verbose output
 126      */
 127     final Log log;


 153      */
 154     protected WriteableScope typevars;
 155 
 156     private List<InterimUsesDirective> interimUses = List.nil();
 157     private List<InterimProvidesDirective> interimProvides = List.nil();
 158 
 159     /** The path name of the class file currently being read.
 160      */
 161     protected JavaFileObject currentClassFile = null;
 162 
 163     /** The class or method currently being read.
 164      */
 165     protected Symbol currentOwner = null;
 166 
 167     /** The module containing the class currently being read.
 168      */
 169     protected ModuleSymbol currentModule = null;
 170 
 171     /** The buffer containing the currently read class file.
 172      */
 173     byte[] buf = new byte[INITIAL_BUFFER_SIZE];
 174 
 175     /** The current input pointer.
 176      */
 177     protected int bp;
 178 
 179     /** The objects of the constant pool.
 180      */
 181     Object[] poolObj;
 182 
 183     /** For every constant pool entry, an index into buf where the
 184      *  defining section of the entry is found.
 185      */
 186     int[] poolIdx;
 187 
 188     /** The major version number of the class file being read. */
 189     int majorVersion;
 190     /** The minor version number of the class file being read. */
 191     int minorVersion;
 192 
 193     /** A table to hold the constant pool indices for method parameter
 194      * names, as given in LocalVariableTable attributes.
 195      */
 196     int[] parameterNameIndices;
 197 
 198     /**
 199      * A table to hold annotations for method parameters.
 200      */
 201     ParameterAnnotations[] parameterAnnotations;
 202 
 203     /**
 204      * A holder for parameter annotations.
 205      */
 206     static class ParameterAnnotations {


 306             diagFactory,
 307             dcfh);
 308     }
 309 
 310     public ClassFinder.BadEnclosingMethodAttr badEnclosingMethod(Symbol sym) {
 311         return new ClassFinder.BadEnclosingMethodAttr (
 312             currentOwner.enclClass(),
 313             currentClassFile,
 314             diagFactory.fragment(Fragments.BadEnclosingMethod(sym)),
 315             diagFactory,
 316             dcfh);
 317     }
 318 
 319 /************************************************************************
 320  * Buffer Access
 321  ***********************************************************************/
 322 
 323     /** Read a character.
 324      */
 325     char nextChar() {
 326         return (char)(((buf[bp++] & 0xFF) << 8) + (buf[bp++] & 0xFF));


 327     }
 328 
 329     /** Read a byte.
 330      */
 331     int nextByte() {
 332         return buf[bp++] & 0xFF;
 333     }
 334 
 335     /** Read an integer.
 336      */
 337     int nextInt() {
 338         return
 339             ((buf[bp++] & 0xFF) << 24) +
 340             ((buf[bp++] & 0xFF) << 16) +
 341             ((buf[bp++] & 0xFF) << 8) +
 342             (buf[bp++] & 0xFF);
 343     }
 344 
 345     /** Extract a character at position bp from buf.
 346      */
 347     char getChar(int bp) {
 348         return
 349             (char)(((buf[bp] & 0xFF) << 8) + (buf[bp+1] & 0xFF));
 350     }
 351 
 352     /** Extract an integer at position bp from buf.
 353      */
 354     int getInt(int bp) {
 355         return
 356             ((buf[bp] & 0xFF) << 24) +
 357             ((buf[bp+1] & 0xFF) << 16) +
 358             ((buf[bp+2] & 0xFF) << 8) +
 359             (buf[bp+3] & 0xFF);
 360     }
 361 
 362 
 363     /** Extract a long integer at position bp from buf.
 364      */
 365     long getLong(int bp) {
 366         DataInputStream bufin =
 367             new DataInputStream(new ByteArrayInputStream(buf, bp, 8));
 368         try {
 369             return bufin.readLong();
 370         } catch (IOException e) {
 371             throw new AssertionError(e);
 372         }
 373     }
 374 
 375     /** Extract a float at position bp from buf.
 376      */
 377     float getFloat(int bp) {
 378         DataInputStream bufin =
 379             new DataInputStream(new ByteArrayInputStream(buf, bp, 4));
 380         try {
 381             return bufin.readFloat();
 382         } catch (IOException e) {
 383             throw new AssertionError(e);
 384         }
 385     }
 386 
 387     /** Extract a double at position bp from buf.
 388      */
 389     double getDouble(int bp) {
 390         DataInputStream bufin =
 391             new DataInputStream(new ByteArrayInputStream(buf, bp, 8));
 392         try {
 393             return bufin.readDouble();
 394         } catch (IOException e) {
 395             throw new AssertionError(e);
 396         }
 397     }
 398 
 399 /************************************************************************
 400  * Constant Pool Access
 401  ***********************************************************************/
 402 
 403     /** Index all constant pool entries, writing their start addresses into
 404      *  poolIdx.
 405      */
 406     void indexPool() {
 407         poolIdx = new int[nextChar()];
 408         poolObj = new Object[poolIdx.length];
 409         int i = 1;
 410         while (i < poolIdx.length) {
 411             poolIdx[i++] = bp;
 412             byte tag = buf[bp++];
 413             switch (tag) {
 414             case CONSTANT_Utf8: case CONSTANT_Unicode: {
 415                 int len = nextChar();
 416                 bp = bp + len;
 417                 break;
 418             }
 419             case CONSTANT_Class:
 420             case CONSTANT_String:
 421             case CONSTANT_MethodType:
 422             case CONSTANT_Module:
 423             case CONSTANT_Package:
 424                 bp = bp + 2;
 425                 break;
 426             case CONSTANT_MethodHandle:
 427                 bp = bp + 3;
 428                 break;
 429             case CONSTANT_Fieldref:
 430             case CONSTANT_Methodref:
 431             case CONSTANT_InterfaceMethodref:
 432             case CONSTANT_NameandType:
 433             case CONSTANT_Integer:
 434             case CONSTANT_Float:
 435             case CONSTANT_Dynamic:
 436             case CONSTANT_InvokeDynamic:
 437                 bp = bp + 4;
 438                 break;
 439             case CONSTANT_Long:
 440             case CONSTANT_Double:
 441                 bp = bp + 8;
 442                 i++;
 443                 break;
 444             default:
 445                 throw badClassFile("bad.const.pool.tag.at",
 446                                    Byte.toString(tag),
 447                                    Integer.toString(bp -1));
 448             }
 449         }
 450     }
 451 
 452     /** Read constant pool entry at start address i, use pool as a cache.
 453      */
 454     Object readPool(int i) {
 455         Object result = poolObj[i];
 456         if (result != null) return result;
 457 
 458         int index = poolIdx[i];
 459         if (index == 0) return null;
 460 
 461         byte tag = buf[index];
 462         switch (tag) {
 463         case CONSTANT_Utf8:
 464             poolObj[i] = names.fromUtf(buf, index + 3, getChar(index + 1));
 465             break;
 466         case CONSTANT_Unicode:
 467             throw badClassFile("unicode.str.not.supported");
 468         case CONSTANT_Class:
 469             poolObj[i] = readClassOrType(getChar(index + 1));
 470             break;
 471         case CONSTANT_String:
 472             // FIXME: (footprint) do not use toString here
 473             poolObj[i] = readName(getChar(index + 1)).toString();
 474             break;
 475         case CONSTANT_Fieldref: {
 476             ClassSymbol owner = readClassSymbol(getChar(index + 1));
 477             NameAndType nt = readNameAndType(getChar(index + 3));
 478             poolObj[i] = new VarSymbol(0, nt.name, nt.uniqueType.type, owner);
 479             break;
 480         }
 481         case CONSTANT_Methodref:
 482         case CONSTANT_InterfaceMethodref: {
 483             ClassSymbol owner = readClassSymbol(getChar(index + 1));
 484             NameAndType nt = readNameAndType(getChar(index + 3));
 485             poolObj[i] = new MethodSymbol(0, nt.name, nt.uniqueType.type, owner);
 486             break;
 487         }
 488         case CONSTANT_NameandType:
 489             poolObj[i] = new NameAndType(
 490                 readName(getChar(index + 1)),
 491                 readType(getChar(index + 3)), types);
 492             break;
 493         case CONSTANT_Integer:
 494             poolObj[i] = getInt(index + 1);
 495             break;
 496         case CONSTANT_Float:
 497             poolObj[i] = Float.valueOf(getFloat(index + 1));
 498             break;
 499         case CONSTANT_Long:
 500             poolObj[i] = Long.valueOf(getLong(index + 1));
 501             break;
 502         case CONSTANT_Double:
 503             poolObj[i] = Double.valueOf(getDouble(index + 1));
 504             break;
 505         case CONSTANT_MethodHandle:
 506             skipBytes(4);
 507             break;
 508         case CONSTANT_MethodType:
 509             skipBytes(3);
 510             break;
 511         case CONSTANT_Dynamic:
 512         case CONSTANT_InvokeDynamic:
 513             skipBytes(5);
 514             break;
 515         case CONSTANT_Module:
 516         case CONSTANT_Package:
 517             // this is temporary for now: treat as a simple reference to the underlying Utf8.
 518             poolObj[i] = readName(getChar(index + 1));
 519             break;
 520         default:
 521             throw badClassFile("bad.const.pool.tag", Byte.toString(tag));
 522         }
 523         return poolObj[i];
 524     }
 525 
 526     /** Read signature and convert to type.
 527      */
 528     Type readType(int i) {
 529         int index = poolIdx[i];
 530         return sigToType(buf, index + 3, getChar(index + 1));
 531     }
 532 
 533     /** If name is an array type or class signature, return the
 534      *  corresponding type; otherwise return a ClassSymbol with given name.
 535      */
 536     Object readClassOrType(int i) {
 537         int index =  poolIdx[i];
 538         int len = getChar(index + 1);
 539         int start = index + 3;
 540         Assert.check(buf[start] == '[' || buf[start + len - 1] != ';');
 541         // by the above assertion, the following test can be
 542         // simplified to (buf[start] == '[')
 543         return (buf[start] == '[' || buf[start + len - 1] == ';')
 544             ? (Object)sigToType(buf, start, len)
 545             : (Object)enterClass(names.fromUtf(internalize(buf, start,
 546                                                            len)));
 547     }
 548 
 549     /** Read signature and convert to type parameters.
 550      */
 551     List<Type> readTypeParams(int i) {
 552         int index = poolIdx[i];
 553         return sigToTypeParams(buf, index + 3, getChar(index + 1));
 554     }
 555 
 556     /** Read class entry.
 557      */
 558     ClassSymbol readClassSymbol(int i) {
 559         Object obj = readPool(i);
 560         if (obj != null && !(obj instanceof ClassSymbol))
 561             throw badClassFile("bad.const.pool.entry",
 562                                currentClassFile.toString(),
 563                                "CONSTANT_Class_info", i);
 564         return (ClassSymbol)obj;
 565     }
 566 
 567     Name readClassName(int i) {
 568         int index = poolIdx[i];
 569         if (index == 0) return null;
 570         byte tag = buf[index];
 571         if (tag != CONSTANT_Class) {
 572             throw badClassFile("bad.const.pool.entry",
 573                                currentClassFile.toString(),
 574                                "CONSTANT_Class_info", i);
 575         }
 576         int nameIndex =  poolIdx[getChar(index + 1)];
 577         int len = getChar(nameIndex + 1);
 578         int start = nameIndex + 3;
 579         if (buf[start] == '[' || buf[start + len - 1] == ';')
 580             throw badClassFile("wrong class name"); //TODO: proper diagnostics
 581         return names.fromUtf(internalize(buf, start, len));
 582     }
 583 
 584     /** Read name.
 585      */
 586     Name readName(int i) {
 587         Object obj = readPool(i);
 588         if (obj != null && !(obj instanceof Name))
 589             throw badClassFile("bad.const.pool.entry",
 590                                currentClassFile.toString(),
 591                                "CONSTANT_Utf8_info or CONSTANT_String_info", i);
 592         return (Name)obj;
 593     }
 594 
 595     /** Read name and type.
 596      */
 597     NameAndType readNameAndType(int i) {
 598         Object obj = readPool(i);
 599         if (obj != null && !(obj instanceof NameAndType))
 600             throw badClassFile("bad.const.pool.entry",
 601                                currentClassFile.toString(),
 602                                "CONSTANT_NameAndType_info", i);
 603         return (NameAndType)obj;
 604     }
 605 
 606     /** Read the name of a module.
 607      * The name is stored in a CONSTANT_Module entry, in
 608      * JVMS 4.2 binary form (using ".", not "/")
 609      */
 610     Name readModuleName(int i) {
 611         return readName(i);
 612     }
 613 
 614     /** Read module_flags.
 615      */
 616     Set<ModuleFlags> readModuleFlags(int flags) {
 617         Set<ModuleFlags> set = EnumSet.noneOf(ModuleFlags.class);
 618         for (ModuleFlags f : ModuleFlags.values()) {
 619             if ((flags & f.value) != 0)
 620                 set.add(f);
 621         }
 622         return set;
 623     }
 624 
 625     /** Read resolution_flags.
 626      */
 627     Set<ModuleResolutionFlags> readModuleResolutionFlags(int flags) {
 628         Set<ModuleResolutionFlags> set = EnumSet.noneOf(ModuleResolutionFlags.class);
 629         for (ModuleResolutionFlags f : ModuleResolutionFlags.values()) {
 630             if ((flags & f.value) != 0)
 631                 set.add(f);
 632         }
 633         return set;


 745                 */
 746                 return t;
 747             }
 748         case 'S':
 749             sigp++;
 750             return syms.shortType;
 751         case 'V':
 752             sigp++;
 753             return syms.voidType;
 754         case 'Z':
 755             sigp++;
 756             return syms.booleanType;
 757         case '[':
 758             sigp++;
 759             return new ArrayType(sigToType(), syms.arrayClass);
 760         case '(':
 761             sigp++;
 762             List<Type> argtypes = sigToTypes(')');
 763             Type restype = sigToType();
 764             List<Type> thrown = List.nil();
 765             while (signature[sigp] == '^') {
 766                 sigp++;
 767                 thrown = thrown.prepend(sigToType());
 768             }
 769             // if there is a typevar in the throws clause we should state it.
 770             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail) {
 771                 if (l.head.hasTag(TYPEVAR)) {
 772                     l.head.tsym.flags_field |= THROWS;
 773                 }
 774             }
 775             return new MethodType(argtypes,
 776                                   restype,
 777                                   thrown.reverse(),
 778                                   syms.methodClass);
 779         case '<':
 780             typevars = typevars.dup(currentOwner);
 781             Type poly = new ForAll(sigToTypeParams(), sigToType());
 782             typevars = typevars.leave();
 783             return poly;
 784         default:
 785             throw badClassFile("bad.signature",


 838                                         // no "rare" types
 839                                         super.setEnclosingType(types.erasure(enclosingType));
 840                                     } else {
 841                                         super.setEnclosingType(types.subst(enclosingType,
 842                                                                            typeParams,
 843                                                                            typeArgs));
 844                                     }
 845                                 } else {
 846                                     super.setEnclosingType(Type.noType);
 847                                 }
 848                             }
 849                             return super.getEnclosingType();
 850                         }
 851                         @Override
 852                         public void setEnclosingType(Type outer) {
 853                             throw new UnsupportedOperationException();
 854                         }
 855                     };
 856                 switch (signature[sigp++]) {
 857                 case ';':
 858                     if (sigp < signature.length && signature[sigp] == '.') {
 859                         // support old-style GJC signatures
 860                         // The signature produced was
 861                         // Lfoo/Outer<Lfoo/X;>;.Lfoo/Outer$Inner<Lfoo/Y;>;
 862                         // rather than say
 863                         // Lfoo/Outer<Lfoo/X;>.Inner<Lfoo/Y;>;
 864                         // so we skip past ".Lfoo/Outer$"
 865                         sigp += (sbp - startSbp) + // "foo/Outer"
 866                             3;  // ".L" and "$"
 867                         signatureBuffer[sbp++] = (byte)'$';
 868                         break;
 869                     } else {
 870                         sbp = startSbp;
 871                         return outer;
 872                     }
 873                 case '.':
 874                     signatureBuffer[sbp++] = (byte)'$';
 875                     break;
 876                 default:
 877                     throw new AssertionError(signature[sigp-1]);
 878                 }


1032         protected final Name name;
1033         protected final ClassFile.Version version;
1034         protected final Set<AttributeKind> kinds;
1035     }
1036 
1037     protected Set<AttributeKind> CLASS_ATTRIBUTE =
1038             EnumSet.of(AttributeKind.CLASS);
1039     protected Set<AttributeKind> MEMBER_ATTRIBUTE =
1040             EnumSet.of(AttributeKind.MEMBER);
1041     protected Set<AttributeKind> CLASS_OR_MEMBER_ATTRIBUTE =
1042             EnumSet.of(AttributeKind.CLASS, AttributeKind.MEMBER);
1043 
1044     protected Map<Name, AttributeReader> attributeReaders = new HashMap<>();
1045 
1046     private void initAttributeReaders() {
1047         AttributeReader[] readers = {
1048             // v45.3 attributes
1049 
1050             new AttributeReader(names.Code, V45_3, MEMBER_ATTRIBUTE) {
1051                 protected void read(Symbol sym, int attrLen) {
1052                     if (readAllOfClassFile || saveParameterNames)
1053                         ((MethodSymbol)sym).code = readCode(sym);
1054                     else
1055                         bp = bp + attrLen;
1056                 }
1057             },
1058 
1059             new AttributeReader(names.ConstantValue, V45_3, MEMBER_ATTRIBUTE) {
1060                 protected void read(Symbol sym, int attrLen) {
1061                     Object v = readPool(nextChar());
1062                     // Ignore ConstantValue attribute if field not final.
1063                     if ((sym.flags() & FINAL) == 0) {
1064                         return;
1065                     }
1066                     VarSymbol var = (VarSymbol) sym;
1067                     switch (var.type.getTag()) {
1068                        case BOOLEAN:
1069                        case BYTE:
1070                        case CHAR:
1071                        case SHORT:
1072                        case INT:
1073                            checkType(var, Integer.class, v);
1074                            break;
1075                        case LONG:
1076                            checkType(var, Long.class, v);
1077                            break;
1078                        case FLOAT:
1079                            checkType(var, Float.class, v);
1080                            break;
1081                        case DOUBLE:


1098                 void checkType(Symbol var, Class<?> clazz, Object value) {
1099                     if (!clazz.isInstance(value)) {
1100                         throw badClassFile("bad.constant.value", value, var, clazz.getSimpleName());
1101                     }
1102                 }
1103             },
1104 
1105             new AttributeReader(names.Deprecated, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
1106                 protected void read(Symbol sym, int attrLen) {
1107                     Symbol s = sym.owner.kind == MDL ? sym.owner : sym;
1108 
1109                     s.flags_field |= DEPRECATED;
1110                 }
1111             },
1112 
1113             new AttributeReader(names.Exceptions, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
1114                 protected void read(Symbol sym, int attrLen) {
1115                     int nexceptions = nextChar();
1116                     List<Type> thrown = List.nil();
1117                     for (int j = 0; j < nexceptions; j++)
1118                         thrown = thrown.prepend(readClassSymbol(nextChar()).type);
1119                     if (sym.type.getThrownTypes().isEmpty())
1120                         sym.type.asMethodType().thrown = thrown.reverse();
1121                 }
1122             },
1123 
1124             new AttributeReader(names.InnerClasses, V45_3, CLASS_ATTRIBUTE) {
1125                 protected void read(Symbol sym, int attrLen) {
1126                     ClassSymbol c = (ClassSymbol) sym;
1127                     if (currentModule.module_info == c) {
1128                         //prevent entering the classes too soon:
1129                         skipInnerClasses();
1130                     } else {
1131                         readInnerClasses(c);
1132                     }
1133                 }
1134             },
1135 
1136             new AttributeReader(names.LocalVariableTable, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
1137                 protected void read(Symbol sym, int attrLen) {
1138                     int newbp = bp + attrLen;


1156                             if (start_pc == 0) {
1157                                 // ensure array large enough
1158                                 if (register >= parameterNameIndices.length) {
1159                                     int newSize =
1160                                             Math.max(register + 1, parameterNameIndices.length + 8);
1161                                     parameterNameIndices =
1162                                             Arrays.copyOf(parameterNameIndices, newSize);
1163                                 }
1164                                 parameterNameIndices[register] = nameIndex;
1165                                 haveParameterNameIndices = true;
1166                             }
1167                         }
1168                     }
1169                     bp = newbp;
1170                 }
1171             },
1172 
1173             new AttributeReader(names.SourceFile, V45_3, CLASS_ATTRIBUTE) {
1174                 protected void read(Symbol sym, int attrLen) {
1175                     ClassSymbol c = (ClassSymbol) sym;
1176                     Name n = readName(nextChar());
1177                     c.sourcefile = new SourceFileObject(n);
1178                     // If the class is a toplevel class, originating from a Java source file,
1179                     // but the class name does not match the file name, then it is
1180                     // an auxiliary class.
1181                     String sn = n.toString();
1182                     if (c.owner.kind == PCK &&
1183                         sn.endsWith(".java") &&
1184                         !sn.equals(c.name.toString()+".java")) {
1185                         c.flags_field |= AUXILIARY;
1186                     }
1187                 }
1188             },
1189 
1190             new AttributeReader(names.Synthetic, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
1191                 protected void read(Symbol sym, int attrLen) {
1192                     sym.flags_field |= SYNTHETIC;
1193                 }
1194             },
1195 
1196             // standard v49 attributes
1197 
1198             new AttributeReader(names.EnclosingMethod, V49, CLASS_ATTRIBUTE) {
1199                 protected void read(Symbol sym, int attrLen) {
1200                     int newbp = bp + attrLen;
1201                     readEnclosingMethodAttr(sym);
1202                     bp = newbp;
1203                 }
1204             },
1205 
1206             new AttributeReader(names.Signature, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
1207                 protected void read(Symbol sym, int attrLen) {
1208                     if (sym.kind == TYP) {
1209                         ClassSymbol c = (ClassSymbol) sym;
1210                         readingClassAttr = true;
1211                         try {
1212                             ClassType ct1 = (ClassType)c.type;
1213                             Assert.check(c == currentOwner);
1214                             ct1.typarams_field = readTypeParams(nextChar());

1215                             ct1.supertype_field = sigToType();
1216                             ListBuffer<Type> is = new ListBuffer<>();
1217                             while (sigp != siglimit) is.append(sigToType());
1218                             ct1.interfaces_field = is.toList();
1219                         } finally {
1220                             readingClassAttr = false;
1221                         }
1222                     } else {
1223                         List<Type> thrown = sym.type.getThrownTypes();
1224                         sym.type = readType(nextChar());
1225                         //- System.err.println(" # " + sym.type);
1226                         if (sym.kind == MTH && sym.type.getThrownTypes().isEmpty())
1227                             sym.type.asMethodType().thrown = thrown;
1228 
1229                     }
1230                 }
1231             },
1232 
1233             // v49 annotation attributes
1234 
1235             new AttributeReader(names.AnnotationDefault, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
1236                 protected void read(Symbol sym, int attrLen) {
1237                     attachAnnotationDefault(sym);
1238                 }
1239             },
1240 
1241             new AttributeReader(names.RuntimeInvisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
1242                 protected void read(Symbol sym, int attrLen) {
1243                     attachAnnotations(sym);
1244                 }


1325                             }
1326                             parameterNameIndices[index++] = nameIndex;
1327                         }
1328                     }
1329                     bp = newbp;
1330                 }
1331             },
1332 
1333             // standard v53 attributes
1334 
1335             new AttributeReader(names.Module, V53, CLASS_ATTRIBUTE) {
1336                 @Override
1337                 protected boolean accepts(AttributeKind kind) {
1338                     return super.accepts(kind) && allowModules;
1339                 }
1340                 protected void read(Symbol sym, int attrLen) {
1341                     if (sym.kind == TYP && sym.owner.kind == MDL) {
1342                         ModuleSymbol msym = (ModuleSymbol) sym.owner;
1343                         ListBuffer<Directive> directives = new ListBuffer<>();
1344 
1345                         Name moduleName = readModuleName(nextChar());
1346                         if (currentModule.name != moduleName) {
1347                             throw badClassFile("module.name.mismatch", moduleName, currentModule.name);
1348                         }
1349 
1350                         Set<ModuleFlags> moduleFlags = readModuleFlags(nextChar());
1351                         msym.flags.addAll(moduleFlags);
1352                         msym.version = readName(nextChar());
1353 
1354                         ListBuffer<RequiresDirective> requires = new ListBuffer<>();
1355                         int nrequires = nextChar();
1356                         for (int i = 0; i < nrequires; i++) {
1357                             ModuleSymbol rsym = syms.enterModule(readModuleName(nextChar()));
1358                             Set<RequiresFlag> flags = readRequiresFlags(nextChar());
1359                             if (rsym == syms.java_base && majorVersion >= V54.major) {
1360                                 if (flags.contains(RequiresFlag.TRANSITIVE)) {
1361                                     throw badClassFile("bad.requires.flag", RequiresFlag.TRANSITIVE);
1362                                 }
1363                                 if (flags.contains(RequiresFlag.STATIC_PHASE)) {
1364                                     throw badClassFile("bad.requires.flag", RequiresFlag.STATIC_PHASE);
1365                                 }
1366                             }
1367                             nextChar(); // skip compiled version
1368                             requires.add(new RequiresDirective(rsym, flags));
1369                         }
1370                         msym.requires = requires.toList();
1371                         directives.addAll(msym.requires);
1372 
1373                         ListBuffer<ExportsDirective> exports = new ListBuffer<>();
1374                         int nexports = nextChar();
1375                         for (int i = 0; i < nexports; i++) {
1376                             Name n = readName(nextChar());
1377                             PackageSymbol p = syms.enterPackage(currentModule, names.fromUtf(internalize(n)));
1378                             Set<ExportsFlag> flags = readExportsFlags(nextChar());
1379                             int nto = nextChar();
1380                             List<ModuleSymbol> to;
1381                             if (nto == 0) {
1382                                 to = null;
1383                             } else {
1384                                 ListBuffer<ModuleSymbol> lb = new ListBuffer<>();
1385                                 for (int t = 0; t < nto; t++)
1386                                     lb.append(syms.enterModule(readModuleName(nextChar())));
1387                                 to = lb.toList();
1388                             }
1389                             exports.add(new ExportsDirective(p, to, flags));
1390                         }
1391                         msym.exports = exports.toList();
1392                         directives.addAll(msym.exports);
1393                         ListBuffer<OpensDirective> opens = new ListBuffer<>();
1394                         int nopens = nextChar();
1395                         if (nopens != 0 && msym.flags.contains(ModuleFlags.OPEN)) {
1396                             throw badClassFile("module.non.zero.opens", currentModule.name);
1397                         }
1398                         for (int i = 0; i < nopens; i++) {
1399                             Name n = readName(nextChar());
1400                             PackageSymbol p = syms.enterPackage(currentModule, names.fromUtf(internalize(n)));
1401                             Set<OpensFlag> flags = readOpensFlags(nextChar());
1402                             int nto = nextChar();
1403                             List<ModuleSymbol> to;
1404                             if (nto == 0) {
1405                                 to = null;
1406                             } else {
1407                                 ListBuffer<ModuleSymbol> lb = new ListBuffer<>();
1408                                 for (int t = 0; t < nto; t++)
1409                                     lb.append(syms.enterModule(readModuleName(nextChar())));
1410                                 to = lb.toList();
1411                             }
1412                             opens.add(new OpensDirective(p, to, flags));
1413                         }
1414                         msym.opens = opens.toList();
1415                         directives.addAll(msym.opens);
1416 
1417                         msym.directives = directives.toList();
1418 
1419                         ListBuffer<InterimUsesDirective> uses = new ListBuffer<>();
1420                         int nuses = nextChar();
1421                         for (int i = 0; i < nuses; i++) {
1422                             Name srvc = readClassName(nextChar());
1423                             uses.add(new InterimUsesDirective(srvc));
1424                         }
1425                         interimUses = uses.toList();
1426 
1427                         ListBuffer<InterimProvidesDirective> provides = new ListBuffer<>();
1428                         int nprovides = nextChar();
1429                         for (int p = 0; p < nprovides; p++) {
1430                             Name srvc = readClassName(nextChar());
1431                             int nimpls = nextChar();
1432                             ListBuffer<Name> impls = new ListBuffer<>();
1433                             for (int i = 0; i < nimpls; i++) {
1434                                 impls.append(readClassName(nextChar()));
1435                             provides.add(new InterimProvidesDirective(srvc, impls.toList()));
1436                             }
1437                         }
1438                         interimProvides = provides.toList();
1439                     }
1440                 }




1441             },
1442 
1443             new AttributeReader(names.ModuleResolution, V53, CLASS_ATTRIBUTE) {
1444                 @Override
1445                 protected boolean accepts(AttributeKind kind) {
1446                     return super.accepts(kind) && allowModules;
1447                 }
1448                 protected void read(Symbol sym, int attrLen) {
1449                     if (sym.kind == TYP && sym.owner.kind == MDL) {
1450                         ModuleSymbol msym = (ModuleSymbol) sym.owner;
1451                         msym.resolutionFlags.addAll(readModuleResolutionFlags(nextChar()));
1452                     }
1453                 }
1454             },
1455         };
1456 
1457         for (AttributeReader r: readers)
1458             attributeReaders.put(r.name, r);
1459     }
1460 
1461     protected void readEnclosingMethodAttr(Symbol sym) {
1462         // sym is a nested class with an "Enclosing Method" attribute
1463         // remove sym from it's current owners scope and place it in
1464         // the scope specified by the attribute
1465         sym.owner.members().remove(sym);
1466         ClassSymbol self = (ClassSymbol)sym;
1467         ClassSymbol c = readClassSymbol(nextChar());
1468         NameAndType nt = readNameAndType(nextChar());
1469 
1470         if (c.members_field == null || c.kind != TYP)
1471             throw badClassFile("bad.enclosing.class", self, c);
1472 
1473         MethodSymbol m = findMethod(nt, c.members_field, self.flags());
1474         if (nt != null && m == null)
1475             throw badEnclosingMethod(self);
1476 
1477         self.name = simpleBinaryName(self.flatname, c.flatname) ;
1478         self.owner = m != null ? m : c;
1479         if (self.name.isEmpty())
1480             self.fullname = names.empty;
1481         else
1482             self.fullname = ClassSymbol.formFullName(self.name, self.owner);
1483 
1484         if (m != null) {
1485             ((ClassType)sym.type).setEnclosingType(m.type);
1486         } else if ((self.flags_field & STATIC) == 0) {
1487             ((ClassType)sym.type).setEnclosingType(c.type);
1488         } else {
1489             ((ClassType)sym.type).setEnclosingType(Type.noType);
1490         }
1491         enterTypevars(self, self.type);
1492         if (!missingTypeVariables.isEmpty()) {
1493             ListBuffer<Type> typeVars =  new ListBuffer<>();
1494             for (Type typevar : missingTypeVariables) {
1495                 typeVars.append(findTypeVar(typevar.tsym.name));
1496             }
1497             foundTypeVariables = typeVars.toList();
1498         } else {
1499             foundTypeVariables = List.nil();
1500         }
1501     }
1502 
1503     // See java.lang.Class
1504     private Name simpleBinaryName(Name self, Name enclosing) {




1505         String simpleBinaryName = self.toString().substring(enclosing.toString().length());
1506         if (simpleBinaryName.length() < 1 || simpleBinaryName.charAt(0) != '$')
1507             throw badClassFile("bad.enclosing.method", self);
1508         int index = 1;
1509         while (index < simpleBinaryName.length() &&
1510                isAsciiDigit(simpleBinaryName.charAt(index)))
1511             index++;
1512         return names.fromString(simpleBinaryName.substring(index));
1513     }
1514 
1515     private MethodSymbol findMethod(NameAndType nt, Scope scope, long flags) {
1516         if (nt == null)
1517             return null;
1518 
1519         MethodType type = nt.uniqueType.type.asMethodType();
1520 
1521         for (Symbol sym : scope.getSymbolsByName(nt.name)) {
1522             if (sym.kind == MTH && isSameBinaryType(sym.type.asMethodType(), type))
1523                 return (MethodSymbol)sym;
1524         }
1525 
1526         if (nt.name != names.init)
1527             // not a constructor
1528             return null;
1529         if ((flags & INTERFACE) != 0)
1530             // no enclosing instance
1531             return null;
1532         if (nt.uniqueType.type.getParameterTypes().isEmpty())
1533             // no parameters
1534             return null;
1535 
1536         // A constructor of an inner class.
1537         // Remove the first argument (the enclosing instance)
1538         nt.setType(new MethodType(nt.uniqueType.type.getParameterTypes().tail,
1539                                  nt.uniqueType.type.getReturnType(),
1540                                  nt.uniqueType.type.getThrownTypes(),
1541                                  syms.methodClass));
1542         // Try searching again
1543         return findMethod(nt, scope, flags);
1544     }
1545 
1546     /** Similar to Types.isSameType but avoids completion */
1547     private boolean isSameBinaryType(MethodType mt1, MethodType mt2) {
1548         List<Type> types1 = types.erasure(mt1.getParameterTypes())
1549             .prepend(types.erasure(mt1.getReturnType()));
1550         List<Type> types2 = mt2.getParameterTypes().prepend(mt2.getReturnType());
1551         while (!types1.isEmpty() && !types2.isEmpty()) {
1552             if (types1.head.tsym != types2.head.tsym)
1553                 return false;
1554             types1 = types1.tail;
1555             types2 = types2.tail;
1556         }
1557         return types1.isEmpty() && types2.isEmpty();
1558     }
1559 
1560     /**
1561      * Character.isDigit answers <tt>true</tt> to some non-ascii
1562      * digits.  This one does not.  <b>copied from java.lang.Class</b>
1563      */
1564     private static boolean isAsciiDigit(char c) {
1565         return '0' <= c && c <= '9';
1566     }
1567 
1568     /** Read member attributes.
1569      */
1570     void readMemberAttrs(Symbol sym) {
1571         readAttrs(sym, AttributeKind.MEMBER);
1572     }
1573 
1574     void readAttrs(Symbol sym, AttributeKind kind) {
1575         char ac = nextChar();
1576         for (int i = 0; i < ac; i++) {
1577             Name attrName = readName(nextChar());
1578             int attrLen = nextInt();
1579             AttributeReader r = attributeReaders.get(attrName);
1580             if (r != null && r.accepts(kind))
1581                 r.read(sym, attrLen);
1582             else  {
1583                 bp = bp + attrLen;
1584             }
1585         }
1586     }
1587 
1588     private boolean readingClassAttr = false;
1589     private List<Type> missingTypeVariables = List.nil();
1590     private List<Type> foundTypeVariables = List.nil();
1591 
1592     /** Read class attributes.
1593      */
1594     void readClassAttrs(ClassSymbol c) {
1595         readAttrs(c, AttributeKind.CLASS);
1596     }
1597 


1660                 } else if (proxy.type.tsym == syms.deprecatedType.tsym) {
1661                     sym.flags_field |= (DEPRECATED | DEPRECATED_ANNOTATION);
1662                     for (Pair<Name, Attribute> v : proxy.values) {
1663                         if (v.fst == names.forRemoval && v.snd instanceof Attribute.Constant) {
1664                             Attribute.Constant c = (Attribute.Constant)v.snd;
1665                             if (c.type == syms.booleanType && ((Integer)c.value) != 0) {
1666                                 sym.flags_field |= DEPRECATED_REMOVAL;
1667                             }
1668                         }
1669                     }
1670                 }
1671                 proxies.append(proxy);
1672             }
1673         }
1674         annotate.normal(new AnnotationCompleter(sym, proxies.toList()));
1675     }
1676 
1677     /** Read parameter annotations.
1678      */
1679     void readParameterAnnotations(Symbol meth) {
1680         int numParameters = buf[bp++] & 0xFF;
1681         if (parameterAnnotations == null) {
1682             parameterAnnotations = new ParameterAnnotations[numParameters];
1683         } else if (parameterAnnotations.length != numParameters) {
1684             throw badClassFile("bad.runtime.invisible.param.annotations", meth);
1685         }
1686         for (int pnum = 0; pnum < numParameters; pnum++) {
1687             if (parameterAnnotations[pnum] == null) {
1688                 parameterAnnotations[pnum] = new ParameterAnnotations();
1689             }
1690             parameterAnnotations[pnum].add(readAnnotations());
1691         }
1692     }
1693 
1694     void attachTypeAnnotations(final Symbol sym) {
1695         int numAttributes = nextChar();
1696         if (numAttributes != 0) {
1697             ListBuffer<TypeAnnotationProxy> proxies = new ListBuffer<>();
1698             for (int i = 0; i < numAttributes; i++)
1699                 proxies.append(readTypeAnnotation());
1700             annotate.normal(new TypeAnnotationCompleter(sym, proxies.toList()));


1704     /** Attach the default value for an annotation element.
1705      */
1706     void attachAnnotationDefault(final Symbol sym) {
1707         final MethodSymbol meth = (MethodSymbol)sym; // only on methods
1708         final Attribute value = readAttributeValue();
1709 
1710         // The default value is set later during annotation. It might
1711         // be the case that the Symbol sym is annotated _after_ the
1712         // repeating instances that depend on this default value,
1713         // because of this we set an interim value that tells us this
1714         // element (most likely) has a default.
1715         //
1716         // Set interim value for now, reset just before we do this
1717         // properly at annotate time.
1718         meth.defaultValue = value;
1719         annotate.normal(new AnnotationDefaultCompleter(meth, value));
1720     }
1721 
1722     Type readTypeOrClassSymbol(int i) {
1723         // support preliminary jsr175-format class files
1724         if (buf[poolIdx[i]] == CONSTANT_Class)
1725             return readClassSymbol(i).type;
1726         return readTypeToProxy(i);
1727     }
1728     Type readEnumType(int i) {
1729         // support preliminary jsr175-format class files
1730         int index = poolIdx[i];
1731         int length = getChar(index + 1);
1732         if (buf[index + length + 2] != ';')
1733             return enterClass(readName(i)).type;
1734         return readTypeToProxy(i);
1735     }
1736     Type readTypeToProxy(int i) {
1737         if (currentModule.module_info == currentOwner) {
1738             int index = poolIdx[i];
1739             return new ProxyType(Arrays.copyOfRange(buf, index + 3, index + 3 + getChar(index + 1)));
1740         } else {
1741             return readType(i);
1742         }
1743     }
1744 
1745     CompoundAnnotationProxy readCompoundAnnotation() {
1746         Type t;
1747         if (currentModule.module_info == currentOwner) {
1748             int index = poolIdx[nextChar()];
1749             t = new ProxyType(Arrays.copyOfRange(buf, index + 3, index + 3 + getChar(index + 1)));
1750         } else {
1751             t = readTypeOrClassSymbol(nextChar());
1752         }
1753         int numFields = nextChar();
1754         ListBuffer<Pair<Name,Attribute>> pairs = new ListBuffer<>();
1755         for (int i=0; i<numFields; i++) {
1756             Name name = readName(nextChar());
1757             Attribute value = readAttributeValue();
1758             pairs.append(new Pair<>(name, value));
1759         }
1760         return new CompoundAnnotationProxy(t, pairs.toList());
1761     }
1762 
1763     TypeAnnotationProxy readTypeAnnotation() {
1764         TypeAnnotationPosition position = readPosition();
1765         CompoundAnnotationProxy proxy = readCompoundAnnotation();
1766 
1767         return new TypeAnnotationProxy(proxy, position);
1768     }
1769 
1770     TypeAnnotationPosition readPosition() {
1771         int tag = nextByte(); // TargetType tag is a byte
1772 
1773         if (!TargetType.isValidTargetTypeValue(tag))
1774             throw badClassFile("bad.type.annotation.value", String.format("0x%02X", tag));
1775 
1776         TargetType type = TargetType.fromTargetTypeValue(tag);


1949             return TypeAnnotationPosition.methodReturn(readTypePath());
1950         case FIELD:
1951             return TypeAnnotationPosition.field(readTypePath());
1952         case UNKNOWN:
1953             throw new AssertionError("jvm.ClassReader: UNKNOWN target type should never occur!");
1954         default:
1955             throw new AssertionError("jvm.ClassReader: Unknown target type for position: " + type);
1956         }
1957     }
1958 
1959     List<TypeAnnotationPosition.TypePathEntry> readTypePath() {
1960         int len = nextByte();
1961         ListBuffer<Integer> loc = new ListBuffer<>();
1962         for (int i = 0; i < len * TypeAnnotationPosition.TypePathEntry.bytesPerEntry; ++i)
1963             loc = loc.append(nextByte());
1964 
1965         return TypeAnnotationPosition.getTypePathFromBinary(loc.toList());
1966 
1967     }
1968 











1969     Attribute readAttributeValue() {
1970         char c = (char) buf[bp++];
1971         switch (c) {
1972         case 'B':
1973             return new Attribute.Constant(syms.byteType, readPool(nextChar()));
1974         case 'C':
1975             return new Attribute.Constant(syms.charType, readPool(nextChar()));
1976         case 'D':
1977             return new Attribute.Constant(syms.doubleType, readPool(nextChar()));
1978         case 'F':
1979             return new Attribute.Constant(syms.floatType, readPool(nextChar()));
1980         case 'I':
1981             return new Attribute.Constant(syms.intType, readPool(nextChar()));
1982         case 'J':
1983             return new Attribute.Constant(syms.longType, readPool(nextChar()));
1984         case 'S':
1985             return new Attribute.Constant(syms.shortType, readPool(nextChar()));
1986         case 'Z':
1987             return new Attribute.Constant(syms.booleanType, readPool(nextChar()));
1988         case 's':
1989             return new Attribute.Constant(syms.stringType, readPool(nextChar()).toString());
1990         case 'e':
1991             return new EnumAttributeProxy(readEnumType(nextChar()), readName(nextChar()));
1992         case 'c':
1993             return new ClassAttributeProxy(readTypeOrClassSymbol(nextChar()));
1994         case '[': {
1995             int n = nextChar();
1996             ListBuffer<Attribute> l = new ListBuffer<>();
1997             for (int i=0; i<n; i++)
1998                 l.append(readAttributeValue());
1999             return new ArrayAttributeProxy(l.toList());
2000         }
2001         case '@':
2002             return readCompoundAnnotation();
2003         default:
2004             throw new AssertionError("unknown annotation tag '" + c + "'");
2005         }
2006     }
2007 
2008     interface ProxyVisitor extends Attribute.Visitor {
2009         void visitEnumAttributeProxy(EnumAttributeProxy proxy);
2010         void visitClassAttributeProxy(ClassAttributeProxy proxy);
2011         void visitArrayAttributeProxy(ArrayAttributeProxy proxy);


2380             JavaFileObject previousClassFile = currentClassFile;
2381             try {
2382                 currentClassFile = classFile;
2383                 List<Attribute.TypeCompound> newList = deproxyTypeCompoundList(proxies);
2384                 sym.setTypeAttributes(newList.prependList(sym.getRawTypeAttributes()));
2385             } finally {
2386                 currentClassFile = previousClassFile;
2387             }
2388         }
2389     }
2390 
2391 
2392 /************************************************************************
2393  * Reading Symbols
2394  ***********************************************************************/
2395 
2396     /** Read a field.
2397      */
2398     VarSymbol readField() {
2399         long flags = adjustFieldFlags(nextChar());
2400         Name name = readName(nextChar());
2401         Type type = readType(nextChar());
2402         VarSymbol v = new VarSymbol(flags, name, type, currentOwner);
2403         readMemberAttrs(v);
2404         return v;
2405     }
2406 
2407     /** Read a method.
2408      */
2409     MethodSymbol readMethod() {
2410         long flags = adjustMethodFlags(nextChar());
2411         Name name = readName(nextChar());
2412         Type type = readType(nextChar());
2413         if (currentOwner.isInterface() &&
2414                 (flags & ABSTRACT) == 0 && !name.equals(names.clinit)) {
2415             if (majorVersion > Version.V52.major ||
2416                     (majorVersion == Version.V52.major && minorVersion >= Version.V52.minor)) {
2417                 if ((flags & (STATIC | PRIVATE)) == 0) {
2418                     currentOwner.flags_field |= DEFAULT;
2419                     flags |= DEFAULT | ABSTRACT;
2420                 }
2421             } else {
2422                 //protect against ill-formed classfiles
2423                 throw badClassFile((flags & STATIC) == 0 ? "invalid.default.interface" : "invalid.static.interface",
2424                                    Integer.toString(majorVersion),
2425                                    Integer.toString(minorVersion));
2426             }
2427         }
2428         if (name == names.init && currentOwner.hasOuterInstance()) {
2429             // Sometimes anonymous classes don't have an outer
2430             // instance, however, there is no reliable way to tell so
2431             // we never strip this$n
2432             // ditto for local classes. Local classes that have an enclosing method set


2536                 // we never strip this$n
2537                 if (!currentOwner.name.isEmpty())
2538                     firstParam += 1;
2539             }
2540 
2541             if (sym.type != jvmType) {
2542                 // reading the method attributes has caused the
2543                 // symbol's type to be changed. (i.e. the Signature
2544                 // attribute.)  This may happen if there are hidden
2545                 // (synthetic) parameters in the descriptor, but not
2546                 // in the Signature.  The position of these hidden
2547                 // parameters is unspecified; for now, assume they are
2548                 // at the beginning, and so skip over them. The
2549                 // primary case for this is two hidden parameters
2550                 // passed into Enum constructors.
2551                 int skip = Code.width(jvmType.getParameterTypes())
2552                         - Code.width(sym.type.getParameterTypes());
2553                 firstParam += skip;
2554             }
2555         }
2556         List<Name> paramNames = List.nil();
2557         ListBuffer<VarSymbol> params = new ListBuffer<>();
2558         int nameIndex = firstParam;
2559         int annotationIndex = 0;
2560         for (Type t: sym.type.getParameterTypes()) {
2561             Name name = parameterName(nameIndex, paramNames);
2562             paramNames = paramNames.prepend(name);
2563             VarSymbol param = new VarSymbol(PARAMETER, name, t, sym);
2564             params.append(param);
2565             if (parameterAnnotations != null) {
2566                 ParameterAnnotations annotations = parameterAnnotations[annotationIndex];
2567                 if (annotations != null && annotations.proxies != null
2568                         && !annotations.proxies.isEmpty()) {
2569                     annotate.normal(new AnnotationCompleter(param, annotations.proxies));
2570                 }
2571             }
2572             nameIndex += sawMethodParameters ? 1 : Code.width(t);
2573             annotationIndex++;
2574         }
2575         if (parameterAnnotations != null && parameterAnnotations.length != annotationIndex) {
2576             throw badClassFile("bad.runtime.invisible.param.annotations", sym);
2577         }
2578         Assert.checkNull(sym.params);
2579         sym.params = params.toList();
2580         parameterAnnotations = null;
2581         parameterNameIndices = null;
2582     }
2583 
2584 
2585     // Returns the name for the parameter at position 'index', either using
2586     // names read from the MethodParameters, or by synthesizing a name that
2587     // is not on the 'exclude' list.
2588     private Name parameterName(int index, List<Name> exclude) {


2589         if (parameterNameIndices != null && index < parameterNameIndices.length
2590                 && parameterNameIndices[index] != 0) {
2591             return readName(parameterNameIndices[index]);
2592         }
2593         String prefix = "arg";
2594         while (true) {
2595             Name argName = names.fromString(prefix + exclude.size());
2596             if (!exclude.contains(argName))
2597                 return argName;
2598             prefix += "$";


2599         }


2600     }
2601 
2602     /**
2603      * skip n bytes
2604      */
2605     void skipBytes(int n) {
2606         bp = bp + n;
2607     }
2608 
2609     /** Skip a field or method
2610      */
2611     void skipMember() {
2612         bp = bp + 6;
2613         char ac = nextChar();
2614         for (int i = 0; i < ac; i++) {
2615             bp = bp + 2;
2616             int attrLen = nextInt();
2617             bp = bp + attrLen;
2618         }
2619     }


2656      *  versions of an inner class are read.
2657      */
2658     void readClass(ClassSymbol c) {
2659         ClassType ct = (ClassType)c.type;
2660 
2661         // allocate scope for members
2662         c.members_field = WriteableScope.create(c);
2663 
2664         // prepare type variable table
2665         typevars = typevars.dup(currentOwner);
2666         if (ct.getEnclosingType().hasTag(CLASS))
2667             enterTypevars(c.owner, ct.getEnclosingType());
2668 
2669         // read flags, or skip if this is an inner class
2670         long f = nextChar();
2671         long flags = adjustClassFlags(f);
2672         if ((flags & MODULE) == 0) {
2673             if (c.owner.kind == PCK || c.owner.kind == ERR) c.flags_field = flags;
2674             // read own class name and check that it matches
2675             currentModule = c.packge().modle;
2676             ClassSymbol self = readClassSymbol(nextChar());
2677             if (c != self) {
2678                 throw badClassFile("class.file.wrong.class",
2679                                    self.flatname);
2680             }
2681         } else {
2682             if (majorVersion < Version.V53.major) {
2683                 throw badClassFile("anachronistic.module.info",
2684                         Integer.toString(majorVersion),
2685                         Integer.toString(minorVersion));
2686             }
2687             c.flags_field = flags;
2688             currentModule = (ModuleSymbol) c.owner;
2689             int this_class = nextChar();
2690             // temp, no check on this_class
2691         }
2692 
2693         // class attributes must be read before class
2694         // skip ahead to read class attributes
2695         int startbp = bp;
2696         nextChar();
2697         char interfaceCount = nextChar();
2698         bp += interfaceCount * 2;
2699         char fieldCount = nextChar();
2700         for (int i = 0; i < fieldCount; i++) skipMember();
2701         char methodCount = nextChar();
2702         for (int i = 0; i < methodCount; i++) skipMember();
2703         readClassAttrs(c);
2704 
2705         if (readAllOfClassFile) {
2706             for (int i = 1; i < poolObj.length; i++) readPool(i);
2707             c.pool = new Pool(poolObj.length, poolObj, types);
2708         }
2709 
2710         // reset and read rest of classinfo
2711         bp = startbp;
2712         int n = nextChar();
2713         if ((flags & MODULE) != 0 && n > 0) {
2714             throw badClassFile("module.info.invalid.super.class");
2715         }
2716         if (ct.supertype_field == null)
2717             ct.supertype_field = (n == 0)
2718                 ? Type.noType
2719                 : readClassSymbol(n).erasure(types);
2720         n = nextChar();
2721         List<Type> is = List.nil();
2722         for (int i = 0; i < n; i++) {
2723             Type _inter = readClassSymbol(nextChar()).erasure(types);
2724             is = is.prepend(_inter);
2725         }
2726         if (ct.interfaces_field == null)
2727             ct.interfaces_field = is.reverse();
2728 
2729         Assert.check(fieldCount == nextChar());
2730         for (int i = 0; i < fieldCount; i++) enterMember(c, readField());
2731         Assert.check(methodCount == nextChar());
2732         for (int i = 0; i < methodCount; i++) enterMember(c, readMethod());
2733 
2734         typevars = typevars.leave();
2735     }
2736 
2737     /** Read inner class info. For each inner/outer pair allocate a
2738      *  member class.
2739      */
2740     void readInnerClasses(ClassSymbol c) {
2741         int n = nextChar();
2742         for (int i = 0; i < n; i++) {
2743             nextChar(); // skip inner class symbol
2744             ClassSymbol outer = readClassSymbol(nextChar());
2745             Name name = readName(nextChar());


2746             if (name == null) name = names.empty;
2747             long flags = adjustClassFlags(nextChar());
2748             if (outer != null) { // we have a member class
2749                 if (name == names.empty)
2750                     name = names.one;
2751                 ClassSymbol member = enterClass(name, outer);
2752                 if ((flags & STATIC) == 0) {
2753                     ((ClassType)member.type).setEnclosingType(outer.type);
2754                     if (member.erasure_field != null)
2755                         ((ClassType)member.erasure_field).setEnclosingType(types.erasure(outer.type));
2756                 }
2757                 if (c == outer) {
2758                     member.flags_field = flags;
2759                     enterMember(c, member);
2760                 }
2761             }
2762         }
2763     }
2764 
2765     /** Read a class definition from the bytes in buf.


2779             if (majorVersion == (maxMajor + 1))
2780                 log.warning(Warnings.BigMajorVersion(currentClassFile,
2781                                                      majorVersion,
2782                                                      maxMajor));
2783             else
2784                 throw badClassFile("wrong.version",
2785                                    Integer.toString(majorVersion),
2786                                    Integer.toString(minorVersion),
2787                                    Integer.toString(maxMajor),
2788                                    Integer.toString(maxMinor));
2789         }
2790 
2791         if (minorVersion == ClassFile.PREVIEW_MINOR_VERSION) {
2792             if (!preview.isEnabled()) {
2793                 log.error(preview.disabledError(currentClassFile, majorVersion));
2794             } else {
2795                 preview.warnPreview(c.classfile, majorVersion);
2796             }
2797         }
2798 
2799         indexPool();

2800         if (signatureBuffer.length < bp) {
2801             int ns = Integer.highestOneBit(bp) << 1;
2802             signatureBuffer = new byte[ns];
2803         }
2804         readClass(c);
2805     }
2806 
2807     public void readClassFile(ClassSymbol c) {
2808         currentOwner = c;
2809         currentClassFile = c.classfile;
2810         warnedAttrs.clear();
2811         filling = true;
2812         target = null;
2813         repeatable = null;
2814         try {
2815             bp = 0;
2816             buf = readInputStream(buf, c.classfile.openInputStream());

2817             readClassBuffer(c);
2818             if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) {
2819                 List<Type> missing = missingTypeVariables;
2820                 List<Type> found = foundTypeVariables;
2821                 missingTypeVariables = List.nil();
2822                 foundTypeVariables = List.nil();
2823                 interimUses = List.nil();
2824                 interimProvides = List.nil();
2825                 filling = false;
2826                 ClassType ct = (ClassType)currentOwner.type;
2827                 ct.supertype_field =
2828                     types.subst(ct.supertype_field, missing, found);
2829                 ct.interfaces_field =
2830                     types.subst(ct.interfaces_field, missing, found);
2831                 ct.typarams_field =
2832                     types.substBounds(ct.typarams_field, missing, found);
2833                 for (List<Type> types = ct.typarams_field; types.nonEmpty(); types = types.tail) {
2834                     types.head.tsym.type = types.head;
2835                 }
2836             } else if (missingTypeVariables.isEmpty() !=


2850                     Assert.check(currentModule.isCompleted());
2851                     currentModule.usesProvidesCompleter =
2852                             new UsesProvidesCompleter(currentModule, interimUses, interimProvides);
2853                 } else {
2854                     currentModule.uses = List.nil();
2855                     currentModule.provides = List.nil();
2856                 }
2857             }
2858         } catch (IOException | ClosedFileSystemException ex) {
2859             throw badClassFile("unable.to.access.file", ex.toString());
2860         } catch (ArrayIndexOutOfBoundsException ex) {
2861             throw badClassFile("bad.class.file", c.flatname);
2862         } finally {
2863             interimUses = List.nil();
2864             interimProvides = List.nil();
2865             missingTypeVariables = List.nil();
2866             foundTypeVariables = List.nil();
2867             filling = false;
2868         }
2869     }
2870     // where
2871         private static byte[] readInputStream(byte[] buf, InputStream s) throws IOException {
2872             try {
2873                 buf = ensureCapacity(buf, s.available());
2874                 int r = s.read(buf);
2875                 int bp = 0;
2876                 while (r != -1) {
2877                     bp += r;
2878                     buf = ensureCapacity(buf, bp);
2879                     r = s.read(buf, bp, buf.length - bp);
2880                 }
2881                 return buf;
2882             } finally {
2883                 try {
2884                     s.close();
2885                 } catch (IOException e) {
2886                     /* Ignore any errors, as this stream may have already
2887                      * thrown a related exception which is the one that
2888                      * should be reported.
2889                      */
2890                 }
2891             }
2892         }
2893         /*
2894          * ensureCapacity will increase the buffer as needed, taking note that
2895          * the new buffer will always be greater than the needed and never
2896          * exactly equal to the needed size or bp. If equal then the read (above)
2897          * will infinitely loop as buf.length - bp == 0.
2898          */
2899         private static byte[] ensureCapacity(byte[] buf, int needed) {
2900             if (buf.length <= needed) {
2901                 byte[] old = buf;
2902                 buf = new byte[Integer.highestOneBit(needed) << 1];
2903                 System.arraycopy(old, 0, buf, 0, old.length);
2904             }
2905             return buf;
2906         }
2907 
2908     /** We can only read a single class file at a time; this
2909      *  flag keeps track of when we are currently reading a class
2910      *  file.
2911      */
2912     public boolean filling = false;
2913 
2914 /************************************************************************
2915  * Adjusting flags
2916  ***********************************************************************/
2917 
2918     long adjustFieldFlags(long flags) {
2919         return flags;
2920     }
2921 
2922     long adjustMethodFlags(long flags) {
2923         if ((flags & ACC_BRIDGE) != 0) {
2924             flags &= ~ACC_BRIDGE;
2925             flags |= BRIDGE;
2926         }


3073                     theTarget = deproxy.deproxyCompound(target);
3074                 }
3075 
3076                 if (repeatable != null) {
3077                     deproxy = new AnnotationDeproxy(proxyOn);
3078                     theRepeatable = deproxy.deproxyCompound(repeatable);
3079                 }
3080             } catch (Exception e) {
3081                 throw new CompletionFailure(sym,
3082                                             () -> ClassReader.this.diagFactory.fragment(Fragments.ExceptionMessage(e.getMessage())),
3083                                             dcfh);
3084             }
3085 
3086             sym.getAnnotationTypeMetadata().setTarget(theTarget);
3087             sym.getAnnotationTypeMetadata().setRepeatable(theRepeatable);
3088         }
3089     }
3090 
3091     private class ProxyType extends Type {
3092 
3093         private final byte[] content;
3094 
3095         public ProxyType(byte[] content) {
3096             super(syms.noSymbol, TypeMetadata.EMPTY);
3097             this.content = content;
3098         }
3099 
3100         @Override
3101         public TypeTag getTag() {
3102             return TypeTag.NONE;
3103         }
3104 
3105         @Override
3106         public Type cloneWithMetadata(TypeMetadata metadata) {
3107             throw new UnsupportedOperationException();
3108         }
3109 
3110         public Type resolve() {
3111             return sigToType(content, 0, content.length);
3112         }
3113 
3114         @Override @DefinedBy(Api.LANGUAGE_MODEL)
3115         public String toString() {
3116             return "<ProxyType>";
3117         }
3118 
3119     }
3120 
3121     private static final class InterimUsesDirective {
3122         public final Name service;
3123 
3124         public InterimUsesDirective(Name service) {
3125             this.service = service;
3126         }
3127 
3128     }
3129 
3130     private static final class InterimProvidesDirective {
3131         public final Name service;


< prev index next >