< 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 
 128     Types types;
 129 
 130     /** The name table. */
 131     final Names names;
 132 
 133     /** Access to files
 134      */
 135     private final JavaFileManager fileManager;


 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     }
 287 
 288 /************************************************************************


 440             return new WildcardType(t, BoundKind.SUPER, syms.boundClass);
 441         }
 442         case 'B':
 443             sigp++;
 444             return syms.byteType;
 445         case 'C':
 446             sigp++;
 447             return syms.charType;
 448         case 'D':
 449             sigp++;
 450             return syms.doubleType;
 451         case 'F':
 452             sigp++;
 453             return syms.floatType;
 454         case 'I':
 455             sigp++;
 456             return syms.intType;
 457         case 'J':
 458             sigp++;
 459             return syms.longType;

 460         case 'L':
 461             {
 462                 // int oldsigp = sigp;
 463                 Type t = classSigToType();
 464                 if (sigp < siglimit && signature[sigp] == '.')
 465                     throw badClassFile("deprecated inner class signature syntax " +
 466                                        "(please recompile from source)");
 467                 /*
 468                 System.err.println(" decoded " +
 469                                    new String(signature, oldsigp, sigp-oldsigp) +
 470                                    " => " + t + " outer " + t.outer());
 471                 */
 472                 return t;
 473             }
 474         case 'S':
 475             sigp++;
 476             return syms.shortType;
 477         case 'V':
 478             sigp++;
 479             return syms.voidType;


 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",
 512                                Convert.utf2string(signature, sigp, 10));
 513         }
 514     }
 515 
 516     byte[] signatureBuffer = new byte[0];
 517     int sbp = 0;
 518     /** Convert class signature to type, where signature is implicit.
 519      */
 520     Type classSigToType() {
 521         if (signature[sigp] != 'L')
 522             throw badClassFile("bad.class.signature",
 523                                Convert.utf2string(signature, sigp, 10));
 524         sigp++;
 525         Type outer = Type.noType;
 526         int startSbp = sbp;
 527 
 528         while (true) {
 529             final byte c = signature[sigp++];
 530             switch (c) {
 531 
 532             case ';': {         // end
 533                 ClassSymbol t = enterClass(names.fromUtf(signatureBuffer,
 534                                                          startSbp,
 535                                                          sbp - startSbp));
 536 
 537                 try {
 538                     return (outer == Type.noType) ?
 539                             t.erasure(types) :
 540                         new ClassType(outer, List.nil(), t);
 541                 } finally {


1373         ListBuffer<CompoundAnnotationProxy> proxies = new ListBuffer<>();
1374         for (CompoundAnnotationProxy proxy : annotations) {
1375             if (proxy.type.tsym.flatName() == syms.proprietaryType.tsym.flatName())
1376                 sym.flags_field |= PROPRIETARY;
1377             else if (proxy.type.tsym.flatName() == syms.profileType.tsym.flatName()) {
1378                 if (profile != Profile.DEFAULT) {
1379                     for (Pair<Name, Attribute> v : proxy.values) {
1380                         if (v.fst == names.value && v.snd instanceof Attribute.Constant) {
1381                             Attribute.Constant c = (Attribute.Constant)v.snd;
1382                             if (c.type == syms.intType && ((Integer)c.value) > profile.value) {
1383                                 sym.flags_field |= NOT_IN_PROFILE;
1384                             }
1385                         }
1386                     }
1387                 }
1388             } else {
1389                 if (proxy.type.tsym == syms.annotationTargetType.tsym) {
1390                     target = proxy;
1391                 } else if (proxy.type.tsym == syms.repeatableType.tsym) {
1392                     repeatable = proxy;


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) {


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
2168             // won't pass the "hasOuterInstance" check above, but those that don't have an
2169             // enclosing method (i.e. from initializers) will pass that check.
2170             boolean local = !currentOwner.owner.members().includes(currentOwner, LookupKind.NON_RECURSIVE);
2171             if (!currentOwner.name.isEmpty() && !local)
2172                 type = new MethodType(adjustMethodParams(flags, type.getParameterTypes()),
2173                                       type.getReturnType(),
2174                                       type.getThrownTypes(),
2175                                       syms.methodClass);
2176         }
2177         MethodSymbol m = new MethodSymbol(flags, name, type, currentOwner);
2178         if (types.isSignaturePolymorphic(m)) {
2179             m.flags_field |= SIGNATURE_POLYMORPHIC;
2180         }
2181         if (saveParameterNames)
2182             initParameterNames(m);


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         }
2627         if ((flags & ACC_VARARGS) != 0) {
2628             flags &= ~ACC_VARARGS;
2629             flags |= VARARGS;
2630         }
2631         return flags;
2632     }
2633 
2634     long adjustClassFlags(long flags) {
2635         if ((flags & ACC_MODULE) != 0) {
2636             flags &= ~ACC_MODULE;
2637             flags |= MODULE;




2638         }
2639         return flags & ~ACC_SUPER; // SUPER and SYNCHRONIZED bits overloaded
2640     }
2641 
2642     /**
2643      * A subclass of JavaFileObject for the sourcefile attribute found in a classfile.
2644      * The attribute is only the last component of the original filename, so is unlikely
2645      * to be valid as is, so operations other than those to access the name throw
2646      * UnsupportedOperationException
2647      */
2648     private static class SourceFileObject implements JavaFileObject {
2649 
2650         /** The file's name.
2651          */
2652         private final Name name;
2653 
2654         public SourceFileObject(Name name) {
2655             this.name = name;
2656         }
2657 




  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 inline types.
 109      */
 110     boolean allowInlineTypes;
 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     /** Switch: Should javac recongnize and handle value based classes specially ?
 121      */
 122     private boolean allowValueBasedClasses;
 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 
 136     Types types;
 137 
 138     /** The name table. */
 139     final Names names;
 140 
 141     /** Access to files
 142      */
 143     private final JavaFileManager fileManager;


 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         allowInlineTypes = Feature.INLINE_TYPES.allowedInSource(source);
 276         saveParameterNames = options.isSet(PARAMETERS);
 277         allowValueBasedClasses = options.isSet("allowValueBasedClasses");
 278 
 279         profile = Profile.instance(context);
 280 
 281         typevars = WriteableScope.create(syms.noSymbol);
 282 
 283         lintClassfile = Lint.instance(context).isEnabled(LintCategory.CLASSFILE);
 284 
 285         initAttributeReaders();
 286     }
 287 
 288     /** Add member to class unless it is synthetic.
 289      */
 290     private void enterMember(ClassSymbol c, Symbol sym) {
 291         // Synthetic members are not entered -- reason lost to history (optimization?).
 292         // Lambda methods must be entered because they may have inner classes (which reference them)
 293         if ((sym.flags_field & (SYNTHETIC|BRIDGE)) != SYNTHETIC || sym.name.startsWith(names.lambda))
 294             c.members_field.enter(sym);
 295     }
 296 
 297 /************************************************************************


 449             return new WildcardType(t, BoundKind.SUPER, syms.boundClass);
 450         }
 451         case 'B':
 452             sigp++;
 453             return syms.byteType;
 454         case 'C':
 455             sigp++;
 456             return syms.charType;
 457         case 'D':
 458             sigp++;
 459             return syms.doubleType;
 460         case 'F':
 461             sigp++;
 462             return syms.floatType;
 463         case 'I':
 464             sigp++;
 465             return syms.intType;
 466         case 'J':
 467             sigp++;
 468             return syms.longType;
 469         case 'Q':
 470         case 'L':
 471             {
 472                 // int oldsigp = sigp;
 473                 Type t = classSigToType();
 474                 if (sigp < siglimit && signature[sigp] == '.')
 475                     throw badClassFile("deprecated inner class signature syntax " +
 476                                        "(please recompile from source)");
 477                 /*
 478                 System.err.println(" decoded " +
 479                                    new String(signature, oldsigp, sigp-oldsigp) +
 480                                    " => " + t + " outer " + t.outer());
 481                 */
 482                 return t;
 483             }
 484         case 'S':
 485             sigp++;
 486             return syms.shortType;
 487         case 'V':
 488             sigp++;
 489             return syms.voidType;


 511             return new MethodType(argtypes,
 512                                   restype,
 513                                   thrown.reverse(),
 514                                   syms.methodClass);
 515         case '<':
 516             typevars = typevars.dup(currentOwner);
 517             Type poly = new ForAll(sigToTypeParams(), sigToType());
 518             typevars = typevars.leave();
 519             return poly;
 520         default:
 521             throw badClassFile("bad.signature",
 522                                Convert.utf2string(signature, sigp, 10));
 523         }
 524     }
 525 
 526     byte[] signatureBuffer = new byte[0];
 527     int sbp = 0;
 528     /** Convert class signature to type, where signature is implicit.
 529      */
 530     Type classSigToType() {
 531         if (signature[sigp] != 'L' && signature[sigp] != 'Q') 
 532             throw badClassFile("bad.class.signature",
 533                                Convert.utf2string(signature, sigp, 10));
 534         sigp++;
 535         Type outer = Type.noType;
 536         int startSbp = sbp;
 537 
 538         while (true) {
 539             final byte c = signature[sigp++];
 540             switch (c) {
 541 
 542             case ';': {         // end
 543                 ClassSymbol t = enterClass(names.fromUtf(signatureBuffer,
 544                                                          startSbp,
 545                                                          sbp - startSbp));
 546 
 547                 try {
 548                     return (outer == Type.noType) ?
 549                             t.erasure(types) :
 550                         new ClassType(outer, List.nil(), t);
 551                 } finally {


1383         ListBuffer<CompoundAnnotationProxy> proxies = new ListBuffer<>();
1384         for (CompoundAnnotationProxy proxy : annotations) {
1385             if (proxy.type.tsym.flatName() == syms.proprietaryType.tsym.flatName())
1386                 sym.flags_field |= PROPRIETARY;
1387             else if (proxy.type.tsym.flatName() == syms.profileType.tsym.flatName()) {
1388                 if (profile != Profile.DEFAULT) {
1389                     for (Pair<Name, Attribute> v : proxy.values) {
1390                         if (v.fst == names.value && v.snd instanceof Attribute.Constant) {
1391                             Attribute.Constant c = (Attribute.Constant)v.snd;
1392                             if (c.type == syms.intType && ((Integer)c.value) > profile.value) {
1393                                 sym.flags_field |= NOT_IN_PROFILE;
1394                             }
1395                         }
1396                     }
1397                 }
1398             } else {
1399                 if (proxy.type.tsym == syms.annotationTargetType.tsym) {
1400                     target = proxy;
1401                 } else if (proxy.type.tsym == syms.repeatableType.tsym) {
1402                     repeatable = proxy;
1403                 } else if (allowValueBasedClasses && sym.kind == TYP && proxy.type.tsym == syms.valueBasedType.tsym) {
1404                     sym.flags_field |= VALUEBASED;
1405                 } else if (proxy.type.tsym == syms.deprecatedType.tsym) {
1406                     sym.flags_field |= (DEPRECATED | DEPRECATED_ANNOTATION);
1407                     for (Pair<Name, Attribute> v : proxy.values) {
1408                         if (v.fst == names.forRemoval && v.snd instanceof Attribute.Constant) {
1409                             Attribute.Constant c = (Attribute.Constant)v.snd;
1410                             if (c.type == syms.booleanType && ((Integer)c.value) != 0) {
1411                                 sym.flags_field |= DEPRECATED_REMOVAL;
1412                             }
1413                         }
1414                     }
1415                 }
1416                 proxies.append(proxy);
1417             }
1418         }
1419         annotate.normal(new AnnotationCompleter(sym, proxies.toList()));
1420     }
1421 
1422     /** Read parameter annotations.
1423      */
1424     void readParameterAnnotations(Symbol meth) {


2155      */
2156     MethodSymbol readMethod() {
2157         long flags = adjustMethodFlags(nextChar());
2158         Name name = poolReader.getName(nextChar());
2159         Type type = poolReader.getType(nextChar());
2160         if (currentOwner.isInterface() &&
2161                 (flags & ABSTRACT) == 0 && !name.equals(names.clinit)) {
2162             if (majorVersion > Version.V52.major ||
2163                     (majorVersion == Version.V52.major && minorVersion >= Version.V52.minor)) {
2164                 if ((flags & (STATIC | PRIVATE)) == 0) {
2165                     currentOwner.flags_field |= DEFAULT;
2166                     flags |= DEFAULT | ABSTRACT;
2167                 }
2168             } else {
2169                 //protect against ill-formed classfiles
2170                 throw badClassFile((flags & STATIC) == 0 ? "invalid.default.interface" : "invalid.static.interface",
2171                                    Integer.toString(majorVersion),
2172                                    Integer.toString(minorVersion));
2173             }
2174         }
2175         if (name == names.init && ((flags & STATIC) != 0)) {
2176             flags &= ~STATIC;
2177             type = new MethodType(type.getParameterTypes(),
2178                     syms.voidType,
2179                     type.getThrownTypes(),
2180                     syms.methodClass);
2181         }
2182         if (name == names.init && currentOwner.hasOuterInstance()) {
2183             // Sometimes anonymous classes don't have an outer
2184             // instance, however, there is no reliable way to tell so
2185             // we never strip this$n
2186             // ditto for local classes. Local classes that have an enclosing method set
2187             // won't pass the "hasOuterInstance" check above, but those that don't have an
2188             // enclosing method (i.e. from initializers) will pass that check.
2189             boolean local = !currentOwner.owner.members().includes(currentOwner, LookupKind.NON_RECURSIVE);
2190             if (!currentOwner.name.isEmpty() && !local)
2191                 type = new MethodType(adjustMethodParams(flags, type.getParameterTypes()),
2192                                       type.getReturnType(),
2193                                       type.getThrownTypes(),
2194                                       syms.methodClass);
2195         }
2196         MethodSymbol m = new MethodSymbol(flags, name, type, currentOwner);
2197         if (types.isSignaturePolymorphic(m)) {
2198             m.flags_field |= SIGNATURE_POLYMORPHIC;
2199         }
2200         if (saveParameterNames)
2201             initParameterNames(m);


2637     long adjustFieldFlags(long flags) {
2638         return flags;
2639     }
2640 
2641     long adjustMethodFlags(long flags) {
2642         if ((flags & ACC_BRIDGE) != 0) {
2643             flags &= ~ACC_BRIDGE;
2644             flags |= BRIDGE;
2645         }
2646         if ((flags & ACC_VARARGS) != 0) {
2647             flags &= ~ACC_VARARGS;
2648             flags |= VARARGS;
2649         }
2650         return flags;
2651     }
2652 
2653     long adjustClassFlags(long flags) {
2654         if ((flags & ACC_MODULE) != 0) {
2655             flags &= ~ACC_MODULE;
2656             flags |= MODULE;
2657         }
2658         if ((flags & ACC_VALUE) != 0) {
2659             flags &= ~ACC_VALUE;
2660             flags |= allowInlineTypes ? VALUE : allowValueBasedClasses ? VALUEBASED : 0;
2661         }
2662         return flags & ~ACC_SUPER; // SUPER and SYNCHRONIZED bits overloaded
2663     }
2664 
2665     /**
2666      * A subclass of JavaFileObject for the sourcefile attribute found in a classfile.
2667      * The attribute is only the last component of the original filename, so is unlikely
2668      * to be valid as is, so operations other than those to access the name throw
2669      * UnsupportedOperationException
2670      */
2671     private static class SourceFileObject implements JavaFileObject {
2672 
2673         /** The file's name.
2674          */
2675         private final Name name;
2676 
2677         public SourceFileObject(Name name) {
2678             this.name = name;
2679         }
2680 


< prev index next >