forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPojoLoader.java
More file actions
102 lines (91 loc) · 3.32 KB
/
PojoLoader.java
File metadata and controls
102 lines (91 loc) · 3.32 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
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 static act.inject.param.ParamValueLoaderService.shouldWaive;
import act.inject.genie.GenieInjector;
import act.util.ActContext;
import act.util.LogSupport;
import org.osgl.$;
import org.osgl.inject.BeanSpec;
import org.osgl.inject.InjectException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
class PojoLoader extends LogSupport implements ParamValueLoader {
final ParamKey key;
final BeanSpec spec;
final GenieInjector injector;
final ParamValueLoaderService service;
final boolean provided;
protected Map<String, FieldLoader> fieldLoaders;
public PojoLoader(ParamKey key, BeanSpec spec, ParamValueLoaderService service) {
this.key = $.requireNotNull(key);
this.spec = spec;
this.injector = service.injector;
this.service = service;
this.provided = service.provided(spec, injector);
this.fieldLoaders = fieldLoaders(key, spec);
}
@Override
public Object load(Object bean, ActContext<?> context, boolean noDefaultValue) {
final $.Var<Object> beanBag = $.var(bean);
$.Factory<Object> beanSource = new $.Factory<Object>() {
@Override
public Object create() {
Object bean = beanBag.get();
if (null == bean) {
try {
bean = provided ? injector.get(spec) : $.newInstance(spec.rawType());
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new InjectException(e, "cannot instantiate %s", spec);
}
}
beanBag.set(bean);
return bean;
}
};
for (FieldLoader fl : fieldLoaders.values()) {
fl.applyTo(beanSource, context);
}
return beanBag.get();
}
@Override
public String bindName() {
return key.toString();
}
private Map<String, FieldLoader> fieldLoaders(ParamKey key, BeanSpec spec) {
Class<?> current = spec.rawType();
Map<String, FieldLoader> fieldLoaders = new HashMap<>();
while (null != current && !current.equals(Object.class)) {
for (Field field : current.getDeclaredFields()) {
if (shouldWaive(field)) {
continue;
}
field.setAccessible(true);
String fieldName = field.getName();
fieldLoaders.put(fieldName, service.fieldLoader(key, field, spec.field(fieldName)));
}
current = current.getSuperclass();
}
return fieldLoaders;
}
}