Tuesday, April 13, 2010

Using PMD Extension and Custom Rule Sets with JDeveloper 11g

 

Recently I’ve worked with a customer that was evaluating the use of PMD code quality tool integrated with JDeveloper 11g and their custom build process (using Maven/ANT) to improve their code quality and enforce their standards. They were specifically interested in custom ADF Business Components rules.

Although I knew that PMD it provides an extension for JDeveloper 11g I’ve never done any work with PMD before. So I dug into its documentation and found out that you can actually create rules based on both XPath expressions and Java.

Without any previous knowledge of PMD or XPath it took me less than an hour to quickly right some basic XPath rules; and believe me, I’m just your average developer! :^)

I’ve created 5 simple XPath-based rules:

  • Avoid using ViewObjectImpl.getRowCount();
  • Avoid calling transaction methods from ADF BC (commit/rollback/potChanges);
  • Avoid using DBTransaction class;
  • Avoid using direct JDBC calls to the Database;
  • Use of wrong super class for your Application Module.

I won’t go into the PMD implementation details. You can check the excellent PMD documentation for that. And just to make it crystal-clear: I’m not a PMD or XPath expert, so the XPath expression can still be wrong or raise false positives; the objective is just to give an idea of what can quickly be done using PMD and XPath no matter which framework you’re using.

So, here is the step by step process:

1. Fire up JDeveloper, go to Help->Check for Updates and download the PMD Extension. If you cannot find it through the Update Center, download it here and install it.

2. After installing, shutdown JDev. We will need to do some refactoring on the extension jar files due to the way the extension reads the rulesets properties.

3. Open your preferred file browser or command line and go to <JDEV_HOME>\jdeveloper\jdev\extensions\net.sourceforge.pmd.jdeveloper.4.2.5.3.0\lib

4. Open the pmd-4.2.5.jar file with your preferred tool. You’ll see that there is a rulesets directory in it. Extract this directory to your file system and remove the rulesets directory from the jar file. We need to do this to modify the rulesets/rulesets.properties config file that is bundled within the jar file.

5. Open the rulesets.properties file and at the end of the rulesets.filenames append rulesets/adfbc.xml . It should look like this:

rulesets.filenames=rulesets/android.xml,rulesets/basic.xml,rulesets/braces.xml,rulesets/clone.xml,rulesets/codesize.xml,rulesets/controversial.xml,rulesets/coupling.xml,rulesets/design.xml,rulesets/finalizers.xml,rulesets/imports.xml,rulesets/j2ee.xml,rulesets/javabeans.xml,rulesets/junit.xml,rulesets/logging-jakarta-commons.xml,rulesets/logging-java.xml,rulesets/migrating.xml,rulesets/naming.xml,rulesets/optimizations.xml,rulesets/strictexception.xml,rulesets/strings.xml,rulesets/sunsecure.xml,rulesets/unusedcode.xml,rulesets/adfbc.xml




6. Download the adfbc.xml ruleset file to the rulesets folder. Now create a new jar called rulesets.jar with the rulesets directory. You can download the one I’ve prepared from here. Just remember that you still need to remove the original rulesets directory from pmd-4.2.5.jar as explained above.



7. Back on the pmd-4.2.5.jar, open its META-INF/MANIFEST.MF and add a reference to rulesets.jar:



Manifest-Version: 1.0

Ant-Version: Apache Ant 1.7.1


Created-By: 11.2-b01 (Sun Microsystems Inc.)


Main-Class: net.sourceforge.pmd.PMD


Class-Path: jaxen-1.1.1.jar asm-3.1.jar rulesets.jar



What we did was to remove the rulesets dependency from the original jar to an external jar that we can modify at will. Unfortunately the import rules feature of the extension does not work as one would expect.



8. Launch JDev and go to Preferences->PMD. You should see the following rule entries at the bottom of the rules list:



PMDCustomRules



9. I’ve created a Bad Practice ADF BC Project and added some offending code in my ApplicationModuleImpl class to test the rules. If I right click on my AM java source and pick PMD at the bottom of the context menu this is the the output generated:



PMDReport 



Here it is also a call for action: if you want to tackle the task of creating new XPath expressions for ADF, or to improve the existing PMD extension, please get in contact with me. I will try my best to help you.



I won’t go into the details of writing a custom Java-based rule; the documentation at the PMD site is pretty good. The only drawback is that PMD is not able to analyze XML files, which is an essential feature for a good ADF analysis. For that, I will be unleashing the full power of ojaudit and how to write custom audit rules to it. Stay tuned!

17 comments:

  1. Excellent post!
    You claim that ojaudit is better than PMD in XML files. Could you please give a little more feedback on this please.
    Thanx in advance!

    ReplyDelete
  2. 讚啦!!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    ReplyDelete
  3. pleasure to find such a good artical! please keep update!!........................................

    ReplyDelete
  4. 人不能像動物一樣活著,而應該追求知識和美德..................................................

    ReplyDelete
  5. Pay somebody back in his own coin..................................................................                           

    ReplyDelete
  6. 一個人的價值,應該看他貢獻了什麼,而不是他取得了什麼............................................................

    ReplyDelete
  7. Hi Maiko,

    what about an ojaudit maven plugin ;)

    ReplyDelete
  8. Hi Maiko,

    I am working with ojaudit but I need a way to use the tool with out any dependency on JDeveloper folder structure. Is that possible?
    Can you please provide any information on documentation about ojaudit.

    Thanks in advance,
    Arvinder Singh.

    Oracle

    Arvinder Singh | Application Engineer
    Oracle Oracle Retail Global Business Unit

    ORACLE India | NR Enclave A Block, EPIP Industrial Area, White Field | 560066 Bangalore

    ReplyDelete
  9. Unfortunatly your files are not available anymore on the google group via the links. Can you please update the links?

    ReplyDelete