Search in sources :

Example 1 with IllegalArgumentCodec

use of org.opendaylight.yangtools.concepts.IllegalArgumentCodec in project mdsal by opendaylight.

the class BindingCodecContext method getLeafNodesUsingReflection.

private ImmutableMap<Method, ValueNodeCodecContext> getLeafNodesUsingReflection(final Class<?> parentClass, final Map<String, DataSchemaNode> getterToLeafSchema) {
    final Map<Method, ValueNodeCodecContext> leaves = new HashMap<>();
    for (final Method method : parentClass.getMethods()) {
        // Only consider non-bridge methods with no arguments
        if (method.getParameterCount() == 0 && !method.isBridge()) {
            final DataSchemaNode schema = getterToLeafSchema.get(method.getName());
            final ValueNodeCodecContext valueNode;
            if (schema instanceof LeafSchemaNode) {
                final LeafSchemaNode leafSchema = (LeafSchemaNode) schema;
                // FIXME: MDSAL-670: this is not right as we need to find a concrete type, but this may return
                // Object.class
                final Class<?> valueType = method.getReturnType();
                final IllegalArgumentCodec<Object, Object> codec = getCodec(valueType, leafSchema.getType());
                valueNode = LeafNodeCodecContext.of(leafSchema, codec, method.getName(), valueType, context.getEffectiveModelContext());
            } else if (schema instanceof LeafListSchemaNode) {
                final Optional<Type> optType = ClassLoaderUtils.getFirstGenericParameter(method.getGenericReturnType());
                checkState(optType.isPresent(), "Failed to find return type for %s", method);
                final Class<?> valueType;
                final Type genericType = optType.get();
                if (genericType instanceof Class<?>) {
                    valueType = (Class<?>) genericType;
                } else if (genericType instanceof ParameterizedType) {
                    valueType = (Class<?>) ((ParameterizedType) genericType).getRawType();
                } else if (genericType instanceof WildcardType) {
                    // FIXME: MDSAL-670: this is not right as we need to find a concrete type
                    valueType = Object.class;
                } else {
                    throw new IllegalStateException("Unexpected return type " + genericType);
                }
                final LeafListSchemaNode leafListSchema = (LeafListSchemaNode) schema;
                final IllegalArgumentCodec<Object, Object> codec = getCodec(valueType, leafListSchema.getType());
                valueNode = new LeafSetNodeCodecContext(leafListSchema, codec, method.getName());
            } else if (schema instanceof AnyxmlSchemaNode) {
                valueNode = new OpaqueNodeCodecContext.Anyxml<>((AnyxmlSchemaNode) schema, method.getName(), opaqueReturnType(method), loader);
            } else if (schema instanceof AnydataSchemaNode) {
                valueNode = new OpaqueNodeCodecContext.Anydata<>((AnydataSchemaNode) schema, method.getName(), opaqueReturnType(method), loader);
            } else {
                verify(schema == null, "Unhandled schema %s for method %s", schema, method);
                // We do not have schema for leaf, so we will ignore it (e.g. getClass).
                continue;
            }
            leaves.put(method, valueNode);
        }
    }
    return ImmutableMap.copyOf(leaves);
}
Also used : LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) Optional(java.util.Optional) HashMap(java.util.HashMap) TypedDataSchemaNode(org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) Method(java.lang.reflect.Method) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) IllegalArgumentCodec(org.opendaylight.yangtools.concepts.IllegalArgumentCodec) AnyxmlSchemaNode(org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode) ParameterizedType(java.lang.reflect.ParameterizedType) ListRuntimeType(org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) OpaqueObject(org.opendaylight.yangtools.yang.binding.OpaqueObject) DataObject(org.opendaylight.yangtools.yang.binding.DataObject) AnydataSchemaNode(org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode)

Aggregations

Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 WildcardType (java.lang.reflect.WildcardType)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 ListRuntimeType (org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType)1 IllegalArgumentCodec (org.opendaylight.yangtools.concepts.IllegalArgumentCodec)1 DataObject (org.opendaylight.yangtools.yang.binding.DataObject)1 OpaqueObject (org.opendaylight.yangtools.yang.binding.OpaqueObject)1 AnydataSchemaNode (org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode)1 AnyxmlSchemaNode (org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode)1 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)1 LeafListSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode)1 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)1 TypedDataSchemaNode (org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode)1