Do you know, that PMD can analyse XML files since version 5? Many of ADF files are XML configuration files - wouldn’t it be a good idea to create some static code analysis rules for ADF too?
Unfortunatly oracle configuration files in most times create references to dtd’s, which are not exist anywhere in the path. Till PMD 5.0.1 such files results in errors, as PMD tries to validate this doctype. I have switched of this validation in PMD release 5.0.2
Following article requires configuration of PMD as described in my blog PMD Integration with JDeveloper through Ant External Tools.
Let’s start from the beginning.
-
At the moment PMD only provide XML check’s against files with the extension *.xml, there is a feature request to change this behaviour. So at the moment, we have to change the code by ourself to reach the result. Load the the 5.0.4 source code.
-
Look for the file src\main\java\net\sourceforge\pmd\lang\Language.java and replace the line
XML("XML", null, "xml", XmlRuleChainVisitor.class, "xml");
with
XML("XML", null, "xml", XmlRuleChainVisitor.class, "xml", "jws", "jpr", "cpx", "xcfg", "dcx", "jpx");</li>
-
Change to directory pmd-src-5.0.4
-
set JAVA_HOME, e.g. to your 1.6 JDeveloper jdk
-
run "mvn clean package", I have succesfully used maven 2.2.1 from <JDev-Home>\jdeveloper\apache-maven-2.2.1\bin</li> <li>If you don’t need further extensions, you can instead running steps 2 till 5 the resulting pmd-5.0.4.jar from my side.
-
Replace the file pmd-5.0.4.jar in pmd-bin-5.0.4\lib
-
Now is the time to create the first adf static code analysis rule. In the adf architecture course I attend, there was a rule, that the taskflow behaviour "No controller transaction" should not be mixed with the other option of this selection list
In the sourcecode this property is described through the absence of the node adfc-config/task-flow-definition/transaction, the other options create this node and additional child nodes, here e.g. for the option "Always create new transaction"
-
In PMD you can create two kind of rules: Java and XPath. For our xml Files XPath should be the efficient option. We have to put our configuraration e.g. in file my_pmd_rules.xml in the same directory as pmd.xml in the last blog. For the above rule the XPath query in line 14 find nodes, which does not contain the transaction node
<?xml version="1.0"?> <ruleset name="Oracle ADF rules" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd"> <description>This ruleset checks Oracle ADF code for bad stuff</description> <!-- ADF Rules ############################################################################################### --> <rule name="OracleAdfTaskflowShouldNotHaveNoControllerTransaktion" since="5.0" language="xml" message='ADF tasklows should normally not contain transaktion behavior "No Controller Transaction"'> <priority>1</priority> <properties> <property name="xpath"> <value> <![CDATA[ adfc-config/task-flow-definition[not(transaction)] ]]> </value> </property> </properties> </rule> </ruleset>
-
Now we have to change the configuration file pmd.xml from the last blog in the marked lines to call our new configuration and run this on files with defined Extensions
<?xml version="1.0" encoding="windows-1252" ?> <project xmlns="antlib:org.apache.tools.ant" name="Project1" default="all" basedir="."> <path id="classpathForPMD"> <fileset dir="pmd-bin-5.0.4\lib"> <include name="**/**.jar"/> </fileset> </path> <taskdef name="pmd" classpathref="classpathForPMD" classname="net.sourceforge.pmd.ant.PMDTask"/> <target name="pmdAll"> <echo message="PMD is running on directory ${dir}" level="info"/> <pmd rulesetfiles="my_pmd_rules.xml" failOnRuleViolation="true" minimumPriority="1"> <fileset dir="${dir}"> <include name="**/*.java"/> <include name="**/*.xml"/> <include name="**/*.jws"/> <include name="**/*.jpr"/> <include name="**/*.cpx"/> <include name="**/*.xcfg"/> <include name="**/*.dcx"/> <include name="**/*.jpx"/> </fileset> </pmd> </target> </project>
-
Now call again pmd from the Fusion Order Demo Application StoreFrontModule
-
You can see the results for xml in the message pane. Unfortunatly the direct jump to violated files does not work at the moment as pmd delivers for every xml violation -1 as line, but I have created an bug for this
That’s it!
Maybe somebody has an idea, where we can build an extension exchange for ADF rules, e.g. for creating rules for ADF Code Guidelines v1.00 or ADF Naming and Project Layout Guidelines v2.00.
Remember: you are not restricted to ADF files, e.g. we do checks to for JDeveloper application (jws) and project files (jpr) as well, as this are xml files too. The configuration you see already in step 2.