diff --git a/src/main/java/org/springframework/data/util/AnnotationDetectionFieldCallback.java b/src/main/java/org/springframework/data/util/AnnotationDetectionFieldCallback.java index be52b1326..d2604fbd3 100755 --- a/src/main/java/org/springframework/data/util/AnnotationDetectionFieldCallback.java +++ b/src/main/java/org/springframework/data/util/AnnotationDetectionFieldCallback.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 the original author or authors. + * Copyright 2012-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,7 @@ import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils.FieldCallback; /** - * A {@link FieldCallback} that will inspect each field for a given annotation. Thie fields type can then be accessed + * A {@link FieldCallback} that will inspect each field for a given annotation. This field's type can then be accessed * afterwards. * * @author Oliver Gierke @@ -55,8 +55,11 @@ public class AnnotationDetectionFieldCallback implements FieldCallback { } Annotation annotation = field.getAnnotation(annotationType); + if (annotation != null) { + this.field = field; + ReflectionUtils.makeAccessible(this.field); } } diff --git a/src/test/java/org/springframework/data/util/AnnotationDetectionFieldCallbackUnitTests.java b/src/test/java/org/springframework/data/util/AnnotationDetectionFieldCallbackUnitTests.java new file mode 100644 index 000000000..d5b73af2e --- /dev/null +++ b/src/test/java/org/springframework/data/util/AnnotationDetectionFieldCallbackUnitTests.java @@ -0,0 +1,77 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.util; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.ReflectionUtils; + +/** + * Unit tests for {@link AnnotationDetectionFieldCallback}. + * + * @author Oliver Gierke + */ +public class AnnotationDetectionFieldCallbackUnitTests { + + /** + * @see DATACMNS-616 + */ + @Test(expected = IllegalArgumentException.class) + public void rejectsNullAnnotationType() { + new AnnotationDetectionFieldCallback(null); + } + + /** + * @see DATACMNS-616 + */ + @Test + @SuppressWarnings("rawtypes") + public void looksUpValueFromPrivateField() { + + AnnotationDetectionFieldCallback callback = new AnnotationDetectionFieldCallback(Autowired.class); + ReflectionUtils.doWithFields(Sample.class, callback); + + assertThat(callback.getType(), is(equalTo((Class) String.class))); + assertThat(callback.getValue(new Sample("foo")), is((Object) "foo")); + } + + /** + * @see DATACMNS-616 + */ + @Test + public void returnsNullForObjectNotContainingAFieldWithTheConfiguredAnnotation() { + + AnnotationDetectionFieldCallback callback = new AnnotationDetectionFieldCallback(Autowired.class); + ReflectionUtils.doWithFields(Empty.class, callback); + + assertThat(callback.getType(), is(nullValue())); + assertThat(callback.getValue(new Empty()), is(nullValue())); + } + + static class Sample { + + @Autowired private final String value; + + public Sample(String value) { + this.value = value; + } + } + + static class Empty {} +}