示例#1
0
  public QueryBuilder setObject(Object object) throws SQLException {

    Method[] methods = object.getClass().getMethods();

    for (Method method : methods) {
      if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
        String name = method.getName().substring(3);
        try {
          if (method.getReturnType().equals(boolean.class)) {
            setBoolean(name, (Boolean) method.invoke(object));
          } else if (method.getReturnType().equals(int.class)) {
            setInteger(name, (Integer) method.invoke(object));
          } else if (method.getReturnType().equals(long.class)) {
            setLong(name, (Long) method.invoke(object));
          } else if (method.getReturnType().equals(double.class)) {
            setDouble(name, (Double) method.invoke(object));
          } else if (method.getReturnType().equals(String.class)) {
            setString(name, (String) method.invoke(object));
          } else if (method.getReturnType().equals(Date.class)) {
            setDate(name, (Date) method.invoke(object));
          } else if (method.getReturnType().equals(Map.class)) {
            if (Context.getConfig().getBoolean("database.xml")) {
              setString(name, MiscFormatter.toXmlString((Map) method.invoke(object)));
            } else {
              setString(name, MiscFormatter.toJsonString((Map) method.invoke(object)));
            }
          }
        } catch (IllegalAccessException | InvocationTargetException error) {
          Log.warning(error);
        }
      }
    }

    return this;
  }