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:
@@ -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()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user