All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.snowdrop.istio.api.internal.MixerResourceDeserializer Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2020 Red Hat, Inc. and/or its affiliates.
 *
 * 

* 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 me.snowdrop.istio.api.internal; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.ObjectCodec; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Type; import java.util.Iterator; import java.util.Map; /** * @author Christophe Laprun */ public interface MixerResourceDeserializer { String POLYMORPHIC_PARAMS_FIELD = "params"; String INSTANCE_TYPE_FIELD = "compiledTemplate"; String HANDLER_TYPE_FIELD = "compiledAdapter"; default Object deserialize(JsonParser parser, String resourceType) throws IOException { ObjectCodec codec = parser.getCodec(); final ObjectNode node = codec.readTree(parser); final Spec newInstance = newInstance(); final Class instanceClass = newInstance.getClass(); final Iterator> fields = node.fields(); while (fields.hasNext()) { final Map.Entry field = fields.next(); final String fieldName = field.getKey(); try { final Field targetClassField = instanceClass.getDeclaredField(fieldName); final Class targetClass; if (fieldName.equals(POLYMORPHIC_PARAMS_FIELD)) { final String resourceName = node.findValue(resourceType).textValue(); targetClass = getImplementationClass(resourceName); } else { final Type type = targetClassField.getAnnotatedType().getType(); targetClass = (Class) type; } targetClassField.setAccessible(true); final Object deserialized = codec.treeToValue(field.getValue(), targetClass); targetClassField.set(newInstance, deserialized); } catch (NoSuchFieldException | IllegalAccessException e) { throw new RuntimeException(e); } } return newInstance; } Spec newInstance(); Class getImplementationClass(String resourceType); }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy