Tuesday, June 17, 2014

Implement PMD and CPD (Copy paste detector) using ANT

You can implement automated PMD and CPD analysis on your java source code using ant.

Why PMD?


PMD is a static code analyser tool. It can be used to check common programming mistakes like class naming, variable naming, cyclomatic complexity, unused variables, unused imports etc. and many more.

Examples:
– Empty try/catch blocks

– Over-complicated expressions

– Using .equals() instead of ‘==’

– Unused variables and imports

– Unnecessary loops and if statements

– Enforce naming conventions

Additionally, PMD comes with a copy-paste detector to find blocks of copied and pasted code. Best of all, you can customize the rules. PMD includes a set of built-in rules and supports the ability to write custom rules. The custom rules can be written in two ways:

1- Using XPath

2- Using Java classes

Here i have used XPath wherever rules are customized.

Why CPD?

The Copy/Paste Detector (CPD) is an add-on to PMD that uses the Rabin–Karp string search algorithm to find duplicated code.
After running this tool against your source code you will be able to find the duplicate code and you can create a utility class or method in some common class and re-use it, instead.


So, Here we go>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Prerequisites jar files for PMD/CPD:

ant-1.8.1.jar
ant-launcher-1.8.1.jar
ant-testutil-1.7.1.jar
asm-3.2.jar
commons-io-2.2.jar
dom4j-1.6.1.jar
javacc-4.1.jar
jaxen-1.1.1.jar
jcommander-1.27.jar
jdom-1.0.jar
junit-4.4.jar
pmd-5.0.5.jar
rhino-1.7R3.jar
saxon-9.1.0.8-dom.jar
saxon-9.1.0.8.jar
xercesImpl-2.9.1.jar
xml-apis-1.3.02.jar
xmlParserAPIs-2.6.2.jar
xom-1.0.jar

Download PMD : http://sourceforge.net/projects/pmd/

Reference: http://pmd.sourceforge.net/

PMD Documentation:

PMD version 5.0.5
http://pmd.sourceforge.net/pmd-5.0.5/

PMD version 5.1.2
http://pmd.sourceforge.net/pmd-5.1.2/


PMD For eclipse : http://sourceforge.net/projects/pmd/files/pmd-eclipse/update-site/

Note:  I am using PMD version 5.0.5. Version 5.1.2 onwards you need to make a small change in rule set for singleton .

RuleSet XML File (pmdrulesets.xml):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ruleset xmlns="http://pmd.sf.net/ruleset/1.0.0" name=""
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"
       xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd">
       <description>
             PMD Plugin preferences rule set
             Priority means: 1 – error, high priority, 2 – error,
             normal priority, 3 – warning, high priority, 4 – warning,
             normal priority and 5 – information.
       </description>
       <rule ref="rulesets/java/typeresolution.xml/LooseCoupling">
             <priority>3</priority>
       </rule>
       <rule
             ref="rulesets/java/typeresolution.xml/CloneMethodMustImplementCloneable">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/typeresolution.xml/UnusedImports">
             <priority>4</priority>
       </rule>
       <rule
             ref="rulesets/java/typeresolution.xml/SignatureDeclareThrowsException">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/braces.xml/ForLoopsMustUseBraces">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/braces.xml">
             <exclude name="IfStmtsMustUseBraces" />
             <exclude name="IfElseStmtsMustUseBraces" />
             <exclude name="WhileLoopsMustUseBraces" />
       </rule>
       <rule ref="rulesets/java/naming.xml/ShortVariable">
             <priority>1</priority>
             <properties>
                    <property name="xpath">
                           <value>
                                 //VariableDeclaratorId[string-length(@Image) &lt; 3]
                           </value>
                    </property>
             </properties>
       </rule>
       <rule ref="rulesets/java/naming.xml/LongVariable">
             <priority>4</priority>
             <properties>
                    <property name="xpath">
                           <value>
                                 //VariableDeclaratorId[string-length(@Image) > 40]
                           </value>
                    </property>
             </properties>
       </rule>
       <rule ref="rulesets/java/naming.xml/ShortMethodName">
             <priority>4</priority>
             <properties>
                    <property name="xpath">
                           <value>
                                 //MethodDeclarator[string-length(@Image) &lt; 2]
                           </value>
                    </property>
             </properties>
       </rule>
       <rule ref="rulesets/java/naming.xml/VariableNamingConventions">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/naming.xml/MethodNamingConventions">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/naming.xml/ClassNamingConventions">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/naming.xml/AbstractNaming">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/naming.xml/AvoidDollarSigns">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/naming.xml/MethodWithSameNameAsEnclosingClass">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/naming.xml/SuspiciousHashcodeMethodName">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/naming.xml/SuspiciousConstantFieldName">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/naming.xml/SuspiciousEqualsMethodName">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/naming.xml/AvoidFieldNameMatchingTypeName">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/naming.xml/AvoidFieldNameMatchingMethodName">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/naming.xml/NoPackage">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/naming.xml/PackageCase">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/naming.xml/MisleadingVariableName">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/naming.xml/BooleanGetMethodName">
             <priority>1</priority>
       </rule>

       <rule ref="rulesets/java/design.xml/UseUtilityClass">
             <priority>1</priority>
       </rule>

       <rule ref="rulesets/java/design.xml/SimplifyBooleanReturns">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/SimplifyBooleanExpressions">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/SwitchStmtsShouldHaveDefault">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/AvoidDeeplyNestedIfStmts">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/AvoidReassigningParameters">
             <priority>2</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/SwitchDensity">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/ConstructorCallsOverridableMethod">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/AccessorClassGeneration">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/FinalFieldCouldBeStatic">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/CloseResource">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/NonStaticInitializer">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/DefaultLabelNotLastInSwitchStmt">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/NonCaseLabelInSwitchStatement">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/OptimizableToArrayCall">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/BadComparison">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/EqualsNull">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/InstantiationToGetClass">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/IdempotentOperations">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/SimpleDateFormatNeedsLocale">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/ImmutableField">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/UseLocaleWithCaseConversions">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/AvoidProtectedFieldInFinalClass">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/AssignmentToNonFinalStatic">
             <priority>1</priority>
       </rule>
       <rule
              ref="rulesets/java/design.xml/MissingStaticMethodInNonInstantiatableClass">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/AvoidSynchronizedAtMethodLevel">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/MissingBreakInSwitch">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/UseNotifyAllInsteadOfNotify">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/AvoidInstanceofChecksInCatchClause">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/AbstractClassWithoutAbstractMethod">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/SimplifyConditional">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/CompareObjectsWithEquals">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/PositionLiteralsFirstInComparisons">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/UnnecessaryLocalBeforeReturn">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/NonThreadSafeSingleton">
             <priority>3</priority>
       </rule>

       <rule ref="rulesets/java/design.xml/UncommentedEmptyMethodBody">
             <priority>1</priority>
       </rule>

       <rule ref="rulesets/java/design.xml/UncommentedEmptyConstructor">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/AvoidConstantsInterface">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/UnsynchronizedStaticDateFormatter">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/design.xml">
             <exclude name="ConfusingTernary" />
       </rule>
       <rule ref="rulesets/java/design.xml/UseCollectionIsEmpty">
             <priority>2</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/PreserveStackTrace">
             <priority>1</priority>
       </rule>

       <rule
              ref="rulesets/java/design.xml/ClassWithOnlyPrivateConstructorsShouldBeFinal">
             <priority>1</priority>
       </rule>

       <rule
              ref="rulesets/java/design.xml/EmptyMethodInAbstractClassShouldBeAbstract">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/SingularField">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/ReturnEmptyArrayRatherThanNull">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/AbstractClassWithoutAnyMethod">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/design.xml/TooFewBranchesForASwitchStatement">
             <priority>1</priority>
       </rule>
       <rule
             ref="rulesets/java/design.xml/FieldDeclarationsShouldBeAtStartOfClass">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/strictexception.xml/AvoidCatchingThrowable">
             <priority>1</priority>
       </rule>
       <rule
             ref="rulesets/java/strictexception.xml/SignatureDeclareThrowsException">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/strictexception.xml/ExceptionAsFlowControl">
             <priority>2</priority>
       </rule>
       <rule ref="rulesets/java/strictexception.xml/AvoidCatchingNPE">
             <priority>3</priority>
       </rule>
       <rule
             ref="rulesets/java/strictexception.xml/AvoidThrowingRawExceptionTypes">
             <priority>1</priority>
       </rule>
       <rule
              ref="rulesets/java/strictexception.xml/AvoidThrowingNullPointerException">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/strictexception.xml/AvoidRethrowingException">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/strictexception.xml/DoNotExtendJavaLangError">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/strictexception.xml/DoNotThrowExceptionInFinally">
             <priority>3</priority>
       </rule>
       <rule
              ref="rulesets/java/strictexception.xml/AvoidThrowingNewInstanceOfSameException">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/strictexception.xml/AvoidCatchingGenericException">
             <priority>2</priority>
       </rule>
       <rule
             ref="rulesets/java/strictexception.xml/AvoidLosingExceptionInformation">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/unusedcode.xml/UnusedPrivateField">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/unusedcode.xml/UnusedLocalVariable">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/unusedcode.xml/UnusedPrivateMethod">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/unusedcode.xml/UnusedFormalParameter">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/logging-java.xml/MoreThanOneLogger">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/logging-java.xml/LoggerIsNotStaticFinal">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/logging-java.xml/SystemPrintln">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/logging-java.xml/AvoidPrintStackTrace">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/strings.xml/AvoidDuplicateLiterals">
             <priority>1</priority>
             <properties>
                    <property name="skipAnnotations">
                           <value>true</value>
                    </property>
                    <property name="maxDuplicateLiterals">
                           <value>4</value>
                    </property>
             </properties>
       </rule>
       <rule ref="rulesets/java/strings.xml/StringInstantiation">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/strings.xml/StringToString">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/strings.xml/InefficientStringBuffering">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/strings.xml/UnnecessaryCaseChange">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/strings.xml/UseStringBufferLength">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/strings.xml/AppendCharacterWithChar">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/strings.xml/ConsecutiveAppendsShouldReuse">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/strings.xml/ConsecutiveLiteralAppends">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/strings.xml/UseIndexOfChar">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/strings.xml/InefficientEmptyStringCheck">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/strings.xml/InsufficientStringBufferDeclaration">
             <priority>2</priority>
       </rule>
       <rule ref="rulesets/java/strings.xml/UselessStringValueOf">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/strings.xml/StringBufferInstantiationWithChar">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/strings.xml/UseEqualsToCompareStrings">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/strings.xml/AvoidStringBufferField">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/migrating.xml/ReplaceVectorWithList">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/migrating.xml/ReplaceHashtableWithMap">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/migrating.xml/ReplaceEnumerationWithIterator">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/migrating.xml/AvoidEnumAsIdentifier">
             <priority>2</priority>
       </rule>
       <rule ref="rulesets/java/migrating.xml/AvoidAssertAsIdentifier">
             <priority>2</priority>
       </rule>
       <rule ref="rulesets/java/migrating.xml/IntegerInstantiation">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/migrating.xml/ByteInstantiation">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/migrating.xml/ShortInstantiation">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/migrating.xml/LongInstantiation">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/migrating.xml/JUnit4TestShouldUseBeforeAnnotation">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/migrating.xml/JUnit4TestShouldUseAfterAnnotation">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/migrating.xml/JUnit4TestShouldUseTestAnnotation">
             <priority>3</priority>
       </rule>
       <rule
             ref="rulesets/java/migrating.xml/JUnit4SuitesShouldUseSuiteAnnotation">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/migrating.xml/JUnitUseExpected">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/j2ee.xml/UseProperClassLoader">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/j2ee.xml/MDBAndSessionBeanNamingConvention">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/j2ee.xml/RemoteSessionInterfaceNamingConvention">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/j2ee.xml/LocalInterfaceSessionNamingConvention">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/j2ee.xml/LocalHomeNamingConvention">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/j2ee.xml/RemoteInterfaceNamingConvention">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/j2ee.xml/DoNotCallSystemExit">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/j2ee.xml/StaticEJBFieldShouldBeFinal">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/j2ee.xml/DoNotUseThreads">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/optimizations.xml/MethodArgumentCouldBeFinal">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/optimizations.xml/LocalVariableCouldBeFinal">
             <priority>1</priority>
       </rule>
       <rule
             ref="rulesets/java/optimizations.xml/AvoidInstantiatingObjectsInLoops">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/optimizations.xml/UseArrayListInsteadOfVector">
             <priority>2</priority>
       </rule>
       <rule ref="rulesets/java/optimizations.xml/SimplifyStartsWith">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/optimizations.xml/UseStringBufferForStringAppends">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/optimizations.xml/UseArraysAsList">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/optimizations.xml/AvoidArrayLoops">
             <priority>2</priority>
       </rule>
       <rule
             ref="rulesets/java/optimizations.xml/UnnecessaryWrapperObjectCreation">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/optimizations.xml/AddEmptyString">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/optimizations.xml/RedundantFieldInitializer">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/optimizations.xml/PrematureDeclaration">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/empty.xml/EmptyCatchBlock">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/empty.xml/EmptyIfStmt">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/empty.xml/EmptyWhileStmt">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/empty.xml/EmptyTryBlock">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/empty.xml/EmptyFinallyBlock">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/empty.xml/EmptySwitchStatements">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/empty.xml/EmptySynchronizedBlock">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/empty.xml/EmptyStaticInitializer">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/empty.xml/EmptyStatementNotInLoop">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/empty.xml/EmptyInitializer">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/unnecessary.xml/UnnecessaryConversionTemporary">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/unnecessary.xml/UnnecessaryReturn">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/unnecessary.xml/UnnecessaryFinalModifier">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/unnecessary.xml/UselessOverridingMethod">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/unnecessary.xml/UselessOperationOnImmutable">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/unnecessary.xml/UnusedNullCheckInEquals">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/JumbledIncrementer">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/ForLoopShouldBeWhileLoop">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/OverrideBothEqualsAndHashcode">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/DoubleCheckedLocking">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/ReturnFromFinallyBlock">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/UnconditionalIfStatement">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/BooleanInstantiation">
             <priority>2</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/CollapsibleIfStatements">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/ClassCastExceptionWithToArray">
             <priority>3</priority>
       </rule>
       <rule
              ref="rulesets/java/basic.xml/AvoidDecimalLiteralsInBigDecimalConstructor">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/MisplacedNullCheck">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/AvoidThreadGroup">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/BrokenNullCheck">
             <priority>2</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/BigIntegerInstantiation">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/AvoidUsingOctalValues">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/AvoidUsingHardCodedIP">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/CheckResultSet">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/AvoidMultipleUnaryOperators">
             <priority>2</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/ExtendsObject">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/CheckSkipResult">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/AvoidBranchingStatementAsLastInLoop">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/DontCallThreadRun">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/basic.xml/DontUseFloatTypeForLoopIndices">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/sunsecure.xml/MethodReturnsInternalArray">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/sunsecure.xml/ArrayIsStoredDirectly">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/coupling.xml/CouplingBetweenObjects">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/coupling.xml/ExcessiveImports">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/coupling.xml/LooseCoupling">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/imports.xml/DuplicateImports">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/imports.xml/DontImportJavaLang">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/imports.xml/UnusedImports">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/imports.xml/ImportFromSamePackage">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/imports.xml/UnnecessaryFullyQualifiedName">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/imports.xml/TooManyStaticImports">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/junit.xml/JUnitStaticSuite">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/junit.xml/JUnitSpelling">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/junit.xml/JUnitAssertionsShouldIncludeMessage">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/junit.xml/JUnitTestsShouldIncludeAssert">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/junit.xml/TestClassWithoutTestCases">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/junit.xml/UnnecessaryBooleanAssertion">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/junit.xml/UseAssertEqualsInsteadOfAssertTrue">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/junit.xml/UseAssertSameInsteadOfAssertTrue">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/junit.xml/UseAssertNullInsteadOfAssertTrue">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/junit.xml/SimplifyBooleanAssertion">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/controversial.xml/NullAssignment">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/controversial.xml/OnlyOneReturn">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/controversial.xml/AssignmentInOperand">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/controversial.xml/AtLeastOneConstructor">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/controversial.xml/DontImportSun">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/controversial.xml/SuspiciousOctalEscape">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/controversial.xml/CallSuperInConstructor">
             <priority>4</priority>
       </rule>
       <rule ref="rulesets/java/controversial.xml/UnnecessaryParentheses">
             <priority>2</priority>
       </rule>
       <rule ref="rulesets/java/controversial.xml/DefaultPackage">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/controversial.xml/BooleanInversion">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/controversial.xml/AvoidUsingShortType">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/controversial.xml/AvoidUsingVolatile">
             <priority>2</priority>
       </rule>
       <rule ref="rulesets/java/controversial.xml/AvoidUsingNativeCode">
             <priority>2</priority>
       </rule>
       <rule ref="rulesets/java/controversial.xml/AvoidAccessibilityAlteration">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/controversial.xml">
             <exclude name="DataflowAnomalyAnalysis" />
             <exclude name="UnnecessaryConstructor" />
             <exclude name="AvoidFinalLocalVariable" />
       </rule>
       <rule
              ref="rulesets/java/controversial.xml/DoNotCallGarbageCollectionExplicitly">
             <priority>2</priority>
       </rule>
       <rule ref="rulesets/java/codesize.xml/NPathComplexity">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/codesize.xml/ExcessiveMethodLength">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/codesize.xml/ExcessiveParameterList">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/codesize.xml/ExcessiveClassLength">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/codesize.xml/CyclomaticComplexity">
             <priority>4</priority>
             <properties>
                    <property name="reportLevel">
                           <value>9</value>
                    </property>
                    <property name="showClassesComplexity">
                           <value>true</value>
                    </property>
                    <property name="showMethodsComplexity">
                           <value>true</value>
                    </property>
             </properties>
       </rule>
       <rule ref="rulesets/java/codesize.xml/ExcessivePublicCount">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/codesize.xml/TooManyFields">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/codesize.xml/NcssMethodCount">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/codesize.xml/NcssTypeCount">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/codesize.xml/NcssConstructorCount">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/codesize.xml/TooManyMethods">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/finalizers.xml/EmptyFinalizer">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/finalizers.xml/FinalizeOnlyCallsSuperFinalize">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/finalizers.xml/FinalizeOverloaded">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/finalizers.xml/FinalizeDoesNotCallSuperFinalize">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/finalizers.xml/FinalizeShouldBeProtected">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/finalizers.xml/AvoidCallingFinalize">
             <priority>3</priority>
       </rule>
       <rule
             ref="rulesets/java/logging-jakarta-commons.xml/UseCorrectExceptionLogging">
             <priority>1</priority>
       </rule>
       <rule ref="rulesets/java/logging-jakarta-commons.xml/ProperLogger">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/javabeans.xml/BeanMembersShouldSerialize">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/javabeans.xml/MissingSerialVersionUID">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/clone.xml/ProperCloneImplementation">
             <priority>2</priority>
       </rule>
       <rule ref="rulesets/java/clone.xml/CloneThrowsCloneNotSupportedException">
             <priority>3</priority>
       </rule>
       <rule ref="rulesets/java/clone.xml/CloneMethodMustImplementCloneable">
             <priority>3</priority>
       </rule>
</ruleset>



Build properties:

src.dir=src
project.name=TestApp
pmd.reports=pmd-reports


Ant Target Configuration:

 <!--Creating the required directories for pmd-->
<target name="pmd-init" description="Creates the required directories for pmd.">
<echo>******Cleaning the ${pmd.reports}******</echo>
<delete dir="${pmd.reports}" />
<echo>******Creating the required directories for pmd******</echo>
<mkdir dir="${pmd.reports}" />
</target>
<!-- PMD Configurations -->
<!--Provided the pmd library path, It can be taken from environment variable 
as well. -->
 <!--Put the above given jar files to the classpath, For me i am having all jar files in  D:/ProjectSofts/Java/pmd-bin-5.0.5/lib/-->

<path id="pmd.classpath">
<fileset dir="D:/ProjectSofts/Java/pmd-bin-5.0.5/lib/">
<include name="*.jar" />
</fileset>
</path>

<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"
classpathref="pmd.classpath" />

<target name="pmd" description="Run code analysis over code to check for standards." depends="pmd-init">
<echo>*****Performing PMD analysis on ${project.name}*****</echo>
<pmd rulesetfiles="pmdrulesets.xml">
<formatter type="net.sourceforge.pmd.renderers.HTMLRenderer"
toFile="${pmd.reports}/PMD-Reports.html" />
<formatter type="net.sourceforge.pmd.renderers.XMLRenderer"
toFile="${pmd.reports}/pmd.xml" />
<fileset dir="${src.dir}" defaultexcludes="yes">
<include name="com/**" />
<exclude name="com/**/*.properties" />
<exclude name="com/**/*.xml" />
</fileset>
</pmd>
<echo>*****PMD analysis report saved into ${pmd.reports}/PMD-Reports.html*****</echo>
</target>

<!--Creating the required directories for cpd-->
<target name="-cpd-init" description="Creates the required directories for cpd.">
 <echo>******Cleaning the ${basedir}/cpd-reports******</echo>
 <delete dir="${basedir}/cpd-reports" />
 <echo>******Creating the required directories for cpd******</echo>
 <mkdir dir="${basedir}/cpd-reports" />
</target>

Implementing CPD (Copy Paste Detector):

<target name="cpd" depends="-cpd-init">
 <echo>*****Performing Copy Paste Detector (CPD) analysis on ${project.name} to find the duplicate codes*****</echo>
 <taskdef name="cpd" classname="net.sourceforge.pmd.cpd.CPDTask" classpathref="pmd.classpath" />
 <cpd minimumTokenCount="100" outputFile="${basedir}/cpd-reports/cpd.xml" format="xml"
 ignoreliterals="true" ignoreIdentifiers="true">
<fileset dir="${src.dir}" defaultexcludes="yes">
  <include name="com/**" />
  <exclude name="com/**/*.properties" />
  <exclude name="com/**/*.xml" />
</fileset>
  </cpd>
  <echo>*****Copy Paste Detector (CPD) analysis report saved into ${basedir}/cpd-reports/cpd.xml*****</echo>
</target>

Note: Here ${pmd.reports} , ${src.dir} , ${project.name}, are configured in build.properties file.



Leave your comments/suggestions below, Otherwise the next time:)

Definition of equals and hashCode

Definition of equals and hashCode :

hashCode() :-

public native int hashCode();
Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.
The general contract of hashCode is:
·         Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
·         If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
·         It is not required that if two objects are unequal according to the java.lang.Object.equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java TM programming language.) 

Equals(Object obj):-

Default Implementation in Object class:
public boolean equals(Object obj) {
        return (this == obj);
}
Indicates whether some other object is "equal to" this one. true if this object is the same as the obj argument; false otherwise.
The equals method implements an equivalence relation on non-null object references:
·         It is reflexive: for any non-null reference value x, x.equals(x) should return true.
·         It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
·         It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
·         It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
·         For any non-null reference value x, x.equals(null) should return false.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.