News

IntelliJ IDEA 7 Milestone 1: Better Code Inspections, Intention Actions and more

Like any new release, IntelliJ IDEA 7 Milestone 1 contains many new features. So many in fact, it is probably impossible to notice them all. Among the many hundreds of inspections and intentions already present in the product, a new inspection or intention may be difficult to notice. Each by itself is just a small feature, but together they are quite powerful.

Here I will list all new Java inspections and intentions I personally have implemented for IntelliJ IDEA 7 Milestone 1. Note that this is not a complete list of all new inspections and intentions in the Milestone, just the ones which are new to the InspectionGadgets and IntentionPowerPack plugins. Many more inspections and intentions were also added to the Milestone for Spring, HTML, Ant and many others.

New Inspections

Inspection Brief Description
Comparable implemented but ‘equals()’ not overridden Classes with implement Comparable but do not override equals() can cause some surprising behavior. For example adding an object of such a class to a SortedSet will break the contract of the set, because that is defined in terms of equals().
Manual array to collection copy Copying an array into a collection using a for loop can be quite verbose. A simple list.addAll(Arrays.asList(array)) can do the trick in one relatively short expression.
‘indexOf()’ expression replacable by ‘contains()’ (for java.util.List) Using indexOf() to test for the presence of an object in a list can be cumbersome. Using the contains() method leads to clearer code.
Unqualified instance field access Some coding styles prefer to qualify each instance field access with a this qualifier. Violations of the style are highlighted by this inspection.
Method with synchronized block could be synchronized method When the entire body of a method is wrapped in a synchronized block synchronizing on this, it would be less verbose to mark the method synchronized and remove the synchronized block.
Method call violates Law of Demeter The Law of Demeter is a design principle sometimes used for Object Oriented designs.
‘assertEquals()’ between objects of inconvertible types This inspection warns if assertEquals() can never succeed because the two object supplied can not be casted to each others types. This is a serious bug in a test case.
Enumeration can be iteration This inspection helps converting older code to the collections framework. Enumerations of a Vector or Hashtable can be replaced with the equivalent Iterator code, using this inspections quick fix.
‘equals()’ or ‘hashCode()’ called on java.net.URL object When the equals() or hashCode() methods are called on a java.net.URL object, it will iniate a DNS lookup. This can be very time consuming and lead to performance issues.
Map or Set may contain java.net.URL objects Map or Set implementations use the equals() and
hashCode() methods to add and retrieve their contents. When the Map or Set contain URL objects, this will result in expensive DNS lookups.
Number comparison using ‘==’ instead of ‘equals()’ A special case of the already existing “Object comparison using ‘==’ instead of ‘equals()’. This can be an extra serious problem for Numbers since autoboxing was introduced in Java 5. Small values of Integers are cached for example, which can lead to hard to predict success and failures of == comparisons.
Implicit call to array ‘.toString()’ When a array is appended to a string it will append the default toString() of the array object. The actual desired outcome is usually to append the contents of the array, not the array object itself. This problem is easily fixable with the included quick fix.
Suspicious indentation after control statement without braces If the statement after an if statement is indented to the same level as the if statement, it may appear as if it is part of the if statement, when in reality it is executed every time.
Unpredictable BigDecimal constructor call Constructing a new BigDecimal object using a double value, may cause rounding errors. It is better to create a new BigDecimal with a string.
Unnecessary unary minus One plus minus one, equals one minus one, which is a simpler expression

New Intentions

Intention Brief Description
Replace For Loop with While Loop Replaces a for loop with the equivalent while loop.
Replace While Loop with Do While Loop Replaces a while loop with the equivalent construct: a do-while loop surrounded by an if statement.
Replace Do While Loop with While Loop Replaces a do-while loop with an equivalent while loop construct.
Replace Operator Assignment with Assignment For example i += 1 becomes i = i + 1
Replace Catch Section with Throws Declaration Removes the catch section and adds a throws declaration to the surrounding method.
Wrap Vararg Arguments with Explicit Array Creation This intention makes the array in a variable argument method call explicit. Useful when the array has to be extracted to a variable.
Replace For-each Loop with Indexed For Loop
and
Replace For-each Loop with Iterator For Loop
These two replace the “Replace For-each Loop with Old Style For Loop” intention in IntelliJ IDEA 6, which was more limited. The first intention replaces a foreach loop, which iterates over an array or list, with an indexed for-loop. The second replaces a foreach loop, which iterates over a collection object with an iterator for-loop construct.

Furthermore, in this Milestone release many improvements and bug fixes were introduced to InspectionGadgets and IntentionPowerPack, making intentions and inspections applicable in wider range of cases; new quick fixes were added and documentation was also improved.

Download the Milestone release to give the new and improved features a try.

Read more about new features and improvements.

Technorati tags: IntelliJ, IDE, IntelliJ IDEA, Code Inspection, Code Analysis
image description