0

How to remove type safety markers for following "Map" in Java usingEclipse?

Map<String, String> errorStatus = exchange.getIn().getHeader("totalCount", Map.class);

It prints following message.

The Expression of type map needs unchecked conversion to confirm to Map<String,String>

enter image description here

UPDATE:

getIn() and getHeader are inbuilt Camel Method. In a route, i have setHeader("headerkey",mapOfErrors) and when i need this error key further i am just fetching it from Camel Message using those functions. Following are available parameters of getHeader(): enter image description here

fatherazrael
  • 5,511
  • 16
  • 71
  • 155
  • It looks like one of those "you can't" situations because the inherent flaws in Java's type system. But I can't say for sure with so little information. – Sweeper May 21 '20 at 10:04
  • 1
    @Amongalen Since when can you do `Map.class`? – Sweeper May 21 '20 at 10:05
  • 1
    @Amongalen Try it. – Aniket Sahrawat May 21 '20 at 10:05
  • If not then shall i write it like Map in place of Map – fatherazrael May 21 '20 at 10:09
  • @fatherazrael No, you should still use `Map`. If this is really one of those "you can't" situations, you can put a `@SuppressWarnings`. But as I said, **I can't tell unless you tell me more information.** For example, what is `getHeader`? What is `getIn`? *What are you doing?* – Sweeper May 21 '20 at 10:17
  • @Sweeper getIn() and getHeader are inbuilt Camel Method. In a route, i have setHeader("headerkey",mapOfErrors) and when i need this error key further i am just fetching it from Camel Message using those functions. I have updated all methods available in description – fatherazrael May 21 '20 at 10:30
  • I think your question has been answered at following links: https://stackoverflow.com/a/262416/2689980 and https://stackoverflow.com/a/509115/2689980 – Abhishek May 21 '20 at 13:52

1 Answers1

0

As you know there is no Map<String,String>.class, so as far as I know for your case you have two options: first create a wrapper class to extend the HashMap and you can use the Wapper.class, which I don't recommend

WrapperClass errorStatus = exchange.getIn().getHeader("totalCount", Wrapper.class);

The second approach just ignore this warning and use @SuppressWarnings("unchecked")

Saif Alradhi
  • 129
  • 3
  • 15