32
32
import java .lang .reflect .Field ;
33
33
import java .lang .reflect .InvocationTargetException ;
34
34
import java .lang .reflect .Method ;
35
+ import java .nio .BufferUnderflowException ;
35
36
import java .nio .ByteBuffer ;
36
37
import java .util .ArrayList ;
37
38
import java .util .Arrays ;
@@ -149,7 +150,7 @@ public boolean hasAnnotation(AnnotatedElement element, Class<? extends Annotatio
149
150
@ SuppressWarnings ("unchecked" )
150
151
@ Override
151
152
public Class <? extends Annotation >[] getAnnotationTypes (AnnotatedElement element ) {
152
- return Arrays .stream (getAnnotationData (element , false )).map (AnnotationValue ::getType ).toArray (Class []::new );
153
+ return Arrays .stream (getAnnotationData (element , false )).map (AnnotationValue ::getType ).filter ( t -> t != null ). toArray (Class []::new );
153
154
}
154
155
155
156
public AnnotationValue [] getDeclaredAnnotationData (AnnotatedElement element ) {
@@ -220,15 +221,19 @@ private AnnotationValue[] getDeclaredAnnotationDataFromRoot(AnnotatedElement roo
220
221
return NO_ANNOTATIONS ;
221
222
}
222
223
ByteBuffer buf = ByteBuffer .wrap (rawAnnotations );
223
- List <AnnotationValue > annotations = new ArrayList <>();
224
- int numAnnotations = buf .getShort () & 0xFFFF ;
225
- for (int i = 0 ; i < numAnnotations ; i ++) {
226
- AnnotationValue annotation = AnnotationValue .extract (buf , getConstantPool (element ), getContainer (element ), false , false );
227
- if (annotation != null ) {
228
- annotations .add (annotation );
224
+ try {
225
+ List <AnnotationValue > annotations = new ArrayList <>();
226
+ int numAnnotations = buf .getShort () & 0xFFFF ;
227
+ for (int i = 0 ; i < numAnnotations ; i ++) {
228
+ AnnotationValue annotation = AnnotationValue .extract (buf , getConstantPool (element ), getContainer (element ), false , false );
229
+ if (annotation != null ) {
230
+ annotations .add (annotation );
231
+ }
229
232
}
233
+ return annotations .toArray (NO_ANNOTATIONS );
234
+ } catch (IllegalArgumentException | BufferUnderflowException ex ) {
235
+ return new AnnotationValue []{AnnotationValue .forAnnotationFormatException ()};
230
236
}
231
- return annotations .toArray (NO_ANNOTATIONS );
232
237
});
233
238
}
234
239
@@ -244,20 +249,24 @@ private AnnotationValue[][] getParameterAnnotationDataFromRoot(Executable rootEl
244
249
return NO_PARAMETER_ANNOTATIONS ;
245
250
}
246
251
ByteBuffer buf = ByteBuffer .wrap (rawParameterAnnotations );
247
- int numParameters = buf .get () & 0xFF ;
248
- AnnotationValue [][] parameterAnnotations = new AnnotationValue [numParameters ][];
249
- for (int i = 0 ; i < numParameters ; i ++) {
250
- List <AnnotationValue > parameterAnnotationList = new ArrayList <>();
251
- int numAnnotations = buf .getShort () & 0xFFFF ;
252
- for (int j = 0 ; j < numAnnotations ; j ++) {
253
- AnnotationValue parameterAnnotation = AnnotationValue .extract (buf , getConstantPool (element ), getContainer (element ), false , false );
254
- if (parameterAnnotation != null ) {
255
- parameterAnnotationList .add (parameterAnnotation );
252
+ try {
253
+ int numParameters = buf .get () & 0xFF ;
254
+ AnnotationValue [][] parameterAnnotations = new AnnotationValue [numParameters ][];
255
+ for (int i = 0 ; i < numParameters ; i ++) {
256
+ List <AnnotationValue > parameterAnnotationList = new ArrayList <>();
257
+ int numAnnotations = buf .getShort () & 0xFFFF ;
258
+ for (int j = 0 ; j < numAnnotations ; j ++) {
259
+ AnnotationValue parameterAnnotation = AnnotationValue .extract (buf , getConstantPool (element ), getContainer (element ), false , false );
260
+ if (parameterAnnotation != null ) {
261
+ parameterAnnotationList .add (parameterAnnotation );
262
+ }
256
263
}
264
+ parameterAnnotations [i ] = parameterAnnotationList .toArray (NO_ANNOTATIONS );
257
265
}
258
- parameterAnnotations [i ] = parameterAnnotationList .toArray (NO_ANNOTATIONS );
266
+ return parameterAnnotations ;
267
+ } catch (IllegalArgumentException | BufferUnderflowException ex ) {
268
+ return new AnnotationValue [][]{new AnnotationValue []{AnnotationValue .forAnnotationFormatException ()}};
259
269
}
260
- return parameterAnnotations ;
261
270
});
262
271
}
263
272
@@ -273,12 +282,21 @@ private TypeAnnotationValue[] getTypeAnnotationDataFromRoot(AnnotatedElement roo
273
282
return NO_TYPE_ANNOTATIONS ;
274
283
}
275
284
ByteBuffer buf = ByteBuffer .wrap (rawTypeAnnotations );
276
- int annotationCount = buf .getShort () & 0xFFFF ;
277
- TypeAnnotationValue [] typeAnnotationValues = new TypeAnnotationValue [annotationCount ];
278
- for (int i = 0 ; i < annotationCount ; i ++) {
279
- typeAnnotationValues [i ] = TypeAnnotationValue .extract (buf , getConstantPool (element ), getContainer (element ));
285
+ try {
286
+ int annotationCount = buf .getShort () & 0xFFFF ;
287
+ TypeAnnotationValue [] typeAnnotationValues = new TypeAnnotationValue [annotationCount ];
288
+ for (int i = 0 ; i < annotationCount ; i ++) {
289
+ typeAnnotationValues [i ] = TypeAnnotationValue .extract (buf , getConstantPool (element ), getContainer (element ));
290
+ }
291
+ return typeAnnotationValues ;
292
+ } catch (IllegalArgumentException | BufferUnderflowException ex ) {
293
+ /*
294
+ * The byte[] arrrays in the TypeAnnotationValue are structurally correct, but have
295
+ * an illegal first targetInfo byte that will throw an AnnotationFormatException
296
+ * during parsing.
297
+ */
298
+ return new TypeAnnotationValue []{new TypeAnnotationValue (new byte []{0x77 }, new byte []{0 }, AnnotationValue .forAnnotationFormatException ())};
280
299
}
281
- return typeAnnotationValues ;
282
300
});
283
301
}
284
302
@@ -294,7 +312,11 @@ private AnnotationMemberValue getAnnotationDefaultDataFromRoot(Method accessorMe
294
312
return null ;
295
313
}
296
314
ByteBuffer buf = ByteBuffer .wrap (rawAnnotationDefault );
297
- return AnnotationMemberValue .extract (buf , getConstantPool (method ), getContainer (method ), false );
315
+ try {
316
+ return AnnotationMemberValue .extract (buf , getConstantPool (method ), getContainer (method ), false );
317
+ } catch (IllegalArgumentException | BufferUnderflowException ex ) {
318
+ return AnnotationValue .forAnnotationFormatException ();
319
+ }
298
320
});
299
321
}
300
322
0 commit comments