Skip to content

Commit 46d4b45

Browse files
author
Gabor Nagy
committed
Allow for checking field level annotations from DiffNode (getFieldAnnotations() functions added, similarly to getPropertyAnnotations()).
1 parent 741520d commit 46d4b45

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/main/java/de/danielbechler/diff/access/PropertyAwareAccessor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public interface PropertyAwareAccessor extends TypeAwareAccessor, CategoryAware,
2626
{
2727
String getPropertyName();
2828

29+
Set<Annotation> getFieldAnnotations();
30+
31+
<T extends Annotation> T getFieldAnnotation(Class<T> annotationClass);
32+
2933
Set<Annotation> getReadMethodAnnotations();
3034

3135
<T extends Annotation> T getReadMethodAnnotation(Class<T> annotationClass);

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,44 @@ public String getPropertyName()
8383
return this.propertyName;
8484
}
8585

86+
/**
87+
* Private function to allow looking for the field recursivery up the superclasses.
88+
*
89+
* @param clazz
90+
* @return
91+
*/
92+
private Set<Annotation> getFieldAnnotations(final Class<?> clazz) {
93+
try {
94+
return new LinkedHashSet<Annotation>(asList(clazz.getDeclaredField(propertyName).getAnnotations()));
95+
} catch (NoSuchFieldException e) {
96+
if (clazz.getSuperclass() != null) {
97+
return getFieldAnnotations(clazz.getSuperclass());
98+
} else {
99+
logger.debug("Cannot find propertyName: {}, declaring class: {}", propertyName, clazz);
100+
return new LinkedHashSet<Annotation>(0);
101+
}
102+
}
103+
}
104+
105+
/**
106+
* @return The annotations of the field.
107+
*/
108+
public Set<Annotation> getFieldAnnotations() {
109+
return getFieldAnnotations(readMethod.getDeclaringClass());
110+
}
111+
112+
public <T extends Annotation> T getFieldAnnotation(final Class<T> annotationClass) {
113+
final Set<? extends Annotation> annotations = getFieldAnnotations();
114+
assert (annotations != null) : "Something is wrong here. " +
115+
"The contract of getReadAnnotations() guarantees a non-null return value.";
116+
for (final Annotation annotation : annotations) {
117+
if (annotationClass.isAssignableFrom(annotation.annotationType())) {
118+
return annotationClass.cast(annotation);
119+
}
120+
}
121+
return null;
122+
}
123+
86124
/**
87125
* @return The annotations of the getter used to access this property.
88126
*/

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,29 @@ public final void visitParents(final Visitor visitor)
443443
}
444444
}
445445

446+
/**
447+
* If this node represents a bean property this method returns all annotations of its field.
448+
*
449+
* @return A set of annotations of this nodes property field or an empty set.
450+
*/
451+
public Set<Annotation> getFieldAnnotations()
452+
{
453+
if (accessor instanceof PropertyAwareAccessor)
454+
{
455+
return unmodifiableSet(((PropertyAwareAccessor) accessor).getFieldAnnotations());
456+
}
457+
return unmodifiableSet(Collections.<Annotation>emptySet());
458+
}
459+
460+
public <T extends Annotation> T getFieldAnnotation(final Class<T> annotationClass)
461+
{
462+
if (accessor instanceof PropertyAwareAccessor)
463+
{
464+
return ((PropertyAwareAccessor) accessor).getFieldAnnotation(annotationClass);
465+
}
466+
return null;
467+
}
468+
446469
/**
447470
* If this node represents a bean property this method returns all annotations of its getter.
448471
*

0 commit comments

Comments
 (0)