简体   繁体   中英

Upgrading to a new Java version: code analysis

I was wandering if there is a code analysis tool able to suggest code changes when upgrading to a new Java version.

For example:

  • look for .close() calls for autocloseable resources when upgrading to Java 7
  • look for multiple catch clauses with the same body when upgrading to Java 7
  • look for unnecessary manual boxing and unboxing when upgrading to Java 5
  • ...

Are there specific tools or rules by existing ones (eg, Checkstyle) that could help?

IntelliJ has a set of inspections tagged as "Language migration", that can help you spot locations where you could benefit from new language structures such as the enhanced for loop.

It can even automatically apply such advices to your whole codebase at once - if it's a no-risk transformation of course.

Findbugs handles .close() as explained at Java resource management: understanding Findbugs results and also catches a lot of other problems.

Finsbugs deals with some problematic boxing, as when used as an operand to == . Eclipse warnings can be configured to warn you about all implicit boxing/unboxing.

I don't know of anything that deals with source-wise identical catch bodies.

I don't know a tool specifically designed for this, but there are a number tools you could use for general code auditing that will detect or suggest these types of upgrades.

  1. CodePro Analytix
  2. FindBugs
  3. PMD (especially the CPD portion for redundant catch blocks)
  4. Checkstyle

The CodePro Eclipse plugin by Google has a number of really useful code analysis features that can find a number of Java 7 features you mentioned.

The Copy/Paste Detector in PMD can detect copy-pasted code in general, so it's good for finding redundant catch blocks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM