Wednesday, September 28, 2011

PMD - Migration Rules

The thirteenth installation of explanation of PMD rules covering Migration Rules. These rules cover the changes that need to be done by developers when moving from older versions of JDK (1.3, 1.4) to the newer versions of Java.

Migration Rules

Contains rules about migrating from one JDK version to another. Don't use these rules directly, rather, use a wrapper ruleset such as migrating_to_13.xml.

ReplaceVectorWithList

Consider replacing Vector usages with the newer java.util.ArrayList if expensive threadsafe operation is not required.

Example

public class Foo {
void bar() {
    Vector v = new Vector();
}
}

ReplaceHashtableWithMap

Consider replacing this Hashtable with the newer java.util.Map

Example

public class Foo {
     void bar() {
        Hashtable h = new Hashtable();
     }
    }

ReplaceEnumerationWithIterator

Consider replacing this Enumeration with the newer java.util.Iterator

Example

public class Foo implements Enumeration {
    private int x = 42;
    public boolean hasMoreElements() {
        return true;
    }
    public Object nextElement() {
        return String.valueOf(i++);
    }
}

AvoidEnumAsIdentifier

Finds all places where 'enum' is used as an identifier.

Example

public class A {
        public  class foo {
            String enum = "foo";
        }
    }

AvoidAssertAsIdentifier

Finds all places where 'assert' is used as an identifier.

Example

public class A {
        public  class foo {
            String assert = "foo";
        }
    }

IntegerInstantiation

In JDK 1.5, calling new Integer() causes memory allocation. Integer.valueOf() is more memory friendly. Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values.

Example

public class Foo {
private Integer i = new Integer(0); // change to Integer i = Integer.valueOf(0);
}

ByteInstantiation

In JDK 1.5, calling new Byte() causes memory allocation. Byte.valueOf() is more memory friendly.

Example

public class Foo {
private Byte i = new Byte(0); // change to Byte i =
Byte.valueOf(0);
}

ShortInstantiation

In JDK 1.5, calling new Short() causes memory allocation. Short.valueOf() is more memory friendly.

Example

public class Foo {
private Short i = new Short(0); // change to Short i =
Short.valueOf(0);
}

LongInstantiation

In JDK 1.5, calling new Long() causes memory allocation. Long.valueOf() is more memory friendly.

Example

public class Foo {
private Long i = new Long(0); // change to Long i =
Long.valueOf(0);
}

JUnit4TestShouldUseBeforeAnnotation

In JUnit 3, the setUp method was used to set up all data entities required in running tests. JUnit 4 skips the setUp method and executes all methods annotated with @Before before all tests

Example

public class MyTest {
    public void setUp() {
        bad();
    }
}
public class MyTest2 {
    @Before public void setUp() {
        good();
    }
}

JUnit4TestShouldUseAfterAnnotation

In JUnit 3, the tearDown method was used to clean up all data entities required in running tests. JUnit 4 skips the tearDown method and executes all methods annotated with @After after running each test

Example

public class MyTest {
    public void tearDown() {
        bad();
    }
}
public class MyTest2 {
    @After public void tearDown() {
        good();
    }
}

JUnit4TestShouldUseTestAnnotation

In JUnit 3, the framework executed all methods which started with the word test as a unit test. In JUnit 4, only methods annotated with the @Test annotation are executed.

Example

public class MyTest {
    public void testBad() {
        doSomething();
    }
 
        @Test
    public void testGood() {
        doSomething();
    }
}

JUnit4SuitesShouldUseSuiteAnnotation

In JUnit 3, test suites are indicated by the suite() method. In JUnit 4, suites are indicated through the @RunWith(Suite.class) annotation.

Example

public class BadExample extends TestCase{
 
    public static Test suite(){
        return new Suite();
    }
}
 
@RunWith(Suite.class)
@SuiteClasses( { TestOne.class, TestTwo.class })
public class GoodTest {
}

JUnitUseExpected

Example

public class MyTest {
        @Test
    public void testBad() {
        try {
            doSomething();
            fail("should have thrown an exception");
        } catch (Exception e) {
        }
    }
 
        @Test(expected=Exception.class)
    public void testGood() {
        doSomething();
    }
}

No comments: