langtools/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java
changeset 15385 ee1eebe7e210
parent 15031 c4fad55a5681
child 15718 8e54c8e43d38
child 15725 e0516b913894
equal deleted inserted replaced
15384:5a8d00abf076 15385:ee1eebe7e210
     1 /*
     1 /*
     2  * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   349         return (char)(((buf[bp++] & 0xFF) << 8) + (buf[bp++] & 0xFF));
   349         return (char)(((buf[bp++] & 0xFF) << 8) + (buf[bp++] & 0xFF));
   350     }
   350     }
   351 
   351 
   352     /** Read a byte.
   352     /** Read a byte.
   353      */
   353      */
   354     byte nextByte() {
   354     int nextByte() {
   355         return buf[bp++];
   355         return buf[bp++] & 0xFF;
   356     }
   356     }
   357 
   357 
   358     /** Read an integer.
   358     /** Read an integer.
   359      */
   359      */
   360     int nextInt() {
   360     int nextInt() {
  1170                     if (allowVarargs)
  1170                     if (allowVarargs)
  1171                         sym.flags_field |= VARARGS;
  1171                         sym.flags_field |= VARARGS;
  1172                 }
  1172                 }
  1173             },
  1173             },
  1174 
  1174 
       
  1175             new AttributeReader(names.RuntimeVisibleTypeAnnotations, V52, CLASS_OR_MEMBER_ATTRIBUTE) {
       
  1176                 protected void read(Symbol sym, int attrLen) {
       
  1177                     attachTypeAnnotations(sym);
       
  1178                 }
       
  1179             },
       
  1180 
       
  1181             new AttributeReader(names.RuntimeInvisibleTypeAnnotations, V52, CLASS_OR_MEMBER_ATTRIBUTE) {
       
  1182                 protected void read(Symbol sym, int attrLen) {
       
  1183                     attachTypeAnnotations(sym);
       
  1184                 }
       
  1185             },
       
  1186 
       
  1187 
  1175             // The following attributes for a Code attribute are not currently handled
  1188             // The following attributes for a Code attribute are not currently handled
  1176             // StackMapTable
  1189             // StackMapTable
  1177             // SourceDebugExtension
  1190             // SourceDebugExtension
  1178             // LineNumberTable
  1191             // LineNumberTable
  1179             // LocalVariableTypeTable
  1192             // LocalVariableTypeTable
  1379         if (pnum != numParameters) {
  1392         if (pnum != numParameters) {
  1380             throw badClassFile("bad.runtime.invisible.param.annotations", meth);
  1393             throw badClassFile("bad.runtime.invisible.param.annotations", meth);
  1381         }
  1394         }
  1382     }
  1395     }
  1383 
  1396 
       
  1397     void attachTypeAnnotations(final Symbol sym) {
       
  1398         int numAttributes = nextChar();
       
  1399         if (numAttributes != 0) {
       
  1400             ListBuffer<TypeAnnotationProxy> proxies =
       
  1401                 ListBuffer.lb();
       
  1402             for (int i = 0; i < numAttributes; i++)
       
  1403                 proxies.append(readTypeAnnotation());
       
  1404             annotate.normal(new TypeAnnotationCompleter(sym, proxies.toList()));
       
  1405         }
       
  1406     }
       
  1407 
  1384     /** Attach the default value for an annotation element.
  1408     /** Attach the default value for an annotation element.
  1385      */
  1409      */
  1386     void attachAnnotationDefault(final Symbol sym) {
  1410     void attachAnnotationDefault(final Symbol sym) {
  1387         final MethodSymbol meth = (MethodSymbol)sym; // only on methods
  1411         final MethodSymbol meth = (MethodSymbol)sym; // only on methods
  1388         final Attribute value = readAttributeValue();
  1412         final Attribute value = readAttributeValue();
  1423             Name name = readName(nextChar());
  1447             Name name = readName(nextChar());
  1424             Attribute value = readAttributeValue();
  1448             Attribute value = readAttributeValue();
  1425             pairs.append(new Pair<Name,Attribute>(name, value));
  1449             pairs.append(new Pair<Name,Attribute>(name, value));
  1426         }
  1450         }
  1427         return new CompoundAnnotationProxy(t, pairs.toList());
  1451         return new CompoundAnnotationProxy(t, pairs.toList());
       
  1452     }
       
  1453 
       
  1454     TypeAnnotationProxy readTypeAnnotation() {
       
  1455         TypeAnnotationPosition position = readPosition();
       
  1456         CompoundAnnotationProxy proxy = readCompoundAnnotation();
       
  1457 
       
  1458         return new TypeAnnotationProxy(proxy, position);
       
  1459     }
       
  1460 
       
  1461     TypeAnnotationPosition readPosition() {
       
  1462         int tag = nextByte(); // TargetType tag is a byte
       
  1463 
       
  1464         if (!TargetType.isValidTargetTypeValue(tag))
       
  1465             throw this.badClassFile("bad.type.annotation.value", String.format("0x%02X", tag));
       
  1466 
       
  1467         TypeAnnotationPosition position = new TypeAnnotationPosition();
       
  1468         TargetType type = TargetType.fromTargetTypeValue(tag);
       
  1469 
       
  1470         position.type = type;
       
  1471 
       
  1472         switch (type) {
       
  1473         // type cast
       
  1474         case CAST:
       
  1475         // instanceof
       
  1476         case INSTANCEOF:
       
  1477         // new expression
       
  1478         case NEW:
       
  1479             position.offset = nextChar();
       
  1480             break;
       
  1481         // local variable
       
  1482         case LOCAL_VARIABLE:
       
  1483         // resource variable
       
  1484         case RESOURCE_VARIABLE:
       
  1485             int table_length = nextChar();
       
  1486             position.lvarOffset = new int[table_length];
       
  1487             position.lvarLength = new int[table_length];
       
  1488             position.lvarIndex = new int[table_length];
       
  1489 
       
  1490             for (int i = 0; i < table_length; ++i) {
       
  1491                 position.lvarOffset[i] = nextChar();
       
  1492                 position.lvarLength[i] = nextChar();
       
  1493                 position.lvarIndex[i] = nextChar();
       
  1494             }
       
  1495             break;
       
  1496         // exception parameter
       
  1497         case EXCEPTION_PARAMETER:
       
  1498             position.exception_index = nextByte();
       
  1499             break;
       
  1500         // method receiver
       
  1501         case METHOD_RECEIVER:
       
  1502             // Do nothing
       
  1503             break;
       
  1504         // type parameter
       
  1505         case CLASS_TYPE_PARAMETER:
       
  1506         case METHOD_TYPE_PARAMETER:
       
  1507             position.parameter_index = nextByte();
       
  1508             break;
       
  1509         // type parameter bound
       
  1510         case CLASS_TYPE_PARAMETER_BOUND:
       
  1511         case METHOD_TYPE_PARAMETER_BOUND:
       
  1512             position.parameter_index = nextByte();
       
  1513             position.bound_index = nextByte();
       
  1514             break;
       
  1515         // class extends or implements clause
       
  1516         case CLASS_EXTENDS:
       
  1517             position.type_index = nextChar();
       
  1518             break;
       
  1519         // throws
       
  1520         case THROWS:
       
  1521             position.type_index = nextChar();
       
  1522             break;
       
  1523         // method parameter
       
  1524         case METHOD_FORMAL_PARAMETER:
       
  1525             position.parameter_index = nextByte();
       
  1526             break;
       
  1527         // method/constructor/reference type argument
       
  1528         case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:
       
  1529         case METHOD_INVOCATION_TYPE_ARGUMENT:
       
  1530         case METHOD_REFERENCE_TYPE_ARGUMENT:
       
  1531             position.offset = nextChar();
       
  1532             position.type_index = nextByte();
       
  1533             break;
       
  1534         // We don't need to worry about these
       
  1535         case METHOD_RETURN:
       
  1536         case FIELD:
       
  1537             break;
       
  1538         // lambda formal parameter
       
  1539         case LAMBDA_FORMAL_PARAMETER:
       
  1540             position.parameter_index = nextByte();
       
  1541             break;
       
  1542         case UNKNOWN:
       
  1543             throw new AssertionError("jvm.ClassReader: UNKNOWN target type should never occur!");
       
  1544         default:
       
  1545             throw new AssertionError("jvm.ClassReader: Unknown target type for position: " + position);
       
  1546         }
       
  1547 
       
  1548         { // See whether there is location info and read it
       
  1549             int len = nextByte();
       
  1550             ListBuffer<Integer> loc = ListBuffer.lb();
       
  1551             for (int i = 0; i < len * TypeAnnotationPosition.TypePathEntry.bytesPerEntry; ++i)
       
  1552                 loc = loc.append(nextByte());
       
  1553             position.location = TypeAnnotationPosition.getTypePathFromBinary(loc.toList());
       
  1554         }
       
  1555 
       
  1556         return position;
  1428     }
  1557     }
  1429 
  1558 
  1430     Attribute readAttributeValue() {
  1559     Attribute readAttributeValue() {
  1431         char c = (char) buf[bp++];
  1560         char c = (char) buf[bp++];
  1432         switch (c) {
  1561         switch (c) {
  1746             try {
  1875             try {
  1747                 currentClassFile = classFile;
  1876                 currentClassFile = classFile;
  1748                 Annotations annotations = sym.annotations;
  1877                 Annotations annotations = sym.annotations;
  1749                 List<Attribute.Compound> newList = deproxyCompoundList(l);
  1878                 List<Attribute.Compound> newList = deproxyCompoundList(l);
  1750                 if (annotations.pendingCompletion()) {
  1879                 if (annotations.pendingCompletion()) {
  1751                     annotations.setAttributes(newList);
  1880                     annotations.setDeclarationAttributes(newList);
  1752                 } else {
  1881                 } else {
  1753                     annotations.append(newList);
  1882                     annotations.append(newList);
  1754                 }
  1883                 }
       
  1884             } finally {
       
  1885                 currentClassFile = previousClassFile;
       
  1886             }
       
  1887         }
       
  1888     }
       
  1889 
       
  1890     class TypeAnnotationCompleter extends AnnotationCompleter {
       
  1891 
       
  1892         List<TypeAnnotationProxy> proxies;
       
  1893 
       
  1894         TypeAnnotationCompleter(Symbol sym,
       
  1895                 List<TypeAnnotationProxy> proxies) {
       
  1896             super(sym, List.<CompoundAnnotationProxy>nil());
       
  1897             this.proxies = proxies;
       
  1898         }
       
  1899 
       
  1900         List<Attribute.TypeCompound> deproxyTypeCompoundList(List<TypeAnnotationProxy> proxies) {
       
  1901             ListBuffer<Attribute.TypeCompound> buf = ListBuffer.lb();
       
  1902             for (TypeAnnotationProxy proxy: proxies) {
       
  1903                 Attribute.Compound compound = deproxyCompound(proxy.compound);
       
  1904                 Attribute.TypeCompound typeCompound = new Attribute.TypeCompound(compound, proxy.position);
       
  1905                 buf.add(typeCompound);
       
  1906             }
       
  1907             return buf.toList();
       
  1908         }
       
  1909 
       
  1910         @Override
       
  1911         public void enterAnnotation() {
       
  1912             JavaFileObject previousClassFile = currentClassFile;
       
  1913             try {
       
  1914                 currentClassFile = classFile;
       
  1915                 List<Attribute.TypeCompound> newList = deproxyTypeCompoundList(proxies);
       
  1916                 sym.annotations.setTypeAttributes(newList.prependList(sym.getRawTypeAttributes()));
  1755             } finally {
  1917             } finally {
  1756                 currentClassFile = previousClassFile;
  1918                 currentClassFile = previousClassFile;
  1757             }
  1919             }
  1758         }
  1920         }
  1759     }
  1921     }