From 6e9fd72612bf2815c304bcd6a7e64f0cb2fa0ee1 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 27 Jul 2011 22:56:45 +0200 Subject: [PATCH] Soon to be used JarLoader. --- src/com/massivecraft/factions/Board.java | 14 +- src/com/massivecraft/factions/Conf.java | 4 +- src/com/massivecraft/factions/FPlayer.java | 8 +- src/com/massivecraft/factions/Faction.java | 8 +- src/com/massivecraft/factions/Factions.java | 31 +- .../factions/MyLocationTypeAdapter.java | 14 +- .../factions/commands/FCommandConfig.java | 2 + ...onymousAndLocalClassExclusionStrategy.java | 38 - .../factions/gson/BaseMapTypeAdapter.java | 44 - src/com/massivecraft/factions/gson/Cache.java | 51 - .../gson/CamelCaseSeparatorNamingPolicy.java | 75 -- .../gson/CircularReferenceException.java | 45 - .../gson/CompositionFieldNamingPolicy.java | 46 - .../gson/DefaultConstructorAllocator.java | 91 -- .../factions/gson/DefaultTypeAdapters.java | 1063 ---------------- .../gson/DelegatingJsonElementVisitor.java | 118 -- .../gson/DisjunctionExclusionStrategy.java | 53 - .../massivecraft/factions/gson/Escaper.java | 160 --- .../factions/gson/ExclusionStrategy.java | 109 -- ...ationDeserializationExclusionStrategy.java | 38 - ...otationSerializationExclusionStrategy.java | 38 - .../factions/gson/FieldAttributes.java | 255 ---- .../factions/gson/FieldNamingPolicy.java | 99 -- .../factions/gson/FieldNamingStrategy.java | 40 - .../factions/gson/FieldNamingStrategy2.java | 37 - .../gson/FieldNamingStrategy2Adapter.java | 38 - src/com/massivecraft/factions/gson/Gson.java | 570 --------- .../factions/gson/GsonBuilder.java | 715 ----------- .../gson/InnerClassExclusionStrategy.java | 43 - .../factions/gson/InstanceCreator.java | 92 -- .../factions/gson/JavaFieldNamingPolicy.java | 51 - .../massivecraft/factions/gson/JsonArray.java | 322 ----- .../gson/JsonArrayDeserializationVisitor.java | 111 -- .../gson/JsonDeserializationContext.java | 44 - .../JsonDeserializationContextDefault.java | 89 -- .../gson/JsonDeserializationVisitor.java | 115 -- .../factions/gson/JsonDeserializer.java | 88 -- .../JsonDeserializerExceptionWrapper.java | 72 -- .../factions/gson/JsonElement.java | 338 ----- .../factions/gson/JsonElementVisitor.java | 47 - .../factions/gson/JsonIOException.java | 46 - .../massivecraft/factions/gson/JsonNull.java | 72 -- .../factions/gson/JsonObject.java | 220 ---- .../JsonObjectDeserializationVisitor.java | 136 -- .../factions/gson/JsonParseException.java | 64 - .../factions/gson/JsonParser.java | 98 -- .../factions/gson/JsonPrimitive.java | 390 ------ .../gson/JsonSerializationContext.java | 49 - .../gson/JsonSerializationContextDefault.java | 65 - .../gson/JsonSerializationVisitor.java | 236 ---- .../factions/gson/JsonSerializer.java | 86 -- .../factions/gson/JsonStreamParser.java | 122 -- .../factions/gson/JsonSyntaxException.java | 47 - .../factions/gson/JsonTreeNavigator.java | 111 -- .../gson/LongSerializationPolicy.java | 74 -- .../LowerCamelCaseSeparatorNamingPolicy.java | 43 - .../factions/gson/LowerCaseNamingPolicy.java | 50 - .../massivecraft/factions/gson/LruCache.java | 57 - .../factions/gson/MapAsArrayTypeAdapter.java | 168 --- .../factions/gson/MapTypeAdapter.java | 78 -- .../gson/MappedObjectConstructor.java | 77 -- .../factions/gson/MemoryRefStack.java | 88 -- .../gson/ModifierBasedExclusionStrategy.java | 53 - .../gson/ModifyFirstLetterNamingPolicy.java | 108 -- .../factions/gson/NullExclusionStrategy.java | 36 - .../factions/gson/ObjectConstructor.java | 47 - .../factions/gson/ObjectNavigator.java | 133 -- .../factions/gson/ObjectTypePair.java | 144 --- src/com/massivecraft/factions/gson/Pair.java | 61 - .../gson/ParameterizedTypeHandlerMap.java | 215 ---- .../factions/gson/Primitives.java | 119 -- .../gson/RecursiveFieldNamingPolicy.java | 45 - .../gson/ReflectingFieldNavigator.java | 106 -- ...ameAnnotationInterceptingNamingPolicy.java | 48 - .../massivecraft/factions/gson/Streams.java | 190 --- .../gson/SyntheticFieldExclusionStrategy.java | 44 - .../factions/gson/UnsafeAllocator.java | 104 -- .../UpperCamelCaseSeparatorNamingPolicy.java | 45 - .../factions/gson/UpperCaseNamingPolicy.java | 48 - .../factions/gson/VersionConstants.java | 29 - .../gson/VersionExclusionStrategy.java | 68 - .../factions/gson/annotations/Expose.java | 79 -- .../gson/annotations/SerializedName.java | 74 -- .../factions/gson/annotations/Since.java | 61 - .../factions/gson/annotations/Until.java | 66 - .../gson/annotations/package-info.java | 6 - .../gson/internal/$Gson$Preconditions.java | 51 - .../factions/gson/internal/$Gson$Types.java | 590 --------- .../factions/gson/internal/package-info.java | 7 - .../factions/gson/package-info.java | 11 - .../factions/gson/reflect/TypeToken.java | 309 ----- .../factions/gson/reflect/package-info.java | 6 - .../factions/gson/stream/JsonReader.java | 1122 ----------------- .../factions/gson/stream/JsonScope.java | 71 -- .../factions/gson/stream/JsonToken.java | 85 -- .../factions/gson/stream/JsonWriter.java | 553 -------- .../gson/stream/MalformedJsonException.java | 44 - .../listeners/FactionsPlayerListener.java | 1 - .../massivecraft/factions/util/JarLoader.java | 59 + src/plugin.yml | 2 +- 100 files changed, 107 insertions(+), 12326 deletions(-) delete mode 100644 src/com/massivecraft/factions/gson/AnonymousAndLocalClassExclusionStrategy.java delete mode 100644 src/com/massivecraft/factions/gson/BaseMapTypeAdapter.java delete mode 100644 src/com/massivecraft/factions/gson/Cache.java delete mode 100644 src/com/massivecraft/factions/gson/CamelCaseSeparatorNamingPolicy.java delete mode 100644 src/com/massivecraft/factions/gson/CircularReferenceException.java delete mode 100644 src/com/massivecraft/factions/gson/CompositionFieldNamingPolicy.java delete mode 100644 src/com/massivecraft/factions/gson/DefaultConstructorAllocator.java delete mode 100644 src/com/massivecraft/factions/gson/DefaultTypeAdapters.java delete mode 100644 src/com/massivecraft/factions/gson/DelegatingJsonElementVisitor.java delete mode 100644 src/com/massivecraft/factions/gson/DisjunctionExclusionStrategy.java delete mode 100644 src/com/massivecraft/factions/gson/Escaper.java delete mode 100644 src/com/massivecraft/factions/gson/ExclusionStrategy.java delete mode 100644 src/com/massivecraft/factions/gson/ExposeAnnotationDeserializationExclusionStrategy.java delete mode 100644 src/com/massivecraft/factions/gson/ExposeAnnotationSerializationExclusionStrategy.java delete mode 100644 src/com/massivecraft/factions/gson/FieldAttributes.java delete mode 100644 src/com/massivecraft/factions/gson/FieldNamingPolicy.java delete mode 100644 src/com/massivecraft/factions/gson/FieldNamingStrategy.java delete mode 100644 src/com/massivecraft/factions/gson/FieldNamingStrategy2.java delete mode 100644 src/com/massivecraft/factions/gson/FieldNamingStrategy2Adapter.java delete mode 100644 src/com/massivecraft/factions/gson/Gson.java delete mode 100644 src/com/massivecraft/factions/gson/GsonBuilder.java delete mode 100644 src/com/massivecraft/factions/gson/InnerClassExclusionStrategy.java delete mode 100644 src/com/massivecraft/factions/gson/InstanceCreator.java delete mode 100644 src/com/massivecraft/factions/gson/JavaFieldNamingPolicy.java delete mode 100644 src/com/massivecraft/factions/gson/JsonArray.java delete mode 100644 src/com/massivecraft/factions/gson/JsonArrayDeserializationVisitor.java delete mode 100644 src/com/massivecraft/factions/gson/JsonDeserializationContext.java delete mode 100644 src/com/massivecraft/factions/gson/JsonDeserializationContextDefault.java delete mode 100644 src/com/massivecraft/factions/gson/JsonDeserializationVisitor.java delete mode 100644 src/com/massivecraft/factions/gson/JsonDeserializer.java delete mode 100644 src/com/massivecraft/factions/gson/JsonDeserializerExceptionWrapper.java delete mode 100644 src/com/massivecraft/factions/gson/JsonElement.java delete mode 100644 src/com/massivecraft/factions/gson/JsonElementVisitor.java delete mode 100644 src/com/massivecraft/factions/gson/JsonIOException.java delete mode 100644 src/com/massivecraft/factions/gson/JsonNull.java delete mode 100644 src/com/massivecraft/factions/gson/JsonObject.java delete mode 100644 src/com/massivecraft/factions/gson/JsonObjectDeserializationVisitor.java delete mode 100644 src/com/massivecraft/factions/gson/JsonParseException.java delete mode 100644 src/com/massivecraft/factions/gson/JsonParser.java delete mode 100644 src/com/massivecraft/factions/gson/JsonPrimitive.java delete mode 100644 src/com/massivecraft/factions/gson/JsonSerializationContext.java delete mode 100644 src/com/massivecraft/factions/gson/JsonSerializationContextDefault.java delete mode 100644 src/com/massivecraft/factions/gson/JsonSerializationVisitor.java delete mode 100644 src/com/massivecraft/factions/gson/JsonSerializer.java delete mode 100644 src/com/massivecraft/factions/gson/JsonStreamParser.java delete mode 100644 src/com/massivecraft/factions/gson/JsonSyntaxException.java delete mode 100644 src/com/massivecraft/factions/gson/JsonTreeNavigator.java delete mode 100644 src/com/massivecraft/factions/gson/LongSerializationPolicy.java delete mode 100644 src/com/massivecraft/factions/gson/LowerCamelCaseSeparatorNamingPolicy.java delete mode 100644 src/com/massivecraft/factions/gson/LowerCaseNamingPolicy.java delete mode 100644 src/com/massivecraft/factions/gson/LruCache.java delete mode 100644 src/com/massivecraft/factions/gson/MapAsArrayTypeAdapter.java delete mode 100644 src/com/massivecraft/factions/gson/MapTypeAdapter.java delete mode 100644 src/com/massivecraft/factions/gson/MappedObjectConstructor.java delete mode 100644 src/com/massivecraft/factions/gson/MemoryRefStack.java delete mode 100644 src/com/massivecraft/factions/gson/ModifierBasedExclusionStrategy.java delete mode 100644 src/com/massivecraft/factions/gson/ModifyFirstLetterNamingPolicy.java delete mode 100644 src/com/massivecraft/factions/gson/NullExclusionStrategy.java delete mode 100644 src/com/massivecraft/factions/gson/ObjectConstructor.java delete mode 100644 src/com/massivecraft/factions/gson/ObjectNavigator.java delete mode 100644 src/com/massivecraft/factions/gson/ObjectTypePair.java delete mode 100644 src/com/massivecraft/factions/gson/Pair.java delete mode 100644 src/com/massivecraft/factions/gson/ParameterizedTypeHandlerMap.java delete mode 100644 src/com/massivecraft/factions/gson/Primitives.java delete mode 100644 src/com/massivecraft/factions/gson/RecursiveFieldNamingPolicy.java delete mode 100644 src/com/massivecraft/factions/gson/ReflectingFieldNavigator.java delete mode 100644 src/com/massivecraft/factions/gson/SerializedNameAnnotationInterceptingNamingPolicy.java delete mode 100644 src/com/massivecraft/factions/gson/Streams.java delete mode 100644 src/com/massivecraft/factions/gson/SyntheticFieldExclusionStrategy.java delete mode 100644 src/com/massivecraft/factions/gson/UnsafeAllocator.java delete mode 100644 src/com/massivecraft/factions/gson/UpperCamelCaseSeparatorNamingPolicy.java delete mode 100644 src/com/massivecraft/factions/gson/UpperCaseNamingPolicy.java delete mode 100644 src/com/massivecraft/factions/gson/VersionConstants.java delete mode 100644 src/com/massivecraft/factions/gson/VersionExclusionStrategy.java delete mode 100644 src/com/massivecraft/factions/gson/annotations/Expose.java delete mode 100644 src/com/massivecraft/factions/gson/annotations/SerializedName.java delete mode 100644 src/com/massivecraft/factions/gson/annotations/Since.java delete mode 100644 src/com/massivecraft/factions/gson/annotations/Until.java delete mode 100644 src/com/massivecraft/factions/gson/annotations/package-info.java delete mode 100644 src/com/massivecraft/factions/gson/internal/$Gson$Preconditions.java delete mode 100644 src/com/massivecraft/factions/gson/internal/$Gson$Types.java delete mode 100644 src/com/massivecraft/factions/gson/internal/package-info.java delete mode 100644 src/com/massivecraft/factions/gson/package-info.java delete mode 100644 src/com/massivecraft/factions/gson/reflect/TypeToken.java delete mode 100644 src/com/massivecraft/factions/gson/reflect/package-info.java delete mode 100644 src/com/massivecraft/factions/gson/stream/JsonReader.java delete mode 100644 src/com/massivecraft/factions/gson/stream/JsonScope.java delete mode 100644 src/com/massivecraft/factions/gson/stream/JsonToken.java delete mode 100644 src/com/massivecraft/factions/gson/stream/JsonWriter.java delete mode 100644 src/com/massivecraft/factions/gson/stream/MalformedJsonException.java create mode 100644 src/com/massivecraft/factions/util/JarLoader.java diff --git a/src/com/massivecraft/factions/Board.java b/src/com/massivecraft/factions/Board.java index f7b70aa3..21609f00 100644 --- a/src/com/massivecraft/factions/Board.java +++ b/src/com/massivecraft/factions/Board.java @@ -12,11 +12,11 @@ import java.util.TreeMap; import org.bukkit.ChatColor; -import com.massivecraft.factions.gson.JsonArray; -import com.massivecraft.factions.gson.JsonElement; -import com.massivecraft.factions.gson.JsonObject; -import com.massivecraft.factions.gson.JsonParser; -import com.massivecraft.factions.gson.reflect.TypeToken; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.reflect.TypeToken; import com.massivecraft.factions.util.AsciiCompass; import com.massivecraft.factions.util.DiscUtil; import com.massivecraft.factions.util.TextUtil; @@ -248,7 +248,7 @@ public class Board { //Factions.log("Saving board to disk"); try { - DiscUtil.write(file, Factions.gson.toJson(dumpAsSaveFormat())); + DiscUtil.write(file, Factions.instance.gson.toJson(dumpAsSaveFormat())); } catch (Exception e) { e.printStackTrace(); Factions.log("Failed to save the board to disk."); @@ -270,7 +270,7 @@ public class Board { try { Type type = new TypeToken>>(){}.getType(); - Map> worldCoordIds = Factions.gson.fromJson(DiscUtil.read(file), type); + Map> worldCoordIds = Factions.instance.gson.fromJson(DiscUtil.read(file), type); loadFromSaveFormat(worldCoordIds); } catch (Exception e) { e.printStackTrace(); diff --git a/src/com/massivecraft/factions/Conf.java b/src/com/massivecraft/factions/Conf.java index f4ee3a83..e89f0ca9 100644 --- a/src/com/massivecraft/factions/Conf.java +++ b/src/com/massivecraft/factions/Conf.java @@ -180,7 +180,7 @@ public class Conf { //Factions.log("Saving config to disk."); try { - DiscUtil.write(file, Factions.gson.toJson(new Conf())); + DiscUtil.write(file, Factions.instance.gson.toJson(new Conf())); } catch (Exception e) { e.printStackTrace(); Factions.log("Failed to save the config to disk."); @@ -199,7 +199,7 @@ public class Conf { } try { - Factions.gson.fromJson(DiscUtil.read(file), Conf.class); + Factions.instance.gson.fromJson(DiscUtil.read(file), Conf.class); } catch (Exception e) { e.printStackTrace(); Factions.log("Failed to load the config from disk."); diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index dceefb68..5488979c 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -9,7 +9,7 @@ import java.util.Map.Entry; import org.bukkit.ChatColor; import org.bukkit.entity.Player; -import com.massivecraft.factions.gson.reflect.TypeToken; +import com.google.gson.reflect.TypeToken; import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.struct.Role; import com.massivecraft.factions.util.DiscUtil; @@ -623,7 +623,7 @@ public class FPlayer { } try { - DiscUtil.write(file, Factions.gson.toJson(playersToSave)); + DiscUtil.write(file, Factions.instance.gson.toJson(playersToSave)); } catch (Exception e) { e.printStackTrace(); Factions.log("Failed to save the players to disk."); @@ -643,7 +643,7 @@ public class FPlayer { try { Type type = new TypeToken>(){}.getType(); - Map instancesFromFile = Factions.gson.fromJson(DiscUtil.read(file), type); + Map instancesFromFile = Factions.instance.gson.fromJson(DiscUtil.read(file), type); instances.clear(); instances.putAll(instancesFromFile); } catch (Exception e) { @@ -707,7 +707,7 @@ public class FPlayer { String name = jsonFile.getName(); name = name.substring(0, name.length() - ext.length()); try { - FPlayer follower = Factions.gson.fromJson(DiscUtil.read(jsonFile), FPlayer.class); + FPlayer follower = Factions.instance.gson.fromJson(DiscUtil.read(jsonFile), FPlayer.class); follower.playerName = name; follower.lastLoginTime = System.currentTimeMillis(); instances.put(follower.playerName, follower); diff --git a/src/com/massivecraft/factions/Faction.java b/src/com/massivecraft/factions/Faction.java index 837487a5..b3235acf 100644 --- a/src/com/massivecraft/factions/Faction.java +++ b/src/com/massivecraft/factions/Faction.java @@ -10,7 +10,7 @@ import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.entity.Player; -import com.massivecraft.factions.gson.reflect.TypeToken; +import com.google.gson.reflect.TypeToken; import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.struct.Role; import com.massivecraft.factions.util.*; @@ -407,7 +407,7 @@ public class Faction { //Factions.log("Saving factions to disk"); try { - DiscUtil.write(file, Factions.gson.toJson(instances)); + DiscUtil.write(file, Factions.instance.gson.toJson(instances)); } catch (IOException e) { e.printStackTrace(); Factions.log("Failed to save the factions to disk due to I/O exception."); @@ -432,7 +432,7 @@ public class Faction { try { Type type = new TypeToken>(){}.getType(); - Map instancesFromFile = Factions.gson.fromJson(DiscUtil.read(file), type); + Map instancesFromFile = Factions.instance.gson.fromJson(DiscUtil.read(file), type); instances.clear(); instances.putAll(instancesFromFile); } catch (Exception e) { @@ -560,7 +560,7 @@ public class Faction { int id = Integer.parseInt(name); try { - Faction faction = Factions.gson.fromJson(DiscUtil.read(jsonFile), Faction.class); + Faction faction = Factions.instance.gson.fromJson(DiscUtil.read(jsonFile), Faction.class); faction.id = id; instances.put(faction.id, faction); Factions.log("loaded pre-1.1 faction "+id); diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index a646ebe9..d9a1c299 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -1,5 +1,6 @@ package com.massivecraft.factions; +import java.io.File; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; @@ -19,17 +20,18 @@ import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import com.massivecraft.factions.commands.*; -import com.massivecraft.factions.gson.Gson; -import com.massivecraft.factions.gson.GsonBuilder; import com.massivecraft.factions.listeners.FactionsBlockListener; import com.massivecraft.factions.listeners.FactionsChatEarlyListener; import com.massivecraft.factions.listeners.FactionsEntityListener; import com.massivecraft.factions.listeners.FactionsPlayerListener; +import com.massivecraft.factions.util.JarLoader; import com.nijiko.permissions.PermissionHandler; import com.nijikokun.bukkit.Permissions.Permissions; import com.earth2me.essentials.chat.EssentialsChat; import com.earth2me.essentials.chat.IEssentialsChatListener; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; /** * The data is saved to disk every 30min and on plugin disable. @@ -41,11 +43,7 @@ public class Factions extends JavaPlugin { public static Factions instance; private Integer saveTask = null; - public final static Gson gson = new GsonBuilder() - .setPrettyPrinting() - .excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE) - .registerTypeAdapter(Location.class, new MyLocationTypeAdapter()) - .create(); + public Gson gson; private final FactionsPlayerListener playerListener = new FactionsPlayerListener(); private final FactionsChatEarlyListener chatEarlyListener = new FactionsChatEarlyListener(); @@ -67,9 +65,23 @@ public class Factions extends JavaPlugin { @Override public void onEnable() { - log("=== INIT START ==="); + log("=== ENABLE START ==="); long timeInitStart = System.currentTimeMillis(); + // Load the gson library we require + File gsonfile = new File("./lib/gson.jar"); + if ( ! JarLoader.load(gsonfile)) { + log(Level.SEVERE, "Disabling myself as "+gsonfile+" is missing."); + this.getServer().getPluginManager().disablePlugin(this); + return; + } + + gson = new GsonBuilder() + .setPrettyPrinting() + .excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE) + .registerTypeAdapter(Location.class, new MyLocationTypeAdapter()) + .create(); + // Add the commands commands.add(new FCommandHelp()); commands.add(new FCommandAdmin()); @@ -156,7 +168,7 @@ public class Factions extends JavaPlugin { if (saveTask == null) saveTask = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new SaveTask(), saveTicks, saveTicks); - log("=== INIT DONE (Took "+(System.currentTimeMillis()-timeInitStart)+"ms) ==="); + log("=== ENABLE DONE (Took "+(System.currentTimeMillis()-timeInitStart)+"ms) ==="); } @Override @@ -167,7 +179,6 @@ public class Factions extends JavaPlugin { } saveAll(); unhookEssentialsChat(); - log("Disabled"); } // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/MyLocationTypeAdapter.java b/src/com/massivecraft/factions/MyLocationTypeAdapter.java index 4b792372..0c31e17b 100644 --- a/src/com/massivecraft/factions/MyLocationTypeAdapter.java +++ b/src/com/massivecraft/factions/MyLocationTypeAdapter.java @@ -6,13 +6,13 @@ import java.util.logging.Level; import org.bukkit.Location; import org.bukkit.World; -import com.massivecraft.factions.gson.JsonDeserializationContext; -import com.massivecraft.factions.gson.JsonDeserializer; -import com.massivecraft.factions.gson.JsonElement; -import com.massivecraft.factions.gson.JsonObject; -import com.massivecraft.factions.gson.JsonParseException; -import com.massivecraft.factions.gson.JsonSerializationContext; -import com.massivecraft.factions.gson.JsonSerializer; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; public class MyLocationTypeAdapter implements JsonDeserializer, JsonSerializer { diff --git a/src/com/massivecraft/factions/commands/FCommandConfig.java b/src/com/massivecraft/factions/commands/FCommandConfig.java index d7dea900..9bb1e4d2 100644 --- a/src/com/massivecraft/factions/commands/FCommandConfig.java +++ b/src/com/massivecraft/factions/commands/FCommandConfig.java @@ -156,6 +156,7 @@ public class FCommandConfig extends FBaseCommand { return; } + @SuppressWarnings("unchecked") Set matSet = (Set)target.get(null); // Material already present, so remove it @@ -174,6 +175,7 @@ public class FCommandConfig extends FBaseCommand { // Set else if (innerType == String.class) { + @SuppressWarnings("unchecked") Set stringSet = (Set)target.get(null); // String already present, so remove it diff --git a/src/com/massivecraft/factions/gson/AnonymousAndLocalClassExclusionStrategy.java b/src/com/massivecraft/factions/gson/AnonymousAndLocalClassExclusionStrategy.java deleted file mode 100644 index 044db718..00000000 --- a/src/com/massivecraft/factions/gson/AnonymousAndLocalClassExclusionStrategy.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2008 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.massivecraft.factions.gson; - -/** - * Strategy for excluding anonymous and local classes. - * - * @author Joel Leitch - */ -final class AnonymousAndLocalClassExclusionStrategy implements ExclusionStrategy { - - public boolean shouldSkipField(FieldAttributes f) { - return isAnonymousOrLocal(f.getDeclaredClass()); - } - - public boolean shouldSkipClass(Class clazz) { - return isAnonymousOrLocal(clazz); - } - - private boolean isAnonymousOrLocal(Class clazz) { - return !Enum.class.isAssignableFrom(clazz) - && (clazz.isAnonymousClass() || clazz.isLocalClass()); - } -} diff --git a/src/com/massivecraft/factions/gson/BaseMapTypeAdapter.java b/src/com/massivecraft/factions/gson/BaseMapTypeAdapter.java deleted file mode 100644 index 0503da1c..00000000 --- a/src/com/massivecraft/factions/gson/BaseMapTypeAdapter.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (C) 2011 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.massivecraft.factions.gson; - -import java.lang.reflect.Type; -import java.util.Map; - -/** - * Captures all the common/shared logic between the old, ({@link MapTypeAdapter}, and - * the new, {@link MapAsArrayTypeAdapter}, map type adapters. - * - * @author Joel Leitch - */ -abstract class BaseMapTypeAdapter - implements JsonSerializer>, JsonDeserializer> { - - protected static final JsonElement serialize(JsonSerializationContext context, - Object src, Type srcType) { - JsonSerializationContextDefault contextImpl = (JsonSerializationContextDefault) context; - return contextImpl.serialize(src, srcType, false); - } - - @SuppressWarnings("unchecked") - protected static final Map constructMapType( - Type mapType, JsonDeserializationContext context) { - JsonDeserializationContextDefault contextImpl = (JsonDeserializationContextDefault) context; - ObjectConstructor objectConstructor = contextImpl.getObjectConstructor(); - return (Map) objectConstructor.construct(mapType); - } -} \ No newline at end of file diff --git a/src/com/massivecraft/factions/gson/Cache.java b/src/com/massivecraft/factions/gson/Cache.java deleted file mode 100644 index 5c23de78..00000000 --- a/src/com/massivecraft/factions/gson/Cache.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) 2010 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.massivecraft.factions.gson; - -/** - * Defines generic cache interface. - * - * @author Inderjeet Singh - * @author Joel Leitch - */ -interface Cache { - - /** - * Adds the new value object into the cache for the given key. If the key already - * exists, then this method will override the value for the key. - * - * @param key the key identifier for the {@code value} object - * @param value the value object to store in the cache - */ - void addElement(K key, V value); - - /** - * Retrieve the cached value for the given {@code key}. - * - * @param key the key identifying the value - * @return the cached value for the given {@code key} - */ - V getElement(K key); - - /** - * Removes the value from the cache for the given key. - * - * @param key the key identifying the value to remove - * @return the value for the given {@code key} - */ - V removeElement(K key); -} diff --git a/src/com/massivecraft/factions/gson/CamelCaseSeparatorNamingPolicy.java b/src/com/massivecraft/factions/gson/CamelCaseSeparatorNamingPolicy.java deleted file mode 100644 index 571504cb..00000000 --- a/src/com/massivecraft/factions/gson/CamelCaseSeparatorNamingPolicy.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (C) 2008 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.massivecraft.factions.gson; - -import com.massivecraft.factions.gson.internal.$Gson$Preconditions; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Type; -import java.util.Collection; - -/** - * Converts the field name that uses camel-case define word separation into separate words that - * are separated by the provided {@code separatorString}. - * - *

The following is an example:

- *
- * class IntWrapper {
- *   public int integerField = 0;
- * }
- *
- * CamelCaseSeparatorNamingPolicy policy = new CamelCaseSeparatorNamingPolicy("_");
- * String translatedFieldName =
- *     policy.translateName(IntWrapper.class.getField("integerField"));
- *
- * assert("integer_Field".equals(translatedFieldName));
- * 
- * - * @author Joel Leitch - */ -final class CamelCaseSeparatorNamingPolicy extends RecursiveFieldNamingPolicy { - private final String separatorString; - - /** - * Constructs a new CamelCaseSeparatorNamingPolicy object that will add the - * {@code separatorString} between each of the words separated by camel case. - * - * @param separatorString the string value to place between words - * @throws IllegalArgumentException thrown if the {@code separatorString} parameter - * is null or empty. - */ - public CamelCaseSeparatorNamingPolicy(String separatorString) { - $Gson$Preconditions.checkNotNull(separatorString); - $Gson$Preconditions.checkArgument(!"".equals(separatorString)); - this.separatorString = separatorString; - } - - @Override - protected String translateName(String target, Type fieldType, - Collection annnotations) { - StringBuilder translation = new StringBuilder(); - for (int i = 0; i < target.length(); i++) { - char character = target.charAt(i); - if (Character.isUpperCase(character) && translation.length() != 0) { - translation.append(separatorString); - } - translation.append(character); - } - - return translation.toString(); - } -} diff --git a/src/com/massivecraft/factions/gson/CircularReferenceException.java b/src/com/massivecraft/factions/gson/CircularReferenceException.java deleted file mode 100644 index 079efe32..00000000 --- a/src/com/massivecraft/factions/gson/CircularReferenceException.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2009 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.massivecraft.factions.gson; - -/** - * Exception class to indicate a circular reference error. - * This class is not part of the public API and hence is not public. - * - * @author Inderjeet Singh - * @author Joel Leitch - */ -final class CircularReferenceException extends RuntimeException { - private static final long serialVersionUID = 7444343294106513081L; - private final Object offendingNode; - - CircularReferenceException(Object offendingNode) { - super("circular reference error"); - this.offendingNode = offendingNode; - } - - public IllegalStateException createDetailedException(FieldAttributes offendingField) { - StringBuilder msg = new StringBuilder(getMessage()); - if (offendingField != null) { - msg.append("\n ").append("Offending field: ").append(offendingField.getName() + "\n"); - } - if (offendingNode != null) { - msg.append("\n ").append("Offending object: ").append(offendingNode); - } - return new IllegalStateException(msg.toString(), this); - } -} diff --git a/src/com/massivecraft/factions/gson/CompositionFieldNamingPolicy.java b/src/com/massivecraft/factions/gson/CompositionFieldNamingPolicy.java deleted file mode 100644 index d9c26fef..00000000 --- a/src/com/massivecraft/factions/gson/CompositionFieldNamingPolicy.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2008 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.massivecraft.factions.gson; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Type; -import java.util.Collection; - -/** - * Performs numerous field naming translations wrapped up as one object. - * - * @author Joel Leitch - */ -abstract class CompositionFieldNamingPolicy extends RecursiveFieldNamingPolicy { - - private final RecursiveFieldNamingPolicy[] fieldPolicies; - - public CompositionFieldNamingPolicy(RecursiveFieldNamingPolicy... fieldNamingPolicies) { - if (fieldNamingPolicies == null) { - throw new NullPointerException("naming policies can not be null."); - } - this.fieldPolicies = fieldNamingPolicies; - } - - @Override - protected String translateName(String target, Type fieldType, Collection annotations) { - for (RecursiveFieldNamingPolicy policy : fieldPolicies) { - target = policy.translateName(target, fieldType, annotations); - } - return target; - } -} diff --git a/src/com/massivecraft/factions/gson/DefaultConstructorAllocator.java b/src/com/massivecraft/factions/gson/DefaultConstructorAllocator.java deleted file mode 100644 index 67b40725..00000000 --- a/src/com/massivecraft/factions/gson/DefaultConstructorAllocator.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (C) 2011 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.massivecraft.factions.gson; - - -import java.lang.reflect.Constructor; - -/** - * Use the default constructor on the class to instantiate an object. - * - * @author Joel Leitch - */ -final class DefaultConstructorAllocator { - private static final Constructor NULL_CONSTRUCTOR = createNullConstructor(); - - private final Cache, Constructor> constructorCache; - - public DefaultConstructorAllocator() { - this(200); - } - - public DefaultConstructorAllocator(int cacheSize) { - constructorCache = new LruCache, Constructor>(cacheSize); - } - - // for testing purpose - final boolean isInCache(Class cacheKey) { - return constructorCache.getElement(cacheKey) != null; - } - - private static final Constructor createNullConstructor() { - try { - return getNoArgsConstructor(Null.class); - } catch (Exception e) { - return null; - } - } - - public T newInstance(Class c) throws Exception { - Constructor constructor = findConstructor(c); - return (constructor != null) ? constructor.newInstance() : null; - } - - @SuppressWarnings("unchecked") - private Constructor findConstructor(Class c) { - Constructor cachedElement = (Constructor) constructorCache.getElement(c); - if (cachedElement != null) { - if (cachedElement == NULL_CONSTRUCTOR) { - return null; - } else { - return cachedElement; - } - } - - Constructor noArgsConstructor = getNoArgsConstructor(c); - if (noArgsConstructor != null) { - constructorCache.addElement(c, noArgsConstructor); - } else { - constructorCache.addElement(c, NULL_CONSTRUCTOR); - } - return noArgsConstructor; - } - - private static Constructor getNoArgsConstructor(Class c) { - try { - Constructor declaredConstructor = c.getDeclaredConstructor(); - declaredConstructor.setAccessible(true); - return declaredConstructor; - } catch (Exception e) { - return null; - } - } - - // placeholder class for Null constructor - private static final class Null { - } -} diff --git a/src/com/massivecraft/factions/gson/DefaultTypeAdapters.java b/src/com/massivecraft/factions/gson/DefaultTypeAdapters.java deleted file mode 100644 index aec33a46..00000000 --- a/src/com/massivecraft/factions/gson/DefaultTypeAdapters.java +++ /dev/null @@ -1,1063 +0,0 @@ -/* - * Copyright (C) 2008 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.massivecraft.factions.gson; - -import com.massivecraft.factions.gson.internal.$Gson$Types; - -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.net.InetAddress; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.net.UnknownHostException; -import java.sql.Time; -import java.sql.Timestamp; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Collection; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Queue; -import java.util.Set; -import java.util.SortedSet; -import java.util.StringTokenizer; -import java.util.TimeZone; -import java.util.TreeSet; -import java.util.UUID; - -/** - * List of all the default type adapters ({@link JsonSerializer}s, {@link JsonDeserializer}s, - * and {@link InstanceCreator}s. - * - * @author Inderjeet Singh - * @author Joel Leitch - */ -final class DefaultTypeAdapters { - - private static final DefaultDateTypeAdapter DATE_TYPE_ADAPTER = new DefaultDateTypeAdapter(); - private static final DefaultJavaSqlDateTypeAdapter JAVA_SQL_DATE_TYPE_ADAPTER = - new DefaultJavaSqlDateTypeAdapter(); - private static final DefaultTimeTypeAdapter TIME_TYPE_ADAPTER = - new DefaultTimeTypeAdapter(); - private static final DefaultTimestampDeserializer TIMESTAMP_DESERIALIZER = - new DefaultTimestampDeserializer(); - - @SuppressWarnings("unchecked") - private static final EnumTypeAdapter ENUM_TYPE_ADAPTER = new EnumTypeAdapter(); - private static final UrlTypeAdapter URL_TYPE_ADAPTER = new UrlTypeAdapter(); - private static final UriTypeAdapter URI_TYPE_ADAPTER = new UriTypeAdapter(); - private static final UuidTypeAdapter UUUID_TYPE_ADAPTER = new UuidTypeAdapter(); - private static final LocaleTypeAdapter LOCALE_TYPE_ADAPTER = new LocaleTypeAdapter(); - private static final DefaultInetAddressAdapter INET_ADDRESS_ADAPTER = - new DefaultInetAddressAdapter(); - private static final CollectionTypeAdapter COLLECTION_TYPE_ADAPTER = new CollectionTypeAdapter(); - private static final MapTypeAdapter MAP_TYPE_ADAPTER = new MapTypeAdapter(); - private static final BigDecimalTypeAdapter BIG_DECIMAL_TYPE_ADAPTER = new BigDecimalTypeAdapter(); - private static final BigIntegerTypeAdapter BIG_INTEGER_TYPE_ADAPTER = new BigIntegerTypeAdapter(); - - private static final BooleanTypeAdapter BOOLEAN_TYPE_ADAPTER = new BooleanTypeAdapter(); - private static final ByteTypeAdapter BYTE_TYPE_ADAPTER = new ByteTypeAdapter(); - private static final CharacterTypeAdapter CHARACTER_TYPE_ADAPTER = new CharacterTypeAdapter(); - private static final DoubleDeserializer DOUBLE_TYPE_ADAPTER = new DoubleDeserializer(); - private static final FloatDeserializer FLOAT_TYPE_ADAPTER = new FloatDeserializer(); - private static final IntegerTypeAdapter INTEGER_TYPE_ADAPTER = new IntegerTypeAdapter(); - private static final LongDeserializer LONG_DESERIALIZER = new LongDeserializer(); - private static final NumberTypeAdapter NUMBER_TYPE_ADAPTER = new NumberTypeAdapter(); - private static final ShortTypeAdapter SHORT_TYPE_ADAPTER = new ShortTypeAdapter(); - private static final StringTypeAdapter STRING_TYPE_ADAPTER = new StringTypeAdapter(); - private static final StringBuilderTypeAdapter STRING_BUILDER_TYPE_ADAPTER = - new StringBuilderTypeAdapter(); - private static final StringBufferTypeAdapter STRING_BUFFER_TYPE_ADAPTER = - new StringBufferTypeAdapter(); - - private static final GregorianCalendarTypeAdapter GREGORIAN_CALENDAR_TYPE_ADAPTER = - new GregorianCalendarTypeAdapter(); - - // The constants DEFAULT_SERIALIZERS, DEFAULT_DESERIALIZERS, and DEFAULT_INSTANCE_CREATORS - // must be defined after the constants for the type adapters. Otherwise, the type adapter - // constants will appear as nulls. - private static final ParameterizedTypeHandlerMap> DEFAULT_SERIALIZERS = - createDefaultSerializers(); - static final ParameterizedTypeHandlerMap> DEFAULT_HIERARCHY_SERIALIZERS = - createDefaultHierarchySerializers(); - private static final ParameterizedTypeHandlerMap> DEFAULT_DESERIALIZERS = - createDefaultDeserializers(); - static final ParameterizedTypeHandlerMap> DEFAULT_HIERARCHY_DESERIALIZERS = - createDefaultHierarchyDeserializers(); - private static final ParameterizedTypeHandlerMap> DEFAULT_INSTANCE_CREATORS = - createDefaultInstanceCreators(); - - private static ParameterizedTypeHandlerMap> createDefaultSerializers() { - ParameterizedTypeHandlerMap> map = - new ParameterizedTypeHandlerMap>(); - - map.register(URL.class, URL_TYPE_ADAPTER); - map.register(URI.class, URI_TYPE_ADAPTER); - map.register(UUID.class, UUUID_TYPE_ADAPTER); - map.register(Locale.class, LOCALE_TYPE_ADAPTER); - map.register(Date.class, DATE_TYPE_ADAPTER); - map.register(java.sql.Date.class, JAVA_SQL_DATE_TYPE_ADAPTER); - map.register(Timestamp.class, DATE_TYPE_ADAPTER); - map.register(Time.class, TIME_TYPE_ADAPTER); - map.register(Calendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER); - map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER); - map.register(BigDecimal.class, BIG_DECIMAL_TYPE_ADAPTER); - map.register(BigInteger.class, BIG_INTEGER_TYPE_ADAPTER); - - // Add primitive serializers - map.register(Boolean.class, BOOLEAN_TYPE_ADAPTER); - map.register(boolean.class, BOOLEAN_TYPE_ADAPTER); - map.register(Byte.class, BYTE_TYPE_ADAPTER); - map.register(byte.class, BYTE_TYPE_ADAPTER); - map.register(Character.class, CHARACTER_TYPE_ADAPTER); - map.register(char.class, CHARACTER_TYPE_ADAPTER); - map.register(Integer.class, INTEGER_TYPE_ADAPTER); - map.register(int.class, INTEGER_TYPE_ADAPTER); - map.register(Number.class, NUMBER_TYPE_ADAPTER); - map.register(Short.class, SHORT_TYPE_ADAPTER); - map.register(short.class, SHORT_TYPE_ADAPTER); - map.register(String.class, STRING_TYPE_ADAPTER); - map.register(StringBuilder.class, STRING_BUILDER_TYPE_ADAPTER); - map.register(StringBuffer.class, STRING_BUFFER_TYPE_ADAPTER); - - map.makeUnmodifiable(); - return map; - } - - private static ParameterizedTypeHandlerMap> createDefaultHierarchySerializers() { - ParameterizedTypeHandlerMap> map = - new ParameterizedTypeHandlerMap>(); - map.registerForTypeHierarchy(Enum.class, ENUM_TYPE_ADAPTER); - map.registerForTypeHierarchy(InetAddress.class, INET_ADDRESS_ADAPTER); - map.registerForTypeHierarchy(Collection.class, COLLECTION_TYPE_ADAPTER); - map.registerForTypeHierarchy(Map.class, MAP_TYPE_ADAPTER); - map.makeUnmodifiable(); - return map; - } - - private static ParameterizedTypeHandlerMap> createDefaultDeserializers() { - ParameterizedTypeHandlerMap> map = - new ParameterizedTypeHandlerMap>(); - map.register(URL.class, wrapDeserializer(URL_TYPE_ADAPTER)); - map.register(URI.class, wrapDeserializer(URI_TYPE_ADAPTER)); - map.register(UUID.class, wrapDeserializer(UUUID_TYPE_ADAPTER)); - map.register(Locale.class, wrapDeserializer(LOCALE_TYPE_ADAPTER)); - map.register(Date.class, wrapDeserializer(DATE_TYPE_ADAPTER)); - map.register(java.sql.Date.class, wrapDeserializer(JAVA_SQL_DATE_TYPE_ADAPTER)); - map.register(Timestamp.class, wrapDeserializer(TIMESTAMP_DESERIALIZER)); - map.register(Time.class, wrapDeserializer(TIME_TYPE_ADAPTER)); - map.register(Calendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER); - map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER); - map.register(BigDecimal.class, BIG_DECIMAL_TYPE_ADAPTER); - map.register(BigInteger.class, BIG_INTEGER_TYPE_ADAPTER); - - // Add primitive deserializers - map.register(Boolean.class, BOOLEAN_TYPE_ADAPTER); - map.register(boolean.class, BOOLEAN_TYPE_ADAPTER); - map.register(Byte.class, BYTE_TYPE_ADAPTER); - map.register(byte.class, BYTE_TYPE_ADAPTER); - map.register(Character.class, wrapDeserializer(CHARACTER_TYPE_ADAPTER)); - map.register(char.class, wrapDeserializer(CHARACTER_TYPE_ADAPTER)); - map.register(Double.class, DOUBLE_TYPE_ADAPTER); - map.register(double.class, DOUBLE_TYPE_ADAPTER); - map.register(Float.class, FLOAT_TYPE_ADAPTER); - map.register(float.class, FLOAT_TYPE_ADAPTER); - map.register(Integer.class, INTEGER_TYPE_ADAPTER); - map.register(int.class, INTEGER_TYPE_ADAPTER); - map.register(Long.class, LONG_DESERIALIZER); - map.register(long.class, LONG_DESERIALIZER); - map.register(Number.class, NUMBER_TYPE_ADAPTER); - map.register(Short.class, SHORT_TYPE_ADAPTER); - map.register(short.class, SHORT_TYPE_ADAPTER); - map.register(String.class, wrapDeserializer(STRING_TYPE_ADAPTER)); - map.register(StringBuilder.class, wrapDeserializer(STRING_BUILDER_TYPE_ADAPTER)); - map.register(StringBuffer.class, wrapDeserializer(STRING_BUFFER_TYPE_ADAPTER)); - - map.makeUnmodifiable(); - return map; - } - - private static ParameterizedTypeHandlerMap> createDefaultHierarchyDeserializers() { - ParameterizedTypeHandlerMap> map = - new ParameterizedTypeHandlerMap>(); - map.registerForTypeHierarchy(Enum.class, wrapDeserializer(ENUM_TYPE_ADAPTER)); - map.registerForTypeHierarchy(InetAddress.class, wrapDeserializer(INET_ADDRESS_ADAPTER)); - map.registerForTypeHierarchy(Collection.class, wrapDeserializer(COLLECTION_TYPE_ADAPTER)); - map.registerForTypeHierarchy(Map.class, wrapDeserializer(MAP_TYPE_ADAPTER)); - map.makeUnmodifiable(); - return map; - } - - @SuppressWarnings("unchecked") - private static ParameterizedTypeHandlerMap> createDefaultInstanceCreators() { - ParameterizedTypeHandlerMap> map = - new ParameterizedTypeHandlerMap>(); - DefaultConstructorAllocator allocator = new DefaultConstructorAllocator(50); - - // Map Instance Creators - map.registerForTypeHierarchy(Map.class, - new DefaultConstructorCreator(LinkedHashMap.class, allocator)); - - // Add Collection type instance creators - DefaultConstructorCreator listCreator = - new DefaultConstructorCreator(ArrayList.class, allocator); - DefaultConstructorCreator queueCreator = - new DefaultConstructorCreator(LinkedList.class, allocator); - DefaultConstructorCreator setCreator = - new DefaultConstructorCreator(HashSet.class, allocator); - DefaultConstructorCreator sortedSetCreator = - new DefaultConstructorCreator(TreeSet.class, allocator); - map.registerForTypeHierarchy(Collection.class, listCreator); - map.registerForTypeHierarchy(Queue.class, queueCreator); - map.registerForTypeHierarchy(Set.class, setCreator); - map.registerForTypeHierarchy(SortedSet.class, sortedSetCreator); - - map.makeUnmodifiable(); - return map; - } - - @SuppressWarnings("unchecked") - private static JsonDeserializer wrapDeserializer(JsonDeserializer deserializer) { - return new JsonDeserializerExceptionWrapper(deserializer); - } - - static ParameterizedTypeHandlerMap> getDefaultSerializers() { - return getDefaultSerializers(false, LongSerializationPolicy.DEFAULT); - } - - static ParameterizedTypeHandlerMap> getAllDefaultSerializers() { - ParameterizedTypeHandlerMap> defaultSerializers = - getDefaultSerializers(false, LongSerializationPolicy.DEFAULT); - defaultSerializers.register(DEFAULT_HIERARCHY_SERIALIZERS); - return defaultSerializers; - } - - static ParameterizedTypeHandlerMap> getAllDefaultDeserializers() { - ParameterizedTypeHandlerMap> defaultDeserializers = - getDefaultDeserializers().copyOf(); - defaultDeserializers.register(DEFAULT_HIERARCHY_DESERIALIZERS); - return defaultDeserializers; - } - - static ParameterizedTypeHandlerMap> getDefaultSerializers( - boolean serializeSpecialFloatingPointValues, LongSerializationPolicy longSerializationPolicy) { - ParameterizedTypeHandlerMap> serializers = - new ParameterizedTypeHandlerMap>(); - - // Double primitive - DefaultTypeAdapters.DoubleSerializer doubleSerializer = - new DefaultTypeAdapters.DoubleSerializer(serializeSpecialFloatingPointValues); - serializers.registerIfAbsent(Double.class, doubleSerializer); - serializers.registerIfAbsent(double.class, doubleSerializer); - - // Float primitive - DefaultTypeAdapters.FloatSerializer floatSerializer = - new DefaultTypeAdapters.FloatSerializer(serializeSpecialFloatingPointValues); - serializers.registerIfAbsent(Float.class, floatSerializer); - serializers.registerIfAbsent(float.class, floatSerializer); - - // Long primitive - DefaultTypeAdapters.LongSerializer longSerializer = - new DefaultTypeAdapters.LongSerializer(longSerializationPolicy); - serializers.registerIfAbsent(Long.class, longSerializer); - serializers.registerIfAbsent(long.class, longSerializer); - - serializers.registerIfAbsent(DEFAULT_SERIALIZERS); - return serializers; - } - - static ParameterizedTypeHandlerMap> getDefaultDeserializers() { - return DEFAULT_DESERIALIZERS; - } - - static ParameterizedTypeHandlerMap> getDefaultInstanceCreators() { - return DEFAULT_INSTANCE_CREATORS; - } - - /** - * This type adapter supports three subclasses of date: Date, Timestamp, and - * java.sql.Date. - */ - static final class DefaultDateTypeAdapter implements JsonSerializer, JsonDeserializer { - private final DateFormat enUsFormat; - private final DateFormat localFormat; - private final DateFormat iso8601Format; - - DefaultDateTypeAdapter() { - this(DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US), - DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT)); - } - - DefaultDateTypeAdapter(String datePattern) { - this(new SimpleDateFormat(datePattern, Locale.US), new SimpleDateFormat(datePattern)); - } - - DefaultDateTypeAdapter(int style) { - this(DateFormat.getDateInstance(style, Locale.US), DateFormat.getDateInstance(style)); - } - - public DefaultDateTypeAdapter(int dateStyle, int timeStyle) { - this(DateFormat.getDateTimeInstance(dateStyle, timeStyle, Locale.US), - DateFormat.getDateTimeInstance(dateStyle, timeStyle)); - } - - DefaultDateTypeAdapter(DateFormat enUsFormat, DateFormat localFormat) { - this.enUsFormat = enUsFormat; - this.localFormat = localFormat; - this.iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US); - this.iso8601Format.setTimeZone(TimeZone.getTimeZone("UTC")); - } - - // These methods need to be synchronized since JDK DateFormat classes are not thread-safe - // See issue 162 - public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { - synchronized (localFormat) { - String dateFormatAsString = enUsFormat.format(src); - return new JsonPrimitive(dateFormatAsString); - } - } - - public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - if (!(json instanceof JsonPrimitive)) { - throw new JsonParseException("The date should be a string value"); - } - Date date = deserializeToDate(json); - if (typeOfT == Date.class) { - return date; - } else if (typeOfT == Timestamp.class) { - return new Timestamp(date.getTime()); - } else if (typeOfT == java.sql.Date.class) { - return new java.sql.Date(date.getTime()); - } else { - throw new IllegalArgumentException(getClass() + " cannot deserialize to " + typeOfT); - } - } - - private Date deserializeToDate(JsonElement json) { - synchronized (localFormat) { - try { - return localFormat.parse(json.getAsString()); - } catch (ParseException ignored) { - } - try { - return enUsFormat.parse(json.getAsString()); - } catch (ParseException ignored) { - } - try { - return iso8601Format.parse(json.getAsString()); - } catch (ParseException e) { - throw new JsonSyntaxException(json.getAsString(), e); - } - } - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(DefaultDateTypeAdapter.class.getSimpleName()); - sb.append('(').append(localFormat.getClass().getSimpleName()).append(')'); - return sb.toString(); - } - } - - static final class DefaultJavaSqlDateTypeAdapter implements JsonSerializer, - JsonDeserializer { - private final DateFormat format; - DefaultJavaSqlDateTypeAdapter() { - this.format = new SimpleDateFormat("MMM d, yyyy"); - } - - public JsonElement serialize(java.sql.Date src, Type typeOfSrc, - JsonSerializationContext context) { - synchronized (format) { - String dateFormatAsString = format.format(src); - return new JsonPrimitive(dateFormatAsString); - } - } - - public java.sql.Date deserialize(JsonElement json, Type typeOfT, - JsonDeserializationContext context) throws JsonParseException { - if (!(json instanceof JsonPrimitive)) { - throw new JsonParseException("The date should be a string value"); - } - try { - synchronized (format) { - Date date = format.parse(json.getAsString()); - return new java.sql.Date(date.getTime()); - } - } catch (ParseException e) { - throw new JsonSyntaxException(e); - } - } - } - - static final class DefaultTimestampDeserializer implements JsonDeserializer { - public Timestamp deserialize(JsonElement json, Type typeOfT, - JsonDeserializationContext context) throws JsonParseException { - Date date = context.deserialize(json, Date.class); - return new Timestamp(date.getTime()); - } - } - - static final class DefaultTimeTypeAdapter implements JsonSerializer