src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java
changeset 48344 89f6aa26fd6c
parent 48081 89829dd3cc54
child 48430 68c6f57c40d4
equal deleted inserted replaced
48343:d4329843abf4 48344:89f6aa26fd6c
   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             },
  1569 
  1593 
  1570 /************************************************************************
  1594 /************************************************************************
  1571  * Reading Java-language annotations
  1595  * Reading Java-language annotations
  1572  ***********************************************************************/
  1596  ***********************************************************************/
  1573 
  1597 
       
  1598     /**
       
  1599      * Save annotations.
       
  1600      */
       
  1601     List<CompoundAnnotationProxy> readAnnotations() {
       
  1602         int numAttributes = nextChar();
       
  1603         ListBuffer<CompoundAnnotationProxy> annotations = new ListBuffer<>();
       
  1604         for (int i = 0; i < numAttributes; i++) {
       
  1605             annotations.append(readCompoundAnnotation());
       
  1606         }
       
  1607         return annotations.toList();
       
  1608     }
       
  1609 
  1574     /** Attach annotations.
  1610     /** Attach annotations.
  1575      */
  1611      */
  1576     void attachAnnotations(final Symbol sym) {
  1612     void attachAnnotations(final Symbol sym) {
  1577         int numAttributes = nextChar();
  1613         attachAnnotations(sym, readAnnotations());
  1578         if (numAttributes != 0) {
  1614     }
  1579             ListBuffer<CompoundAnnotationProxy> proxies = new ListBuffer<>();
  1615 
  1580             for (int i = 0; i<numAttributes; i++) {
  1616     /**
  1581                 CompoundAnnotationProxy proxy = readCompoundAnnotation();
  1617      * Attach annotations.
  1582                 if (proxy.type.tsym == syms.proprietaryType.tsym)
  1618      */
  1583                     sym.flags_field |= PROPRIETARY;
  1619     void attachAnnotations(final Symbol sym, List<CompoundAnnotationProxy> annotations) {
  1584                 else if (proxy.type.tsym == syms.profileType.tsym) {
  1620         if (annotations.isEmpty()) {
  1585                     if (profile != Profile.DEFAULT) {
  1621             return;
  1586                         for (Pair<Name,Attribute> v: proxy.values) {
  1622         }
  1587                             if (v.fst == names.value && v.snd instanceof Attribute.Constant) {
  1623         ListBuffer<CompoundAnnotationProxy> proxies = new ListBuffer<>();
  1588                                 Attribute.Constant c = (Attribute.Constant) v.snd;
  1624         for (CompoundAnnotationProxy proxy : annotations) {
  1589                                 if (c.type == syms.intType && ((Integer) c.value) > profile.value) {
  1625             if (proxy.type.tsym == syms.proprietaryType.tsym)
  1590                                     sym.flags_field |= NOT_IN_PROFILE;
  1626                 sym.flags_field |= PROPRIETARY;
  1591                                 }
  1627             else if (proxy.type.tsym == syms.profileType.tsym) {
       
  1628                 if (profile != Profile.DEFAULT) {
       
  1629                     for (Pair<Name, Attribute> v : proxy.values) {
       
  1630                         if (v.fst == names.value && v.snd instanceof Attribute.Constant) {
       
  1631                             Attribute.Constant c = (Attribute.Constant)v.snd;
       
  1632                             if (c.type == syms.intType && ((Integer)c.value) > profile.value) {
       
  1633                                 sym.flags_field |= NOT_IN_PROFILE;
  1592                             }
  1634                             }
  1593                         }
  1635                         }
  1594                     }
  1636                     }
  1595                 } else {
  1637                 }
  1596                     if (proxy.type.tsym == syms.annotationTargetType.tsym) {
  1638             } else {
  1597                         target = proxy;
  1639                 if (proxy.type.tsym == syms.annotationTargetType.tsym) {
  1598                     } else if (proxy.type.tsym == syms.repeatableType.tsym) {
  1640                     target = proxy;
  1599                         repeatable = proxy;
  1641                 } else if (proxy.type.tsym == syms.repeatableType.tsym) {
  1600                     } else if (proxy.type.tsym == syms.deprecatedType.tsym) {
  1642                     repeatable = proxy;
  1601                         sym.flags_field |= (DEPRECATED | DEPRECATED_ANNOTATION);
  1643                 } else if (proxy.type.tsym == syms.deprecatedType.tsym) {
  1602                         for (Pair<Name, Attribute> v : proxy.values) {
  1644                     sym.flags_field |= (DEPRECATED | DEPRECATED_ANNOTATION);
  1603                             if (v.fst == names.forRemoval && v.snd instanceof Attribute.Constant) {
  1645                     for (Pair<Name, Attribute> v : proxy.values) {
  1604                                 Attribute.Constant c = (Attribute.Constant) v.snd;
  1646                         if (v.fst == names.forRemoval && v.snd instanceof Attribute.Constant) {
  1605                                 if (c.type == syms.booleanType && ((Integer) c.value) != 0) {
  1647                             Attribute.Constant c = (Attribute.Constant)v.snd;
  1606                                     sym.flags_field |= DEPRECATED_REMOVAL;
  1648                             if (c.type == syms.booleanType && ((Integer)c.value) != 0) {
  1607                                 }
  1649                                 sym.flags_field |= DEPRECATED_REMOVAL;
  1608                             }
  1650                             }
  1609                         }
  1651                         }
  1610                     }
  1652                     }
  1611 
  1653                 }
  1612                     proxies.append(proxy);
  1654                 proxies.append(proxy);
  1613                 }
  1655             }
  1614             }
  1656         }
  1615             annotate.normal(new AnnotationCompleter(sym, proxies.toList()));
  1657         annotate.normal(new AnnotationCompleter(sym, proxies.toList()));
  1616         }
  1658     }
  1617     }
  1659 
  1618 
  1660     /** Read parameter annotations.
  1619     /** Attach parameter annotations.
  1661      */
  1620      */
  1662     void readParameterAnnotations(Symbol meth) {
  1621     void attachParameterAnnotations(final Symbol method) {
       
  1622         final MethodSymbol meth = (MethodSymbol)method;
       
  1623         int numParameters = buf[bp++] & 0xFF;
  1663         int numParameters = buf[bp++] & 0xFF;
  1624         List<VarSymbol> parameters = meth.params();
  1664         if (parameterAnnotations == null) {
  1625         int pnum = 0;
  1665             parameterAnnotations = new ParameterAnnotations[numParameters];
  1626         while (parameters.tail != null) {
  1666         } else if (parameterAnnotations.length != numParameters) {
  1627             attachAnnotations(parameters.head);
       
  1628             parameters = parameters.tail;
       
  1629             pnum++;
       
  1630         }
       
  1631         if (pnum != numParameters) {
       
  1632             throw badClassFile("bad.runtime.invisible.param.annotations", meth);
  1667             throw badClassFile("bad.runtime.invisible.param.annotations", meth);
       
  1668         }
       
  1669         for (int pnum = 0; pnum < numParameters; pnum++) {
       
  1670             if (parameterAnnotations[pnum] == null) {
       
  1671                 parameterAnnotations[pnum] = new ParameterAnnotations();
       
  1672             }
       
  1673             parameterAnnotations[pnum].add(readAnnotations());
  1633         }
  1674         }
  1634     }
  1675     }
  1635 
  1676 
  1636     void attachTypeAnnotations(final Symbol sym) {
  1677     void attachTypeAnnotations(final Symbol sym) {
  1637         int numAttributes = nextChar();
  1678         int numAttributes = nextChar();
  2392         try {
  2433         try {
  2393             readMemberAttrs(m);
  2434             readMemberAttrs(m);
  2394         } finally {
  2435         } finally {
  2395             currentOwner = prevOwner;
  2436             currentOwner = prevOwner;
  2396         }
  2437         }
  2397         if (saveParameterNames)
  2438         setParameters(m, type);
  2398             setParameterNames(m, type);
       
  2399 
  2439 
  2400         if ((flags & VARARGS) != 0) {
  2440         if ((flags & VARARGS) != 0) {
  2401             final Type last = type.getParameterTypes().last();
  2441             final Type last = type.getParameterTypes().last();
  2402             if (last == null || !last.hasTag(ARRAY)) {
  2442             if (last == null || !last.hasTag(ARRAY)) {
  2403                 m.flags_field &= ~VARARGS;
  2443                 m.flags_field &= ~VARARGS;
  2446         haveParameterNameIndices = false;
  2486         haveParameterNameIndices = false;
  2447         sawMethodParameters = false;
  2487         sawMethodParameters = false;
  2448     }
  2488     }
  2449 
  2489 
  2450     /**
  2490     /**
  2451      * Set the parameter names for a symbol from the name index in the
  2491      * Set the parameters for a method symbol, including any names and
  2452      * parameterNameIndicies array. The type of the symbol may have changed
  2492      * annotations that were read.
  2453      * while reading the method attributes (see the Signature attribute).
  2493      *
  2454      * This may be because of generic information or because anonymous
  2494      * <p>The type of the symbol may have changed while reading the
  2455      * synthetic parameters were added.   The original type (as read from
  2495      * method attributes (see the Signature attribute). This may be
  2456      * the method descriptor) is used to help guess the existence of
  2496      * because of generic information or because anonymous synthetic
       
  2497      * parameters were added.   The original type (as read from the
       
  2498      * method descriptor) is used to help guess the existence of
  2457      * anonymous synthetic parameters.
  2499      * anonymous synthetic parameters.
  2458      * On completion, sym.savedParameter names will either be null (if
  2500      */
  2459      * no parameter names were found in the class file) or will be set to a
  2501     void setParameters(MethodSymbol sym, Type jvmType) {
  2460      * list of names, one per entry in sym.type.getParameterTypes, with
       
  2461      * any missing names represented by the empty name.
       
  2462      */
       
  2463     void setParameterNames(MethodSymbol sym, Type jvmType) {
       
  2464         // if no names were found in the class file, there's nothing more to do
       
  2465         if (!haveParameterNameIndices)
       
  2466             return;
       
  2467         // If we get parameter names from MethodParameters, then we
  2502         // If we get parameter names from MethodParameters, then we
  2468         // don't need to skip.
  2503         // don't need to skip.
  2469         int firstParam = 0;
  2504         int firstParam = 0;
  2470         if (!sawMethodParameters) {
  2505         if (!sawMethodParameters) {
  2471             firstParam = ((sym.flags() & STATIC) == 0) ? 1 : 0;
  2506             firstParam = ((sym.flags() & STATIC) == 0) ? 1 : 0;
  2472             // the code in readMethod may have skipped the first
  2507             // the code in readMethod may have skipped the first
  2473             // parameter when setting up the MethodType. If so, we
  2508             // parameter when setting up the MethodType. If so, we
  2474             // make a corresponding allowance here for the position of
  2509             // make a corresponding allowance here for the position of
  2475             // the first parameter.  Note that this assumes the
  2510             // the first parameter.  Note that this assumes the
  2476             // skipped parameter has a width of 1 -- i.e. it is not
  2511             // skipped parameter has a width of 1 -- i.e. it is not
  2477         // a double width type (long or double.)
  2512             // a double width type (long or double.)
  2478         if (sym.name == names.init && currentOwner.hasOuterInstance()) {
  2513             if (sym.name == names.init && currentOwner.hasOuterInstance()) {
  2479             // Sometimes anonymous classes don't have an outer
  2514                 // Sometimes anonymous classes don't have an outer
  2480             // instance, however, there is no reliable way to tell so
  2515                 // instance, however, there is no reliable way to tell so
  2481             // we never strip this$n
  2516                 // we never strip this$n
  2482             if (!currentOwner.name.isEmpty())
  2517                 if (!currentOwner.name.isEmpty())
  2483                 firstParam += 1;
  2518                     firstParam += 1;
  2484         }
  2519             }
  2485 
  2520 
  2486         if (sym.type != jvmType) {
  2521             if (sym.type != jvmType) {
  2487                 // reading the method attributes has caused the
  2522                 // reading the method attributes has caused the
  2488                 // symbol's type to be changed. (i.e. the Signature
  2523                 // symbol's type to be changed. (i.e. the Signature
  2489                 // attribute.)  This may happen if there are hidden
  2524                 // attribute.)  This may happen if there are hidden
  2490                 // (synthetic) parameters in the descriptor, but not
  2525                 // (synthetic) parameters in the descriptor, but not
  2491                 // in the Signature.  The position of these hidden
  2526                 // in the Signature.  The position of these hidden
  2492                 // parameters is unspecified; for now, assume they are
  2527                 // parameters is unspecified; for now, assume they are
  2493                 // at the beginning, and so skip over them. The
  2528                 // at the beginning, and so skip over them. The
  2494                 // primary case for this is two hidden parameters
  2529                 // primary case for this is two hidden parameters
  2495                 // passed into Enum constructors.
  2530                 // passed into Enum constructors.
  2496             int skip = Code.width(jvmType.getParameterTypes())
  2531                 int skip = Code.width(jvmType.getParameterTypes())
  2497                     - Code.width(sym.type.getParameterTypes());
  2532                         - Code.width(sym.type.getParameterTypes());
  2498             firstParam += skip;
  2533                 firstParam += skip;
  2499         }
  2534             }
  2500         }
  2535         }
  2501         List<Name> paramNames = List.nil();
  2536         List<Name> paramNames = List.nil();
  2502         int index = firstParam;
  2537         ListBuffer<VarSymbol> params = new ListBuffer<>();
       
  2538         int nameIndex = firstParam;
       
  2539         int annotationIndex = 0;
  2503         for (Type t: sym.type.getParameterTypes()) {
  2540         for (Type t: sym.type.getParameterTypes()) {
  2504             int nameIdx = (index < parameterNameIndices.length
  2541             Name name = parameterName(nameIndex, paramNames);
  2505                     ? parameterNameIndices[index] : 0);
       
  2506             Name name = nameIdx == 0 ? names.empty : readName(nameIdx);
       
  2507             paramNames = paramNames.prepend(name);
  2542             paramNames = paramNames.prepend(name);
  2508             index += sawMethodParameters ? 1 : Code.width(t);
  2543             VarSymbol param = new VarSymbol(PARAMETER, name, t, sym);
  2509         }
  2544             params.append(param);
  2510         sym.savedParameterNames = paramNames.reverse();
  2545             if (parameterAnnotations != null) {
       
  2546                 ParameterAnnotations annotations = parameterAnnotations[annotationIndex];
       
  2547                 if (annotations != null && annotations.proxies != null
       
  2548                         && !annotations.proxies.isEmpty()) {
       
  2549                     annotate.normal(new AnnotationCompleter(param, annotations.proxies));
       
  2550                 }
       
  2551             }
       
  2552             nameIndex += sawMethodParameters ? 1 : Code.width(t);
       
  2553             annotationIndex++;
       
  2554         }
       
  2555         if (parameterAnnotations != null && parameterAnnotations.length != annotationIndex) {
       
  2556             throw badClassFile("bad.runtime.invisible.param.annotations", sym);
       
  2557         }
       
  2558         Assert.checkNull(sym.params);
       
  2559         sym.params = params.toList();
       
  2560         parameterAnnotations = null;
       
  2561         parameterNameIndices = null;
       
  2562     }
       
  2563 
       
  2564 
       
  2565     // Returns the name for the parameter at position 'index', either using
       
  2566     // names read from the MethodParameters, or by synthesizing a name that
       
  2567     // is not on the 'exclude' list.
       
  2568     private Name parameterName(int index, List<Name> exclude) {
       
  2569         if (parameterNameIndices != null && index < parameterNameIndices.length
       
  2570                 && parameterNameIndices[index] != 0) {
       
  2571             return readName(parameterNameIndices[index]);
       
  2572         }
       
  2573         String prefix = "arg";
       
  2574         while (true) {
       
  2575             Name argName = names.fromString(prefix + exclude.size());
       
  2576             if (!exclude.contains(argName))
       
  2577                 return argName;
       
  2578             prefix += "$";
       
  2579         }
  2511     }
  2580     }
  2512 
  2581 
  2513     /**
  2582     /**
  2514      * skip n bytes
  2583      * skip n bytes
  2515      */
  2584      */