I have a function created where i have used @SuppressWarning("unchecked") at method level and it works as expected but i need to move this @SuppressWarning("unchecked") to only those lines of code where the warning is coming in and not at method level:
public static <T> T marshal(Class<T> cls , String xml){
T res;
if(cls == xml.getClass()){
res=(T) xml;--->Need to use @SuppressWarning("unchecked") before this line
}else{
JAXBContext ct= JAXBContext.newInstance(cls);
Unmarshal marshal=ctx.createUnmarshller();
res=(T)marshal.unmarshal(new StringReader(xml));-->Need to use @SuppressWarning("unchecked") before this line
}
return res;
}
In the above function I have to use @SuppressWarning("unchecked") after if and else because these are the places where warnings are shown.
You could use a generic method to handle @SuppressWarning("unchecked") when appropriate, so defined like:
@SuppressWarnings("unchecked")
public <C> C cast(Object o) {
return (C) o;
}
However, when you use it and want to have checks you need to check for possible ClassCastException upon assignment (see this). So like:
try {
res = cast(xml);
} catch (ClassCastException cce) {
// a bug, help!
}
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