Disabling auto-boxing for Java in IntelliJ IDEA

∥☆過路亽.° 提交于 2019-12-07 08:34:28

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/15818875/disabling-auto-boxing-for-java-in-intellij-idea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!