blob: a6e27b8eb12c1d79a59f8b30234302ec5afad757 [file] [log] [blame]
Dariusz Lukszadb006d82016-03-14 22:55:50 +01001// Copyright (C) 2016 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.gerrit.server.events;
15
16import static com.google.common.base.Preconditions.checkArgument;
17
18import com.google.common.base.Supplier;
19import com.google.common.base.Suppliers;
20import com.google.gson.JsonDeserializationContext;
21import com.google.gson.JsonDeserializer;
22import com.google.gson.JsonElement;
23import com.google.gson.JsonParseException;
Dariusz Lukszadb006d82016-03-14 22:55:50 +010024import java.lang.reflect.ParameterizedType;
25import java.lang.reflect.Type;
26
27public class SupplierDeserializer implements JsonDeserializer<Supplier<?>> {
28
29 @Override
Dave Borowitz292fa152016-11-13 09:56:32 -080030 public Supplier<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
31 throws JsonParseException {
Dariusz Lukszadb006d82016-03-14 22:55:50 +010032 checkArgument(typeOfT instanceof ParameterizedType);
33 ParameterizedType parameterizedType = (ParameterizedType) typeOfT;
34 if (parameterizedType.getActualTypeArguments().length != 1) {
Dave Borowitz292fa152016-11-13 09:56:32 -080035 throw new JsonParseException("Expected one parameter type in Supplier interface.");
Dariusz Lukszadb006d82016-03-14 22:55:50 +010036 }
37 Type supplierOf = parameterizedType.getActualTypeArguments()[0];
38 return Suppliers.ofInstance(context.deserialize(json, supplierOf));
39 }
40}