Skip to content

Commit bf9dea4

Browse files
committed
Defensively copy array returned from TypeDescriptor
Update the internal proxy used in `TypeDescriptor` so that it returns a cloned array for calls to `getDeclaredAnnotations` or `getAnnotations`. Closes gh-22695
1 parent 0589989 commit bf9dea4

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
765765

766766
@Override
767767
public Annotation[] getAnnotations() {
768-
return (this.annotations != null ? this.annotations : EMPTY_ANNOTATION_ARRAY);
768+
return (this.annotations != null ? this.annotations.clone() : EMPTY_ANNOTATION_ARRAY);
769769
}
770770

771771
@Override

spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -168,6 +168,13 @@ public void parameterAnnotated() throws Exception {
168168
assertEquals(123, t1.getAnnotation(ParameterAnnotation.class).value());
169169
}
170170

171+
@Test
172+
public void getAnnotationsReturnsClonedArray() throws Exception {
173+
TypeDescriptor t = new TypeDescriptor(new MethodParameter(getClass().getMethod("testAnnotatedMethod", String.class), 0));
174+
t.getAnnotations()[0] = null;
175+
assertNotNull(t.getAnnotations()[0]);
176+
}
177+
171178
@Test
172179
public void propertyComplex() throws Exception {
173180
Property property = new Property(getClass(), getClass().getMethod("getComplexProperty"),

0 commit comments

Comments
 (0)