src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java
changeset 48430 68c6f57c40d4
parent 48427 b08405cc467a
parent 48344 89f6aa26fd6c
child 48826 c4d9d1b08e2e
equal deleted inserted replaced
48429:e9a564028f2f 48430:68c6f57c40d4
   189 
   189 
   190     /** A table to hold the constant pool indices for method parameter
   190     /** A table to hold the constant pool indices for method parameter
   191      * names, as given in LocalVariableTable attributes.
   191      * names, as given in LocalVariableTable attributes.
   192      */
   192      */
   193     int[] parameterNameIndices;
   193     int[] parameterNameIndices;
       
   194 
       
   195     /**
       
   196      * A table to hold annotations for method parameters.
       
   197      */
       
   198     ParameterAnnotations[] parameterAnnotations;
       
   199 
       
   200     /**
       
   201      * A holder for parameter annotations.
       
   202      */
       
   203     static class ParameterAnnotations {
       
   204         List<CompoundAnnotationProxy> proxies;
       
   205 
       
   206         void add(List<CompoundAnnotationProxy> newAnnotations) {
       
   207             if (proxies == null) {
       
   208                 proxies = newAnnotations;
       
   209             } else {
       
   210                 proxies = proxies.prependList(newAnnotations);
       
   211             }
       
   212         }
       
   213     }
   194 
   214 
   195     /**
   215     /**
   196      * Whether or not any parameter names have been found.
   216      * Whether or not any parameter names have been found.
   197      */
   217      */
   198     boolean haveParameterNameIndices;
   218     boolean haveParameterNameIndices;
  1216                 }
  1236                 }
  1217             },
  1237             },
  1218 
  1238 
  1219             new AttributeReader(names.RuntimeInvisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1239             new AttributeReader(names.RuntimeInvisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1220                 protected void read(Symbol sym, int attrLen) {
  1240                 protected void read(Symbol sym, int attrLen) {
  1221                     attachParameterAnnotations(sym);
  1241                     readParameterAnnotations(sym);
  1222                 }
  1242                 }
  1223             },
  1243             },
  1224 
  1244 
  1225             new AttributeReader(names.RuntimeVisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1245             new AttributeReader(names.RuntimeVisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1226                 protected void read(Symbol sym, int attrLen) {
  1246                 protected void read(Symbol sym, int attrLen) {
  1228                 }
  1248                 }
  1229             },
  1249             },
  1230 
  1250 
  1231             new AttributeReader(names.RuntimeVisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1251             new AttributeReader(names.RuntimeVisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
  1232                 protected void read(Symbol sym, int attrLen) {
  1252                 protected void read(Symbol sym, int attrLen) {
  1233                     attachParameterAnnotations(sym);
  1253                     readParameterAnnotations(sym);
  1234                 }
  1254                 }
  1235             },
  1255             },
  1236 
  1256 
  1237             // additional "legacy" v49 attributes, superceded by flags
  1257             // additional "legacy" v49 attributes, superceded by flags
  1238 
  1258 
  1286                     if (saveParameterNames) {
  1306                     if (saveParameterNames) {
  1287                         sawMethodParameters = true;
  1307                         sawMethodParameters = true;
  1288                         int numEntries = nextByte();
  1308                         int numEntries = nextByte();
  1289                         parameterNameIndices = new int[numEntries];
  1309                         parameterNameIndices = new int[numEntries];
  1290                         haveParameterNameIndices = true;
  1310                         haveParameterNameIndices = true;
       
  1311                         int index = 0;
  1291                         for (int i = 0; i < numEntries; i++) {
  1312                         for (int i = 0; i < numEntries; i++) {
  1292                             int nameIndex = nextChar();
  1313                             int nameIndex = nextChar();
  1293                             int flags = nextChar();
  1314                             int flags = nextChar();
  1294                             parameterNameIndices[i] = nameIndex;
  1315                             if ((flags & (Flags.MANDATED | Flags.SYNTHETIC)) != 0) {
       
  1316                                 continue;
       
  1317                             }
       
  1318                             parameterNameIndices[index++] = nameIndex;
  1295                         }
  1319                         }
  1296                     }
  1320                     }
  1297                     bp = newbp;
  1321                     bp = newbp;
  1298                 }
  1322                 }
  1299             },
  1323             },
  1578 
  1602 
  1579 /************************************************************************
  1603 /************************************************************************
  1580  * Reading Java-language annotations
  1604  * Reading Java-language annotations
  1581  ***********************************************************************/
  1605  ***********************************************************************/
  1582 
  1606 
       
  1607     /**
       
  1608      * Save annotations.
       
  1609      */
       
  1610     List<CompoundAnnotationProxy> readAnnotations() {
       
  1611         int numAttributes = nextChar();
       
  1612         ListBuffer<CompoundAnnotationProxy> annotations = new ListBuffer<>();
       
  1613         for (int i = 0; i < numAttributes; i++) {
       
  1614             annotations.append(readCompoundAnnotation());
       
  1615         }
       
  1616         return annotations.toList();
       
  1617     }
       
  1618 
  1583     /** Attach annotations.
  1619     /** Attach annotations.
  1584      */
  1620      */
  1585     void attachAnnotations(final Symbol sym) {
  1621     void attachAnnotations(final Symbol sym) {
  1586         int numAttributes = nextChar();
  1622         attachAnnotations(sym, readAnnotations());
  1587         if (numAttributes != 0) {
  1623     }
  1588             ListBuffer<CompoundAnnotationProxy> proxies = new ListBuffer<>();
  1624 
  1589             for (int i = 0; i<numAttributes; i++) {
  1625     /**
  1590                 CompoundAnnotationProxy proxy = readCompoundAnnotation();
  1626      * Attach annotations.
  1591                 if (proxy.type.tsym == syms.proprietaryType.tsym)
  1627      */
  1592                     sym.flags_field |= PROPRIETARY;
  1628     void attachAnnotations(final Symbol sym, List<CompoundAnnotationProxy> annotations) {
  1593                 else if (proxy.type.tsym == syms.profileType.tsym) {
  1629         if (annotations.isEmpty()) {
  1594                     if (profile != Profile.DEFAULT) {
  1630             return;
  1595                         for (Pair<Name,Attribute> v: proxy.values) {
  1631         }
  1596                             if (v.fst == names.value && v.snd instanceof Attribute.Constant) {
  1632         ListBuffer<CompoundAnnotationProxy> proxies = new ListBuffer<>();
  1597                                 Attribute.Constant c = (Attribute.Constant) v.snd;
  1633         for (CompoundAnnotationProxy proxy : annotations) {
  1598                                 if (c.type == syms.intType && ((Integer) c.value) > profile.value) {
  1634             if (proxy.type.tsym == syms.proprietaryType.tsym)
  1599                                     sym.flags_field |= NOT_IN_PROFILE;
  1635                 sym.flags_field |= PROPRIETARY;
  1600                                 }
  1636             else if (proxy.type.tsym == syms.profileType.tsym) {
       
  1637                 if (profile != Profile.DEFAULT) {
       
  1638                     for (Pair<Name, Attribute> v : proxy.values) {
       
  1639                         if (v.fst == names.value && v.snd instanceof Attribute.Constant) {
       
  1640                             Attribute.Constant c = (Attribute.Constant)v.snd;
       
  1641                             if (c.type == syms.intType && ((Integer)c.value) > profile.value) {
       
  1642                                 sym.flags_field |= NOT_IN_PROFILE;
  1601                             }
  1643                             }
  1602                         }
  1644                         }
  1603                     }
  1645                     }
  1604                 } else {
  1646                 }
  1605                     if (proxy.type.tsym == syms.annotationTargetType.tsym) {
  1647             } else {
  1606                         target = proxy;
  1648                 if (proxy.type.tsym == syms.annotationTargetType.tsym) {
  1607                     } else if (proxy.type.tsym == syms.repeatableType.tsym) {
  1649                     target = proxy;
  1608                         repeatable = proxy;
  1650                 } else if (proxy.type.tsym == syms.repeatableType.tsym) {
  1609                     } else if (proxy.type.tsym == syms.deprecatedType.tsym) {
  1651                     repeatable = proxy;
  1610                         sym.flags_field |= (DEPRECATED | DEPRECATED_ANNOTATION);
  1652                 } else if (proxy.type.tsym == syms.deprecatedType.tsym) {
  1611                         for (Pair<Name, Attribute> v : proxy.values) {
  1653                     sym.flags_field |= (DEPRECATED | DEPRECATED_ANNOTATION);
  1612                             if (v.fst == names.forRemoval && v.snd instanceof Attribute.Constant) {
  1654                     for (Pair<Name, Attribute> v : proxy.values) {
  1613                                 Attribute.Constant c = (Attribute.Constant) v.snd;
  1655                         if (v.fst == names.forRemoval && v.snd instanceof Attribute.Constant) {
  1614                                 if (c.type == syms.booleanType && ((Integer) c.value) != 0) {
  1656                             Attribute.Constant c = (Attribute.Constant)v.snd;
  1615                                     sym.flags_field |= DEPRECATED_REMOVAL;
  1657                             if (c.type == syms.booleanType && ((Integer)c.value) != 0) {
  1616                                 }
  1658                                 sym.flags_field |= DEPRECATED_REMOVAL;
  1617                             }
  1659                             }
  1618                         }
  1660                         }
  1619                     }
  1661                     }
  1620 
  1662                 }
  1621                     proxies.append(proxy);
  1663                 proxies.append(proxy);
  1622                 }
  1664             }
  1623             }
  1665         }
  1624             annotate.normal(new AnnotationCompleter(sym, proxies.toList()));
  1666         annotate.normal(new AnnotationCompleter(sym, proxies.toList()));
  1625         }
  1667     }
  1626     }
  1668 
  1627 
  1669     /** Read parameter annotations.
  1628     /** Attach parameter annotations.
  1670      */
  1629      */
  1671     void readParameterAnnotations(Symbol meth) {
  1630     void attachParameterAnnotations(final Symbol method) {
       
  1631         final MethodSymbol meth = (MethodSymbol)method;
       
  1632         int numParameters = buf[bp++] & 0xFF;
  1672         int numParameters = buf[bp++] & 0xFF;
  1633         List<VarSymbol> parameters = meth.params();
  1673         if (parameterAnnotations == null) {
  1634         int pnum = 0;
  1674             parameterAnnotations = new ParameterAnnotations[numParameters];
  1635         while (parameters.tail != null) {
  1675         } else if (parameterAnnotations.length != numParameters) {
  1636             attachAnnotations(parameters.head);
       
  1637             parameters = parameters.tail;
       
  1638             pnum++;
       
  1639         }
       
  1640         if (pnum != numParameters) {
       
  1641             throw badClassFile("bad.runtime.invisible.param.annotations", meth);
  1676             throw badClassFile("bad.runtime.invisible.param.annotations", meth);
       
  1677         }
       
  1678         for (int pnum = 0; pnum < numParameters; pnum++) {
       
  1679             if (parameterAnnotations[pnum] == null) {
       
  1680                 parameterAnnotations[pnum] = new ParameterAnnotations();
       
  1681             }
       
  1682             parameterAnnotations[pnum].add(readAnnotations());
  1642         }
  1683         }
  1643     }
  1684     }
  1644 
  1685 
  1645     void attachTypeAnnotations(final Symbol sym) {
  1686     void attachTypeAnnotations(final Symbol sym) {
  1646         int numAttributes = nextChar();
  1687         int numAttributes = nextChar();
  2401         try {
  2442         try {
  2402             readMemberAttrs(m);
  2443             readMemberAttrs(m);
  2403         } finally {
  2444         } finally {
  2404             currentOwner = prevOwner;
  2445             currentOwner = prevOwner;
  2405         }
  2446         }
  2406         if (saveParameterNames)
  2447         setParameters(m, type);
  2407             setParameterNames(m, type);
       
  2408 
  2448 
  2409         if ((flags & VARARGS) != 0) {
  2449         if ((flags & VARARGS) != 0) {
  2410             final Type last = type.getParameterTypes().last();
  2450             final Type last = type.getParameterTypes().last();
  2411             if (last == null || !last.hasTag(ARRAY)) {
  2451             if (last == null || !last.hasTag(ARRAY)) {
  2412                 m.flags_field &= ~VARARGS;
  2452                 m.flags_field &= ~VARARGS;
  2455         haveParameterNameIndices = false;
  2495         haveParameterNameIndices = false;
  2456         sawMethodParameters = false;
  2496         sawMethodParameters = false;
  2457     }
  2497     }
  2458 
  2498 
  2459     /**
  2499     /**
  2460      * Set the parameter names for a symbol from the name index in the
  2500      * Set the parameters for a method symbol, including any names and
  2461      * parameterNameIndicies array. The type of the symbol may have changed
  2501      * annotations that were read.
  2462      * while reading the method attributes (see the Signature attribute).
  2502      *
  2463      * This may be because of generic information or because anonymous
  2503      * <p>The type of the symbol may have changed while reading the
  2464      * synthetic parameters were added.   The original type (as read from
  2504      * method attributes (see the Signature attribute). This may be
  2465      * the method descriptor) is used to help guess the existence of
  2505      * because of generic information or because anonymous synthetic
       
  2506      * parameters were added.   The original type (as read from the
       
  2507      * method descriptor) is used to help guess the existence of
  2466      * anonymous synthetic parameters.
  2508      * anonymous synthetic parameters.
  2467      * On completion, sym.savedParameter names will either be null (if
  2509      */
  2468      * no parameter names were found in the class file) or will be set to a
  2510     void setParameters(MethodSymbol sym, Type jvmType) {
  2469      * list of names, one per entry in sym.type.getParameterTypes, with
       
  2470      * any missing names represented by the empty name.
       
  2471      */
       
  2472     void setParameterNames(MethodSymbol sym, Type jvmType) {
       
  2473         // if no names were found in the class file, there's nothing more to do
       
  2474         if (!haveParameterNameIndices)
       
  2475             return;
       
  2476         // If we get parameter names from MethodParameters, then we
  2511         // If we get parameter names from MethodParameters, then we
  2477         // don't need to skip.
  2512         // don't need to skip.
  2478         int firstParam = 0;
  2513         int firstParam = 0;
  2479         if (!sawMethodParameters) {
  2514         if (!sawMethodParameters) {
  2480             firstParam = ((sym.flags() & STATIC) == 0) ? 1 : 0;
  2515             firstParam = ((sym.flags() & STATIC) == 0) ? 1 : 0;
  2481             // the code in readMethod may have skipped the first
  2516             // the code in readMethod may have skipped the first
  2482             // parameter when setting up the MethodType. If so, we
  2517             // parameter when setting up the MethodType. If so, we
  2483             // make a corresponding allowance here for the position of
  2518             // make a corresponding allowance here for the position of
  2484             // the first parameter.  Note that this assumes the
  2519             // the first parameter.  Note that this assumes the
  2485             // skipped parameter has a width of 1 -- i.e. it is not
  2520             // skipped parameter has a width of 1 -- i.e. it is not
  2486         // a double width type (long or double.)
  2521             // a double width type (long or double.)
  2487         if (sym.name == names.init && currentOwner.hasOuterInstance()) {
  2522             if (sym.name == names.init && currentOwner.hasOuterInstance()) {
  2488             // Sometimes anonymous classes don't have an outer
  2523                 // Sometimes anonymous classes don't have an outer
  2489             // instance, however, there is no reliable way to tell so
  2524                 // instance, however, there is no reliable way to tell so
  2490             // we never strip this$n
  2525                 // we never strip this$n
  2491             if (!currentOwner.name.isEmpty())
  2526                 if (!currentOwner.name.isEmpty())
  2492                 firstParam += 1;
  2527                     firstParam += 1;
  2493         }
  2528             }
  2494 
  2529 
  2495         if (sym.type != jvmType) {
  2530             if (sym.type != jvmType) {
  2496                 // reading the method attributes has caused the
  2531                 // reading the method attributes has caused the
  2497                 // symbol's type to be changed. (i.e. the Signature
  2532                 // symbol's type to be changed. (i.e. the Signature
  2498                 // attribute.)  This may happen if there are hidden
  2533                 // attribute.)  This may happen if there are hidden
  2499                 // (synthetic) parameters in the descriptor, but not
  2534                 // (synthetic) parameters in the descriptor, but not
  2500                 // in the Signature.  The position of these hidden
  2535                 // in the Signature.  The position of these hidden
  2501                 // parameters is unspecified; for now, assume they are
  2536                 // parameters is unspecified; for now, assume they are
  2502                 // at the beginning, and so skip over them. The
  2537                 // at the beginning, and so skip over them. The
  2503                 // primary case for this is two hidden parameters
  2538                 // primary case for this is two hidden parameters
  2504                 // passed into Enum constructors.
  2539                 // passed into Enum constructors.
  2505             int skip = Code.width(jvmType.getParameterTypes())
  2540                 int skip = Code.width(jvmType.getParameterTypes())
  2506                     - Code.width(sym.type.getParameterTypes());
  2541                         - Code.width(sym.type.getParameterTypes());
  2507             firstParam += skip;
  2542                 firstParam += skip;
  2508         }
  2543             }
  2509         }
  2544         }
  2510         List<Name> paramNames = List.nil();
  2545         List<Name> paramNames = List.nil();
  2511         int index = firstParam;
  2546         ListBuffer<VarSymbol> params = new ListBuffer<>();
       
  2547         int nameIndex = firstParam;
       
  2548         int annotationIndex = 0;
  2512         for (Type t: sym.type.getParameterTypes()) {
  2549         for (Type t: sym.type.getParameterTypes()) {
  2513             int nameIdx = (index < parameterNameIndices.length
  2550             Name name = parameterName(nameIndex, paramNames);
  2514                     ? parameterNameIndices[index] : 0);
       
  2515             Name name = nameIdx == 0 ? names.empty : readName(nameIdx);
       
  2516             paramNames = paramNames.prepend(name);
  2551             paramNames = paramNames.prepend(name);
  2517             index += sawMethodParameters ? 1 : Code.width(t);
  2552             VarSymbol param = new VarSymbol(PARAMETER, name, t, sym);
  2518         }
  2553             params.append(param);
  2519         sym.savedParameterNames = paramNames.reverse();
  2554             if (parameterAnnotations != null) {
       
  2555                 ParameterAnnotations annotations = parameterAnnotations[annotationIndex];
       
  2556                 if (annotations != null && annotations.proxies != null
       
  2557                         && !annotations.proxies.isEmpty()) {
       
  2558                     annotate.normal(new AnnotationCompleter(param, annotations.proxies));
       
  2559                 }
       
  2560             }
       
  2561             nameIndex += sawMethodParameters ? 1 : Code.width(t);
       
  2562             annotationIndex++;
       
  2563         }
       
  2564         if (parameterAnnotations != null && parameterAnnotations.length != annotationIndex) {
       
  2565             throw badClassFile("bad.runtime.invisible.param.annotations", sym);
       
  2566         }
       
  2567         Assert.checkNull(sym.params);
       
  2568         sym.params = params.toList();
       
  2569         parameterAnnotations = null;
       
  2570         parameterNameIndices = null;
       
  2571     }
       
  2572 
       
  2573 
       
  2574     // Returns the name for the parameter at position 'index', either using
       
  2575     // names read from the MethodParameters, or by synthesizing a name that
       
  2576     // is not on the 'exclude' list.
       
  2577     private Name parameterName(int index, List<Name> exclude) {
       
  2578         if (parameterNameIndices != null && index < parameterNameIndices.length
       
  2579                 && parameterNameIndices[index] != 0) {
       
  2580             return readName(parameterNameIndices[index]);
       
  2581         }
       
  2582         String prefix = "arg";
       
  2583         while (true) {
       
  2584             Name argName = names.fromString(prefix + exclude.size());
       
  2585             if (!exclude.contains(argName))
       
  2586                 return argName;
       
  2587             prefix += "$";
       
  2588         }
  2520     }
  2589     }
  2521 
  2590 
  2522     /**
  2591     /**
  2523      * skip n bytes
  2592      * skip n bytes
  2524      */
  2593      */