Package org.eweb4j.util

Examples of org.eweb4j.util.ReflectUtil


        return (List<T>) (_list.isEmpty() ? null : _list);
      } else {
        while (rs.next()) {
          t = cls.newInstance();
          ReflectUtil ru = new ReflectUtil(t);
          ORMConfigBean ormBean = ORMConfigBeanCache.get(cls);

          for (Iterator<Property> it = ormBean.getProperty()
              .iterator(); it.hasNext();) {
            Property p = it.next();
            String type = p.getType();
            if (type == null)
              continue;

            // 如果查询出来的字段名字没有,则不进行值注入
            boolean flag = false;
            for (String col : columns) {
              if (col.equalsIgnoreCase(p.getColumn())) {
                flag = true;
                continue;
              }
            }

            if (!flag)
              continue;

            Method m = ru.getSetter(p.getName());
            if (m == null)
              continue;

            Object value = rs.getObject(p.getColumn());
            if (value == null)
              continue;

            String v = String.valueOf(value);
            if (v == null) {
              v = "";
            }

            if ("int".equalsIgnoreCase(type)
                || "java.lang.Integer".equalsIgnoreCase(type)) {
              if ("".equals(v.trim())) {
                v = "0";
              }
              m.invoke(t, Integer.parseInt(v));
            } else if ("long".equalsIgnoreCase(type)
                || "java.lang.Long".equalsIgnoreCase(type)) {
              if ("".equals(v.trim())) {
                v = "0";
              }
              m.invoke(t, Long.parseLong(v));
            } else if ("float".equalsIgnoreCase(type)
                || "java.lang.Float".equalsIgnoreCase(type)) {
              if ("".equals(v.trim())) {
                v = "0.0";
              }
              m.invoke(t, Float.parseFloat(v));
            } else if ("double".equalsIgnoreCase(type)
                || "java.lang.Double".equalsIgnoreCase(type)) {
              if ("".equals(v.trim())) {
                v = "0.0";
              }

              m.invoke(t, Float.parseFloat(v));
            } else if ("string".equalsIgnoreCase(type)
                || "java.lang.String".equalsIgnoreCase(type)) {
              m.invoke(t, v);
            } else if ("boolean".equalsIgnoreCase(type) || "java.lang.Boolean".equalsIgnoreCase(type)){
              if ("1".equals(v.trim()) || "true".equals(v.trim())){
                m.invoke(t, true);
              }else if ("0".equals(v.trim()) || "false".equals(v.trim())){
                m.invoke(t, false);
              }
            } else if ("date".equalsIgnoreCase(type) || "java.sql.Date".equalsIgnoreCase(type) || "java.util.Date".equalsIgnoreCase(type)) {
              m.invoke(t, value);
            } else if (PropType.ONE_ONE.equalsIgnoreCase(type) || PropType.MANY_ONE.equalsIgnoreCase(type)) {
              if ("".equals(v))
                continue;

              Field field = ru.getField(p.getName());
              Class<?> tarClass = field.getType();

              String tarFKField = null;

              tarFKField = ORMConfigBeanUtil.getIdField(tarClass);
View Full Code Here


  @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)
View Full Code Here

   * @throws DAOException
   */
  public void init(Object t, List<Field> fields) throws DAOException {
    this.t = t;
    this.fields = fields;
    this.ru = new ReflectUtil(this.t);
    this.table = ORMConfigBeanUtil.getTable(t.getClass());
    // 主类的ID属性名
    this.idField = ORMConfigBeanUtil.getIdField(this.t.getClass());
    this.idColumn = ORMConfigBeanUtil.getIdColumn(this.t.getClass());
    this.idGetter = ru.getGetter(idField);
View Full Code Here

      try {
        Object _tarObj = tarGetter.invoke(t);
        Object tarObj = null;
        boolean flag = false;
        if (_tarObj != null) {
          Method tarFKFieldGetter = new ReflectUtil(_tarObj).getGetter(tarFKField);
          if (tarFKFieldGetter != null && tarFKFieldGetter.invoke(_tarObj) != null)
            tarObj = DAOFactory.getSelectDAO(dsName).selectOne(_tarObj, tarFKField);
          else
            flag = true;
        } else
View Full Code Here

  }

  public void init(Object t, List<Field> fields) throws DAOException {
    this.t = t;
    this.fields = fields;
    this.ru = new ReflectUtil(this.t);
    this.table = ORMConfigBeanUtil.getTable(this.t.getClass());
    // 主类的ID属性名
    this.idField = ORMConfigBeanUtil.getIdField(this.t.getClass());
    this.idSetter = ru.getSetter(idField);
    if (this.idSetter == null)
View Full Code Here

          for (int i = 0; i < tarList.size(); i++) {
            Object tarObj = tarList.get(i);
            String from = froms[0].name();
            String to = tos[0].name();
   
            ReflectUtil tarRu = new ReflectUtil(tarObj);
            Method tarIdGetter = tarRu.getGetter(tarIdField);
            Object _tarIdVal = null;
   
            try {
              _tarIdVal = tarIdGetter.invoke(tarObj);
            } catch (Exception e) {
              throw new DAOException(tarIdGetter + " invoke exception ",e);
            }
   
            if (_tarIdVal == null)
              continue;
   
            String tarIdVal = String.valueOf(_tarIdVal);
            Object tempObj = DAOFactory.getSelectDAO(dsName).selectOne(tarClass, new String[] { tarIdField },new String[] { tarIdVal });
   
            if (tempObj == null) {
              // 如果目标对象不存在于数据库,则将目标对象插入到数据库
              Object tarIdValObj = DAOFactory.getInsertDAO(dsName).insert(tarObj);
              // 将获取到的id值注入到tarObj中
              Method tarIdSetter = tarRu.getSetter(tarIdField);
              try {
                tarIdSetter.invoke(tarObj, tarIdValObj);
              } catch (Exception e) {
                throw new DAOException(tarIdSetter + " invoke exception ", e);
              }
View Full Code Here

        String _format = "delete from %s where %s = ? and %s = ?";
        for (int i = 0; i < tarList.size(); i++) {
          Object tarObj = tarList.get(i);
          if (tarObj == null)
            continue;
          ReflectUtil tarRu = new ReflectUtil(tarObj);
          String tarIdField = ORMConfigBeanUtil.getIdField(tarClass);
          Method tarIdGetter = tarRu.getGetter(tarIdField);
          Object toValObj = null;

          try {
            toValObj = tarIdGetter.invoke(tarObj);
          } catch (Exception e) {
View Full Code Here

        tarList = (List<?>) tarGetter.invoke(t);

        if (tarList != null && tarList.size() > 0) {
          for (int i = 0; i < tarList.size(); i++) {
            Object tarObj = tarList.get(i);
            ReflectUtil tarRu = new ReflectUtil(tarObj);
            String tarIdField = ORMConfigBeanUtil.getIdField(tarClass);
            Method tarIdGetter = tarRu.getGetter(tarIdField);
            Object tarIdValObj = tarIdGetter.invoke(tarObj);
            if (tarIdValObj == null)
              continue;
            String tarIdVal = String.valueOf(tarIdValObj);
            // 查询 select %s from {tarTable} where {tarIdColumn} = {tarIdVal}
View Full Code Here

          && !clsName.endsWith("Model")) {
       
        return ;
      }
     
      ReflectUtil ru = new ReflectUtil(t);
      Field[] fields = null;
      if (fieldNames == null || fieldNames.length == 0) {
        fields = ru.getFields();
      } else {
        List<Field> fieldList = new ArrayList<Field>();
        for (String n : fieldNames) {
          if (n != null && !"".equals(n.trim())) {
            Field f = ru.getField(n);
            if (f != null) {
              fieldList.add(f);
            }
          }
        }
        if (fieldList.size() > 0) {
          fields = fieldList.toArray(new Field[] {});
        }
      }
      if (fields != null) {
        List<Field> oneList = new ArrayList<Field>();
        List<Field> manyList = new ArrayList<Field>();
        List<Field> manyManyList = new ArrayList<Field>();
        for (Field f : fields) {
          Method getter = ru.getGetter(f.getName());

          if (f.isAnnotationPresent(OneToMany.class)
              || (getter != null && getter.isAnnotationPresent(OneToMany.class))){
            manyList.add(f);
          } else if (f.isAnnotationPresent(ManyToOne.class)
View Full Code Here

    }

    StringBuilder condition = new StringBuilder();
    StringBuilder valuesSb = new StringBuilder();
    condition.append(idColumn + " = ");
    ReflectUtil ru = new ReflectUtil(t);
    try {
      if (map == null) {
        Method idGetter = ru.getGetter(idField);
        if (idGetter == null)
          throw new SqlCreateException("can not find id getter");
        idValue = idGetter.invoke(t);
      }

      condition.append("'" + idValue + "'");

      for (int i = 0; i < columns.length; i++) {
        String column = columns[i];
        String field = fields[i];
        Object value = null;
        // id 字段不允许
        if (idColumn != null && idColumn.equalsIgnoreCase(column))
          continue;

        if (map != null && values != null) {
          value = values[i];
        } else {
          Method getter = ru.getGetter(field);
          if (getter == null)
            continue;

          Object _value = getter.invoke(t);
          if (_value == null)
            continue;

          if (ClassUtil.isPojo(_value.getClass())) {
            Field f = ru.getField(field);
            OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
            if (oneAnn == null)
              oneAnn = f.getAnnotation(OneToOne.class);
           
            ManyToOne manyToOneAnn = null;
            if (oneAnn == null){
              manyToOneAnn = getter.getAnnotation(ManyToOne.class);
              if (manyToOneAnn == null)
                manyToOneAnn = f.getAnnotation(ManyToOne.class);
             
            }
           
            if (oneAnn != null || manyToOneAnn != null) {
              ReflectUtil tarRu = new ReflectUtil(_value);
              String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());

              Method tarFKGetter = tarRu.getGetter(tarFKField);
              value = tarFKGetter.invoke(_value);
            }
          }

          if (value == null)
View Full Code Here

TOP

Related Classes of org.eweb4j.util.ReflectUtil

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.