使用Simian檢查冗余代碼


首先,我們到官方站點上下載最新的版本,注意了,simian並非免費工具,如果你用它來檢查開源代碼或非商業代碼的話,它是免費使用的,如果是商業應用的話,就需要付費了。
      下載完畢后,將文件解壓,將simian所在的路徑添加到windows環境變量path下,同時,將simian-2.2.12.exe的文件名改成simian.exe,這樣方便我們的使用,simian是命令行工具,在cmd中運行simian即可:
       

c:\>simian.exe [options] files

下面介紹具體的使用方法:
  • 檢查包括子目錄下的所有的c#文件:

"-recurse=*.cs" 

  • 檢查當前目錄下的所有c#文件 ,並且只檢查代碼3行以上重復的代碼

-threshold=3 "*.cs"

  • 檢查所有的c#文件:

"*.cs"

應用舉例:

  • 在c:\project\src下有一個名為sample.cs的文件,如果想檢查該文件中是否存在冗余代碼,命令行如下:

           c:\project\src> simian "sample.cs"

  •  想檢查c:\project\src下的所有cs文件,包括子目錄中存在的冗余代碼,命令行如下:

           c:\project\src> simian "-recurse=*.cs"

2.4

 

java -jar D:\Android\soft\simian\bin\simian.jar -threshold=4 -formatter=xml:simian.xml "**"

 

 

 

Command Line Interface

This method allows you to execute Simian from the command line, shell scripts, batch files, etc. scanning a directory for all files matching a pattern.

The general form for the Java version is:

    java -jar simian.jar [options] [files]

 

And for the .NET version is:

    simian.exe [options] [files]

 

The files can be specified as any regular shell glob or simply a list of files and can be mixed with the -includes option. (See below for examples.)

For example, to find all java files in all sub-directories of the current directory:

    "**/*.java"

 

To find all java files in the current directory and set the threshold to 3:

    -threshold=3 "*.java"

 

To find all C# files in the current directory:

    "*.cs"

 

To find all C and header in all sub-directories of the current directory:

    **/*.c **/*.h

 

To find all java files in two different directories:

    "/csharp-source/*.cs" "/java-source/*.java"

 

To find all java files in all sub-directories, excluding Test classes:

    -includes=**/*.java -excludes=**/*Test.java

 

To find all java files in the current directory and ignore numbers:

    -ignoreNumbers "*.java"

 

To find all java files and display the results in xml format:

    -formatter=xml "*.rb"

 

To find all ruby files and sends the results in emacs compatible format to a file:

    -formatter=emacs:c:\temp\simian.log "*.rb"

 

To read configuration from a file (where each line of the file specifies at most one of any of the valid command-line arguments):

    -config=simian.config

 

Notes

For most projects (including the nearly 400,000 LOC JDK 1.4 source base), the default VM size seems to be adequate. If you encounter:

    Exception in thread "main" java.lang.OutOfMemoryError

 

you will need to increase the VM heap size using the -mx JVM option.

Ant Task

This method allows you to integrate Simian with the Ant, a java based build tool.

Somewhere in your build.xml file, define the task:

    <taskdef resource="simiantask.properties" classpath="simian.jar"/>

 

And finally, create a target to run the checker. For all defaults:

    <simian>
        <fileset dir="./main" includes="**/*.java"/>
    </simian>

 

To exclude test classes if they exists in the same tree as the source:

    <simian threshold="6">
        <fileset dir="./main" includes="**/*.java" excludes="**/*Test.java"/>
    </simian>

 

To change the minimum number of lines that is considered a match:

    <simian threshold="6">
        <fileset dir="./main" includes="**/*.java"/>
    </simian>

 

To force the language used for processing:

    <simian language="java">
        <fileset dir="./main" includes="**/*.*"/>
    </simian>

 

To have the build fail one or more matches are found:

    <simian failOnDuplication="true">
        <fileset dir="./main" includes="**/*.java"/>
    </simian>

 

To set a build property if one or more matches are found:

    <simian failureProperty="test.failure">
        <fileset dir="./main" includes="**/*.java"/>
    </simian>

 

By default, Simian outputs plain text using the default Ant logger. You can override this by using the nested formatter element. The formatter takes a type(either "plain""xml""emacs""vs"; or "yml") and an optional filename (toFile). For example, to send output to a file:

    <simian>
        <formatter type="plain" toFile="simian-log.txt"/>
    </simian>

 

To produce XML output:

    <simian>
        <formatter type="xml" toFile="simian-log.xml"/>
    </simian>

 

You may specify any number of formatter elements allowing you to produce both XML and plain text output if necessary

Checkstyle Plugin

This method allows you to integrate Simian with Checkstyle 3+. A remarkable java based code checker.

First, ensure the simian.jar file is on the classpath. Next, add the check to your configuiration file. (Note: The plugin runs as a FileSetCheck):

    <module name="Checker">
        ...

        <module name="com.harukizaemon.simian.SimianCheck"/>
    </module>

 

To change the minimum number of lines that is considered a match:

    <module name="com.harukizaemon.simian.SimianCheck"/>
        <property name="threshold" value="6"/>
    <module/>

 

To force the language used for processing:

    <module name="com.harukizaemon.simian.SimianCheck"/>
        <property name="language" value="java"/>
    <module/>

 

IntelliJ Integration

This method allows you to integrate Simian with IntelliJ as an external tool. This will then allow you to run simian from within IntelliJ and go straight to any matches by clicking on the file location.

This is really a short-cut way to define the tool using Options|External Tools.

Open the _.xml file in the config/tools directory of the your intellij settings (usually under the intellij home directory or under your home directory as .IntelliJIdea).

Add the following block:

    <tool name="Simian" showInMainMenu="true" showInEditor="true" showInProject="true"
          showInSearchPopup="true" disabled="false" useConsole="true" synchronizeAfterRun="true">
    <exec>
      <option name="COMMAND" value="$JDKPath$\bin\java.exe" />
      <option name="PARAMETERS" value="-jar simian.jar $FileDir$\**\*.java" />
      <option name="WORKING_DIRECTORY" />
    </exec>
    <filter>
      <option name="NAME" value="Duplicate Location" />
      <option name="DESCRIPTION" value="" />
      <option name="REGEXP" value=" Between lines $LINE$ and [0-9]* in $FILE_PATH$" />
    </filter>
  </tool>

 

Make sure to put the full path to simian.jar in the PARAMETERS option and the appropriate java runtime in the COMMAND option.

The PARAMETERS follows the general form of the command line interface allowing you to set parsing options, threshold, etc.

Re-start IntelliJ and you can click on Tools|Simian.

You should be able to click on the matching lines and go straight to the source code.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM