Search in sources :

Example 21 with ReflectUtil

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;
}
Also used : Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 22 with ReflectUtil

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 });
            }
        }
    }
}
Also used : Hashtable(java.util.Hashtable) Method(java.lang.reflect.Method) ReflectUtil(org.eweb4j.util.ReflectUtil) Entry(java.util.Map.Entry) UploadFile(org.eweb4j.mvc.upload.UploadFile) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) UploadFile(org.eweb4j.mvc.upload.UploadFile)

Example 23 with ReflectUtil

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;
}
Also used : ReflectUtil(org.eweb4j.util.ReflectUtil) Method(java.lang.reflect.Method)

Example 24 with ReflectUtil

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;
}
Also used : ReflectUtil(org.eweb4j.util.ReflectUtil) HashMap(java.util.HashMap)

Example 25 with ReflectUtil

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;
}
Also used : ReflectUtil(org.eweb4j.util.ReflectUtil) Annotation(java.lang.annotation.Annotation)

Aggregations

ReflectUtil (org.eweb4j.util.ReflectUtil)49 Method (java.lang.reflect.Method)37 Field (java.lang.reflect.Field)26 JoinColumn (javax.persistence.JoinColumn)14 ManyToOne (javax.persistence.ManyToOne)10 ArrayList (java.util.ArrayList)9 OneToOne (javax.persistence.OneToOne)9 DAOException (org.eweb4j.orm.dao.DAOException)9 JoinTable (javax.persistence.JoinTable)7 HashMap (java.util.HashMap)6 ManyToMany (javax.persistence.ManyToMany)6 OneToMany (javax.persistence.OneToMany)6 ORMConfigBean (org.eweb4j.orm.config.bean.ORMConfigBean)6 Property (org.eweb4j.orm.config.bean.Property)6 Entry (java.util.Map.Entry)5 File (java.io.File)4 List (java.util.List)4 Map (java.util.Map)4 ConfigBean (org.eweb4j.config.bean.ConfigBean)4 Hashtable (java.util.Hashtable)3