< prev index next >

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

Print this page




  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;
 124 
 125     /** The symbol table. */
 126     Symtab syms;
 127 


 247     protected ClassReader(Context context) {
 248         context.put(classReaderKey, this);
 249         annotate = Annotate.instance(context);
 250         names = Names.instance(context);
 251         syms = Symtab.instance(context);
 252         types = Types.instance(context);
 253         fileManager = context.get(JavaFileManager.class);
 254         if (fileManager == null)
 255             throw new AssertionError("FileManager initialization error");
 256         diagFactory = JCDiagnostic.Factory.instance(context);
 257         dcfh = DeferredCompletionFailureHandler.instance(context);
 258 
 259         log = Log.instance(context);
 260 
 261         Options options = Options.instance(context);
 262         verbose         = options.isSet(Option.VERBOSE);
 263 
 264         Source source = Source.instance(context);
 265         preview = Preview.instance(context);
 266         allowModules     = Feature.MODULES.allowedInSource(source);


 267 
 268         saveParameterNames = options.isSet(PARAMETERS);
 269 
 270         profile = Profile.instance(context);
 271 
 272         typevars = WriteableScope.create(syms.noSymbol);
 273 
 274         lintClassfile = Lint.instance(context).isEnabled(LintCategory.CLASSFILE);
 275 
 276         initAttributeReaders();
 277     }
 278 
 279     /** Add member to class unless it is synthetic.
 280      */
 281     private void enterMember(ClassSymbol c, Symbol sym) {
 282         // Synthetic members are not entered -- reason lost to history (optimization?).
 283         // Lambda methods must be entered because they may have inner classes (which reference them)
 284         if ((sym.flags_field & (SYNTHETIC|BRIDGE)) != SYNTHETIC || sym.name.startsWith(names.lambda))
 285             c.members_field.enter(sym);
 286     }


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)


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());




  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     /** Switch: allow sealed
 109      */
 110     boolean allowSealedTypes;
 111 
 112     /** Switch: allow records
 113      */
 114     boolean allowRecords;
 115 
 116    /** Lint option: warn about classfile issues
 117      */
 118     boolean lintClassfile;
 119 
 120     /** Switch: preserve parameter names from the variable table.
 121      */
 122     public boolean saveParameterNames;
 123 
 124     /**
 125      * The currently selected profile.
 126      */
 127     public final Profile profile;
 128 
 129     /** The log to use for verbose output
 130      */
 131     final Log log;
 132 
 133     /** The symbol table. */
 134     Symtab syms;
 135 


 255     protected ClassReader(Context context) {
 256         context.put(classReaderKey, this);
 257         annotate = Annotate.instance(context);
 258         names = Names.instance(context);
 259         syms = Symtab.instance(context);
 260         types = Types.instance(context);
 261         fileManager = context.get(JavaFileManager.class);
 262         if (fileManager == null)
 263             throw new AssertionError("FileManager initialization error");
 264         diagFactory = JCDiagnostic.Factory.instance(context);
 265         dcfh = DeferredCompletionFailureHandler.instance(context);
 266 
 267         log = Log.instance(context);
 268 
 269         Options options = Options.instance(context);
 270         verbose         = options.isSet(Option.VERBOSE);
 271 
 272         Source source = Source.instance(context);
 273         preview = Preview.instance(context);
 274         allowModules     = Feature.MODULES.allowedInSource(source);
 275         allowSealedTypes = Feature.SEALED.allowedInSource(source);
 276         allowRecords = Feature.RECORDS.allowedInSource(source);
 277 
 278         saveParameterNames = options.isSet(PARAMETERS);
 279 
 280         profile = Profile.instance(context);
 281 
 282         typevars = WriteableScope.create(syms.noSymbol);
 283 
 284         lintClassfile = Lint.instance(context).isEnabled(LintCategory.CLASSFILE);
 285 
 286         initAttributeReaders();
 287     }
 288 
 289     /** Add member to class unless it is synthetic.
 290      */
 291     private void enterMember(ClassSymbol c, Symbol sym) {
 292         // Synthetic members are not entered -- reason lost to history (optimization?).
 293         // Lambda methods must be entered because they may have inner classes (which reference them)
 294         if ((sym.flags_field & (SYNTHETIC|BRIDGE)) != SYNTHETIC || sym.name.startsWith(names.lambda))
 295             c.members_field.enter(sym);
 296     }


1174                     }
1175                 }
1176 
1177                 private Name classNameMapper(byte[] arr, int offset, int length) {
1178                     return names.fromUtf(ClassFile.internalize(arr, offset, length));
1179                 }
1180             },
1181 
1182             new AttributeReader(names.ModuleResolution, V53, CLASS_ATTRIBUTE) {
1183                 @Override
1184                 protected boolean accepts(AttributeKind kind) {
1185                     return super.accepts(kind) && allowModules;
1186                 }
1187                 protected void read(Symbol sym, int attrLen) {
1188                     if (sym.kind == TYP && sym.owner.kind == MDL) {
1189                         ModuleSymbol msym = (ModuleSymbol) sym.owner;
1190                         msym.resolutionFlags.addAll(readModuleResolutionFlags(nextChar()));
1191                     }
1192                 }
1193             },
1194 
1195             new AttributeReader(names.PermittedSubtypes, V57, CLASS_ATTRIBUTE) {
1196                 @Override
1197                 protected boolean accepts(AttributeKind kind) {
1198                     return super.accepts(kind) && allowSealedTypes;
1199                 }
1200                 protected void read(Symbol sym, int attrLen) {
1201                     if (sym.kind == TYP) {
1202                         ClassType sealed = (ClassType)sym.type;
1203                         ListBuffer<Type> subtypes = new ListBuffer<>();
1204                         int numberOfPermittedSubtypes = nextChar();
1205                         for (int i = 0; i < numberOfPermittedSubtypes; i++) {
1206                             Type ct = poolReader.getClass(nextChar()).erasure(types);
1207                             subtypes.add(ct);
1208                         }
1209                         sealed.permitted = subtypes.toList();
1210                     }
1211                 }
1212             },
1213 
1214             new AttributeReader(names.Record, V57, CLASS_ATTRIBUTE) {
1215                 @Override
1216                 protected boolean accepts(AttributeKind kind) {
1217                     return super.accepts(kind) && allowRecords;
1218                 }
1219                 protected void read(Symbol sym, int attrLen) {
1220                     if (sym.kind == TYP) {
1221                         sym.flags_field |= RECORD;
1222                     }
1223                     bp = bp + attrLen;
1224                 }
1225             }
1226         };
1227 
1228         for (AttributeReader r: readers)
1229             attributeReaders.put(r.name, r);
1230     }
1231 
1232     protected void readEnclosingMethodAttr(Symbol sym) {
1233         // sym is a nested class with an "Enclosing Method" attribute
1234         // remove sym from it's current owners scope and place it in
1235         // the scope specified by the attribute
1236         sym.owner.members().remove(sym);
1237         ClassSymbol self = (ClassSymbol)sym;
1238         ClassSymbol c = poolReader.getClass(nextChar());
1239         NameAndType nt = optPoolEntry(nextChar(), poolReader::getNameAndType, null);
1240 
1241         if (c.members_field == null || c.kind != TYP)
1242             throw badClassFile("bad.enclosing.class", self, c);
1243 
1244         MethodSymbol m = findMethod(nt, c.members_field, self.flags());
1245         if (nt != null && m == null)


2465                         Integer.toString(majorVersion),
2466                         Integer.toString(minorVersion));
2467             }
2468             c.flags_field = flags;
2469             currentModule = (ModuleSymbol) c.owner;
2470             int this_class = nextChar();
2471             // temp, no check on this_class
2472         }
2473 
2474         // class attributes must be read before class
2475         // skip ahead to read class attributes
2476         int startbp = bp;
2477         nextChar();
2478         char interfaceCount = nextChar();
2479         bp += interfaceCount * 2;
2480         char fieldCount = nextChar();
2481         for (int i = 0; i < fieldCount; i++) skipMember();
2482         char methodCount = nextChar();
2483         for (int i = 0; i < methodCount; i++) skipMember();
2484         readClassAttrs(c);
2485 
2486         if (ct.permitted != null && !ct.permitted.isEmpty()) {
2487             c.flags_field |= SEALED;
2488             c.flags_field &= ~FINAL;
2489         }
2490 
2491         // reset and read rest of classinfo
2492         bp = startbp;
2493         int n = nextChar();
2494         if ((flags & MODULE) != 0 && n > 0) {
2495             throw badClassFile("module.info.invalid.super.class");
2496         }
2497         if (ct.supertype_field == null)
2498             ct.supertype_field =
2499                     optPoolEntry(n, idx -> poolReader.getClass(idx).erasure(types), Type.noType);
2500         n = nextChar();
2501         List<Type> is = List.nil();
2502         for (int i = 0; i < n; i++) {
2503             Type _inter = poolReader.getClass(nextChar()).erasure(types);
2504             is = is.prepend(_inter);
2505         }
2506         if (ct.interfaces_field == null)
2507             ct.interfaces_field = is.reverse();
2508 
2509         Assert.check(fieldCount == nextChar());


< prev index next >