Skip to content

Commit b9916ab

Browse files
committed
DATACMNS-616 - Fixed field value lookup for private fields in AnnotationDetectionFieldCallback.
We now make the field detected by the callback accessible so that the value lookup doesn't fail for private fields. Added a few more unit tests to verify behavior.
1 parent 49a00cb commit b9916ab

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

src/main/java/org/springframework/data/util/AnnotationDetectionFieldCallback.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 the original author or authors.
2+
* Copyright 2012-2014 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.
@@ -23,7 +23,7 @@
2323
import org.springframework.util.ReflectionUtils.FieldCallback;
2424

2525
/**
26-
* A {@link FieldCallback} that will inspect each field for a given annotation. Thie fields type can then be accessed
26+
* A {@link FieldCallback} that will inspect each field for a given annotation. This field's type can then be accessed
2727
* afterwards.
2828
*
2929
* @author Oliver Gierke
@@ -55,8 +55,11 @@ public void doWith(Field field) throws IllegalArgumentException, IllegalAccessEx
5555
}
5656

5757
Annotation annotation = field.getAnnotation(annotationType);
58+
5859
if (annotation != null) {
60+
5961
this.field = field;
62+
ReflectionUtils.makeAccessible(this.field);
6063
}
6164
}
6265

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.util;
17+
18+
import static org.hamcrest.Matchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.Test;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.util.ReflectionUtils;
24+
25+
/**
26+
* Unit tests for {@link AnnotationDetectionFieldCallback}.
27+
*
28+
* @author Oliver Gierke
29+
*/
30+
public class AnnotationDetectionFieldCallbackUnitTests {
31+
32+
/**
33+
* @see DATACMNS-616
34+
*/
35+
@Test(expected = IllegalArgumentException.class)
36+
public void rejectsNullAnnotationType() {
37+
new AnnotationDetectionFieldCallback(null);
38+
}
39+
40+
/**
41+
* @see DATACMNS-616
42+
*/
43+
@Test
44+
@SuppressWarnings("rawtypes")
45+
public void looksUpValueFromPrivateField() {
46+
47+
AnnotationDetectionFieldCallback callback = new AnnotationDetectionFieldCallback(Autowired.class);
48+
ReflectionUtils.doWithFields(Sample.class, callback);
49+
50+
assertThat(callback.getType(), is(equalTo((Class) String.class)));
51+
assertThat(callback.getValue(new Sample("foo")), is((Object) "foo"));
52+
}
53+
54+
/**
55+
* @see DATACMNS-616
56+
*/
57+
@Test
58+
public void returnsNullForObjectNotContainingAFieldWithTheConfiguredAnnotation() {
59+
60+
AnnotationDetectionFieldCallback callback = new AnnotationDetectionFieldCallback(Autowired.class);
61+
ReflectionUtils.doWithFields(Empty.class, callback);
62+
63+
assertThat(callback.getType(), is(nullValue()));
64+
assertThat(callback.getValue(new Empty()), is(nullValue()));
65+
}
66+
67+
static class Sample {
68+
69+
@Autowired private final String value;
70+
71+
public Sample(String value) {
72+
this.value = value;
73+
}
74+
}
75+
76+
static class Empty {}
77+
}

0 commit comments

Comments
 (0)