LoginSignup
20
13

More than 5 years have passed since last update.

PMD Apexで静的解析

Last updated at Posted at 2017-01-23

はじめに

PMDとは

PMDとは、様々な言語のソースコードを静的解析するツールであり、以下のような潜在的問題を識別するためのルールセットがあります(参照:Wikipedia)。

  • バグの可能性:空のtry/catch/finally/switchブロック
  • デッドコード:使われていないローカル変数、パラメータ、privateメソッド
  • 空のif/whileステートメント
  • 複雑な式:whileループを可能にしたforループのステートメントが必要ない場合
  • 準最適コード:無駄な文字列/ストリングバッファの使用
  • 高い循環的複雑度測定を使ったクラス
  • 重複コード:コピー・アンド・ペーストされたコードはバグもコピー・アンド・ペーストされていると解釈でき、保守性が低下してしまう。

PMD Apexとは

Apexとは、Force.comプラットフォーム上で実行する、強い型付けのプログラミング言語です。PMD Apexは、そのApexを静的解析するためのルールセットをまとめたPMDプラグインです。
ルールセットしては、下記のようなものを含みます。

ルール 説明
ApexUnit These rules deal with different problems that can occur with Apex unit tests.
Complexity The Complexity ruleset contains rules that find problems related to code size or complexity.
Performance The Performance ruleset contains a collection of good practices which should be followed.
Security These rules deal with different security problems that can occur within Apex.
Style The Style Ruleset contains rules regarding preferred usage of names and identifiers.

導入(Ant)

ライブラリや設定を準備する

下記のようにPMDをAntで実行できるようにします。

  • PMD Webサイトからコンパイル済みのパッケージが入ったZipファイルをダウンロードします。
  • Zipファイルを解凍します。今回はApache Antで実行できるようにするために、対象プロジェクトにlibをコピーします。
  • 下記を参考にbuild.xmlを書き換えます。
build.xml
<?xml version="1.0" encoding="UTF-8" ?>
<project name="PMD Sample">
  <!-- PMDで利用するjarファイルにパスを設定する -->
  <path id="pmd.classpath">
    <pathelement location="${build}"/>
    <fileset dir="lib/">
      <include name="*.jar"/>
    </fileset>
  </path>

  <!-- PMD Antタスクを利用できるように設定する -->
  <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.classpath"/> 

  <!-- PMD Antタスクをコールして静的解析する -->
  <target name="pmd">
    <mkdir dir="target"/>
    <pmd rulesetfiles="apex-apexunit,apex-complexity,apex-performance,apex-security,apex-style" encoding="UTF-8">
      <!-- 静的解析結果をXMLファイルに出力する -->
      <formatter type="xml" toFile="target/pmd.xml">
        <param name="linkPrefix" value="http://pmd.sourceforge.net/xref/"/>
      </formatter>
      <!-- 静的解析結果をコンソールに出力する -->
      <formatter type="text" toConsole="true" />
      <!-- 対象ファイル -->
      <fileset dir="src">
        <include name="**/*.cls" />
        <include name="**/*.trigger" />
      </fileset>
    </pmd>
  </target>
</project>

静的解析を実行する

以上で準備完了なので、コマンドラインよりAntを実行します。

> ant pmd

PMD Apexは、src以下のApexクラス、トリガーを静的解析し、target/pmd.xmlに静的解析結果をXMLファイルとして出力します。また、コンソールにも静的解析結果を出力します。

Jenkinsのプラグインも用意されているので、上記で生成されたpmd.xmlファイルを指定することで、Jenkinsから静的解析結果を参照できるようになります。
HTML形式で出力したい場合は、type="html"としたタグを追加すると、HTML形式で表示できる静的解析結果が出力します。

結び

今回は、Force.com Apexで利用できる静的解析ツールであるPMDを紹介しました。また、CIで利用しやすいようにAntで実行する方法も紹介しました。
ソースコードの問題点を見つけ、きれいな状態に保つために静的解析は有用なツールですので、活用していきたいと思います。

追記

  • (2017/2/1) PMD 5.5.3 で Security のルールセットが追加になりました。
20
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
13