forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataPropertyRepository.java
More file actions
186 lines (163 loc) · 5.11 KB
/
DataPropertyRepository.java
File metadata and controls
186 lines (163 loc) · 5.11 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package act.data;
import act.app.App;
import act.app.AppServiceBase;
import act.util.ActContext;
import act.util.PropertySpec;
import org.joda.time.*;
import org.osgl.util.C;
import org.rythmengine.utils.S;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
/**
* Keep the property information of Data class
*/
public class DataPropertyRepository extends AppServiceBase<DataPropertyRepository> {
/**
* all classes that can NOT be decomposed in terms of get properties
*/
private Set<Class> terminators;
/**
* name of terminator classes.
*/
private Set<String> extendedTerminators;
/**
* Map a list of property path to class name
*/
private Map<String, List<String>> repo = C.newMap();
private OutputFieldsCache outputFieldsCache = new OutputFieldsCache();
public DataPropertyRepository(App app) {
super(app, true);
_init();
}
@Override
protected void releaseResources() {
extendedTerminators.clear();
terminators.clear();
repo.clear();
}
/**
* Returns the complete property list of a class
* @param c the class
* @return the property list of the class
*/
public synchronized List<String> propertyListOf(Class<?> c) {
String cn = c.getName();
List<String> ls = repo.get(cn);
if (ls != null) {
return ls;
}
ls = buildPropertyList(c);
repo.put(cn, ls);
return ls;
}
public List<String> outputFields(PropertySpec.MetaInfo spec, Class<?> componentClass, ActContext context) {
return outputFieldsCache.getOutputFields(spec, componentClass, context);
}
private List<String> buildPropertyList(Class c) {
Method[] ma = c.getMethods();
String context = "";
List<String> retLst = C.newList();
for (Method m: ma) {
buildPropertyPath(context, m, retLst);
}
return retLst;
}
private void buildPropertyPath(String context, Method m, List<String> repo) {
if (m.getParameterTypes().length > 0) {
return;
}
String name = m.getName();
if ("getClass".equals(name)) {
return;
}
String propName = "";
if (name.startsWith("get")) {
propName = getPropName(name);
} else if (name.startsWith("is")) {
propName = isPropName(name);
}
if (S.isEmpty(propName)) {
return;
}
Class c = m.getReturnType();
if (Class.class.equals(c)) {
return;
}
if (Enum.class.isAssignableFrom(c)) {
repo.add(context + propName);
return;
}
if (Iterable.class.isAssignableFrom(c)) {
Type t = m.getGenericReturnType();
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
Type[] ta = pt.getActualTypeArguments();
for (Type t0: ta) {
Class<?> c0 = (Class) t0;
List<String> retTypeProperties = propertyListOf(c0);
context = context + propName + ".";
for (String s: retTypeProperties) {
repo.add(context + s);
}
}
}
return;
}
if (terminators.contains(c) || extendedTerminators.contains(c.getName())) {
repo.add(context + propName);
return;
}
List<String> retTypeProperties = propertyListOf(c);
context = context + propName + ".";
for (String s : retTypeProperties) {
repo.add(context + s);
}
}
private static String getPropName(String name) {
return S.lowerFirst(name.substring(3));
}
private static String isPropName(String name) {
return S.lowerFirst(name.substring(2));
}
private void _init() {
Set<Class> s = C.newSet();
s.add(boolean.class);
s.add(byte.class);
s.add(char.class);
s.add(short.class);
s.add(int.class);
s.add(float.class);
s.add(long.class);
s.add(double.class);
s.add(Boolean.class);
s.add(Byte.class);
s.add(Character.class);
s.add(Short.class);
s.add(Integer.class);
s.add(Float.class);
s.add(Long.class);
s.add(Double.class);
s.add(String.class);
s.add(Date.class);
s.add(java.sql.Date.class);
s.add(Calendar.class);
s.add(DateTime.class);
s.add(Instant.class);
s.add(LocalDate.class);
s.add(LocalDateTime.class);
s.add(LocalTime.class);
terminators = s;
Set<String> s0 = C.newSet();
for (Class c: s) {
s0.add(c.getName());
}
s0.add("java.time.Instant");
s0.add("java.time.LocalTime");
s0.add("java.time.LocalDate");
s0.add("java.time.LocalDateTime");
s0.add("org.bson.types.ObjectId");
extendedTerminators = s0;
}
}