1. 程式人生 > >使用Simian工具掃描重複程式碼

使用Simian工具掃描重複程式碼

1. simian命令列的格式:

  1. java -jar simian.jar [options] [files]
  2. simian.exe [options] [files]

2. Usage: [options] [files]

命令 描述
-balanceCurlyBraces[+/-] Accounts for curly braces when breaking lines
-balanceParentheses[+/-] Accounts for parentheses when breaking lines
-balanceSquareBrackets[+/-] Accounts for square brackets when breaking lines
-config=FNAME Reads the configuration from the specifiedfile
-defaultLanguage=LANG Assumes files are in the specified language if none can be inferred
-excludes=SPEC Excludes files matching the specified pattern
-failOnDuplication[+/-/%] Exits with a failure return code if duplication detected
-formatter=TYPE[:FNAME] Uses the specified output format when reporting
-ignoreBlocks=START:END Ignores all lines between START/END
-ignoreCharacterCase[+/-] Matches character literals irrespective of case
-ignoreCharacters[+/-] Completely ignores character literals
-ignoreCurlyBraces[+/-] Completely ignores curly braces
-ignoreIdentifierCase[+/-] Matches identifiers irresepctive of case
-ignoreIdentifiers[+/-] Completely ignores identifiers
-ignoreLiterals[+/-] Completely ignores all literals (strings, numbers and characters)
-ignoreModifiers[+/-] Ignores modifiers (public, private, static, etc.)
-ignoreNumbers[+/-] Completely ignores numbers
-ignoreOverlappingBlocks[+/-] Ignores blocks that wholly or partially overlap
-ignoreRegions[+/-] Ignores all lines between #region/#endregion
-ignoreStringCase[+/-] Matches string literals irrespective of case
-ignoreStrings[+/-] Completely ignores the contents of strings
-ignoreSubtypeNames[+/-] Matches on similar type names (eg. Reader and FilterReader)
-ignoreVariableNames[+/-] Completely ignores variable names (fields, parameters and locals)
-includes=SPEC Including files matching the specified pattern
-language=LANG Assumes ALL files are in the specified language
-reportDuplicateText[+/-] Prints the duplicate text in reports
-threshold=COUNT Matches will contain at least the specified number of lines

3. 參照下圖分5個步驟:

這裡寫圖片描述

  1. 開啟cmd,cd進入simian-2.3.35資料夾的bin目錄;
  2. 執行命令java -jar simian-2.35.jar
  3. 後面可以根據自己的需求新增選擇;(如-ignoreVariableNames+ 表示忽略變數名,+號表示要新增這個屬性,-號表示不新增這個屬性(也可以不寫);-threshold=10 表示10行以上的重複才統計;)
  4. 需要掃描的檔案的路徑E:\...\main\**.java;(**.java表示main資料夾下的所有java檔案;)
  5. 將統計出來的資料寫入生成的duplicates10.txt檔案中;(檔名隨意,預設生成在simian的bin目錄下;)

4. 生成的duplicates10.txt內容:

Similarity Analyser 2.3.35 - http://www.harukizaemon.com/simian
Copyright (c) 2003-2013 Simon Harris. All rights reserved.
Simian is not free unless used solely for non-commercial or evaluation purposes.
// 配置的options選項,如ignoreVariableNames=true, threshold=10等
{failOnDuplication=true, ignoreCharacterCase=true, ignoreCurlyBraces=true, ignoreIdentifierCase=true, ignoreModifiers=true, ignoreStringCase=true, ignoreVariableNames=true, threshold=7}
Found 12 duplicate lines in the following files:
Between lines 64 and 75 in E:…\main\java\com\example\ui\activity\MainActivity.java
Between lines 62 and 73 in E:…\main\java\com\example\ui\activity\MainActivity.java
Found 13 duplicate lines in the following files:
Between lines 81 and 98 in E:…\main\java\com\example\module_widget\widget\Bezier3.java
Between lines 68 and 84 in E:…\main\java\com\example\module_widget\widget\Bezier2.java

……

Found 173 duplicate lines in 19 blocks in 9 files
Processed a total of 1409 significant (2905 raw) lines in 25 files
Processing time: 0.047sec

參考:

《移動App效能評測優化》