使用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