Thursday, October 18, 2007

Generics (A Compile-Time Protection)

All the generic code is strictly for the compiler. Through a process called "type erasure," the compiler does all of its verifications on your generic code and then strips the type information out of the class bytecode. None of your typing information exists at runtime.
In other words, even though you write
List myList = new ArrayList();


By the time the compiler is done with it, the JVM sees what it always saw before Java 5 and generics:
List myList = new ArrayList();

The compiler even inserts the casts for you—the casts you had to do to get things out of a pre-Java 5 collection. Think of generics as strictly a compile-time protection. The compiler uses generic type information (the in the angle brackets) to make sure that your code doesn't put the wrong things into a collection, and that you do not assign what you get from a collection to the wrong reference type. But NONE of this protection exists at runtime.

public class GenericTest
{
public static void main(String[] args)
{
List list1 = new ArrayList();
list1.add(7);
list1.add(16);
NonGeneric ng = new NonGeneric ();
ng.insert(list1);
System.out.println(list1);
}
}

class NonGeneric
{
void insert(List list)
{
list.add("hello");
}
}

Above code will compile and run without errors. Output will be
[7, 16, hello]

Improving Code Quality with PMD and Eclipse

PMD is a static code analyzer for Java. Developers use PMD to comply with coding standards and deliver quality code. Team leaders and Quality Assurance folks use it to change the nature of code reviews. PMD has the potential to transform a mechanical and syntax check oriented code review into a to dynamic peer-to-peer discussion.

What is PMD?
PMD works by scanning Java code and checks for violations in three major areas:

Compliance with coding standards such as:


Naming conventions - class, method, parameter and variable names
Class and method length
Existence and formatting of comments and JavaDocs
Coding antipatterns such as:

Empty try/catch/finally/switch blocks
Unused local variables, parameters and private methods
Empty if/while statements
Overcomplicated expressions - unnecessary if statements, for loops that could be while loops
Classes with high Cyclomatic Complexity measurements
Cut and Paste Detector (CPD) - a tool that scans files and looks for suspect code replication. CPD can be parameterized by the minimum size of the code block.
In its current version, PMD comes packaged with 149 rules in 19 rulesets. Most of these rules can be parameterized at runtime by supplying properties or parameters. The standard package offers many well-thought rules. In addition users also have the ability to add their own rules for particular coding convention or quality metrics.

Here are some of the rules distributed with PMD:

EmptyFinalizer - If the finalize() method is empty, then it does not need to exist.
EmptyFinallyBlock - Avoid empty finally blocks - these can be deleted.
UnnecessaryReturn - Avoid unnecessary return statements
OnlyOneReturn - A method should have only one exit point, and that should be the last statement in the method.
CyclomaticComplexity - Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
TooManyFields - Classes that have too many fields could be redesigned to have fewer fields, possibly through some nested object grouping of some of the information. For example, a class with city/state/zip fields could instead have one Address field.
LongVariable - Detects when a field, formal or local variable is declared with a long name.
NoPackage - Detects when a class or interface does not have a package definition

Compiling code in Eclipse

One of the best features of Eclipse is that it does not use the standard java compiler (javac.exe) which comes as part of the JDK. Eclipse instead comes bundled with

it's own compiler, which allows you to configure it in many interesting ways.For e.g. Some of the things you can do at compile time are as follows Report an error/warning for missing javadocs on public methods. Report an error/warning for malformed/incorrectly written javadocs.Report an error/warning for undocumented empty blocks.Report an error/warning for unused import statement.Report an error/warning for access to a null pointer.etc

All these settings can be configured by the settings under

Preferences -> Java -> Compiler.

Many of these settings are set to 'ignore' by default.

If set appropriately, Eclipse by itself can improve the quality of code which we write.

Wednesday, October 17, 2007

Don't Invoke Potentially Overridable Methods from a Constructor

You already know that you can't access any nonstatic things prior to your
superconstructor running, but keep in mind that even after an object's
superconstructor has completed, the object is still in an incomplete state
until after its constructor has finished. Polymorphism still works in a
constructor.

look at following example:-

public class B extends A

{

B()

{

}

public static void main(String[] arg)

{

new B().method();

}

public void method()

{

System.out.println("Class B method");

}

}

class A

{

A()

{

method();

}

public void method()

{

System.out.println("Class A method");

}

}

O/P will be

Class B method

Class B method

don't try this(called overridden method from constructor ) sort of stuff in
your code. If it's a final or private instance method, then you're safe
since you know it'll never be overridden.

Thread stop and suspend

Threads should not be stopped using stop or suspend. These methods are deprecated.

Instead, threads should use the following simple idiom to stop in a well behaved manner.

public final class SimpleThread implements Runnable {

public void run() {
boolean isDone = false;

while (!isStopRequested() && !isDone){
//perform the work
}
}

/**
* Request that this thread stop running.
*/
public synchronized void requestStop(){
fIsStopRequested = true;
}

// PRIVATE ////////

private boolean fIsStopRequested;

private synchronized boolean isStopRequested() {
return fIsStopRequested;
}
}