-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented support for serializing Enums, refactoring
- Loading branch information
Showing
12 changed files
with
75 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...al/config/serializer/BiomeSerializer.java → ...ig/serializer/bukkit/BiomeSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...config/serializer/LocationSerializer.java → ...serializer/bukkit/LocationSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...config/serializer/MaterialSerializer.java → ...serializer/bukkit/MaterialSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...ig/serializer/PotionEffectSerializer.java → ...alizer/bukkit/PotionEffectSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/pl/mikigal/config/serializer/java/EnumSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package pl.mikigal.config.serializer.java; | ||
|
||
import pl.mikigal.config.BukkitConfiguration; | ||
import pl.mikigal.config.exception.InvalidConfigException; | ||
import pl.mikigal.config.serializer.Serializer; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* Built-in serializer for Enums | ||
* @see Enum | ||
* @see Serializer | ||
* @since 1.2.6 | ||
* @author Mikołaj Gałązka | ||
*/ | ||
public class EnumSerializer extends Serializer<Enum> { | ||
|
||
@Override | ||
protected void saveObject(String path, Enum object, BukkitConfiguration configuration) { | ||
configuration.set(path + ".value", object.toString()); | ||
configuration.set(path + ".type", object.getClass().getName()); | ||
} | ||
|
||
@Override | ||
public Enum deserialize(String path, BukkitConfiguration configuration) { | ||
String value = configuration.getString(path + ".value"); | ||
String classPath = configuration.getString(path + ".type"); | ||
Class<?> clazz; | ||
Method valueOfMethod; | ||
|
||
try { | ||
clazz = Class.forName(classPath); | ||
valueOfMethod = clazz.getMethod("valueOf", String.class); | ||
} catch (ClassNotFoundException | NoSuchMethodException e) { | ||
throw new RuntimeException("An error occurred while deserializing class '" + classPath + "'", e); | ||
} | ||
|
||
try { | ||
return (Enum<?>) valueOfMethod.invoke(null, value); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
throw new InvalidConfigException("Value " + value + " is not valid for type " + classPath, e); | ||
} | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
...gal/config/serializer/UUIDSerializer.java → ...onfig/serializer/java/UUIDSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.