Wednesday, May 5, 2010

How to use PMD in Eclipse ?

Before launching Eclipse make sure you have enough memory for PMD.(If not know how to install PMD Eclipse plugin then follow this link How to install PMD in Eclipse ? ) This is particularly important when analyzing large projects. In these situations PMD tends to be memory-hungry. Hence, make sure to start with as much memory as you can afford, for example 512M (eclipse.exe -vmargs -Xmx512M)
    1. Launch Eclipse
    2. Let you have previously created a Java Project PMDDemo, Otherwise you need to create a new project in your workspace File | New | Project...
    3. In the Package Explorer right-click on PMDDemo project and select New | Class
    4. In the following dialog enter the class name as DemoPMD and click Finish
    5. A new class DemoPMD is created in project's default package. Paste the following code into the new class:

      public class DemoOMD {
       
        private static final String bAD = "BAD";
        
          public void BadMethodName() {
                  System.out.println("Hello PMD World!");
           }
        
           public String thisIsCutAndPaste(String first, int second) {
                  System.out.println("New world");
                  return "New world";
           }
        }
    6. Similarly, create a second class DemoPMDThread and paste the following code:

    public class DemoPMDThread extends Thread {

       public DemoPMDThread(String str) {
             super(str);
      }

     public void run() {
             for (int i = 0; i < 100; i++) {
                     System.out.println(i + " " + getName());
                     try {
                             sleep((long) (Math.random() * 1000));
                     } catch (InterruptedException e) {

                     }
             }
             System.out.println("DONE! " + getName());
      }

      public void WRITE_SOMETHING(String INPUT_PARAMETER) {
             System.out.println(INPUT_PARAMETER);
      }

      public static void main(String[] args) {
             new Yang("Good").start();
             new Yang("Bad").start();
      }

      public String thisIsCutAndPaste(String pFirst, int pSecond) {
             System.out.println("New world");
             return "New world";
      }
    }


    7. In the Package Explorer right-click on QA Project and select PMD | Check Code With PMD
       
      Wait for PMD to scan DemoPMD and DemoPMDThread
      If PMD Violations view is not open navigate to Window | Show View | Other... and select PMD | PMD Violations, you'll get following view...


      In the PMD Violations view notice a list of 27 violations. In large projects this list could easily grow up to several thousand. This is one of the reasons PMD allows violations be filtered by priority. Priority is a configurable attribute of a PMD rule. PMD assigns priorities from 1 to 5 and each priority is represented by a colored square at the top-right corner of the view. These little squares are actually clickable on-off switches used to control the visibility of the violations they represent.
      The results table itself is well laid out and most columns are sortable. It is also possible to get more detail on a violation by simply right-clicking it and selecting Show Details. PMD pops-up a dialog with information such as rule name, implementation class, message, description and an example. This feature can be helpful when trying to makes sense of a new rule or letting inhouse developers know about a particular company rule or coding convention.



        Finally, in the PMD Violations table it is possible to add review annotations to the source where the violation occurred. This can be an effective way to mark target files for further review. Just right-click on any violation in the list and select Mark review. PMD will insert a review annotation to the Java source right above the violation line itself. The review annotation should look like this:

        // @PMD:REVIEWED:MethodNamingConventions: by Sanjay Singh on 5/08/10 5:04 PM

        Review annotations can be removed anytime by right-clicking QA Project and selecting PMD | Clear violations reviews. Similarly, PMD Violations can be cleaned-up by right-clicking QA Project and selecting PMD | Clear PMD Violations..

        No comments:

        Post a Comment