@@ -424,23 +424,35 @@ private ElementValue readElementValue(@Nonnull AnnotationScope scope) throws IOE
424424 case 'J' : // long
425425 case 'S' : // short
426426 case 'Z' : // boolean
427+ {
427428 int index = is .readUnsignedShort ();
428429 CpEntry entry = cp .get (index );
429- return new PrimitiveElementValue (tag , entry );
430+ if (entry != null )
431+ return new PrimitiveElementValue (tag , entry );
432+ throw new IOException ("Invalid element: " + tag );
433+ }
430434 case 's' : // String
435+ {
431436 int utfIndex = is .readUnsignedShort ();
432- CpUtf8 utf = (CpUtf8 ) cp .get (utfIndex );
433- return new Utf8ElementValue (tag , utf );
437+ CpEntry entry = cp .get (utfIndex );
438+ if (entry instanceof CpUtf8 utf )
439+ return new Utf8ElementValue (tag , utf );
440+ throw new IOException ("Invalid element: String" );
441+ }
434442 case 'e' : // Enum
435443 int typename = is .readUnsignedShort ();
436444 int constname = is .readUnsignedShort ();
437- CpUtf8 type = (CpUtf8 ) cp .get (typename );
438- CpUtf8 constant = (CpUtf8 ) cp .get (constname );
439- return new EnumElementValue (tag , type , constant );
445+ CpEntry entryType = cp .get (typename );
446+ CpEntry entryConstant = cp .get (constname );
447+ if (entryType instanceof CpUtf8 type && entryConstant instanceof CpUtf8 constant )
448+ return new EnumElementValue (tag , type , constant );
449+ throw new IOException ("Invalid element: Enum" );
440450 case 'c' : // Class
441451 int classInfoIndex = is .readUnsignedShort ();
442- CpUtf8 classInfo = (CpUtf8 ) cp .get (classInfoIndex );
443- return new ClassElementValue (tag , classInfo );
452+ CpEntry entry = cp .get (classInfoIndex );
453+ if (entry instanceof CpUtf8 className )
454+ return new ClassElementValue (tag , className );
455+ throw new IOException ("Invalid element: Class" );
444456 case '@' : // Annotation
445457 Annotation nestedAnnotation = readAnnotation (scope );
446458 return new AnnotationElementValue (tag , nestedAnnotation );
0 commit comments