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

Print this page




1452             Name name = readName(nextChar());
1453             Attribute value = readAttributeValue();
1454             pairs.append(new Pair<>(name, value));
1455         }
1456         return new CompoundAnnotationProxy(t, pairs.toList());
1457     }
1458 
1459     TypeAnnotationProxy readTypeAnnotation() {
1460         TypeAnnotationPosition position = readPosition();
1461         CompoundAnnotationProxy proxy = readCompoundAnnotation();
1462 
1463         return new TypeAnnotationProxy(proxy, position);
1464     }
1465 
1466     TypeAnnotationPosition readPosition() {
1467         int tag = nextByte(); // TargetType tag is a byte
1468 
1469         if (!TargetType.isValidTargetTypeValue(tag))
1470             throw this.badClassFile("bad.type.annotation.value", String.format("0x%02X", tag));
1471 
1472         TypeAnnotationPosition position = new TypeAnnotationPosition();
1473         TargetType type = TargetType.fromTargetTypeValue(tag);
1474 
1475         position.type = type;
1476 
1477         switch (type) {
1478         // instanceof
1479         case INSTANCEOF:






1480         // new expression
1481         case NEW:






1482         // constructor/method reference receiver
1483         case CONSTRUCTOR_REFERENCE:
1484         case METHOD_REFERENCE:
1485             position.offset = nextChar();
1486             break;










1487         // local variable
1488         case LOCAL_VARIABLE:















1489         // resource variable
1490         case RESOURCE_VARIABLE:
1491             int table_length = nextChar();



1492             position.lvarOffset = new int[table_length];
1493             position.lvarLength = new int[table_length];
1494             position.lvarIndex = new int[table_length];
1495 
1496             for (int i = 0; i < table_length; ++i) {
1497                 position.lvarOffset[i] = nextChar();
1498                 position.lvarLength[i] = nextChar();
1499                 position.lvarIndex[i] = nextChar();
1500             }
1501             break;

1502         // exception parameter
1503         case EXCEPTION_PARAMETER:
1504             position.exception_index = nextChar();
1505             break;




1506         // method receiver
1507         case METHOD_RECEIVER:
1508             // Do nothing
1509             break;
1510         // type parameter
1511         case CLASS_TYPE_PARAMETER:
1512         case METHOD_TYPE_PARAMETER:
1513             position.parameter_index = nextByte();
1514             break;






1515         // type parameter bound
1516         case CLASS_TYPE_PARAMETER_BOUND:
1517         case METHOD_TYPE_PARAMETER_BOUND:
1518             position.parameter_index = nextByte();
1519             position.bound_index = nextByte();
1520             break;









1521         // class extends or implements clause
1522         case CLASS_EXTENDS:
1523             position.type_index = nextChar();
1524             break;


1525         // throws
1526         case THROWS:
1527             position.type_index = nextChar();
1528             break;


1529         // method parameter
1530         case METHOD_FORMAL_PARAMETER:
1531             position.parameter_index = nextByte();
1532             break;


1533         // type cast
1534         case CAST:







1535         // method/constructor/reference type argument
1536         case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:
1537         case METHOD_INVOCATION_TYPE_ARGUMENT:
1538         case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT:
1539         case METHOD_REFERENCE_TYPE_ARGUMENT:
1540             position.offset = nextChar();
1541             position.type_index = nextByte();
1542             break;

























1543         // We don't need to worry about these
1544         case METHOD_RETURN:

1545         case FIELD:
1546             break;
1547         case UNKNOWN:
1548             throw new AssertionError("jvm.ClassReader: UNKNOWN target type should never occur!");
1549         default:
1550             throw new AssertionError("jvm.ClassReader: Unknown target type for position: " + position);

1551         }
1552 
1553         { // See whether there is location info and read it
1554             int len = nextByte();
1555             ListBuffer<Integer> loc = new ListBuffer<>();
1556             for (int i = 0; i < len * TypeAnnotationPosition.TypePathEntry.bytesPerEntry; ++i)
1557                 loc = loc.append(nextByte());
1558             position.location = TypeAnnotationPosition.getTypePathFromBinary(loc.toList());
1559         }
1560 
1561         return position;

1562     }
1563 
1564     Attribute readAttributeValue() {
1565         char c = (char) buf[bp++];
1566         switch (c) {
1567         case 'B':
1568             return new Attribute.Constant(syms.byteType, readPool(nextChar()));
1569         case 'C':
1570             return new Attribute.Constant(syms.charType, readPool(nextChar()));
1571         case 'D':
1572             return new Attribute.Constant(syms.doubleType, readPool(nextChar()));
1573         case 'F':
1574             return new Attribute.Constant(syms.floatType, readPool(nextChar()));
1575         case 'I':
1576             return new Attribute.Constant(syms.intType, readPool(nextChar()));
1577         case 'J':
1578             return new Attribute.Constant(syms.longType, readPool(nextChar()));
1579         case 'S':
1580             return new Attribute.Constant(syms.shortType, readPool(nextChar()));
1581         case 'Z':




1452             Name name = readName(nextChar());
1453             Attribute value = readAttributeValue();
1454             pairs.append(new Pair<>(name, value));
1455         }
1456         return new CompoundAnnotationProxy(t, pairs.toList());
1457     }
1458 
1459     TypeAnnotationProxy readTypeAnnotation() {
1460         TypeAnnotationPosition position = readPosition();
1461         CompoundAnnotationProxy proxy = readCompoundAnnotation();
1462 
1463         return new TypeAnnotationProxy(proxy, position);
1464     }
1465 
1466     TypeAnnotationPosition readPosition() {
1467         int tag = nextByte(); // TargetType tag is a byte
1468 
1469         if (!TargetType.isValidTargetTypeValue(tag))
1470             throw this.badClassFile("bad.type.annotation.value", String.format("0x%02X", tag));
1471 

1472         TargetType type = TargetType.fromTargetTypeValue(tag);
1473 


1474         switch (type) {
1475         // instanceof
1476         case INSTANCEOF: {
1477             final int offset = nextChar();
1478             final TypeAnnotationPosition position =
1479                 TypeAnnotationPosition.instanceOf(readTypePath());
1480             position.offset = offset;
1481             return position;
1482         }
1483         // new expression
1484         case NEW: {
1485             final int offset = nextChar();
1486             final TypeAnnotationPosition position =
1487                 TypeAnnotationPosition.newObj(readTypePath());
1488             position.offset = offset;
1489             return position;
1490         }
1491         // constructor/method reference receiver
1492         case CONSTRUCTOR_REFERENCE: {
1493             final int offset = nextChar();
1494             final TypeAnnotationPosition position =
1495                 TypeAnnotationPosition.constructorRef(readTypePath());
1496             position.offset = offset;
1497             return position;
1498         }
1499         case METHOD_REFERENCE: {
1500             final int offset = nextChar();
1501             final TypeAnnotationPosition position =
1502                 TypeAnnotationPosition.methodRef(readTypePath());
1503             position.offset = offset;
1504             return position;
1505         }
1506         // local variable
1507         case LOCAL_VARIABLE: {
1508             final int table_length = nextChar();
1509             final TypeAnnotationPosition position =
1510                 TypeAnnotationPosition.localVariable(readTypePath());
1511 
1512             position.lvarOffset = new int[table_length];
1513             position.lvarLength = new int[table_length];
1514             position.lvarIndex = new int[table_length];
1515 
1516             for (int i = 0; i < table_length; ++i) {
1517                 position.lvarOffset[i] = nextChar();
1518                 position.lvarLength[i] = nextChar();
1519                 position.lvarIndex[i] = nextChar();
1520             }
1521             return position;
1522         }
1523         // resource variable
1524         case RESOURCE_VARIABLE: {
1525             final int table_length = nextChar();
1526             final TypeAnnotationPosition position =
1527                 TypeAnnotationPosition.resourceVariable(readTypePath());
1528 
1529             position.lvarOffset = new int[table_length];
1530             position.lvarLength = new int[table_length];
1531             position.lvarIndex = new int[table_length];
1532 
1533             for (int i = 0; i < table_length; ++i) {
1534                 position.lvarOffset[i] = nextChar();
1535                 position.lvarLength[i] = nextChar();
1536                 position.lvarIndex[i] = nextChar();
1537             }
1538             return position;
1539         }
1540         // exception parameter
1541         case EXCEPTION_PARAMETER: {
1542             final int exception_index = nextChar();
1543             final TypeAnnotationPosition position =
1544                 TypeAnnotationPosition.exceptionParameter(readTypePath());
1545             position.exception_index = exception_index;
1546             return position;
1547         }
1548         // method receiver
1549         case METHOD_RECEIVER:
1550             return TypeAnnotationPosition.methodReceiver(readTypePath());

1551         // type parameter
1552         case CLASS_TYPE_PARAMETER: {
1553             final int parameter_index = nextByte();
1554             return TypeAnnotationPosition
1555                 .typeParameter(readTypePath(), parameter_index);
1556         }
1557         case METHOD_TYPE_PARAMETER: {
1558             final int parameter_index = nextByte();
1559             return TypeAnnotationPosition
1560                 .methodTypeParameter(readTypePath(), parameter_index);
1561         }
1562         // type parameter bound
1563         case CLASS_TYPE_PARAMETER_BOUND: {
1564             final int parameter_index = nextByte();
1565             final int bound_index = nextByte();
1566             return TypeAnnotationPosition
1567                 .typeParameterBound(readTypePath(), parameter_index,
1568                                     bound_index);
1569         }
1570         case METHOD_TYPE_PARAMETER_BOUND: {
1571             final int parameter_index = nextByte();
1572             final int bound_index = nextByte();
1573             return TypeAnnotationPosition
1574                 .methodTypeParameterBound(readTypePath(), parameter_index,
1575                                           bound_index);
1576         }
1577         // class extends or implements clause
1578         case CLASS_EXTENDS: {
1579             final int type_index = nextChar();
1580             return TypeAnnotationPosition.classExtends(readTypePath(),
1581                                                        type_index);
1582         }
1583         // throws
1584         case THROWS: {
1585             final int type_index = nextChar();
1586             return TypeAnnotationPosition.methodThrows(readTypePath(),
1587                                                        type_index);
1588         }
1589         // method parameter
1590         case METHOD_FORMAL_PARAMETER: {
1591             final int parameter_index = nextByte();
1592             return TypeAnnotationPosition.methodParameter(readTypePath(),
1593                                                           parameter_index);
1594         }
1595         // type cast
1596         case CAST: {
1597             final int offset = nextChar();
1598             final int type_index = nextByte();
1599             final TypeAnnotationPosition position =
1600                 TypeAnnotationPosition.typeCast(readTypePath(), type_index);
1601             position.offset = offset;
1602             return position;
1603         }
1604         // method/constructor/reference type argument
1605         case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT: {
1606             final int offset = nextChar();
1607             final int type_index = nextByte();
1608             final TypeAnnotationPosition position = TypeAnnotationPosition
1609                 .constructorInvocationTypeArg(readTypePath(), type_index);
1610             position.offset = offset;
1611             return position;
1612         }
1613         case METHOD_INVOCATION_TYPE_ARGUMENT: {
1614             final int offset = nextChar();
1615             final int type_index = nextByte();
1616             final TypeAnnotationPosition position = TypeAnnotationPosition
1617                 .methodInvocationTypeArg(readTypePath(), type_index);
1618             position.offset = offset;
1619             return position;
1620         }
1621         case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT: {
1622             final int offset = nextChar();
1623             final int type_index = nextByte();
1624             final TypeAnnotationPosition position = TypeAnnotationPosition
1625                 .constructorRefTypeArg(readTypePath(), type_index);
1626             position.offset = offset;
1627             return position;
1628         }
1629         case METHOD_REFERENCE_TYPE_ARGUMENT: {
1630             final int offset = nextChar();
1631             final int type_index = nextByte();
1632             final TypeAnnotationPosition position = TypeAnnotationPosition
1633                 .methodRefTypeArg(readTypePath(), type_index);
1634             position.offset = offset;
1635             return position;
1636         }
1637         // We don't need to worry about these
1638         case METHOD_RETURN:
1639             return TypeAnnotationPosition.methodReturn(readTypePath());
1640         case FIELD:
1641             return TypeAnnotationPosition.field(readTypePath());
1642         case UNKNOWN:
1643             throw new AssertionError("jvm.ClassReader: UNKNOWN target type should never occur!");
1644         default:
1645             throw new AssertionError("jvm.ClassReader: Unknown target type for position: " + type);
1646         }
1647     }
1648 
1649     List<TypeAnnotationPosition.TypePathEntry> readTypePath() {
1650         int len = nextByte();
1651         ListBuffer<Integer> loc = new ListBuffer<>();
1652         for (int i = 0; i < len * TypeAnnotationPosition.TypePathEntry.bytesPerEntry; ++i)
1653             loc = loc.append(nextByte());


1654 
1655         return TypeAnnotationPosition.getTypePathFromBinary(loc.toList());
1656 
1657     }
1658 
1659     Attribute readAttributeValue() {
1660         char c = (char) buf[bp++];
1661         switch (c) {
1662         case 'B':
1663             return new Attribute.Constant(syms.byteType, readPool(nextChar()));
1664         case 'C':
1665             return new Attribute.Constant(syms.charType, readPool(nextChar()));
1666         case 'D':
1667             return new Attribute.Constant(syms.doubleType, readPool(nextChar()));
1668         case 'F':
1669             return new Attribute.Constant(syms.floatType, readPool(nextChar()));
1670         case 'I':
1671             return new Attribute.Constant(syms.intType, readPool(nextChar()));
1672         case 'J':
1673             return new Attribute.Constant(syms.longType, readPool(nextChar()));
1674         case 'S':
1675             return new Attribute.Constant(syms.shortType, readPool(nextChar()));
1676         case 'Z':