src/share/classes/sun/reflect/annotation/TypeAnnotationParser.java

Print this page
rev 8830 : 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
Reviewed-by: duke

*** 392,426 **** private static TypeAnnotation parseTypeAnnotation(ByteBuffer buf, ConstantPool cp, AnnotatedElement baseDecl, Class<?> container) { TypeAnnotationTargetInfo ti = parseTargetInfo(buf); LocationInfo locationInfo = LocationInfo.parseLocationInfo(buf); Annotation a = AnnotationParser.parseAnnotation(buf, cp, container, false); if (ti == null) // Inside a method for example return null; return new TypeAnnotation(ti, locationInfo, a, baseDecl); } private static TypeAnnotationTargetInfo parseTargetInfo(ByteBuffer buf) { ! byte posCode = buf.get(); switch(posCode) { case CLASS_TYPE_PARAMETER: case METHOD_TYPE_PARAMETER: { ! byte index = buf.get(); TypeAnnotationTargetInfo res; if (posCode == CLASS_TYPE_PARAMETER) res = new TypeAnnotationTargetInfo(TypeAnnotationTarget.CLASS_TYPE_PARAMETER, index); else res = new TypeAnnotationTargetInfo(TypeAnnotationTarget.METHOD_TYPE_PARAMETER, index); return res; } // unreachable break; case CLASS_EXTENDS: { ! short index = buf.getShort(); if (index == -1) { return new TypeAnnotationTargetInfo(TypeAnnotationTarget.CLASS_EXTENDS); } else if (index >= 0) { TypeAnnotationTargetInfo res = new TypeAnnotationTargetInfo(TypeAnnotationTarget.CLASS_IMPLEMENTS, index); --- 392,431 ---- private static TypeAnnotation parseTypeAnnotation(ByteBuffer buf, ConstantPool cp, AnnotatedElement baseDecl, Class<?> container) { + try { TypeAnnotationTargetInfo ti = parseTargetInfo(buf); LocationInfo locationInfo = LocationInfo.parseLocationInfo(buf); Annotation a = AnnotationParser.parseAnnotation(buf, cp, container, false); if (ti == null) // Inside a method for example return null; return new TypeAnnotation(ti, locationInfo, a, baseDecl); + } catch (IllegalArgumentException | // Bad type in const pool at specified index + BufferUnderflowException e) { + throw new AnnotationFormatError(e); + } } private static TypeAnnotationTargetInfo parseTargetInfo(ByteBuffer buf) { ! int posCode = buf.get() & 0xFF; switch(posCode) { case CLASS_TYPE_PARAMETER: case METHOD_TYPE_PARAMETER: { ! int index = buf.get() & 0xFF; TypeAnnotationTargetInfo res; if (posCode == CLASS_TYPE_PARAMETER) res = new TypeAnnotationTargetInfo(TypeAnnotationTarget.CLASS_TYPE_PARAMETER, index); else res = new TypeAnnotationTargetInfo(TypeAnnotationTarget.METHOD_TYPE_PARAMETER, index); return res; } // unreachable break; case CLASS_EXTENDS: { ! short index = buf.getShort(); //needs to be signed if (index == -1) { return new TypeAnnotationTargetInfo(TypeAnnotationTarget.CLASS_EXTENDS); } else if (index >= 0) { TypeAnnotationTargetInfo res = new TypeAnnotationTargetInfo(TypeAnnotationTarget.CLASS_IMPLEMENTS, index);
*** 435,445 **** case METHOD_RETURN: return new TypeAnnotationTargetInfo(TypeAnnotationTarget.METHOD_RETURN); case METHOD_RECEIVER: return new TypeAnnotationTargetInfo(TypeAnnotationTarget.METHOD_RECEIVER); case METHOD_FORMAL_PARAMETER: { ! byte index = buf.get(); return new TypeAnnotationTargetInfo(TypeAnnotationTarget.METHOD_FORMAL_PARAMETER, index); } //unreachable break; case THROWS: return parseShortTarget(TypeAnnotationTarget.THROWS, buf); --- 440,450 ---- case METHOD_RETURN: return new TypeAnnotationTargetInfo(TypeAnnotationTarget.METHOD_RETURN); case METHOD_RECEIVER: return new TypeAnnotationTargetInfo(TypeAnnotationTarget.METHOD_RECEIVER); case METHOD_FORMAL_PARAMETER: { ! int index = buf.get() & 0xFF; return new TypeAnnotationTargetInfo(TypeAnnotationTarget.METHOD_FORMAL_PARAMETER, index); } //unreachable break; case THROWS: return parseShortTarget(TypeAnnotationTarget.THROWS, buf);
*** 484,499 **** } throw new AnnotationFormatError("Could not parse bytes for type annotations"); } private static TypeAnnotationTargetInfo parseShortTarget(TypeAnnotationTarget target, ByteBuffer buf) { ! short index = buf.getShort(); return new TypeAnnotationTargetInfo(target, index); } private static TypeAnnotationTargetInfo parse2ByteTarget(TypeAnnotationTarget target, ByteBuffer buf) { ! byte count = buf.get(); ! byte secondaryIndex = buf.get(); return new TypeAnnotationTargetInfo(target, count, secondaryIndex); } } --- 489,504 ---- } throw new AnnotationFormatError("Could not parse bytes for type annotations"); } private static TypeAnnotationTargetInfo parseShortTarget(TypeAnnotationTarget target, ByteBuffer buf) { ! int index = buf.getShort() & 0xFFFF; return new TypeAnnotationTargetInfo(target, index); } private static TypeAnnotationTargetInfo parse2ByteTarget(TypeAnnotationTarget target, ByteBuffer buf) { ! int count = buf.get() & 0xFF; ! int secondaryIndex = buf.get() & 0xFF; return new TypeAnnotationTargetInfo(target, count, secondaryIndex); } }