use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class BeanXMLReader method readRecursion.
@SuppressWarnings("unchecked")
private <T> T readRecursion(Element bean) throws Exception {
Class<T> clazz = (Class<T>) this.classes.get(bean.getName());
T o = clazz.newInstance();
ReflectUtil ru = new ReflectUtil(o);
Field[] fields = ru.getFields();
for (Field f : fields) {
String n = f.getName();
Method m = ru.getSetter(n);
if (m == null)
continue;
Skip skip = f.getAnnotation(Skip.class);
if (skip != null)
continue;
AttrTag attrTag = f.getAnnotation(AttrTag.class);
Writeonly writeonly = f.getAnnotation(Writeonly.class);
if (writeonly != null)
continue;
if (attrTag != null) {
if ("clazz".equals(n))
n = "class";
Attribute a = bean.attribute(n);
if (a != null)
m.invoke(o, a.getData());
} else if (ClassUtil.isPojo(f.getType())) {
Element el = bean.element(n);
if (el == null)
continue;
String cls = this.classes.get(el.getName()).getName();
Object object = Thread.currentThread().getContextClassLoader().loadClass(cls).newInstance();
// 递归
object = readRecursion(el);
m.invoke(o, object);
} else if (ClassUtil.isListClass(f)) {
List<?> eList = bean.elements(n);
if (eList == null)
continue;
List<Object> list = new ArrayList<Object>();
for (Iterator<?> it = eList.iterator(); it.hasNext(); ) {
Element e = (Element) it.next();
// 递归
list.add(readRecursion(e));
}
m.invoke(o, list);
} else if (ClassUtil.isListString(f)) {
List<?> eList = bean.elements(n);
if (eList == null)
continue;
List<String> list = new ArrayList<String>();
for (Iterator<?> it = eList.iterator(); it.hasNext(); ) {
Element e = (Element) it.next();
list.add(e.getText());
}
m.invoke(o, list);
} else {
if ("clazz".equals(n))
n = "class";
Element a = bean.element(n);
if (a == null)
continue;
m.invoke(o, String.valueOf(a.getData()));
}
}
return o;
}
use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class ParamUtil method injectFile.
public static void injectFile(Context context, ReflectUtil ru, String startName) throws Exception {
Hashtable<String, Object> hasPojo = new Hashtable<String, Object>();
paramForeach: for (Entry<String, List<UploadFile>> en : context.getUploadMap().entrySet()) {
String paramName = en.getKey();
List<UploadFile> paramValue = en.getValue();
if (paramValue == null || paramValue.size() == 0)
continue;
Method setter = null;
String[] pojoParamNames = paramName.split("\\.");
if (pojoParamNames.length > 1) {
Object lastPojo = ru.getObject();
if (startName != null && startName.trim().length() > 0) {
if (!pojoParamNames[0].equals(startName))
continue;
}
int lastIndex = pojoParamNames.length - 1;
for (int i = 0; i < lastIndex; i++) {
if (startName != null && startName.trim().length() > 0) {
if ((i + 1) == lastIndex)
break;
lastPojo = getLastPojo(lastPojo, pojoParamNames[i + 1], hasPojo);
} else {
lastPojo = getLastPojo(lastPojo, pojoParamNames[i], hasPojo);
}
if (lastPojo == null)
continue paramForeach;
}
String _paramName = pojoParamNames[lastIndex];
if (Map.class.isAssignableFrom(lastPojo.getClass())) {
Map<String, Object> map = (HashMap<String, Object>) lastPojo;
if (paramValue.size() <= 1)
map.put(_paramName, paramValue.get(0));
else
map.put(_paramName, paramValue);
lastPojo = map;
}
ReflectUtil lpRu = new ReflectUtil(lastPojo);
setter = lpRu.getSetter(_paramName);
if (setter == null)
continue;
Class<?> clazz = setter.getParameterTypes()[0];
if (File.class.isAssignableFrom(clazz)) {
setter.invoke(lastPojo, paramValue.get(0).getTmpFile());
} else if (File[].class.isAssignableFrom(clazz)) {
File[] files = new File[paramValue.size()];
for (int j = 0; j < files.length; j++) files[j] = paramValue.get(j).getTmpFile();
setter.invoke(lastPojo, new Object[] { files });
}
if (UploadFile.class.isAssignableFrom(clazz)) {
setter.invoke(lastPojo, paramValue.get(0));
} else if (UploadFile[].class.isAssignableFrom(clazz)) {
UploadFile[] files = new UploadFile[paramValue.size()];
for (int j = 0; j < files.length; j++) files[j] = paramValue.get(j);
setter.invoke(lastPojo, new Object[] { files });
}
} else {
setter = ru.getSetter(paramName);
if (setter == null)
continue;
Class<?> clazz = setter.getParameterTypes()[0];
if (File.class.isAssignableFrom(clazz)) {
setter.invoke(ru.getObject(), paramValue.get(0).getTmpFile());
} else if (File[].class.isAssignableFrom(clazz)) {
File[] files = new File[paramValue.size()];
for (int j = 0; j < files.length; j++) files[j] = paramValue.get(j).getTmpFile();
setter.invoke(ru.getObject(), new Object[] { files });
}
if (UploadFile.class.isAssignableFrom(clazz)) {
setter.invoke(ru.getObject(), paramValue.get(0));
} else if (UploadFile[].class.isAssignableFrom(clazz)) {
UploadFile[] files = new UploadFile[paramValue.size()];
for (int j = 0; j < files.length; j++) files[j] = paramValue.get(j);
setter.invoke(ru.getObject(), new Object[] { files });
}
}
}
}
use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class ParamUtil method getLastPojo.
private static Object getLastPojo(Object parentPojo, String pojoParamName, Hashtable<String, Object> hasPojo) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (parentPojo == null)
return null;
if (Map.class.isAssignableFrom(parentPojo.getClass())) {
return parentPojo;
}
ReflectUtil _ru = new ReflectUtil(parentPojo);
Method pojoSetter = _ru.getSetter(pojoParamName);
if (pojoSetter == null)
return parentPojo;
Class<?> pojoClass = pojoSetter.getParameterTypes()[0];
Object subPojo = hasPojo.get(pojoParamName);
if (subPojo == null) {
if (Map.class.isAssignableFrom(pojoClass)) {
subPojo = new HashMap<String, Object>();
} else {
subPojo = pojoClass.newInstance();
}
hasPojo.put(pojoParamName, subPojo);
}
pojoSetter.invoke(parentPojo, subPojo);
return subPojo;
}
use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class ActionExecution method injectParam2Map.
private Object injectParam2Map(String startName) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
ReflectUtil ru = new ReflectUtil(map);
this.injectActionCxt2Pojo(ru);
// 注入mvc action 请求参数
ParamUtil.injectParam(this.context, ru, startName);
return map;
}
use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class ActionExecution method initPojo.
private Object initPojo() throws Exception {
Class<?> clazz = ActionClassCache.get(this.context.getActionConfigBean().getClazz());
Annotation singletonAnn = clazz.getAnnotation(Singleton.class);
if (singletonAnn != null) {
this.actionObject = SingleBeanCache.get(clazz.getName());
if (this.actionObject == null) {
this.actionObject = clazz.newInstance();
SingleBeanCache.add(clazz.getName(), this.actionObject);
}
} else
this.actionObject = clazz.newInstance();
ru = new ReflectUtil(this.actionObject);
return this.actionObject;
}
Aggregations