aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/web/JsonConverter.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-12-07 10:09:08 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-12-07 10:09:08 +1300
commit5b57c0fdb81bae87ca487e1d0087f8927b3fd07f (patch)
treec3733bca54002ca694b6d7b83b147de87212c5eb /src/org/traccar/web/JsonConverter.java
parent33b2a5c9ab70ffe7bbdd074ce430920d2a0d3941 (diff)
downloadtrackermap-server-5b57c0fdb81bae87ca487e1d0087f8927b3fd07f.tar.gz
trackermap-server-5b57c0fdb81bae87ca487e1d0087f8927b3fd07f.tar.bz2
trackermap-server-5b57c0fdb81bae87ca487e1d0087f8927b3fd07f.zip
Replace factory method with reflection
Diffstat (limited to 'src/org/traccar/web/JsonConverter.java')
-rw-r--r--src/org/traccar/web/JsonConverter.java31
1 files changed, 7 insertions, 24 deletions
diff --git a/src/org/traccar/web/JsonConverter.java b/src/org/traccar/web/JsonConverter.java
index 2ef61fb13..8bf5607fb 100644
--- a/src/org/traccar/web/JsonConverter.java
+++ b/src/org/traccar/web/JsonConverter.java
@@ -35,7 +35,6 @@ import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.traccar.helper.Log;
-import org.traccar.model.Factory;
import org.traccar.model.MiscFormatter;
public final class JsonConverter {
@@ -43,32 +42,12 @@ public final class JsonConverter {
private JsonConverter() {
}
- private static <T> T newClassInstance(Class<T> clazz) {
- try {
- return clazz.newInstance();
- } catch (InstantiationException | IllegalAccessException e) {
- throw new IllegalArgumentException();
- }
- }
-
private static final DateTimeFormatter DATE_FORMAT = ISODateTimeFormat.dateTime();
public static Date parseDate(String value) {
return DATE_FORMAT.parseDateTime(value).toDate();
}
- public static <T extends Factory> T objectFromJson(Reader reader, T prototype) throws ParseException {
- try (JsonReader jsonReader = Json.createReader(reader)) {
- return objectFromJson(jsonReader.readObject(), prototype);
- }
- }
-
- public static <T extends Factory> T objectFromJson(JsonObject json, T prototype) {
- T object = (T) prototype.create();
- Method[] methods = object.getClass().getMethods();
- return objectFromJson(json, object, methods);
- }
-
public static <T> T objectFromJson(Reader reader, Class<T> clazz) throws ParseException {
try (JsonReader jsonReader = Json.createReader(reader)) {
return objectFromJson(jsonReader.readObject(), clazz);
@@ -76,9 +55,13 @@ public final class JsonConverter {
}
public static <T> T objectFromJson(JsonObject json, Class<T> clazz) {
- T object = newClassInstance(clazz);
- Method[] methods = object.getClass().getMethods();
- return objectFromJson(json, object, methods);
+ try {
+ T object = clazz.newInstance();
+ Method[] methods = object.getClass().getMethods();
+ return objectFromJson(json, object, methods);
+ } catch (InstantiationException | IllegalAccessException e) {
+ throw new IllegalArgumentException();
+ }
}
private static <T> T objectFromJson(JsonObject json, T object, Method[] methods) {