-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathFieldAccessor.java
More file actions
113 lines (96 loc) · 2.29 KB
/
FieldAccessor.java
File metadata and controls
113 lines (96 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package de.danielbechler.diff.introspection;
import de.danielbechler.diff.access.PropertyAwareAccessor;
import de.danielbechler.diff.selector.BeanPropertyElementSelector;
import de.danielbechler.diff.selector.ElementSelector;
import de.danielbechler.util.Assert;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class FieldAccessor implements PropertyAwareAccessor
{
private final Field field;
FieldAccessor(final Field field)
{
Assert.notNull(field, "field");
this.field = field;
}
public Class<?> getType()
{
return field.getType();
}
public Set<String> getCategoriesFromAnnotation()
{
return Collections.emptySet();
}
public ElementSelector getElementSelector()
{
return new BeanPropertyElementSelector(getPropertyName());
}
public Object get(Object target)
{
try
{
return field.get(target);
}
catch (IllegalAccessException e)
{
throw new PropertyReadException(getPropertyName(), getType(), e);
}
}
public void set(Object target, Object value)
{
try
{
field.setAccessible(true);
field.set(target, value);
}
catch (IllegalAccessException e)
{
throw new PropertyWriteException(getPropertyName(), getType(), value, e);
}
finally
{
field.setAccessible(false);
}
}
public void unset(Object target)
{
}
public String getPropertyName()
{
return field.getName();
}
public Set<Annotation> getFieldAnnotations()
{
final Set<Annotation> fieldAnnotations = new HashSet<Annotation>(field.getAnnotations().length);
fieldAnnotations.addAll(Arrays.asList(field.getAnnotations()));
return fieldAnnotations;
}
public <T extends Annotation> T getFieldAnnotation(Class<T> annotationClass)
{
return field.getAnnotation(annotationClass);
}
public Set<Annotation> getReadMethodAnnotations()
{
return Collections.emptySet();
}
public <T extends Annotation> T getReadMethodAnnotation(Class<T> annotationClass)
{
return null;
}
public boolean isExcludedByAnnotation()
{
ObjectDiffProperty annotation = getFieldAnnotation(ObjectDiffProperty.class);
return annotation != null && annotation.excluded();
}
@Override
public String toString()
{
return "FieldAccessor{" +
"field=" + field +
'}';
}
}