Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439

PMD scans Java source code and looks for potential problems including possible bugs, dead or duplicate code, or suboptimal code. PMD is a powerful tool and is used by Software Engineers in industry as well as students learning to write Java.

Running PMD as a BlueJ plug-in

PMD is best run from an IDE (integrated development environment). The PMD plug-in for BlueJ only tests two rule-sets: basic and unused code. However, it is the easiest way to use PMD, and so these rulesets will be the (only) ones we use for marking at the moment. You run PMD from BlueJ as follows:

  • In the BlueJ class window, right click on the Java class you want to check.
  • Select PMD then click on Check code from the menu.
  • A window will pop up with either a list of warnings or with the message "No problems found!".
  • Each warning comes with a line number and an explanation. Go to the given line number and refactor your code (that is, make improvements to it) as necessary until the PMD check gives the message "No problems found!".
  • Note: if you haven't already done so you will need to turn on the display line numbers option for BlueJ. Navigate the menus BlueJ then Preferences. Under the Editor tab tick the "Display line numbers" option. Now when you open any class you should see the line numbers displayed down the left hand side.
  • The PMD plug-in for BlueJ is incomplete in that it does not allow you to check all the PMD rule sets. For lab marking, that is all you have to do: if you get the BlueJ message "No problems found!" then you will get full marks for style.

    Running PMD from the Command Line

    More adventurous students may wish to run PMD from the command line in order to try out different rule sets and different feedback formats. You do this as follows:

  • Open a windows cmd terminal. You can find a shortcut for this in the SSH FTM X-WIn folder on the desktop for CSSE lab machines.
  • In the terminal window, navigate to the directory where the Java source files you want to test are stored. For example, in your terminal you type: cd H:\Lab_Work\lab06\ If you don't know your directory name, try Open Recent in BlueJ and it will show you the full path for recent project directories.
  • Run PMD by typing the following command in the terminal,
      C:\bin\pmd-4.2.5\bin\pmd MyClass.java text basic,design,unusedcode  
    MyClass.java is the name of the source file you want to check
    text is the format for PMD output format. You could also generate output as html using,
      C:\bin\pmd-4.2.5\bin\pmd MyClass.java html basic,unusedcode > report.html  
    Then view your report by opening it in your favourite browser. For example, in the terminal type explorer report.html
    The final comma-separated list describes the rule sets you want to check. Here we have specified the basic, design and unused code rules. You may also wish to try all or some of the following rulesets
    basic,braces,codesize,coupling,design,imports,naming,optimizations,strings,typeresolution,unusedcode.  
    If you get many errors then it is much easier to check just one or two rulesets at a time.
  • Refactor your code to address the PMD warnings and repeat the checking steps.
  • A local CSSE copy of the including descriptions of all the rules is available by clicking on the link.

    You can find out more about the rulesets and download PMD to run at home from