From ef5f959f737a6277b146e5eb4e34827638b1f93c Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 30 Sep 2020 23:06:59 -0700 Subject: Various minor code cleanup --- .../java/org/traccar/database/QueryBuilder.java | 116 ++++++++------------- 1 file changed, 46 insertions(+), 70 deletions(-) (limited to 'src/main/java/org/traccar/database/QueryBuilder.java') diff --git a/src/main/java/org/traccar/database/QueryBuilder.java b/src/main/java/org/traccar/database/QueryBuilder.java index 5528b2320..f57f27e88 100644 --- a/src/main/java/org/traccar/database/QueryBuilder.java +++ b/src/main/java/org/traccar/database/QueryBuilder.java @@ -318,96 +318,72 @@ public final class QueryBuilder { final Class parameterType, final Method method, final String name) { if (parameterType.equals(boolean.class)) { - processors.add(new ResultSetProcessor() { - @Override - public void process(T object, ResultSet resultSet) throws SQLException { - try { - method.invoke(object, resultSet.getBoolean(name)); - } catch (IllegalAccessException | InvocationTargetException error) { - LOGGER.warn("Set property error", error); - } + processors.add((object, resultSet) -> { + try { + method.invoke(object, resultSet.getBoolean(name)); + } catch (IllegalAccessException | InvocationTargetException error) { + LOGGER.warn("Set property error", error); } }); } else if (parameterType.equals(int.class)) { - processors.add(new ResultSetProcessor() { - @Override - public void process(T object, ResultSet resultSet) throws SQLException { - try { - method.invoke(object, resultSet.getInt(name)); - } catch (IllegalAccessException | InvocationTargetException error) { - LOGGER.warn("Set property error", error); - } + processors.add((object, resultSet) -> { + try { + method.invoke(object, resultSet.getInt(name)); + } catch (IllegalAccessException | InvocationTargetException error) { + LOGGER.warn("Set property error", error); } }); } else if (parameterType.equals(long.class)) { - processors.add(new ResultSetProcessor() { - @Override - public void process(T object, ResultSet resultSet) throws SQLException { - try { - method.invoke(object, resultSet.getLong(name)); - } catch (IllegalAccessException | InvocationTargetException error) { - LOGGER.warn("Set property error", error); - } + processors.add((object, resultSet) -> { + try { + method.invoke(object, resultSet.getLong(name)); + } catch (IllegalAccessException | InvocationTargetException error) { + LOGGER.warn("Set property error", error); } }); } else if (parameterType.equals(double.class)) { - processors.add(new ResultSetProcessor() { - @Override - public void process(T object, ResultSet resultSet) throws SQLException { - try { - method.invoke(object, resultSet.getDouble(name)); - } catch (IllegalAccessException | InvocationTargetException error) { - LOGGER.warn("Set property error", error); - } + processors.add((object, resultSet) -> { + try { + method.invoke(object, resultSet.getDouble(name)); + } catch (IllegalAccessException | InvocationTargetException error) { + LOGGER.warn("Set property error", error); } }); } else if (parameterType.equals(String.class)) { - processors.add(new ResultSetProcessor() { - @Override - public void process(T object, ResultSet resultSet) throws SQLException { - try { - method.invoke(object, resultSet.getString(name)); - } catch (IllegalAccessException | InvocationTargetException error) { - LOGGER.warn("Set property error", error); - } + processors.add((object, resultSet) -> { + try { + method.invoke(object, resultSet.getString(name)); + } catch (IllegalAccessException | InvocationTargetException error) { + LOGGER.warn("Set property error", error); } }); } else if (parameterType.equals(Date.class)) { - processors.add(new ResultSetProcessor() { - @Override - public void process(T object, ResultSet resultSet) throws SQLException { - try { - Timestamp timestamp = resultSet.getTimestamp(name); - if (timestamp != null) { - method.invoke(object, new Date(timestamp.getTime())); - } - } catch (IllegalAccessException | InvocationTargetException error) { - LOGGER.warn("Set property error", error); + processors.add((object, resultSet) -> { + try { + Timestamp timestamp = resultSet.getTimestamp(name); + if (timestamp != null) { + method.invoke(object, new Date(timestamp.getTime())); } + } catch (IllegalAccessException | InvocationTargetException error) { + LOGGER.warn("Set property error", error); } }); } else if (parameterType.equals(byte[].class)) { - processors.add(new ResultSetProcessor() { - @Override - public void process(T object, ResultSet resultSet) throws SQLException { - try { - method.invoke(object, resultSet.getBytes(name)); - } catch (IllegalAccessException | InvocationTargetException error) { - LOGGER.warn("Set property error", error); - } + processors.add((object, resultSet) -> { + try { + method.invoke(object, resultSet.getBytes(name)); + } catch (IllegalAccessException | InvocationTargetException error) { + LOGGER.warn("Set property error", error); } }); } else { - processors.add(new ResultSetProcessor() { - @Override - public void process(T object, ResultSet resultSet) throws SQLException { - String value = resultSet.getString(name); - if (value != null && !value.isEmpty()) { - try { - method.invoke(object, Context.getObjectMapper().readValue(value, parameterType)); - } catch (InvocationTargetException | IllegalAccessException | IOException error) { - LOGGER.warn("Set property error", error); - } + processors.add((object, resultSet) -> { + String value = resultSet.getString(name); + if (value != null && !value.isEmpty()) { + try { + method.invoke(object, Context.getObjectMapper().readValue(value, parameterType)); + } catch (InvocationTargetException | IllegalAccessException | IOException error) { + LOGGER.warn("Set property error", error); } } }); @@ -453,12 +429,12 @@ public final class QueryBuilder { while (resultSet.next()) { try { - T object = clazz.newInstance(); + T object = clazz.getDeclaredConstructor().newInstance(); for (ResultSetProcessor processor : processors) { processor.process(object, resultSet); } result.add(object); - } catch (InstantiationException | IllegalAccessException e) { + } catch (ReflectiveOperationException e) { throw new IllegalArgumentException(); } } -- cgit v1.2.3