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);
}
Aggregations