Fixed database bugs.

Made the code more robust by using isAssignableBy() to check super
classes.
This commit is contained in:
Tastybento 2017-12-31 12:51:57 -08:00
parent 109c0edca3
commit 5b6e3cec7e
2 changed files with 11 additions and 16 deletions

View File

@ -140,17 +140,16 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
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<Type> 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<Object,Object> value = new HashMap<Object, Object>();
Map<Object,Object> value = new HashMap<Object, Object>();
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<T> extends AbstractDatabaseHandler<T> {
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<T> extends AbstractDatabaseHandler<T> {
// TODO: this may not work with all keys. Further serialization may be required.
//Set<Object> value = new HashSet((List<Object>) 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<T> extends AbstractDatabaseHandler<T> {
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 {

View File

@ -669,10 +669,7 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// 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<T> extends AbstractDatabaseHandler<T> {
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<T> extends AbstractDatabaseHandler<T> {
while (collectionResultSet.next()) {
((Set<Object>) 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<T> extends AbstractDatabaseHandler<T> {
//plugin.getLogger().info("DEBUG: collectionResultSet size = " + collectionResultSet.getFetchSize());
((List<Object>) 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<T> extends AbstractDatabaseHandler<T> {
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 {