From 5b6e3cec7e4b397f44a96d5b87a3ea7adc39065e Mon Sep 17 00:00:00 2001 From: Tastybento Date: Sun, 31 Dec 2017 12:51:57 -0800 Subject: [PATCH] Fixed database bugs. Made the code more robust by using isAssignableBy() to check super classes. --- .../database/flatfile/FlatFileDatabaseHandler.java | 13 ++++++------- .../database/mysql/MySQLDatabaseHandler.java | 14 +++++--------- 2 files changed, 11 insertions(+), 16 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 eaa5e2faf..69c113a1e 100644 --- a/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseHandler.java +++ b/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseHandler.java @@ -140,17 +140,16 @@ public class FlatFileDatabaseHandler extends AbstractDatabaseHandler { if (DEBUG) plugin.getLogger().info("DEBUG: " + field.getName() + ": " + propertyDescriptor.getPropertyType().getTypeName()); if (config.contains(field.getName())) { - if (propertyDescriptor.getPropertyType().equals(HashMap.class)) { - + if (Map.class.isAssignableFrom(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); if (DEBUG) - plugin.getLogger().info("DEBUG: is HashMap<" + keyType.getTypeName() + ", " + valueType.getTypeName() + ">"); + plugin.getLogger().info("DEBUG: is Map or HashMap<" + keyType.getTypeName() + ", " + valueType.getTypeName() + ">"); // TODO: this may not work with all keys. Further serialization may be required. - HashMap value = new HashMap(); + Map 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())); @@ -161,7 +160,7 @@ public class FlatFileDatabaseHandler extends AbstractDatabaseHandler { value.put(mapKey, mapValue); } method.invoke(instance, value); - } else if (propertyDescriptor.getPropertyType().equals(Set.class)) { + } else if (Set.class.isAssignableFrom(propertyDescriptor.getPropertyType())) { if (DEBUG) { plugin.getLogger().info("DEBUG: is Set " + propertyDescriptor.getReadMethod().getGenericReturnType().getTypeName()); plugin.getLogger().info("DEBUG: adding a set"); @@ -186,7 +185,7 @@ public class FlatFileDatabaseHandler extends AbstractDatabaseHandler { // 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)) { + } else if (List.class.isAssignableFrom(propertyDescriptor.getPropertyType())) { //plugin.getLogger().info("DEBUG: is Set " + propertyDescriptor.getReadMethod().getGenericReturnType().getTypeName()); if (DEBUG) plugin.getLogger().info("DEBUG: adding a set"); @@ -362,7 +361,7 @@ public class FlatFileDatabaseHandler extends AbstractDatabaseHandler { value = plugin.getServer().getWorld((String)value); } // Enums - if (clazz.getSuperclass() != null && clazz.getSuperclass().equals(Enum.class)) { + if (Enum.class.isAssignableFrom(clazz)) { //Custom enums are a child of the Enum class. // Find out the value try { 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 6bfb1a4ad..39625868b 100644 --- a/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabaseHandler.java +++ b/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabaseHandler.java @@ -669,10 +669,7 @@ public class MySQLDatabaseHandler extends AbstractDatabaseHandler { // once we get the value from the database Method method = propertyDescriptor.getWriteMethod(); // If the type is a Collection, then we need to deal with set and map tables - if (propertyDescriptor.getPropertyType().equals(Set.class) || - propertyDescriptor.getPropertyType().equals(Map.class) || - propertyDescriptor.getPropertyType().equals(HashMap.class) || - propertyDescriptor.getPropertyType().equals(ArrayList.class)) { + if (Collection.class.isAssignableFrom(propertyDescriptor.getPropertyType())) { // Collection //plugin.getLogger().info("DEBUG: Collection"); // TODO Get the values from the subsidiary tables. @@ -692,7 +689,7 @@ public class MySQLDatabaseHandler extends AbstractDatabaseHandler { ResultSet collectionResultSet = collStatement.executeQuery(); //plugin.getLogger().info("DEBUG: collectionResultSet = " + collectionResultSet.toString()); // Do single dimension types (set and list) - if (propertyDescriptor.getPropertyType().equals(Set.class)) { + if (Set.class.isAssignableFrom(propertyDescriptor.getPropertyType())) { if (DEBUG) plugin.getLogger().info("DEBUG: adding a set"); // Loop through the collection resultset @@ -708,7 +705,7 @@ public class MySQLDatabaseHandler extends AbstractDatabaseHandler { while (collectionResultSet.next()) { ((Set) value).add(deserialize(collectionResultSet.getObject(1),Class.forName(setType.getTypeName()))); } - } else if (propertyDescriptor.getPropertyType().equals(ArrayList.class)) { + } else if (List.class.isAssignableFrom(propertyDescriptor.getPropertyType())) { if (DEBUG) plugin.getLogger().info("DEBUG: Adding a list "); // Loop through the collection resultset @@ -723,8 +720,7 @@ public class MySQLDatabaseHandler extends AbstractDatabaseHandler { //plugin.getLogger().info("DEBUG: collectionResultSet size = " + collectionResultSet.getFetchSize()); ((List) value).add(deserialize(collectionResultSet.getObject(1),Class.forName(setType.getTypeName()))); } - } else if (propertyDescriptor.getPropertyType().equals(Map.class) || - propertyDescriptor.getPropertyType().equals(HashMap.class)) { + } else if (Map.class.isAssignableFrom(propertyDescriptor.getPropertyType())) { if (DEBUG) plugin.getLogger().info("DEBUG: Adding a map "); // Loop through the collection resultset @@ -806,7 +802,7 @@ public class MySQLDatabaseHandler extends AbstractDatabaseHandler { value = plugin.getServer().getWorld((String)value); } // Enums - if (clazz.getSuperclass() != null && clazz.getSuperclass().equals(Enum.class)) { + if (Enum.class.isAssignableFrom(clazz)) { //Custom enums are a child of the Enum class. // Find out the value try {