/
PropertyMutator.java
120 lines (100 loc) · 3.47 KB
/
PropertyMutator.java
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
114
115
116
117
118
119
120
package com.owlike.genson.reflect;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import com.owlike.genson.*;
import com.owlike.genson.stream.ObjectReader;
public abstract class PropertyMutator extends BeanProperty implements Comparable<PropertyMutator> {
Deserializer<Object> propertyDeserializer;
protected PropertyMutator(String name, Type type, Class<?> declaringClass, Class<?> concreteClass,
Annotation[] annotations, int modifiers) {
super(name, type, declaringClass, concreteClass, annotations, modifiers);
}
public Object deserialize(ObjectReader reader, Context ctx) {
try {
return propertyDeserializer.deserialize(reader, ctx);
} catch (Throwable th) {
throw couldNotDeserialize(th);
}
}
public void deserialize(Object into, ObjectReader reader, Context ctx) {
Object propValue = null;
try {
propValue = propertyDeserializer.deserialize(reader, ctx);
} catch (Throwable th) {
throw couldNotDeserialize(th);
}
mutate(into, propValue);
}
public abstract void mutate(Object target, Object value);
public int compareTo(PropertyMutator o) {
return o.priority() - priority();
}
protected JsonBindingException couldNotMutate(Exception e) {
return new JsonBindingException("Could not mutate value of property named '"
+ name + "' using mutator " + signature(), e);
}
protected JsonBindingException couldNotDeserialize(Throwable e) {
return new JsonBindingException("Could not deserialize to property '" + name + "' of class " + declaringClass, e);
}
public static class MethodMutator extends PropertyMutator {
protected final Method _setter;
public MethodMutator(String name, Method setter, Type type, Class<?> concreteClass) {
super(name, type, setter.getDeclaringClass(), concreteClass, setter.getAnnotations(), setter.getModifiers());
this._setter = setter;
if (!_setter.isAccessible()) {
_setter.setAccessible(true);
}
}
@Override
public void mutate(Object target, Object value) {
try {
_setter.invoke(target, value);
} catch (IllegalArgumentException e) {
throw couldNotMutate(e);
} catch (IllegalAccessException e) {
throw couldNotMutate(e);
} catch (InvocationTargetException e) {
throw couldNotMutate(e);
}
}
@Override
public String signature() {
return _setter.toGenericString();
}
@Override
public int priority() {
return 100;
}
}
public static class FieldMutator extends PropertyMutator {
protected final Field _field;
public FieldMutator(String name, Field field, Type type, Class<?> concreteClass) {
super(name, type, field.getDeclaringClass(), concreteClass, field.getAnnotations(), field.getModifiers());
this._field = field;
if (!_field.isAccessible()) {
_field.setAccessible(true);
}
}
@Override
public void mutate(Object target, Object value) {
try {
_field.set(target, value);
} catch (IllegalArgumentException e) {
throw couldNotMutate(e);
} catch (IllegalAccessException e) {
throw couldNotMutate(e);
}
}
@Override
public String signature() {
return _field.toGenericString();
}
@Override
public int priority() {
return 0;
}
}
}