I am playing around with the C# protobuf implementation and I might have
run into a problem.
When calling the RpcCallback with a null IMessage, the c# protobuf impl
crashes in the AbstractBuilder with a null reference exception.
The funny thing is that when I run with a Java client, the java protobuf
impl does not crash with a NullException. I attached a pic in the end
pointing to the exception.
Looking at the 2 implementations, there a small difference on how the Java
and C# protobuf were implemented:
JAVA
public static <Type extends Message>
RpcCallback<Message> generalizeCallback(
final RpcCallback<Type> originalCallback,
final Class<Type> originalClass,
final Type defaultInstance) {
return new RpcCallback<Message>() {
public void run(Message parameter) {
Type typedParameter;
try {
//This does not throw an exception if called with null Message.
// So we don't fall into the catch stmt, avoiding the Null
Exception at MergeFrom
typedParameter = originalClass.cast(parameter);
} catch (ClassCastException e) {
typedParameter = copyAsType(defaultInstance, parameter);
}
originalCallback.run(typedParameter);
}
};
}
C#
public static Action<IMessage> GeneralizeCallback<TMessage, TBuilder>
(Action<TMessage> action, TMessage defaultInstance)
where TMessage : class, IMessage<TMessage, TBuilder>
where TBuilder : IBuilder<TMessage, TBuilder> {
return message => {
//This is null. So we fall into the if stmt, causing a Null Exception
at MergeFrom
TMessage castMessage = message as TMessage;
if (castMessage == null) {
castMessage =
defaultInstance.CreateBuilderForType().MergeFrom(message).Build();
}
action(castMessage);
};
}
Is there a way the cast done in the C# implementation could be done
differently such that both implementations would be consistent?