diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-12-07 10:09:08 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-12-07 10:09:08 +1300 |
commit | 5b57c0fdb81bae87ca487e1d0087f8927b3fd07f (patch) | |
tree | c3733bca54002ca694b6d7b83b147de87212c5eb /src/org/traccar/database/QueryBuilder.java | |
parent | 33b2a5c9ab70ffe7bbdd074ce430920d2a0d3941 (diff) | |
download | trackermap-server-5b57c0fdb81bae87ca487e1d0087f8927b3fd07f.tar.gz trackermap-server-5b57c0fdb81bae87ca487e1d0087f8927b3fd07f.tar.bz2 trackermap-server-5b57c0fdb81bae87ca487e1d0087f8927b3fd07f.zip |
Replace factory method with reflection
Diffstat (limited to 'src/org/traccar/database/QueryBuilder.java')
-rw-r--r-- | src/org/traccar/database/QueryBuilder.java | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/src/org/traccar/database/QueryBuilder.java b/src/org/traccar/database/QueryBuilder.java index ca6335556..d0c1c649f 100644 --- a/src/org/traccar/database/QueryBuilder.java +++ b/src/org/traccar/database/QueryBuilder.java @@ -38,7 +38,6 @@ import javax.json.stream.JsonParsingException; import javax.sql.DataSource; import org.traccar.Context; import org.traccar.helper.Log; -import org.traccar.model.Factory; import org.traccar.model.MiscFormatter; public final class QueryBuilder { @@ -272,8 +271,8 @@ public final class QueryBuilder { void process(T object, ResultSet resultSet) throws SQLException; } - public <T extends Factory> T executeQuerySingle(T prototype) throws SQLException { - Collection<T> result = executeQuery(prototype); + public <T> T executeQuerySingle(Class<T> clazz) throws SQLException { + Collection<T> result = executeQuery(clazz); if (!result.isEmpty()) { return result.iterator().next(); } else { @@ -281,7 +280,7 @@ public final class QueryBuilder { } } - private <T extends Factory> void addProcessors( + private <T> void addProcessors( List<ResultSetProcessor<T>> processors, Class<?> parameterType, final Method method, final String name) { if (parameterType.equals(boolean.class)) { @@ -367,7 +366,7 @@ public final class QueryBuilder { } } - public <T extends Factory> Collection<T> executeQuery(T prototype) throws SQLException { + public <T> Collection<T> executeQuery(Class<T> clazz) throws SQLException { List<T> result = new LinkedList<>(); if (query != null) { @@ -380,7 +379,7 @@ public final class QueryBuilder { List<ResultSetProcessor<T>> processors = new LinkedList<>(); - Method[] methods = prototype.getClass().getMethods(); + Method[] methods = clazz.getMethods(); for (final Method method : methods) { if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) { @@ -404,11 +403,15 @@ public final class QueryBuilder { } while (resultSet.next()) { - T object = (T) prototype.create(); - for (ResultSetProcessor<T> processor : processors) { - processor.process(object, resultSet); + try { + T object = clazz.newInstance(); + for (ResultSetProcessor<T> processor : processors) { + processor.process(object, resultSet); + } + result.add(object); + } catch (InstantiationException | IllegalAccessException e) { + throw new IllegalArgumentException(); } - result.add(object); } } |