don't use gson's reflection serialization on non RuneLite classes

java >=16 disallows access to most private fields which makes these
fail with the reflection type adapter
This commit is contained in:
Max Weber
2020-12-22 12:00:31 -07:00
committed by Adam
parent 61f732d330
commit 2b3ce15516
19 changed files with 402 additions and 41 deletions

View File

@@ -25,10 +25,16 @@
package net.runelite.http.api;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.awt.Color;
import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import net.runelite.http.api.gson.ColorTypeAdapter;
import net.runelite.http.api.gson.InstantTypeAdapter;
import net.runelite.http.api.gson.IllegalReflectionExclusion;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.MediaType;
@@ -46,7 +52,7 @@ public class RuneLiteAPI
public static final String RUNELITE_MACHINEID = "RUNELITE-MACHINEID";
public static final OkHttpClient CLIENT;
public static final Gson GSON = new Gson();
public static final Gson GSON;
public static final MediaType JSON = MediaType.parse("application/json");
public static String userAgent;
@@ -96,6 +102,23 @@ public class RuneLiteAPI
}
})
.build();
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder
.registerTypeAdapter(Instant.class, new InstantTypeAdapter())
.registerTypeAdapter(Color.class, new ColorTypeAdapter());
boolean assertionsEnabled = false;
assert assertionsEnabled = true;
if (assertionsEnabled)
{
IllegalReflectionExclusion jbe = new IllegalReflectionExclusion();
gsonBuilder.addSerializationExclusionStrategy(jbe);
gsonBuilder.addDeserializationExclusionStrategy(jbe);
}
GSON = gsonBuilder.create();
}
public static HttpUrl getSessionBase()

View File

@@ -0,0 +1,80 @@
/*
* Copyright (c) 2020 Abex
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.api.gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.awt.Color;
import java.io.IOException;
public class ColorTypeAdapter extends TypeAdapter<Color>
{
@Override
public void write(JsonWriter out, Color value) throws IOException
{
if (value == null)
{
out.nullValue();
return;
}
int rgba = value.getRGB();
out.beginObject()
.name("value")
.value(rgba)
.endObject();
}
@Override
public Color read(JsonReader in) throws IOException
{
switch (in.peek())
{
case NULL:
in.nextNull();
return null;
case BEGIN_OBJECT:
in.beginObject();
double value = 0;
while (in.peek() != JsonToken.END_OBJECT)
{
switch (in.nextName())
{
case "value":
value = in.nextDouble();
break;
default:
in.skipValue();
break;
}
}
in.endObject();
return new Color((int) value, true);
}
return null; // throws
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright (c) 2020 Abex
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.api.gson;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import java.lang.reflect.Modifier;
public class IllegalReflectionExclusion implements ExclusionStrategy
{
@Override
public boolean shouldSkipField(FieldAttributes f)
{
if (f.getDeclaringClass().getName().startsWith("net.runelite"))
{
return false;
}
assert !Modifier.isPrivate(f.getDeclaringClass().getModifiers()) : "gsoning private class " + f.getDeclaringClass().getName();
try
{
f.getDeclaringClass().getField(f.getName());
}
catch (NoSuchFieldException e)
{
throw new AssertionError("gsoning private field " + f.getDeclaringClass() + "." + f.getName());
}
return false;
}
@Override
public boolean shouldSkipClass(Class<?> clazz)
{
return false;
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright (c) 2020 Abex
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.api.gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.time.Instant;
// Just add water!
public class InstantTypeAdapter extends TypeAdapter<Instant>
{
@Override
public void write(JsonWriter out, Instant value) throws IOException
{
if (value == null)
{
out.nullValue();
return;
}
out.beginObject()
.name("seconds")
.value(value.getEpochSecond())
.name("nanos")
.value(value.getNano())
.endObject();
}
@Override
public Instant read(JsonReader in) throws IOException
{
if (in.peek() == JsonToken.NULL)
{
in.nextNull();
return null;
}
long seconds = 0;
int nanos = 0;
in.beginObject();
while (in.peek() != JsonToken.END_OBJECT)
{
switch (in.nextName())
{
case "nanos":
nanos = in.nextInt();
break;
case "seconds":
seconds = in.nextLong();
break;
}
}
in.endObject();
return Instant.ofEpochSecond(seconds, nanos);
}
}

View File

@@ -25,11 +25,11 @@
package net.runelite.http.api.ws;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import net.runelite.http.api.RuneLiteAPI;
import net.runelite.http.api.ws.messages.Handshake;
import net.runelite.http.api.ws.messages.LoginResponse;
import net.runelite.http.api.ws.messages.party.Join;
@@ -76,7 +76,7 @@ public class WebsocketGsonFactory
public static Gson build(final RuntimeTypeAdapterFactory<WebsocketMessage> factory)
{
return new GsonBuilder()
return RuneLiteAPI.GSON.newBuilder()
.registerTypeAdapterFactory(factory)
.create();
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright (c) 2021 Abex
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.api.gson;
import java.awt.Color;
import net.runelite.http.api.RuneLiteAPI;
import org.junit.Assert;
import org.junit.Test;
public class ColorTypeAdapterTest
{
@Test
public void test()
{
test("null", null);
test("{\"value\":-13347208,\"falpha\":0.0}", new Color(0x12345678, false));
test("{\"value\":305419896,\"falpha\":0.0}", new Color(0x12345678, true));
test("{\"value\":-1.4221317E7,\"falpha\":0.0}", new Color(0xFF26FFFB, true));
}
private void test(String json, Color object)
{
Color parsed = RuneLiteAPI.GSON.fromJson(json, Color.class);
Assert.assertEquals(object, parsed);
String serialized = RuneLiteAPI.GSON.toJson(object);
Color roundTripped = RuneLiteAPI.GSON.fromJson(serialized, Color.class);
Assert.assertEquals(object, roundTripped);
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright (c) 2021 Abex
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.api.gson;
import java.time.Instant;
import net.runelite.http.api.RuneLiteAPI;
import org.junit.Assert;
import org.junit.Test;
public class InstantTypeAdapterTest
{
@Test
public void test()
{
test("null", null);
test("{\"seconds\":1609538310,\"nanos\":291698903}", Instant.ofEpochSecond(1609538310, 291698903));
}
private void test(String json, Instant object)
{
Instant parsed = RuneLiteAPI.GSON.fromJson(json, Instant.class);
Assert.assertEquals(object, parsed);
String serialized = RuneLiteAPI.GSON.toJson(object);
Instant roundTripped = RuneLiteAPI.GSON.fromJson(serialized, Instant.class);
Assert.assertEquals(object, roundTripped);
}
}

View File

@@ -24,6 +24,7 @@
*/
package net.runelite.client;
import com.google.gson.Gson;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.name.Names;
@@ -51,6 +52,7 @@ import net.runelite.client.plugins.PluginManager;
import net.runelite.client.task.Scheduler;
import net.runelite.client.util.DeferredEventBus;
import net.runelite.client.util.ExecutorServiceExceptionLogger;
import net.runelite.http.api.RuneLiteAPI;
import net.runelite.http.api.chat.ChatClient;
import okhttp3.OkHttpClient;
@@ -86,6 +88,8 @@ public class RuneLiteModule extends AbstractModule
bind(PluginManager.class);
bind(SessionManager.class);
bind(Gson.class).toInstance(RuneLiteAPI.GSON);
bind(Callbacks.class).to(Hooks.class);
bind(EventBus.class)

View File

@@ -64,6 +64,7 @@ public class SessionManager
private final WSClient wsClient;
private final File sessionFile;
private final AccountClient accountClient;
private final Gson gson;
@Inject
private SessionManager(
@@ -71,13 +72,15 @@ public class SessionManager
ConfigManager configManager,
EventBus eventBus,
WSClient wsClient,
OkHttpClient okHttpClient)
OkHttpClient okHttpClient,
Gson gson)
{
this.configManager = configManager;
this.eventBus = eventBus;
this.wsClient = wsClient;
this.sessionFile = sessionfile;
this.accountClient = new AccountClient(okHttpClient);
this.gson = gson;
eventBus.register(this);
}
@@ -94,7 +97,7 @@ public class SessionManager
try (FileInputStream in = new FileInputStream(sessionFile))
{
session = new Gson().fromJson(new InputStreamReader(in, StandardCharsets.UTF_8), AccountSession.class);
session = gson.fromJson(new InputStreamReader(in, StandardCharsets.UTF_8), AccountSession.class);
log.debug("Loaded session for {}", session.getUsername());
}
@@ -124,7 +127,7 @@ public class SessionManager
try (Writer fw = new OutputStreamWriter(new FileOutputStream(sessionFile), StandardCharsets.UTF_8))
{
new Gson().toJson(accountSession, fw);
gson.toJson(accountSession, fw);
log.debug("Saved session to {}", sessionFile);
}

View File

@@ -25,6 +25,7 @@
package net.runelite.client.externalplugins;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
@@ -59,11 +60,13 @@ import okio.BufferedSource;
public class ExternalPluginClient
{
private final OkHttpClient okHttpClient;
private final Gson gson;
@Inject
private ExternalPluginClient(OkHttpClient okHttpClient)
private ExternalPluginClient(OkHttpClient okHttpClient, Gson gson)
{
this.okHttpClient = okHttpClient;
this.gson = gson;
}
public List<ExternalPluginManifest> downloadManifest() throws IOException, VerificationException
@@ -94,7 +97,7 @@ public class ExternalPluginClient
throw new VerificationException("Unable to verify external plugin manifest");
}
return RuneLiteAPI.GSON.fromJson(new String(data, StandardCharsets.UTF_8),
return gson.fromJson(new String(data, StandardCharsets.UTF_8),
new TypeToken<List<ExternalPluginManifest>>()
{
}.getType());
@@ -156,7 +159,7 @@ public class ExternalPluginClient
Request request = new Request.Builder()
.url(url)
.post(RequestBody.create(RuneLiteAPI.JSON, RuneLiteAPI.GSON.toJson(plugins)))
.post(RequestBody.create(RuneLiteAPI.JSON, gson.toJson(plugins)))
.build();
okHttpClient.newCall(request).enqueue(new Callback()
@@ -190,7 +193,7 @@ public class ExternalPluginClient
}
// CHECKSTYLE:OFF
return RuneLiteAPI.GSON.fromJson(new InputStreamReader(res.body().byteStream()), new TypeToken<Map<String, Integer>>(){}.getType());
return gson.fromJson(new InputStreamReader(res.body().byteStream()), new TypeToken<Map<String, Integer>>(){}.getType());
// CHECKSTYLE:ON
}
catch (JsonSyntaxException ex)

View File

@@ -32,7 +32,6 @@ import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
import lombok.extern.slf4j.Slf4j;
import net.runelite.http.api.RuneLiteAPI;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
@@ -47,11 +46,13 @@ public class CrowdsourcingManager
{
private static final String CROWDSOURCING_BASE = "https://crowdsource.runescape.wiki/runelite";
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static final Gson GSON = RuneLiteAPI.GSON;
@Inject
private OkHttpClient okHttpClient;
@Inject
private Gson gson;
private List<Object> data = new ArrayList<>();
public void storeEvent(Object event)
@@ -77,7 +78,7 @@ public class CrowdsourcingManager
Request r = new Request.Builder()
.url(CROWDSOURCING_BASE)
.post(RequestBody.create(JSON, GSON.toJson(temp)))
.post(RequestBody.create(JSON, gson.toJson(temp)))
.build();
okHttpClient.newCall(r).enqueue(new Callback()

View File

@@ -128,7 +128,6 @@ public class GrandExchangePlugin extends Plugin
private static final String BUY_LIMIT_GE_TEXT = "<br>Buy limit: ";
private static final String BUY_LIMIT_KEY = "buylimit";
private static final Gson GSON = new Gson();
private static final Duration BUY_LIMIT_RESET = Duration.ofHours(4);
static final String SEARCH_GRAND_EXCHANGE = "Search Grand Exchange";
@@ -183,6 +182,9 @@ public class GrandExchangePlugin extends Plugin
@Inject
private ConfigManager configManager;
@Inject
private Gson gson;
private Widget grandExchangeText;
private Widget grandExchangeItem;
private String grandExchangeExamine;
@@ -253,12 +255,12 @@ public class GrandExchangePlugin extends Plugin
{
return null;
}
return GSON.fromJson(offer, SavedOffer.class);
return gson.fromJson(offer, SavedOffer.class);
}
private void setOffer(int slot, SavedOffer offer)
{
configManager.setRSProfileConfiguration("geoffer", Integer.toString(slot), GSON.toJson(offer));
configManager.setRSProfileConfiguration("geoffer", Integer.toString(slot), gson.toJson(offer));
}
private void deleteOffer(int slot)

View File

@@ -73,8 +73,6 @@ public class GroundMarkerPlugin extends Plugin
private static final String WALK_HERE = "Walk here";
private static final String REGION_PREFIX = "region_";
private static final Gson GSON = new Gson();
@Getter(AccessLevel.PACKAGE)
private final List<ColorTileMarker> points = new ArrayList<>();
@@ -105,6 +103,9 @@ public class GroundMarkerPlugin extends Plugin
@Inject
private GroundMarkerSharingManager sharingManager;
@Inject
private Gson gson;
void savePoints(int regionId, Collection<GroundMarkerPoint> points)
{
if (points == null || points.isEmpty())
@@ -113,7 +114,7 @@ public class GroundMarkerPlugin extends Plugin
return;
}
String json = GSON.toJson(points);
String json = gson.toJson(points);
configManager.setConfiguration(CONFIG_GROUP, REGION_PREFIX + regionId, json);
}
@@ -126,7 +127,7 @@ public class GroundMarkerPlugin extends Plugin
}
// CHECKSTYLE:OFF
return GSON.fromJson(json, new TypeToken<List<GroundMarkerPoint>>(){}.getType());
return gson.fromJson(json, new TypeToken<List<GroundMarkerPoint>>(){}.getType());
// CHECKSTYLE:ON
}

View File

@@ -53,7 +53,6 @@ import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.game.chatbox.ChatboxPanelManager;
import net.runelite.client.menus.MenuManager;
import net.runelite.client.menus.WidgetMenuOption;
import net.runelite.http.api.RuneLiteAPI;
@Slf4j
class GroundMarkerSharingManager
@@ -61,22 +60,23 @@ class GroundMarkerSharingManager
private static final WidgetMenuOption EXPORT_MARKERS_OPTION = new WidgetMenuOption("Export", "Ground Markers", WORLD_MAP_OPTION);
private static final WidgetMenuOption IMPORT_MARKERS_OPTION = new WidgetMenuOption("Import", "Ground Markers", WORLD_MAP_OPTION);
private static final Gson GSON = RuneLiteAPI.GSON;
private final GroundMarkerPlugin plugin;
private final Client client;
private final MenuManager menuManager;
private final ChatMessageManager chatMessageManager;
private final ChatboxPanelManager chatboxPanelManager;
private final Gson gson;
@Inject
private GroundMarkerSharingManager(GroundMarkerPlugin plugin, Client client, MenuManager menuManager, ChatMessageManager chatMessageManager, ChatboxPanelManager chatboxPanelManager)
private GroundMarkerSharingManager(GroundMarkerPlugin plugin, Client client, MenuManager menuManager,
ChatMessageManager chatMessageManager, ChatboxPanelManager chatboxPanelManager, Gson gson)
{
this.plugin = plugin;
this.client = client;
this.menuManager = menuManager;
this.chatMessageManager = chatMessageManager;
this.chatboxPanelManager = chatboxPanelManager;
this.gson = gson;
}
void addMenuOptions()
@@ -135,7 +135,7 @@ class GroundMarkerSharingManager
return;
}
final String exportDump = GSON.toJson(activePoints);
final String exportDump = gson.toJson(activePoints);
log.debug("Exported ground markers: {}", exportDump);
@@ -173,7 +173,7 @@ class GroundMarkerSharingManager
try
{
// CHECKSTYLE:OFF
importPoints = GSON.fromJson(clipboardText, new TypeToken<List<GroundMarkerPoint>>(){}.getType());
importPoints = gson.fromJson(clipboardText, new TypeToken<List<GroundMarkerPoint>>(){}.getType());
// CHECKSTYLE:ON
}
catch (JsonSyntaxException e)

View File

@@ -88,7 +88,6 @@ public class ObjectIndicatorsPlugin extends Plugin
private static final String MARK = "Mark object";
private static final String UNMARK = "Unmark object";
private final Gson GSON = new Gson();
@Getter(AccessLevel.PACKAGE)
private final List<ColorTileObject> objects = new ArrayList<>();
private final Map<Integer, Set<ObjectPoint>> points = new HashMap<>();
@@ -108,6 +107,9 @@ public class ObjectIndicatorsPlugin extends Plugin
@Inject
private ObjectIndicatorsConfig config;
@Inject
private Gson gson;
@Provides
ObjectIndicatorsConfig provideConfig(ConfigManager configManager)
{
@@ -428,7 +430,7 @@ public class ObjectIndicatorsPlugin extends Plugin
}
else
{
final String json = GSON.toJson(points);
final String json = gson.toJson(points);
configManager.setConfiguration(CONFIG_GROUP, "region_" + id, json);
}
}
@@ -442,7 +444,7 @@ public class ObjectIndicatorsPlugin extends Plugin
return null;
}
Set<ObjectPoint> points = GSON.fromJson(json, new TypeToken<Set<ObjectPoint>>()
Set<ObjectPoint> points = gson.fromJson(json, new TypeToken<Set<ObjectPoint>>()
{
}.getType());
// Prior to multiloc support the plugin would mark objects named "null", which breaks

View File

@@ -88,6 +88,9 @@ public class ScreenMarkerPlugin extends Plugin
@Inject
private ScreenMarkerCreationOverlay overlay;
@Inject
private Gson gson;
@Getter
@Inject
private ColorPickerManager colorPickerManager;
@@ -266,7 +269,6 @@ public class ScreenMarkerPlugin extends Plugin
return;
}
final Gson gson = new Gson();
final String json = gson
.toJson(screenMarkers.stream().map(ScreenMarkerOverlay::getMarker).collect(Collectors.toList()));
configManager.setConfiguration(CONFIG_GROUP, CONFIG_KEY, json);
@@ -279,7 +281,6 @@ public class ScreenMarkerPlugin extends Plugin
return Stream.empty();
}
final Gson gson = new Gson();
final List<ScreenMarker> screenMarkerData = gson.fromJson(json, new TypeToken<ArrayList<ScreenMarker>>()
{
}.getType());

View File

@@ -53,6 +53,9 @@ public class ClockManager
@Inject
private Notifier notifier;
@Inject
private Gson gson;
@Getter
private final List<Timer> timers = new CopyOnWriteArrayList<>();
@@ -183,7 +186,6 @@ public class ClockManager
if (!Strings.isNullOrEmpty(timersJson))
{
final Gson gson = new Gson();
final List<Timer> timers = gson.fromJson(timersJson, new TypeToken<ArrayList<Timer>>()
{
}.getType());
@@ -200,7 +202,6 @@ public class ClockManager
if (!Strings.isNullOrEmpty(stopwatchesJson))
{
final Gson gson = new Gson();
final List<Stopwatch> stopwatches = gson.fromJson(stopwatchesJson, new TypeToken<ArrayList<Stopwatch>>()
{
}.getType());
@@ -227,14 +228,12 @@ public class ClockManager
void saveTimers()
{
final Gson gson = new Gson();
final String json = gson.toJson(timers);
configManager.setConfiguration(TimeTrackingConfig.CONFIG_GROUP, TimeTrackingConfig.TIMERS, json);
}
void saveStopwatches()
{
final Gson gson = new Gson();
final String json = gson.toJson(stopwatches);
configManager.setConfiguration(TimeTrackingConfig.CONFIG_GROUP, TimeTrackingConfig.STOPWATCHES, json);
}

View File

@@ -67,7 +67,6 @@ public class WikiSearchChatboxTextInput extends ChatboxTextInput
private static final int PREDICTION_DEBOUNCE_DELAY_MS = 200;
private final ChatboxPanelManager chatboxPanelManager;
private final Gson gson = new Gson();
private Future<?> runningRequest = null;
private List<String> predictions = ImmutableList.of();
@@ -78,7 +77,7 @@ public class WikiSearchChatboxTextInput extends ChatboxTextInput
@Inject
public WikiSearchChatboxTextInput(ChatboxPanelManager chatboxPanelManager, ClientThread clientThread,
ScheduledExecutorService scheduledExecutorService, @Named("developerMode") final boolean developerMode,
OkHttpClient okHttpClient)
OkHttpClient okHttpClient, Gson gson)
{
super(chatboxPanelManager, clientThread);
this.chatboxPanelManager = chatboxPanelManager;

View File

@@ -26,6 +26,7 @@
package net.runelite.client.util;
import com.google.common.base.Strings;
import com.google.gson.Gson;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.datatransfer.Clipboard;
@@ -54,7 +55,6 @@ import net.runelite.api.GameState;
import net.runelite.api.WorldType;
import net.runelite.client.Notifier;
import static net.runelite.client.RuneLite.SCREENSHOT_DIR;
import net.runelite.http.api.RuneLiteAPI;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
@@ -75,6 +75,7 @@ public class ImageCapture
private final Client client;
private final Notifier notifier;
private final OkHttpClient okHttpClient;
private final Gson gson;
private final String imgurClientId;
@Inject
@@ -82,12 +83,14 @@ public class ImageCapture
final Client client,
final Notifier notifier,
final OkHttpClient okHttpClient,
final Gson gson,
@Named("runelite.imgur.client.id") final String imgurClientId
)
{
this.client = client;
this.notifier = notifier;
this.okHttpClient = okHttpClient;
this.gson = gson;
this.imgurClientId = imgurClientId;
}
@@ -204,7 +207,7 @@ public class ImageCapture
*/
private void uploadScreenshot(File screenshotFile, boolean notify) throws IOException
{
String json = RuneLiteAPI.GSON.toJson(new ImageUploadRequest(screenshotFile));
String json = gson.toJson(new ImageUploadRequest(screenshotFile));
Request request = new Request.Builder()
.url(IMGUR_IMAGE_UPLOAD_URL)
@@ -225,8 +228,8 @@ public class ImageCapture
{
try (InputStream in = response.body().byteStream())
{
ImageUploadResponse imageUploadResponse = RuneLiteAPI.GSON
.fromJson(new InputStreamReader(in, StandardCharsets.UTF_8), ImageUploadResponse.class);
ImageUploadResponse imageUploadResponse =
gson.fromJson(new InputStreamReader(in, StandardCharsets.UTF_8), ImageUploadResponse.class);
if (imageUploadResponse.isSuccess())
{