commit fc573a846525ef66393671f0dcbbef29426c1b4d Author: Nasser Grainawi Date: Wed Aug 26 16:59:36 2020 -0600 Use auto-value-gson Fixes an old TODO and opens up using the library in the replication plugin. Change-Id: Ibf639c51f5b11a95c70d89e3e57cbb768bac6236 diff --git a/Documentation/licenses.txt b/Documentation/licenses.txt index 98e99d4..605b493 100644 --- a/Documentation/licenses.txt +++ b/Documentation/licenses.txt @@ -44,6 +44,7 @@ Apache2.0 * auto:auto-value * auto:auto-value-annotations +* auto:auto-value-gson * commons:codec * commons:compress * commons:dbcp diff --git a/WORKSPACE b/WORKSPACE index 4c2fe35..48fa1d6 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -640,6 +640,38 @@ maven_jar( sha1 = "eff48ed53995db2dadf0456426cc1f8700136f86", ) +AUTO_VALUE_GSON_VERSION = "1.3.0" + +maven_jar( + name = "auto-value-gson-runtime", + artifact = "com.ryanharter.auto.value:auto-value-gson-runtime:" + AUTO_VALUE_GSON_VERSION, + sha1 = "a69a9db5868bb039bd80f60661a771b643eaba59", +) + +maven_jar( + name = "auto-value-gson-extension", + artifact = "com.ryanharter.auto.value:auto-value-gson-extension:" + AUTO_VALUE_GSON_VERSION, + sha1 = "6a61236d17b58b05e32b4c532bcb348280d2212b", +) + +maven_jar( + name = "auto-value-gson-factory", + artifact = "com.ryanharter.auto.value:auto-value-gson-factory:" + AUTO_VALUE_GSON_VERSION, + sha1 = "b1f01918c0d6cb1f5482500e6b9e62589334dbb0", +) + +maven_jar( + name = "javapoet", + artifact = "com.squareup:javapoet:1.13.0", + sha1 = "d6562d385049f35eb50403fa86bb11cce76b866a", +) + +maven_jar( + name = "autotransient", + artifact = "io.sweers.autotransient:autotransient:1.0.0", + sha1 = "38b1c630b8e76560221622289f37be40105abb3d", +) + declare_nongoogle_deps() LUCENE_VERS = "6.6.5" diff --git a/java/com/google/gerrit/entities/BUILD b/java/com/google/gerrit/entities/BUILD index 66d1869..c0f5de6 100644 --- a/java/com/google/gerrit/entities/BUILD +++ b/java/com/google/gerrit/entities/BUILD @@ -10,11 +10,13 @@ java_library( deps = [ "//java/com/google/gerrit/common:annotations", "//java/com/google/gerrit/extensions:api", + "//lib:gson", "//lib:guava", "//lib:jgit", "//lib:protobuf", "//lib/auto:auto-value", "//lib/auto:auto-value-annotations", + "//lib/auto:auto-value-gson", "//lib/errorprone:annotations", "//lib/flogger:api", "//proto:cache_java_proto", diff --git a/java/com/google/gerrit/entities/Change.java b/java/com/google/gerrit/entities/Change.java index 845a9bb..aab72ea 100644 --- a/java/com/google/gerrit/entities/Change.java +++ b/java/com/google/gerrit/entities/Change.java @@ -21,6 +21,9 @@ import com.google.auto.value.AutoValue; import com.google.common.primitives.Ints; import com.google.gerrit.common.Nullable; import com.google.gerrit.extensions.client.ChangeStatus; +import com.google.gson.Gson; +import com.google.gson.TypeAdapter; +import com.google.gson.annotations.SerializedName; import java.sql.Timestamp; import java.util.Arrays; import java.util.Optional; @@ -283,6 +286,7 @@ public final class Change { return Change.key(KeyUtil.decode(str)); } + @SerializedName("id") abstract String key(); public String get() { @@ -307,6 +311,10 @@ public final class Change { public final String toString() { return get(); } + + public static TypeAdapter typeAdapter(Gson gson) { + return new AutoValue_Change_Key.GsonTypeAdapter(gson); + } } /** Minimum database status constant for an open change. */ diff --git a/java/com/google/gerrit/entities/EntitiesAdapterFactory.java b/java/com/google/gerrit/entities/EntitiesAdapterFactory.java new file mode 100644 index 0000000..e6a06fd --- /dev/null +++ b/java/com/google/gerrit/entities/EntitiesAdapterFactory.java @@ -0,0 +1,25 @@ +// Copyright (C) 2020 The Android Open Source Project +// +// 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.google.gerrit.entities; + +import com.google.gson.TypeAdapterFactory; +import com.ryanharter.auto.value.gson.GsonTypeAdapterFactory; + +@GsonTypeAdapterFactory +public abstract class EntitiesAdapterFactory implements TypeAdapterFactory { + public static TypeAdapterFactory create() { + return new AutoValueGson_EntitiesAdapterFactory(); + } +} diff --git a/java/com/google/gerrit/server/change/ChangeKeyAdapter.java b/java/com/google/gerrit/server/change/ChangeKeyAdapter.java deleted file mode 100644 index 0db4cea..0000000 --- a/java/com/google/gerrit/server/change/ChangeKeyAdapter.java +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (C) 2019 The Android Open Source Project -// -// 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.google.gerrit.server.change; - -import com.google.gerrit.entities.Change; -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; -import java.lang.reflect.Type; - -/** - * Adapter that serializes {@link com.google.gerrit.entities.Change.Key}'s {@code key} field as - * {@code id}, for backwards compatibility in stream-events. - */ -// TODO(dborowitz): auto-value-gson should support this directly using @SerializedName on the -// AutoValue method. -public class ChangeKeyAdapter implements JsonSerializer, JsonDeserializer { - @Override - public JsonElement serialize(Change.Key src, Type typeOfSrc, JsonSerializationContext context) { - JsonObject obj = new JsonObject(); - obj.addProperty("id", src.get()); - return obj; - } - - @Override - public Change.Key deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - JsonElement keyJson = json.getAsJsonObject().get("id"); - if (keyJson == null || !keyJson.isJsonPrimitive() || !keyJson.getAsJsonPrimitive().isString()) { - throw new JsonParseException("Key is not a string: " + keyJson); - } - String key = keyJson.getAsJsonPrimitive().getAsString(); - return Change.key(key); - } -} diff --git a/java/com/google/gerrit/server/events/EventGsonProvider.java b/java/com/google/gerrit/server/events/EventGsonProvider.java index 688507b..72cf7be 100644 --- a/java/com/google/gerrit/server/events/EventGsonProvider.java +++ b/java/com/google/gerrit/server/events/EventGsonProvider.java @@ -15,9 +15,8 @@ package com.google.gerrit.server.events; import com.google.common.base.Supplier; -import com.google.gerrit.entities.Change; +import com.google.gerrit.entities.EntitiesAdapterFactory; import com.google.gerrit.entities.Project; -import com.google.gerrit.server.change.ChangeKeyAdapter; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.inject.Provider; @@ -29,8 +28,8 @@ public class EventGsonProvider implements Provider { .registerTypeAdapter(Event.class, new EventDeserializer()) .registerTypeAdapter(Supplier.class, new SupplierSerializer()) .registerTypeAdapter(Supplier.class, new SupplierDeserializer()) - .registerTypeAdapter(Change.Key.class, new ChangeKeyAdapter()) .registerTypeAdapter(Project.NameKey.class, new ProjectNameKeyAdapter()) + .registerTypeAdapterFactory(EntitiesAdapterFactory.create()) .create(); } } diff --git a/lib/auto/BUILD b/lib/auto/BUILD index 1da7f50..18b9b91 100644 --- a/lib/auto/BUILD +++ b/lib/auto/BUILD @@ -27,6 +27,21 @@ java_plugin( ], ) +java_plugin( + name = "auto-value-gson-plugin", + processor_class = "com.ryanharter.auto.value.gson.factory.AutoValueGsonAdapterFactoryProcessor", + deps = [ + "@auto-value-annotations//jar", + "@auto-value-gson-extension//jar", + "@auto-value-gson-factory//jar", + "@auto-value-gson-runtime//jar", + "@auto-value//jar", + "@autotransient//jar", + "@gson//jar", + "@javapoet//jar", + ], +) + java_library( name = "auto-value", data = ["//lib:LICENSE-Apache2.0"], @@ -50,3 +65,17 @@ java_library( visibility = ["//visibility:public"], exports = ["@auto-value-annotations//jar"], ) + +java_library( + name = "auto-value-gson", + data = ["//lib:LICENSE-Apache2.0"], + exported_plugins = [ + ":auto-value-gson-plugin", + ], + visibility = ["//visibility:public"], + exports = [ + "@auto-value-gson-extension//jar", + "@auto-value-gson-factory//jar", + "@auto-value-gson-runtime//jar", + ], +)