0

I have a method looks like this.

public <E extends Enum<E> & Some> void setOther(final E some) {
    setOther(ofNullable(some).map(e -> e.name()).orElse(null);
}

Now IntelliJ suggests to use a method reference for e -> e.name() part.

And I changed it.

public <E extends Enum<E> & Some> void setOther(final E some) {
    setOther(ofNullable(some).map(Enum::name).orElse(null);
}

Now compiler complains about it.

... ....java:nnn: warning: [rawtypes] found raw type: Enum
        setOther(ofNullable(some).map(Enum::name).orElse(null));
                                      ^

Putting @SuppressWarnings({"uncheked"}) doesn't work.

How can I eliminate it? Should I stick to e -> e.name()?

Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
  • What compiler and version are you using? And please include the exact warning. Maybe it doesn't even come from the compiler but from another source code checking tool that you have installed. Javac of 1.8.0_131 nor the Java compiler of Eclipse 4.7.2RC2 generate any warnings for your code. (They do initially show errors because you are missing a closing parenthesis `)` in your first two code snippets.) – Erwin Bolwidt Jan 31 '18 at 03:45
  • Should it be `Some::name` then? – Thiyagu Jan 31 '18 at 03:46
  • 1
    And in any case, if the warning is for `[rawtypes]` you should use `@SuppressWarnings("rawtypes")` – Erwin Bolwidt Jan 31 '18 at 03:47
  • `Optional` is not a general replacement for if/else. You can write this method as `setOther(some != null ? some.name() : null);`, which is a lot more readable in my opinion. And it will eliminate generics issues entirely. – VGR Jan 31 '18 at 04:24
  • @user7 `Some` is an interface. – Jin Kwon Jan 31 '18 at 05:07
  • @ErwinBolwidt I got this warning with Gradle 4.4.1 and JDK 1.8.0_161. – Jin Kwon Jan 31 '18 at 05:12
  • 1
    While it’s possible to work-around it using `E::name`, I consider the warning a compiler bug. See also [this answer](https://stackoverflow.com/a/37189738/2711488). – Holger Feb 21 '18 at 12:34

1 Answers1

1

Change

.map(Enum::name)

To

.map(E::name)
Anton Tupy
  • 951
  • 5
  • 16