0

I have a part of a code where "Currencies" is Enum and "Currency" is Entity:

public final Map<Currencies, Currency> currencies = new EnumMap<>(Currencies.class);

But Eclipse generates a warning:

Unsafe null type conversion (type annotations):
The value of type '@NonNull EnumMap<TestDataServiceImpl.@NonNull Currencies,Currency>'
is made accessible using the less-annotated type 'Map<TestDataServiceImpl.Currencies,Currency>',
corresponding supertype is 'Map<TestDataServiceImpl.@NonNull Currencies,Currency>'

What is the right way to handle it? Should I wrap my map with Optional? Is it mandatory? Should I ignore the warning as I know there will be no null case?

horvoje
  • 643
  • 6
  • 21
  • 2
    I generally prefer not to make mutable objects public, but also you can do exactly what it suggests and annotate `Currency` with `@NonNull`. – chrylis -cautiouslyoptimistic- May 30 '20 at 01:49
  • @chrylis -cautiouslyoptimistic - You were right, no need for it to be public. Now it's private, but also I had to put @NonNull as you adviced. Now I have: `private final Map<@NonNull Currencies, Currency> currencies = new EnumMap<>(Currencies.class);` which is extremely ugly but does not trigger a warning. If you make it an answer, I will mark it green. – horvoje May 30 '20 at 13:31
  • 1
    @horvoje It doesn't look extremely ugly to me, unless you considered the original code to also be extremely ugly. Your new version is only 1 token longer (9 characters) than your original code, and is explicit about types that previously would have had to be documented in code comments, so your code is probably shorter overall. – mernst May 30 '20 at 17:29
  • @mernst - Ok, I can agree with you but... Injecting annotations between parent and inner class looks to me ugly: `TestDataServiceImpl.@NonNull Currencies` But it works, that's important :) – horvoje May 30 '20 at 23:58
  • 1
    @horvoje Yes, I agree that aspect of the Java annotation syntax is ugly. It was forced by the existing Java syntax. Annotations on fully-qualified type uses `java.util. @NonNull String` are ugly in the same way. You can avoid these by using import statements, then using simple names as type uses. – mernst May 31 '20 at 13:55

0 Answers0