/
Iocs.java
133 lines (117 loc) · 4.34 KB
/
Iocs.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
121
122
123
124
125
126
127
128
129
130
131
132
133
package org.nutz.ioc;
import java.util.Map;
import java.util.Map.Entry;
import org.nutz.ioc.meta.IocEventSet;
import org.nutz.ioc.meta.IocField;
import org.nutz.ioc.meta.IocObject;
import org.nutz.ioc.meta.IocValue;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.lang.meta.Pair;
/**
* @author zozoh(zozohtnt@gmail.com)
* @author wendal(wendal1985@gmail.com)
*
*/
public abstract class Iocs {
private static final String OBJFIELDS = "^(type|scope|singleton|fields|args|events|factory)$";
public static boolean isIocObject(Map<String, ?> map) {
for (Entry<String, ?> en : map.entrySet())
if (!en.getKey().matches(OBJFIELDS))
return false;
return true;
}
public static Pair<Class<?>> parseName(String name) {
String _name = null;
Class<?> type = null;
int pos = name.indexOf(':');
if (pos < 0) {
_name = Strings.trim(name);
} else {
_name = Strings.trim(name.substring(0, pos));
try {
String typeName = Strings.trim(name.substring(pos + 1));
type = Lang.loadClass(typeName);
}
catch (ClassNotFoundException e) {
throw Lang.wrapThrow(e);
}
}
return new Pair<Class<?>>(_name, type);
}
/**
* 查看一下 me 中有没有缺少的属性,没有的话,从 it 补充
*/
public static IocObject mergeWith(IocObject me, IocObject it) {
// merge type
if (me.getType() == null)
me.setType(it.getType());
// don't need merge signleon
// merge events
if (me.getEvents() == null) {
me.setEvents(it.getEvents());
} else if (it.getEvents() != null) {
IocEventSet eventSet = it.getEvents();
IocEventSet myEventSet = me.getEvents();
if (Strings.isBlank(myEventSet.getCreate()))
myEventSet.setCreate(eventSet.getCreate());
if (Strings.isBlank(myEventSet.getDepose()))
myEventSet.setDepose(eventSet.getDepose());
if (Strings.isBlank(myEventSet.getFetch()))
myEventSet.setFetch(eventSet.getFetch());
}
// merge scope
if (Strings.isBlank(me.getScope())) {
me.setScope(it.getScope());
}
// merge arguments
if (!me.hasArgs())
me.copyArgys(it.getArgs());
// merge fields
for (IocField fld : it.getFields().values())
if (!me.hasField(fld.getName()))
me.addField(fld);
return me;
}
public static IocValue convert(String value, boolean dftRefer) {
IocValue iocValue = new IocValue();
if (dftRefer && value.startsWith(":")) {
iocValue.setType(IocValue.TYPE_NORMAL);
iocValue.setValue(value.substring(1));
// } else if (value.startsWith("$")) {
// iocValue.setType(IocValue.TYPE_REFER);
// iocValue.setValue(value.substring(1));
}
else if (value.contains(":")) {
String type = value.substring(0, value.indexOf(':'));
if (IocValue.types.contains(type)) {
iocValue.setType(type);
if (value.endsWith(":")) {
iocValue.setValue("");
}
else
iocValue.setValue(value.substring(value.indexOf(':') + 1));
} else {
iocValue.setType(IocValue.TYPE_NORMAL);
iocValue.setValue(value);
}
} else {
// XXX 兼容性改变, 1.b.52 开始默认就是refer, 如果真的要输入常量
//log.info("auto set as refer:" + value);
iocValue.setType("refer");
iocValue.setValue(value);
}
return iocValue;
}
public static Object self(Object obj) {
return obj;
}
public static IocObject wrap(Object obj) {
IocObject iobj = new IocObject();
iobj.setType(obj.getClass());
iobj.setFactory(Iocs.class.getName() + ":self");
IocValue ival = new IocValue(IocValue.TYPE_NORMAL, obj);
iobj.addArg(ival);
return iobj;
}
}