Saturday, November 25, 2006

Tools to clean/analyse your junk code

While we all write the "best code in the world", only few of us actually take the pain review/analyse it for improvements(for the obvious reason that best cannot be improved!).


Fortunately java world provide a bunch of tools to analyze your source code and provide recommendations to clean them. Eclipse and java development has evolved to a point where most of the development related activities can be acheived from a single IDE with plugins.

Source code analyzers typically work using a set of rules. These rules can be modified, changed, etc based upon requirements. Every tool comes bundled with its own set of rules. Rules belong to categories. You can execute rules/categories selectively or all of them on your source code. Recommendations are then presented in tabular form. You then have a choice of fixing it on your own or allowing the tool to fix it. Caution: Some rules might alter the logic of your source code, so make sure you are aware of the changes before you execute them. Popularity of a tool depends on the effectiveness of the rules and the number of rules in it.

Here are some interesting tools:

Eclipse Refactor:
Right click your source in eclipse and check the options under "source" and "refactor". You can do lot of things like "correct indentation", "format" and other cleanups. This is a good way to make your code look clean and green.

PMD:(free)

PMD scans Java source code and looks for potential problems like:

  • Possible bugs - empty try/catch/finally/switch statements
  • Dead code - unused local variables, parameters and private methods
  • Suboptimal code - wasteful String/StringBuffer usage
  • Overcomplicated expressions - unnecessary if statements, for loops that could be while loops
  • Duplicate code - copied/pasted code means copied/pasted bugs
You can either use the default(around 200) rules or write your own rules using xpath/java.
AppPerfect (commercial)
  • Applies over 600 high-value coding rules, including optimization, portability, i18n and coding standards, while analyzing your source code
  • Automatically fixes many of the violations
  • Support for JDK 1.5-specific syntax and keywords
  • Comprehensive reports including custom report designer and export of reports in PDF, Excel and HTML formats
  • Command line invocation to integrate with build scripts (including ANT-based built scripts)

Eclipse Metrics(free)
Checks for:

Other links:
@Eclipse source code analyzer
@Cyclomatic complexity
@Maven plugin for reporting
[Checkstyle, FindBugs, PMD, Lint4j, JavaNCSS, JCoverage, Cobertura, Emma, Clover, Tasks List]

Tuesday, November 21, 2006

"do you know"

All static methods are automatically final!

Instances of the class Class represent classes and interfaces in a running Java application.


Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

Changing log4j properties at run time

I had a requirement where the log4j property file generated should have a unique id appended to it. This id is generated inside the running application. One simple way of doing it is: Make use of system variables inside the log4j properties file:
log4j.appender.R.File=
d:/mylogs/servicelogger[%d{MYAPPENDER}].log
After you have done this, make sure you set MYAPPENDER in the system environment.

Another way of doing this is: read the log4j properties inside the application, locate the specific property(in our case: log4j.appender.R.File). Change this property and instantiate log4j using this new set of properties. Here is the code to do this:
prop.load(new FileInputStream(logPropertyFile));
String userLogFName = (String)prop.getProperty("log4j.appender.R.File");
prop.remove("log4j.appender.R.File");
prop.setProperty("log4j.appender.R.File", newFName);
org.apache.log4j.PropertyConfigurator.configure(prop);
this.serviceLogger = new
Log4JLogger(org.apache.log4j.Logger.getLogger(DTM_LOG4J_CATEGORY_NAME));

Monday, November 13, 2006

soapUI: Tool to ease your Webservices development

I have often used altova tools to ease my XML and webservices(SOAP) development. Also, bea users have witnessed how easy it is to use an IDE to simplify rudimentary webservices/soap/WSDL related tasks. Well, soapui makes life much simpler for SOA developers. Here is quick look at soapUI features:

soapUI has a broad set of features that greatly ease the development, integration and testing of webservices;

Web Service Inspection and Invocation

The following features for inspecting and invoking web services are currently available:
  • Imported WSDL:s are shown as a hierarchy view of interfaces (PortTypes) and their operations
  • Automatic generation of requests from associated schema (both with/without optional schema elements)
  • Manage unlimited number of requests for each operation
  • Manage multiple service endpoints for each interface
  • Support for Basic, Digest, WS-Security and NTLM authentication
  • Support for Attachments; MTOM, SOAP with Attachments, Inline files
  • Support for both SOAP 1.1 and SOAP 1.2
  • Syntax highlighting editor with undo/redo, formatting, etc.
  • HTTP wire log shows actual requests sent and received

Web Service Development and Validation

The following features are available for development of Web Services:

Web Service Functional Testing

The following features for functional testing web services are currently available:

Web Service Load Testing

The following features for load testing web services are currently available:


@link