39

Jetbrains has recently decided that java.util.Optional (as well as Guava's com.google.common.base.Optional) shouldn't be used as field or parameter type... thanks, but no thanks... How can this warning be disabled?

As a side note: triggering warnings on this specific usage of Optional seems completely unjustified...

Andrey
  • 8,882
  • 10
  • 58
  • 82
  • 8
    I assume you've read this: http://stackoverflow.com/a/26328555/3973077 – Paul Boddington Mar 22 '16 at 22:48
  • 1
    This actually looks like a great warning I'll have to look into using... – Louis Wasserman Mar 22 '16 at 22:56
  • 1
    @LouisWasserman the problem is that you *don't* have to look into it - this feature is opt-out. i'm curious to know why you'd want this... what are the alternatives? going back to passing nulls around and/or throwing exceptions? – Andrey Mar 23 '16 at 11:50
  • 1
    @PaulBoddington how exactly is that question about using `Optional` as *return value* from getters relevant to its usage as a *field or parameter type*? – Andrey Mar 23 '16 at 13:51
  • 1
    @Andrey I linked to an answer, not a question. The answer I linked to says `You should almost never use it as a field of something or a method parameter`. – Paul Boddington Mar 23 '16 at 14:04
  • 6
    @PaulBoddington and i assume you're read this http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html ? so, i'll let the readers decide what's more important: a single person's unbacked claim in a public forum that the intention of Optional `was to provide a limited mechanism for library method return types` or an official article from Oracle demonstrating the contrary... i maintain the claim is unbacked because there's nothing in the javadocs of Optional to support it. – Andrey Mar 23 '16 at 15:22
  • 11
    @Andrey That single person is Brian Goetz who is Oracle's lead Java guy. I agree with you that there are times when you should break advice (whoever the advice comes from), and I have at times broken advice from both Oracle and BG. On this one I am with BG though (if I have any disagreement it's that I would probably have used `Optional` and `OptionalInt` etc as a return type in *fewer* cases than they decided to). I only linked as it's interesting reading - I wasn't trying to antagonise :) – Paul Boddington Mar 23 '16 at 15:32
  • 5
    @PaulBoddington no offense taken, just being objective. I'm aware of who Brian Goetz is, but why let a person's reputation cloud the facts? He's still just one person doing nothing more than *expressing an opinion*. Let's also not forget the fact that a slew of other languages have similar constructs without any usage restrictions. And whether it's Haskell's Maybe or Java's Optional it's still a monad and neither its definition nor its usage is a subject to change or interpretation. – Andrey Mar 23 '16 at 16:00
  • 1
    Would it help to have the same advice in Guava's Javadocs? – Louis Wasserman Mar 23 '16 at 16:24
  • 8
    @LouisWasserman *same* advice, no. But an advice specifically explaining why Optional shouldn't be used as a field or, especially, parameter type would help. So far, the only genuine reason I've seen for not using java.util.Optional is because it isn't serializable. However, this argument doesn't apply to Guava's Optional. Finally, Guava's Optional isn't a monad so should really be discussed separately from java.util.Optional altogether. – Andrey Mar 23 '16 at 17:01
  • Thanks for the discussion. Based on it I've just decided to create more signatures in a Spring rest controller instead of using Optional PathVariables. – Gunnar May 25 '20 at 14:33

1 Answers1

35

From IntelliJ's preferences/settings:

  1. Select Editor > Inspections
  2. Expand Java > Abstraction issues
  3. Uncheck 'Optional' used as field or parameter type
Andrey
  • 8,882
  • 10
  • 58
  • 82
  • 68
    If this question were reopened here would be my answer: `@SuppressWarnings("OptionalUsedAsFieldOrParameterType")` See https://github.com/JetBrains/intellij-community/blob/master/plugins/InspectionGadgets/src/inspectionDescriptions/OptionalUsedAsFieldOrParameterType.html – Karl the Pagan Oct 12 '16 at 17:54
  • 5
    @KarlthePagan Based on the tip at https://stackoverflow.com/a/40005307/1108305 it turns out you can generally search for the inspection text in IntelliJ's [InspectionDescriptions](https://github.com/JetBrains/intellij-community/tree/master/plugins/InspectionGadgets/src/inspectionDescriptions) git directory to find the value to use with `@SuppressWarnings`. – M. Justin May 08 '19 at 20:53