26

I like the generics-feature in java and use it often. But I have a problem, if I use libraries that aren't yet aware of generics. An example are servlets. If you use ServletRequest.getParameterMap() the result will be a raw map, but it includes only String as keys and String[] as values. So I want to assign it to a Map<String, String[]>. But for this assignment I get an warning. How can I avoid this warning with the language, not by simply suppressing the warning with the @SuppressWarnings annotation.

7 Answers 7

41

As others have said the warnings cannot be avoided except by suppressing them. The issue IMHO is that either you have to litter your code with annotations that apply to small scope or ignore them globally and risk errors.

IIRC there is a proposal to generate warnings where the raw types are being returned instead of at the call.

Meanwhile, I think the best approach is to use a wrapper method so that the warnings are limited to a single place, where it is safe to ignore them:

class NoWarn {
  public static Map<String, String[]> getParameterMap(ServletRequest r) 
  {
    @SuppressWarnings("unchecked")
    Map<String, String[]> result = r.getParameterMap();
    return result;
  }
}

Note This answer was edited with a comment that annotations cannot be inside method bodies. That is incorrect, the above is syntactically correct. I have reverted the change.

3
  • I hope that the proposal you mentioned will be implemented fast (Java7?), because that would clearly solve my problem.
    – Mnementh
    Jul 20, 2009 at 10:41
  • The edit would be correct if "result" were declared outside of the method... In that case you'd need to move the annotation to the line before the method.
    – Manius
    May 6, 2019 at 6:40
  • @Manius your comment confused me for a bit (this being a 10 year old answer perhaps), until I realized that the annotation is currently on the variable definition. May 6, 2019 at 16:37
8

The cleanest thing you can do is to encapsulate the conversion from legacy to generic code and suppress the warning only there.

E.g. you could put a generic facade on your legacy library, though this might not always be worthwhile.

1
  • 1
    +1 Write a boilerplate generified wrapper around the legacy API and slap warning suppressions on that.
    – skaffman
    Jul 15, 2009 at 10:35
2

How can I avoid this warning with the language, not by simply suppressing the warning with the SuppressWarnings-annotation.

The annotation is the way to avoid the warning with the language. There is no other way.

2

Stumbled upon this question as I was also trying to figure out a way to avoid using the suppress annotation in such cases. I found another alternative which I thought was worth mentioning:

    Map<?, ?> map = servletRequest.getParameterMap();
    String[] values = (String[]) map.get("key");

We are basically using wildcard '?' to indicate that the map can have any type of key and value.

Potential downside I see here is that we are doing an explicit cast while fetching values, which I think will result in a slight performance overhead during runtime.

1

I don't think you can. The warning will appear unless you suppress it, or filter it from your IDE's warnings list.

1

I had the same problem, i just turned off all generic warnings and im happy :) You could also turn off serialVersionUID warning since many people dont use serialVersionUID.

in Eclipse - Window/Perferences/Java/Compiler/Errors/Warnings and turn off all Generic types.

P.S. Many bad warnings make you ignore all the warnings and some might be usefull.

3
  • 1
    I use warnings as a possibility for improvement. I don't get many warnings, because I fix them all on the way. Normally the unchecked-conversion-warnings are very helpful to find code-parts that can be improved. But in this case I don't see a way without suppressing this warning.
    – Mnementh
    Jul 15, 2009 at 12:03
  • 1
    you are lucky, i now have 61 warnings in project, even unused imports warning. i wont fix it since they are not in my code, so Ive closed the Problems Tab and just look for yellow tags in my source code.
    – IAdapter
    Jul 15, 2009 at 12:30
  • I fix warnings also in code of others in the same project. If problems occur we discuss the rules for the checkers.
    – Mnementh
    Jul 15, 2009 at 16:11
0

as others said, the only way to get rid of this warning is to suppress it.

the best practice is to encapsulate the warning using methods and classes.

but with other warnings, always try to solve the problem that are making them, like remove unused imports and etc... it makes your application leaner and better.

happy coding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.