Javatpoint Logo
Javatpoint Logo

What is PMD?

PMD is an open-source static source code analyzer that reports on issues found inside application code. PMD incorporates work in rule sets and supports the capacity to compose custom guidelines. PMD doesn't report aggregation blunders, as it can just deal with very shaped source documents. Issues revealed by PMD are fairly wasteful code or awful programming propensities, which can lessen the presentation and viability of the program, assuming that they amass. It can examine records written in Java, JavaScript, Peak and Visualforce, PLSQL, Apache Speed, XML, and XSL.

Also, it incorporates CPD, the duplicate glue indicator. CPD tracks down copied code in Java, C, C++, C#, Sweet, PHP, Ruby, Fortran, JavaScript, PLSQL, Apache Speed, Scala, Objective C, Matlab, Python, Go, Quick and Salesforce.com Zenith and Visualforce.

While PMD doesn't authoritatively represent anything, a few retroacronyms have been proposed, including, for example, Programming Slip-up Indicator and Undertaking Complies with Time constraints.

Working of PMD

  1. Parse order line boundaries (see net.sourceforge.pmd.cli.PMDParameters) Likewise, load the steady investigation store document
  2. Load rulesets/rules
  3. Decide dialects (rules of various dialects may be blended in rulesets)
  4. Decide documents (utilizes the given source registry, channel by the language's record expansions)
  5. Set up the renderer
  6. Sort the documents by name
  7. Check whether we can utilize the steady investigation store (if the rulesets are transformed, it will be invalid)
  8. Set up the SourceCodeProcessor in view of the arrangement
  9. Break down the documents. Either single-strung or multi-strung equal. This assignment is embodied in net.Sourceforge.pmd.processor.PMDRunnable:
  10. Make input stream
  11. Call source code processor (net.Sourceforge.pmd.SourceCodeProcessor):
  12. Decide the language
  13. Check whether the record is as of now examined and an outcome is accessible from the investigation store.
  14. Parse the source code. The result is the root AST hub.
  15. Continuously run the SymbolFacade guest. It assembles scopes, and tracks down announcements and utilizations.
  16. Run DFA (information stream examination) guest (in the event that something like one rule requires it) for building control stream charts and information stream hubs.
  17. Run TypeResolution guest (in the event that somewhere around one rule requires it)
  18. FUTURE: Run multifile investigation (on the off chance that somewhere around one rule requires it)
  19. Execute the guidelines:
  20. First, run the guidelines that were selected for the standard chain component
  21. Run the wide range of various standards and let them cross the AST. The principles can utilize the image table, type goal data and DFA hubs.
  22. The standards will report tracked-down issues as RuleViolations.
  23. Render the tracked down infringement into the needed organization (XML, text, HTML, … )
  24. Store the steady examination reserve
  25. Contingent upon the quantity of infringement found, exit with code 0 or 4.

PMD Working Rules

Prior to running standards, PMD parses the source document into an information structure called an abstract syntax tree (AST). This tree addresses the syntactic construction of the code and encodes syntactic relations between source code components.

For example, in Java, technique statements have a place with a class: in the AST, the hubs addressing strategy statements will be relatives of a hub addressing the statement of their encasing class. This portrayal is hence a lot more extravagant than the first source code (which, for a program, is only a chain of characters) or the symbolic chain created by a lexer (which is, for example, what Checkstyle deals with).

Reasonably, PMD rules work by matching a "pattern" against the AST of a document. Rules investigate the AST and find hubs that fulfil a few circumstances that are normal for the particular thing the standard is attempting to signal. Leads then, at that point, report an infringement on these hubs.

Installation of PMD in Different Operating Systems

1. Windows:

  • Download the pmd through https://github.com/pmd/pmd/releases/download/pmd_releases%2F6.49.0/pmd-bin-6.49.0.zip
  • Extricate the zip-archiver, for example, to C:\pmd-canister 6.49.0
  • Add organizer C:\pmd-receptacle 6.49.0\bin to the path, all things considered
    For all time: Utilizing Framework Properties discourse > Environment variable > Attach to path factor
    For a brief time, at order line: SET PATH=C:\pmd-bin-6.49.0\bin;%PATH%
  • Execute at command line: pmd.bat - d c:\src - R rulesets/java/quickstart.xml - f text

2. Linux Operating System:

3. Mac Operating System:

4. Windows( Chocolaty):

  • Install Chocolatey, if you don't already have it!
  • Install PMD: choco install -y pmd
  • Run from the command-line! pmd -d c:\src -R rulesets/java/quickstart.xml -f text

Writing New Rules

PMD upholds two methods for characterizing rules: utilizing an XPath query or utilizing a Java visitor. XPath rules are a lot simpler to set up since they're characterized straightforwardly in your ruleset XML and are expressive enough for almost any undertaking.

Then again, a few pieces of PMD's Programming interface are just open from Java, for example, getting to the utilization of a statement. Furthermore, Java rules permit you to do some convoluted handling, to which an XPath rule couldn't scale.

Eventually, picking one technique or the other relies upon the trouble of what your standard does. I'd encourage you to keep to XPath except if you have no other decision.

Defining a New Rule in XML

New rules should be proclaimed in a ruleset before they're referred to. This is the situation for both XPath and Java rules. To do this, the standard component is utilized, yet rather than referencing the ref property, it specifies the class characteristic with the execution class of your standard.

Example:

https://github.com/pmd/pmd/releases/download/pmd_releases%2F6.49.0/pmd-bin-6.49.0.zip

Documentation in PMD

The documentation sources can be found in two puts in light of how they are produced:

  • The ones that are physically composed (like the one you are perusing);
  • What's more, the ones that are produced consequently from the class documents. All the standard documentation pages are produced like that.

Handwritten Documentation

All handwritten documentation is put away in the subfolders under docs/pages. The organizer structure looks like the sidebar structure. Since all pages utilize a straightforward permalink, in the delivered HTML pages, all pages are levelled in one registry. This makes it simple to see the documentation additionally disconnected.

Rule Documentation

The classes for a language %lang% are situated in pmd-%lang%/src/primary/assets/classification/%lang% . So for Java the classes can be found under pmd-java/src/primary/assets/classification/java. The XML classification records in this catalogue are changed during incorporation into markdown pages depicting the principles they contain. These pages are set under docs/like the written by hand documentation and are then delivered with Jekyll like most of them. The standard documentation generator is the different submodulepmd-doc.

Changing the documentation of a standard should, in this way, not be finished on the markdown page, however straightforwardly on the XML rule tag compared to the standard, in the significant classification record.

The XML documentation of rules can contain GitHub seasoned markdown. Simply envelop the markdown inside the CDATA area by the XML. CDATA segments safeguard all organizing inside the delimiters and permit them to compose code tests without getting away from exceptional XML characters.

Example:







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA