Disabling auto-boxing for Java in IntelliJ IDEA
Asked Answered
D

2

5

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?

Dacoity answered 4/4, 2013 at 18:27 Comment(5)
Out of interest why would you want to do this?Ourselves
@Ourselves I bet the answer will be: because it is not performant.Footpace
Not that I know if. It's built into the JDK, and it's not configurable.Naamana
here is the link: docs.oracle.com/javase/1.5.0/docs/guide/language/… we have a specific project. I do not think though it make sense to disable it for whole project. but my company asks to do it. I know it is configurable in eclipse at least, then I think it is possible to do in IDEA as well.Dacoity
Maybe if you set the java compiler compliance to 1.4.Plaintiff
L
14

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.

Lorient answered 4/4, 2013 at 18:37 Comment(0)
A
3

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.

Ardell answered 4/4, 2013 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.