im new to generics concept in java
In my application im using generics in one class, following is the my sample code
public class GPBFormat<T extends Message> implements IGPBFormat<T> {
@Override
public byte[] serialize(T t) 
{
    return t.toByteArray();
}
@Override
public T deSerialize(byte[] value) 
{
    T.parseFrom(value);
    return null;
}
im above code im using parseFrom method, but the problem is this method will exist only in concrete classes i.e which classes extends the Message class, so im unable to access parseFrom method with this generics. How can i solve this?
Thanks, R.Ramesh
Pass in to the constructor(s) a factory object, along the lines of:
interface Parser<T extends Message> {
    T parseFrom(byte[] value);
}
If GPBFormat does little more than you've quoted, then perhaps it should be abstract and not delegate to a separate factory.
Are these protocol buffers? Is your parseFrom method static?
If parseFrom is not static, you should do
@Override
public boolean deSerialize(T message, byte[] value) 
{
    // protocol messages return boolean whether parsing succeeded
    return message.newBuilderForType().mergeFrom(value).build();
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With