java - Implementing a MessageBodyReader for an enum -
i trying implement new messagebodyreader/writer combination, having trouble reader part. given following declaration
public class jsr367enumprovider<t extends enum> implements messagebodyreader<t>, messagebodywriter<t>
works there's warning saying enum
raw. fix tried put in enum<?>
however yield error in readfrom
method
@override public t readfrom(final class<t> type, final type generictype, final annotation[] annotations, final mediatype mediatype, final multivaluedmap<string, string> httpheaders, final inputstream inputstream) throws ioexception, webapplicationexception { // todo support other encodings using http header try (scanner s = new scanner(inputstream).usedelimiter("\\a")) { return (t) enum.valueof(type, s.next()); } }
which warns of unchecked cast. there way around without resorting @supresswarnings
?
no, there's not. recall signature of method you're trying cast:
enum.valueof(class<t> enumtype, string name)
strictly speaking on code you've provided, cannot rid of warning. suppress it, follow practices, such what's prescribed in effective java 2nd edition: item 24: eliminate unchecked warnings:
- eliminate every unchecked warning can.
- if can't eliminate warning, , can prove code provoked warning typesafe, (and then) suppress warning
@suppresswarning("unchecked")
annotation.- always use
suppresswarning
annotation on smallest scope possible.- every time use
@suppresswarning("unchecked")
annotation, add comment saying why it's safe so.
Comments
Post a Comment