-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
SetterFieldImpl.java
117 lines (101 loc) · 2.86 KB
/
SetterFieldImpl.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
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.property.access.spi;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Locale;
import org.hibernate.PropertyAccessException;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.property.access.internal.AbstractFieldSerialForm;
/**
* Field-based implementation of Setter
*
* @author Steve Ebersole
*/
public class SetterFieldImpl implements Setter {
private final Class containerClass;
private final String propertyName;
private final Field field;
private final Method setterMethod;
public SetterFieldImpl(Class containerClass, String propertyName, Field field) {
this.containerClass = containerClass;
this.propertyName = propertyName;
this.field = field;
this.setterMethod = ReflectHelper.setterMethodOrNull( containerClass, propertyName, field.getType() );
}
public Class getContainerClass() {
return containerClass;
}
public String getPropertyName() {
return propertyName;
}
protected Field getField() {
return field;
}
@Override
public void set(Object target, Object value, SessionFactoryImplementor factory) {
try {
field.set( target, value );
}
catch (Exception e) {
if (value == null && field.getType().isPrimitive()) {
throw new PropertyAccessException(
e,
String.format(
Locale.ROOT,
"Null value was assigned to a property [%s.%s] of primitive type",
containerClass,
propertyName
),
true,
containerClass,
propertyName
);
}
else {
throw new PropertyAccessException(
e,
String.format(
Locale.ROOT,
"Could not set field value [%s] value by reflection : [%s.%s]",
value,
containerClass,
propertyName
),
true,
containerClass,
propertyName
);
}
}
}
@Override
public String getMethodName() {
return setterMethod != null ? setterMethod.getName() : null;
}
@Override
public Method getMethod() {
return setterMethod;
}
private Object writeReplace() {
return new SerialForm( containerClass, propertyName, field );
}
private static class SerialForm extends AbstractFieldSerialForm implements Serializable {
private final Class containerClass;
private final String propertyName;
private SerialForm(Class containerClass, String propertyName, Field field) {
super( field );
this.containerClass = containerClass;
this.propertyName = propertyName;
}
private Object readResolve() {
return new SetterFieldImpl( containerClass, propertyName, resolveField() );
}
}
}