generics - Using raw type with interface in Java -
i'm trying find information raw types , possible use interface following way:
public class globalconverter { public interface listener { void onready(object t); } public void convert(string string, class<?> resultclass, listener listener) { try { listener.onready(resultclass.newinstance()); } catch (illegalaccessexception e) { } catch (instantiationexception e) { } } public void test() { convert("test", myclass.class, new listener() { @override public void onready(object object /* possible myclass object ? */) { } }); } }
what i'm trying achieve above, end user onready
callback return resultclass
type of object. hints/explanations highly appreciated.
thanks.
i'll make listener
generic:
public interface listener<t> { void onready(t t); }
and convert
method should generic:
public <t> void convert(string string, class<t> resultclass, listener<t> listener) { try { listener.onready(resultclass.newinstance()); } catch (illegalaccessexception e) { } catch (instantiationexception e) { } }
and call like:
convert("test", myclass.class, new listener<myclass>() { @override public void onready(myclass object) { } });
Comments
Post a Comment