Using specific implementation type for a java interface method having generics parameter. How to avoid uncheked cast?

Up vote 1 down vote favorite 1 share g+ share fb share tw.

I have the following interface public interface ITransform { V convert(T object) throws Exception; } I have a class, Class1 that uses this interface as its method parameter. But it expects a specific impl type(ChannelBuffer) in the place of T as shown below. Public static V readObject(ChannelBuffer buffer, int length, ITransform transformer) { ChannelBuffer buffer = buffer.

ReadSlice(length); V obj = null; obj = transformer. Convert(buffer); return obj; } I have an implementing class, Class2 for the ITransform interface as shown below. Public class Transformer implements ITransform{ @Override public Object convert(ChannelBuffer buffer) throws Exception { return someObject; } } The issue I am facing is that when I want to call the readObject of Class1 from Class3, I need to do an unchecked cast as shown below.

Public V readObject(ITransform converter) { @SuppressWarnings("unchecked") ITransform decoder = (ITransform) converter; return class1. ReadObject(buffer, decoder); } The reason that Class3 itself cannot have the ChannelBuffer in its readObject method parameter is that it is implementing an Interface2 method V readObject(ITransform converter) I do not want to modify the Interface2 method to have ChannelBuffer since that will tie all implementations of the interface to ChannelBuffer. Java generics template-methods link|improve this question edited Nov 9 '11 at 14:57 asked Nov 9 '11 at 14:25Abe551313 78% accept rate.

– Hanno Binder Nov 9 '11 at 14:33 @Hanno Binder I did that right after I posted...:) Thanks anyway. I have one remaining q. Class3 instance is holding a byte buffer internally.

It can contain different types of objects(String, int etc). But if I initialize class3 like this ->new Class3, how can I use readObject method of same instance of Class3 to get the integer/ other types? – Abe Nov 9 '11 at 14:45 I don't think Class3 should be generic in this case.

The V in ITransform converter should determine the type of the returned object. At a glance, your Class1.readObject() already does this (almost? ).

– Hanno Binder Nov 9 '11 at 15:26 I thought so! Ok, thanks for confirming. Yes Class1.

ReadObject is able to return V without needing any additional casting. I guess one unchecked cast seems to be unavoidable. – Abe Nov 9 '11 at 15:54.

Interface Interface2 public V readObject(ITransform converter) class Class3 implements Interface2 public V readObject(ITransform converter) { return class1. ReadObject(converter, decoder); }.

1) FYI...Casts are not unsafe in Java as they are in C. 2) The safety can be avoided by simply using instanceof to test whether the object is the right type before the cast. 3) Interfaces are simply contracts.

Would it help to create another interface? 4) You could make the readObject method part of the interface and make it's implementation optional by throwing NotSupportedException Overall, what you're describing usually indicates a problem in your model, but I can't help much with that because I don't know your domain. Let me know if I can assist further.

1 I think by "unsafe" casts he means "unchecked. " Casting to a generic type results in an unchecked cast since type parameters are usually erased at runtime. And similarly, instanceof cannot check whether an object has a certain set of type parameters.

– Mark Peters Nov 9 '11 at 14:50 instanceof would not be so useful since java uses type erasure and at runtime both of them would be the same. Yes, my model could have issue. I am having a byte buffer in Class3 which can contain multiple objects like String, Integer in it.

Clients can pass the appropriate transformer and decode the object from the byte buffer using readObject method. That is what I am trying to achieve. – Abe Nov 9 '11 at 14:51 So a transformer needs and input method and an output method, right?

I mean you can't transform something without a way to put things in or get things out. If it's a question of types of what you're passing in, then you could create another set of classes called transformer input source or something that took what you were passing in. Sorry, forgot about the type erasure stuff.

– Thom Nov 9 '11 at 15:25 Types could be set in another machine in the form of serialized(not necessarily java serialization) bytes and sent over the network. The code reading from the byte buffer is expected to know the sequence of objects, for e.g. 1st String second SomeClass 3rdInteger etc. It then uses appropriate transformer to transform and retrieve back the objects. Am I clear now?

– Abe Nov 9 '11 at 15:56 1 Then all of your transformers need to read an array of bytes and use a factory method (and perhaps chain of responsibility) to create the proper transformer. – Thom Nov 9 '11 at 17:08.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions