From ec5dacfc3a55bc9b41c99afc055659b67a563cdd Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 21 Oct 2017 23:08:46 -0700 Subject: [PATCH] Fixed issues around storing Longs in the database. --- .../flatfile/FlatFileDatabaseHandler.java | 140 ++++++++++-------- .../managers/AbstractDatabaseHandler.java | 8 +- .../database/mysql/MySQLDatabaseHandler.java | 2 +- 3 files changed, 87 insertions(+), 63 deletions(-) diff --git a/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseHandler.java b/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseHandler.java index 3967eb46c..04ab935e8 100644 --- a/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseHandler.java +++ b/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseHandler.java @@ -37,6 +37,7 @@ import us.tastybento.bskyblock.util.Util; public class FlatFileDatabaseHandler extends AbstractDatabaseHandler { private static final String DATABASE_FOLDER_NAME = "database"; + private static final boolean DEBUG = false; public FlatFileDatabaseHandler(BSkyBlock plugin, Class type, DatabaseConnecter databaseConnecter) { super(plugin, type, databaseConnecter); } @@ -135,66 +136,81 @@ public class FlatFileDatabaseHandler extends AbstractDatabaseHandler { PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), type); Method method = propertyDescriptor.getWriteMethod(); - //plugin.getLogger().info("DEBUG: " + field.getName() + ": " + propertyDescriptor.getPropertyType().getTypeName()); - if (propertyDescriptor.getPropertyType().equals(HashMap.class)) { + if (DEBUG) + plugin.getLogger().info("DEBUG: " + field.getName() + ": " + propertyDescriptor.getPropertyType().getTypeName()); + if (config.contains(field.getName())) { + if (propertyDescriptor.getPropertyType().equals(HashMap.class)) { + + // Note that we have no idea what type this is + List collectionTypes = Util.getCollectionParameterTypes(method); + // collectionTypes should be 2 long + Type keyType = collectionTypes.get(0); + Type valueType = collectionTypes.get(1); + if (DEBUG) + plugin.getLogger().info("DEBUG: is HashMap<" + keyType.getTypeName() + ", " + valueType.getTypeName() + ">"); + // TODO: this may not work with all keys. Further serialization may be required. + HashMap value = new HashMap(); + for (String key : config.getConfigurationSection(field.getName()).getKeys(false)) { + Object mapKey = deserialize(key,Class.forName(keyType.getTypeName())); + Object mapValue = deserialize(config.get(field.getName() + "." + key), Class.forName(valueType.getTypeName())); + if (DEBUG) { + plugin.getLogger().info("DEBUG: mapKey = " + mapKey + " (" + mapKey.getClass().getCanonicalName() + ")"); + plugin.getLogger().info("DEBUG: mapValue = " + mapValue + " (" + mapValue.getClass().getCanonicalName() + ")"); + } + value.put(mapKey, mapValue); + } + method.invoke(instance, value); + } else if (propertyDescriptor.getPropertyType().equals(Set.class)) { + if (DEBUG) { + plugin.getLogger().info("DEBUG: is Set " + propertyDescriptor.getReadMethod().getGenericReturnType().getTypeName()); + plugin.getLogger().info("DEBUG: adding a set"); + } + // Loop through the collection resultset + // Note that we have no idea what type this is + List collectionTypes = Util.getCollectionParameterTypes(method); + // collectionTypes should be only 1 long + Type setType = collectionTypes.get(0); + if (DEBUG) + plugin.getLogger().info("DEBUG: is HashSet<" + setType.getTypeName() + ">"); + Set value = new HashSet(); + if (DEBUG) { + plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes); + plugin.getLogger().info("DEBUG: setType = " + setType.getTypeName()); + } + for (Object listValue: config.getList(field.getName())) { + //plugin.getLogger().info("DEBUG: collectionResultSet size = " + collectionResultSet.getFetchSize()); + ((Set) value).add(deserialize(listValue,Class.forName(setType.getTypeName()))); + } + + // TODO: this may not work with all keys. Further serialization may be required. + //Set value = new HashSet((List) config.getList(field.getName())); + method.invoke(instance, value); + } else if (propertyDescriptor.getPropertyType().equals(ArrayList.class)) { + //plugin.getLogger().info("DEBUG: is Set " + propertyDescriptor.getReadMethod().getGenericReturnType().getTypeName()); + if (DEBUG) + plugin.getLogger().info("DEBUG: adding a set"); + // Loop through the collection resultset + // Note that we have no idea what type this is + List collectionTypes = Util.getCollectionParameterTypes(method); + // collectionTypes should be only 1 long + Type setType = collectionTypes.get(0); + List value = new ArrayList(); + //plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes); + //plugin.getLogger().info("DEBUG: setType = " + setType.getTypeName()); + for (Object listValue: config.getList(field.getName())) { + //plugin.getLogger().info("DEBUG: collectionResultSet size = " + collectionResultSet.getFetchSize()); + ((List) value).add(deserialize(listValue,Class.forName(setType.getTypeName()))); + } + // TODO: this may not work with all keys. Further serialization may be required. + //Set value = new HashSet((List) config.getList(field.getName())); + method.invoke(instance, value); + } else { + // Not a collection + Object value = config.get(field.getName()); + method.invoke(instance, deserialize(value,propertyDescriptor.getPropertyType())); - // Note that we have no idea what type this is - List collectionTypes = Util.getCollectionParameterTypes(method); - // collectionTypes should be 2 long - Type keyType = collectionTypes.get(0); - Type valueType = collectionTypes.get(1); - //plugin.getLogger().info("DEBUG: is HashMap<" + keyType.getTypeName() + ", " + valueType.getTypeName() + ">"); - // TODO: this may not work with all keys. Further serialization may be required. - HashMap value = new HashMap(); - for (String key : config.getConfigurationSection(field.getName()).getKeys(false)) { - Object mapKey = deserialize(key,Class.forName(keyType.getTypeName())); - Object mapValue = deserialize(config.get(field.getName() + "." + key), Class.forName(valueType.getTypeName())); - value.put(mapKey, mapValue); } - method.invoke(instance, value); - } else if (propertyDescriptor.getPropertyType().equals(Set.class)) { - //plugin.getLogger().info("DEBUG: is Set " + propertyDescriptor.getReadMethod().getGenericReturnType().getTypeName()); - //plugin.getLogger().info("DEBUG: adding a set"); - // Loop through the collection resultset - // Note that we have no idea what type this is - List collectionTypes = Util.getCollectionParameterTypes(method); - // collectionTypes should be only 1 long - Type setType = collectionTypes.get(0); - //plugin.getLogger().info("DEBUG: is HashSet<" + setType.getTypeName() + ">"); - Set value = new HashSet(); - //plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes); - //plugin.getLogger().info("DEBUG: setType = " + setType.getTypeName()); - for (Object listValue: config.getList(field.getName())) { - //plugin.getLogger().info("DEBUG: collectionResultSet size = " + collectionResultSet.getFetchSize()); - ((Set) value).add(deserialize(listValue,Class.forName(setType.getTypeName()))); - } - // TODO: this may not work with all keys. Further serialization may be required. - //Set value = new HashSet((List) config.getList(field.getName())); - method.invoke(instance, value); - } else if (propertyDescriptor.getPropertyType().equals(ArrayList.class)) { - //plugin.getLogger().info("DEBUG: is Set " + propertyDescriptor.getReadMethod().getGenericReturnType().getTypeName()); - //plugin.getLogger().info("DEBUG: adding a set"); - // Loop through the collection resultset - // Note that we have no idea what type this is - List collectionTypes = Util.getCollectionParameterTypes(method); - // collectionTypes should be only 1 long - Type setType = collectionTypes.get(0); - List value = new ArrayList(); - //plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes); - //plugin.getLogger().info("DEBUG: setType = " + setType.getTypeName()); - for (Object listValue: config.getList(field.getName())) { - //plugin.getLogger().info("DEBUG: collectionResultSet size = " + collectionResultSet.getFetchSize()); - ((List) value).add(deserialize(listValue,Class.forName(setType.getTypeName()))); - } - // TODO: this may not work with all keys. Further serialization may be required. - //Set value = new HashSet((List) config.getList(field.getName())); - method.invoke(instance, value); - } else { - // Not a collection - Object value = config.get(field.getName()); - method.invoke(instance, deserialize(value,propertyDescriptor.getPropertyType())); } - } return instance; @@ -301,7 +317,12 @@ public class FlatFileDatabaseHandler extends AbstractDatabaseHandler { } private Object deserialize(Object value, Class clazz) { - //plugin.getLogger().info("DEBUG: deserialize - class is " + clazz.getCanonicalName()); + if (DEBUG) { + plugin.getLogger().info("DEBUG: deserialize - class is " + clazz.getCanonicalName()); + plugin.getLogger().info("DEBUG: value is " + value); + if (value != null) + plugin.getLogger().info("DEBUG: value class is " + value.getClass().getCanonicalName()); + } if (value instanceof String && value.equals("null")) { // If the value is null as a string, return null return null; @@ -311,6 +332,9 @@ public class FlatFileDatabaseHandler extends AbstractDatabaseHandler { return value; } // Types that need to be deserialized + if (clazz.equals(Long.class) && value.getClass().equals(Integer.class)) { + return new Long((Integer)value); + } if (clazz.equals(UUID.class)) { value = UUID.fromString((String)value); } diff --git a/src/main/java/us/tastybento/bskyblock/database/managers/AbstractDatabaseHandler.java b/src/main/java/us/tastybento/bskyblock/database/managers/AbstractDatabaseHandler.java index 23a4c2856..e64c95c50 100644 --- a/src/main/java/us/tastybento/bskyblock/database/managers/AbstractDatabaseHandler.java +++ b/src/main/java/us/tastybento/bskyblock/database/managers/AbstractDatabaseHandler.java @@ -77,7 +77,7 @@ public abstract class AbstractDatabaseHandler { * instead of the names of the variables * @return */ - protected String getColumns(boolean usePlaceHolders) { + public String getColumns(boolean usePlaceHolders) { StringBuilder sb = new StringBuilder(); boolean first = true; @@ -109,7 +109,7 @@ public abstract class AbstractDatabaseHandler { * @throws SQLException * @throws ClassNotFoundException */ - protected abstract List loadObjects() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, SecurityException, ClassNotFoundException; + public abstract List loadObjects() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, SecurityException, ClassNotFoundException; /** * Creates a filled with values from the corresponding @@ -125,7 +125,7 @@ public abstract class AbstractDatabaseHandler { * @throws ClassNotFoundException * @throws SecurityException */ - protected abstract T loadObject(String uniqueId) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, SecurityException, ClassNotFoundException; + public abstract T loadObject(String uniqueId) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, SecurityException, ClassNotFoundException; /** * Save T into the corresponding database @@ -140,7 +140,7 @@ public abstract class AbstractDatabaseHandler { * @throws SQLException * @throws NoSuchMethodException */ - protected abstract void saveObject(T instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, SecurityException, InstantiationException, NoSuchMethodException; + public abstract void saveObject(T instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, SecurityException, InstantiationException, NoSuchMethodException; /** * Deletes the object with the unique id from the database diff --git a/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabaseHandler.java b/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabaseHandler.java index 7b13d3a39..93ebdcba8 100644 --- a/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabaseHandler.java +++ b/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabaseHandler.java @@ -540,7 +540,7 @@ public class MySQLDatabaseHandler extends AbstractDatabaseHandler { * @see us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler#selectObject(java.lang.String) */ @Override - protected T loadObject(String uniqueId) throws InstantiationException, + public T loadObject(String uniqueId) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, SecurityException, ClassNotFoundException { Connection connection = null;