Jtest logo




Contents  Previous  Next  Index

OPT.UISO


Avoid unnecessary "instanceof" evaluations

Description

This rule flags unnecessary "instanceof" evaluations.

If the "static" type of the left-hand-side is already the same type as the right-hand-side, then use of the "instanceof" expression is always true.

Example

 package OPT;
 
 public class UISO {
     public UISO () {}
 }
 
 class Dog extends UISO {
     void method (Dog dog, UISO u) {
         Dog d = dog;
         if (d instanceof UISO) // always true.
             System.out.println("Dog is a UISO"); 
         UISO uiso = u;
         if (uiso instanceof Object) // always true.
             System.out.println("uiso is an Object"); 
     }
 }

Repair

Remove the unnecessary "instanceof" evaluations:

 class Dog extends UISO {
     void method () {
         Dog d;
         System.out.println ("Dog is an UISO");
         System.out.println ("UISO is an UISO");
     }

Contents  Previous  Next  Index

ParaSoft logo
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft