Skip to content

Commit 2d5bc0e

Browse files
committed
Added convenience method to access property annotations
1 parent ff67c3a commit 2d5bc0e

File tree

5 files changed

+76
-6
lines changed

5 files changed

+76
-6
lines changed

src/main/java/de/danielbechler/diff/accessor/PropertyAccessor.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,21 @@ public Set<Annotation> getReadMethodAnnotations()
199199
return new LinkedHashSet<Annotation>(Arrays.asList(readMethod.getAnnotations()));
200200
}
201201

202+
public <T extends Annotation> T getReadMethodAnnotation(final Class<T> annotationClass)
203+
{
204+
final Set<? extends Annotation> annotations = getReadMethodAnnotations();
205+
assert (annotations != null) : "Something is wrong here. " +
206+
"The contract of getReadAnnotations() guarantees a non-null return value.";
207+
for (final Annotation annotation : annotations)
208+
{
209+
if (annotationClass.isAssignableFrom(annotation.annotationType()))
210+
{
211+
return annotationClass.cast(annotation);
212+
}
213+
}
214+
return null;
215+
}
216+
202217
@Override
203218
public String toString()
204219
{

src/main/java/de/danielbechler/diff/node/DefaultNode.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,15 @@ public Set<Annotation> getPropertyAnnotations()
294294
return unmodifiableSet(Collections.<Annotation>emptySet());
295295
}
296296

297+
public <T extends Annotation> T getPropertyAnnotation(final Class<T> annotationClass)
298+
{
299+
if (accessor instanceof PropertyAccessor)
300+
{
301+
return ((PropertyAccessor) accessor).getReadMethodAnnotation(annotationClass);
302+
}
303+
return null;
304+
}
305+
297306
public final boolean isRootNode()
298307
{
299308
return accessor instanceof RootAccessor;

src/main/java/de/danielbechler/diff/node/Node.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,7 @@ public enum State
198198
* @return A set of annotations of this nodes property getter or an empty set.
199199
*/
200200
Set<Annotation> getPropertyAnnotations();
201+
202+
<T extends Annotation> T getPropertyAnnotation(Class<T> annotationClass);
203+
201204
}

src/test/java/de/danielbechler/diff/accessor/PropertyAccessorShould.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717
package de.danielbechler.diff.accessor;
1818

1919
import de.danielbechler.diff.accessor.exception.*;
20+
import de.danielbechler.diff.annotation.*;
2021
import de.danielbechler.diff.mock.*;
2122
import de.danielbechler.diff.path.*;
22-
import org.fest.assertions.api.*;
2323
import org.testng.annotations.*;
2424

2525
import java.lang.annotation.*;
2626
import java.lang.reflect.*;
27-
import java.util.*;
2827

2928
import static java.util.Arrays.*;
3029
import static org.fest.assertions.api.Assertions.*;
@@ -152,17 +151,39 @@ public void includes_accessor_type_in_string_representation()
152151
}
153152

154153
@Test
155-
public void returns_annotations_of_getter_as_set() throws Exception
154+
public void returns_annotations_of_property_getter() throws Exception
156155
{
157156
accessor = PropertyAccessorBuilder.forPropertyOf(ObjectWithAnnotatedProperty.class)
158157
.property("value", String.class)
159158
.readOnly(false)
160159
.build();
161160
final Annotation[] expectedAnnotations = ObjectWithAnnotatedProperty.class.getMethod("getValue")
162161
.getAnnotations();
163-
Assertions.assertThat(expectedAnnotations).hasSize(2);
162+
assertThat(expectedAnnotations).hasSize(2);
163+
assertThat(accessor.getReadMethodAnnotations()).containsAll(asList(expectedAnnotations));
164+
}
164165

165-
final Set<Annotation> set = accessor.getReadMethodAnnotations();
166-
Assertions.assertThat(set).containsAll(asList(expectedAnnotations));
166+
@Test
167+
public void returns_specific_annotation_of_property_getter() throws Exception
168+
{
169+
accessor = PropertyAccessorBuilder.forPropertyOf(ObjectWithAnnotatedProperty.class)
170+
.property("value", String.class)
171+
.readOnly(false)
172+
.build();
173+
final ObjectDiffProperty expectedAnnotation = ObjectWithAnnotatedProperty.class.getMethod("getValue")
174+
.getAnnotation(ObjectDiffProperty.class);
175+
assertThat(expectedAnnotation).isNotNull();
176+
final ObjectDiffProperty annotation = accessor.getReadMethodAnnotation(ObjectDiffProperty.class);
177+
assertThat(annotation).isEqualTo(expectedAnnotation);
178+
}
179+
180+
@Test
181+
public void returns_null_if_specific_annotation_of_property_getter_does_not_exist() throws Exception
182+
{
183+
accessor = PropertyAccessorBuilder.forPropertyOf(ObjectWithAnnotatedProperty.class)
184+
.property("value", String.class)
185+
.readOnly(false)
186+
.build();
187+
assertThat(accessor.getReadMethodAnnotation(Override.class)).isNull();
167188
}
168189
}

src/test/java/de/danielbechler/diff/node/DefaultNodeTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package de.danielbechler.diff.node;
1818

1919
import de.danielbechler.diff.accessor.*;
20+
import de.danielbechler.diff.mock.*;
2021
import de.danielbechler.diff.path.*;
2122
import org.fest.assertions.api.*;
2223
import org.hamcrest.core.*;
@@ -191,4 +192,25 @@ public void testShould_return_empty_set_of_property_annotations_if_accessor_is_n
191192

192193
Assertions.assertThat(annotations).containsAll(Arrays.asList(annotation));
193194
}
195+
196+
@Test
197+
public void test_get_property_annotation_should_delegate_call_to_property_accessor()
198+
{
199+
final PropertyAccessor propertyAccessor = mock(PropertyAccessor.class);
200+
when(propertyAccessor.getReadMethodAnnotation(ObjectDiffTest.class)).thenReturn(null);
201+
202+
new DefaultNode(propertyAccessor, Object.class).getPropertyAnnotation(ObjectDiffTest.class);
203+
204+
verify(propertyAccessor, times(1)).getReadMethodAnnotation(ObjectDiffTest.class);
205+
}
206+
207+
@Test
208+
public void test_get_property_annotation_should_return_null_if_accessor_is_not_property_accessor()
209+
{
210+
final Accessor propertyAccessor = mock(Accessor.class);
211+
212+
final ObjectDiffTest annotation = new DefaultNode(propertyAccessor, Object.class).getPropertyAnnotation(ObjectDiffTest.class);
213+
214+
assertThat(annotation).isNull();
215+
}
194216
}

0 commit comments

Comments
 (0)