Question

Is there a way how to disable auto-boxing for Java 5 and 6 in IntelliJ IDEA, to not allow a developer to use this feature in the IDE?

Was it helpful?

Solution

I don't think you can disable auto-boxing outright while maintaining the target compile version - that's a feature of the specific Java version.

What you can do in IntelliJ is change the inspection level of Auto-boxing to "Error". To do that:

  • Go to Settings > Inspections, and type "boxing" into the search bar.
  • Click on "Auto-boxing".
  • Set the severity to "Error". This will cause the inspections test to report any occurrence of auto-boxing as an error. You should do the same for auto-unboxing as well.

Further down the line, you can add code inspections which run when testing the code (PMD, FindBugs, Cobertura, et. al.) which will fail the build if anything is being auto-boxed or auto-unboxed.

OTHER TIPS

You can't really disable autoboxing without making your Java a form of "not-Java"; however, you can reduce the impact of some of the worst autoboxing issues.

FindBugs, a code analyzer, has a few specific autoboxing rules to avoid circumstances where autoboxing becomes quite problematic.

They all start with the "Bx:" identifier:

  1. Bx: Primitive value is boxed and then immediately unboxed (BX_BOXING_IMMEDIATELY_UNBOXED)
  2. Bx: Primitive value is boxed then unboxed to perform primitive coercion (BX_BOXING_IMMEDIATELY_UNBOXED_TO_PERFORM_COERCION)
  3. Bx: Boxed value is unboxed and then immediately reboxed (BX_UNBOXING_IMMEDIATELY_REBOXED)
  4. Bx: Method allocates a boxed primitive just to call toString (DM_BOXED_PRIMITIVE_TOSTRING)
  5. Bx: Method invokes inefficient floating-point Number constructor; use static valueOf instead (DM_FP_NUMBER_CTOR)
  6. Bx: Method invokes inefficient Number constructor; use static valueOf instead (DM_NUMBER_CTOR)

You can integrate a Findbug report into your build, and depending on the build system you use, even have the build complain or fail based on the presence of issues found by FindBugs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top