forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldLoader.java
More file actions
100 lines (93 loc) · 3.51 KB
/
FieldLoader.java
File metadata and controls
100 lines (93 loc) · 3.51 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
package act.inject.param;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.Act;
import act.data.Sensitive;
import act.util.ActContext;
import act.validation.Password;
import org.osgl.$;
import org.osgl.Lang;
import org.osgl.inject.InjectException;
import java.lang.reflect.Field;
/**
* Load instance loaded by {@link ParamValueLoader} into {@link java.lang.reflect.Field}
*/
class FieldLoader {
private final Field field;
private final Class<?> fieldType;
private final boolean isString;
private final ParamValueLoader loader;
private final ParamValueLoader stringValueLoader;
private final boolean isSensitive;
private final boolean isPassword;
private Lang.TypeConverter<Object, Object> converter;
FieldLoader(Field field, ParamValueLoader loader, ParamValueLoader stringValueLoader, Lang.TypeConverter<Object, Object> converter) {
Class<?> type = field.getType();
boolean isString = String.class == type;
boolean isCharArray = char[].class == type;
this.isString = isString;
this.isSensitive = isString && null != field.getAnnotation(Sensitive.class);
this.isPassword = (isString || isCharArray) && null != field.getAnnotation(Password.class);
this.field = field;
this.fieldType = type;
this.loader = $.requireNotNull(loader);
this.stringValueLoader = stringValueLoader;
this.converter = converter;
}
FieldLoader(Field field, ParamValueLoader loader) {
this(field, loader, null, null);
}
public void applyTo($.Func0<Object> beanSource, ActContext context) {
Object fieldValue = loader.load(null, context, true);
if (null == fieldValue && null != converter) {
// try converter
Object o = stringValueLoader.load(null, context, true);
if (null != o) {
fieldValue = converter.convert(o);
}
}
// #429 ensure POJO instance get initialized
if (null == fieldValue) {
// counter effect to #429 - We don't want to leave an empty reference for JPA model entities
//beanSource.apply();
// #689 initialize field if it is an array or a container
if (fieldType.isArray()) {
return;
}
return;
}
try {
if (isSensitive) {
fieldValue = Act.crypto().encrypt((String)fieldValue);
} else if (isPassword) {
if (isString) {
fieldValue = Act.crypto().passwordHash((String) fieldValue);
} else {
char[] ca = $.convert(fieldValue).to(char[].class);
fieldValue = Act.crypto().passwordHash(ca);
}
}
Object bean = beanSource.apply();
field.set(bean, fieldValue);
} catch (Exception e) {
throw new InjectException(e);
}
}
}