Beispiel #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;
  }
Beispiel #2
0
  public static <T> JsonObject objectToJson(T object) {

    JsonObjectBuilder json = Json.createObjectBuilder();

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

    for (Method method : methods) {
      if (method.isAnnotationPresent(JsonIgnore.class)) {
        continue;
      }
      if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
        String name = Introspector.decapitalize(method.getName().substring(3));
        try {
          if (method.getReturnType().equals(boolean.class)) {
            json.add(name, (Boolean) method.invoke(object));
          } else if (method.getReturnType().equals(int.class)) {
            json.add(name, (Integer) method.invoke(object));
          } else if (method.getReturnType().equals(long.class)) {
            json.add(name, (Long) method.invoke(object));
          } else if (method.getReturnType().equals(double.class)) {
            json.add(name, (Double) method.invoke(object));
          } else if (method.getReturnType().equals(String.class)) {
            String value = (String) method.invoke(object);
            if (value != null) {
              json.add(name, value);
            }
          } else if (method.getReturnType().equals(Date.class)) {
            Date value = (Date) method.invoke(object);
            if (value != null) {
              json.add(name, dateFormat.format(value));
            }
          } else if (method.getReturnType().equals(Map.class)) {
            json.add(name, MiscFormatter.toJson((Map) method.invoke(object)));
          }
        } catch (IllegalAccessException | InvocationTargetException error) {
        }
      }
    }

    return json.build();
  }
Beispiel #3
0
  public static <T extends Factory> T objectFromJson(JsonObject json, T prototype)
      throws ParseException {
    T object = (T) prototype.create();

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

    for (final Method method : methods) {
      if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) {

        final String name = Introspector.decapitalize(method.getName().substring(3));
        Class<?> parameterType = method.getParameterTypes()[0];

        if (json.containsKey(name))
          try {
            if (parameterType.equals(boolean.class)) {
              method.invoke(object, json.getBoolean(name));
            } else if (parameterType.equals(int.class)) {
              method.invoke(object, json.getJsonNumber(name).intValue());
            } else if (parameterType.equals(long.class)) {
              if (json.get(name).getValueType() == JsonValue.ValueType.NUMBER) {
                method.invoke(object, json.getJsonNumber(name).longValue());
              }
            } else if (parameterType.equals(double.class)) {
              method.invoke(object, json.getJsonNumber(name).doubleValue());
            } else if (parameterType.equals(String.class)) {
              method.invoke(object, json.getString(name));
            } else if (parameterType.equals(Date.class)) {
              method.invoke(object, dateFormat.parse(json.getString(name)));
            } else if (parameterType.equals(Map.class)) {
              method.invoke(object, MiscFormatter.fromJson(json.getJsonObject(name)));
            }
          } catch (IllegalAccessException | InvocationTargetException error) {
          }
      }
    }

    return object;
  }