Package com.tonic.analysis.similarity
Class MethodSimilarityAnalyzer
- java.lang.Object
-
- com.tonic.analysis.similarity.MethodSimilarityAnalyzer
-
public class MethodSimilarityAnalyzer extends Object
Pairwise method-similarity analyzer over a ClassPool, used to find duplicates and renamed copies.
-
-
Constructor Summary
Constructors Constructor Description MethodSimilarityAnalyzer(ClassPool classPool)Creates an analyzer over the given pool; call buildIndex before querying.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbuildIndex()Builds signatures for every method with code in the pool, replacing any previous index.SimilarityResultcompare(MethodSignature sig1, MethodSignature sig2, SimilarityMetric primaryMetric)Scores two signatures on every metric.List<SimilarityResult>findAllSimilar(SimilarityMetric metric, double minScore)Compares every indexed pair and keeps those scoring at or above the threshold.List<SimilarityResult>findDuplicates()Finds pairs scoring at least 0.95 on the combined metric.List<SimilarityResult>findRenamedCopies()Finds pairs with near-identical opcode sequences but different method names.List<List<MethodSignature>>findSimilarityGroups(SimilarityMetric metric, double minScore)Clusters similar methods into connected components over the pairwise similarity graph.List<SimilarityResult>findSimilarTo(MethodSignature target, SimilarityMetric metric, double minScore)Finds indexed methods similar to a given signature.List<SimilarityResult>findSimilarTo(String className, String methodName, String descriptor, SimilarityMetric metric, double minScore)Finds indexed methods similar to the referenced method.intgetMethodCount()MethodSignaturegetSignature(String className, String methodName, String descriptor)Looks up an indexed signature by method reference.List<MethodSignature>getSignatures()voidsetProgressCallback(Consumer<String> callback)Sets a callback to receive progress messages during indexing and comparison.
-
-
-
Constructor Detail
-
MethodSimilarityAnalyzer
public MethodSimilarityAnalyzer(ClassPool classPool)
Creates an analyzer over the given pool; call buildIndex before querying.- Parameters:
classPool- the pool of classes to analyze
-
-
Method Detail
-
setProgressCallback
public void setProgressCallback(Consumer<String> callback)
Sets a callback to receive progress messages during indexing and comparison.- Parameters:
callback- the consumer of progress strings
-
buildIndex
public void buildIndex()
Builds signatures for every method with code in the pool, replacing any previous index.
-
findAllSimilar
public List<SimilarityResult> findAllSimilar(SimilarityMetric metric, double minScore)
Compares every indexed pair and keeps those scoring at or above the threshold.- Parameters:
metric- the metric to score withminScore- the minimum overall score to keep- Returns:
- matching pairs, best first
-
findSimilarTo
public List<SimilarityResult> findSimilarTo(String className, String methodName, String descriptor, SimilarityMetric metric, double minScore)
Finds indexed methods similar to the referenced method.- Parameters:
className- the owning class namemethodName- the method namedescriptor- the method descriptormetric- the metric to score withminScore- the minimum overall score to keep- Returns:
- matching results, best first; empty if the method is not indexed
-
findSimilarTo
public List<SimilarityResult> findSimilarTo(MethodSignature target, SimilarityMetric metric, double minScore)
Finds indexed methods similar to a given signature.- Parameters:
target- the signature to match againstmetric- the metric to score withminScore- the minimum overall score to keep- Returns:
- matching results, best first
-
compare
public SimilarityResult compare(MethodSignature sig1, MethodSignature sig2, SimilarityMetric primaryMetric)
Scores two signatures on every metric.- Parameters:
sig1- the first signaturesig2- the second signatureprimaryMetric- the metric that determines the overall score- Returns:
- the result holding all metric scores
-
findDuplicates
public List<SimilarityResult> findDuplicates()
Finds pairs scoring at least 0.95 on the combined metric.- Returns:
- the potential duplicate pairs, best first
-
findRenamedCopies
public List<SimilarityResult> findRenamedCopies()
Finds pairs with near-identical opcode sequences but different method names.- Returns:
- the candidate renamed-copy pairs
-
findSimilarityGroups
public List<List<MethodSignature>> findSimilarityGroups(SimilarityMetric metric, double minScore)
Clusters similar methods into connected components over the pairwise similarity graph.- Parameters:
metric- the metric to score withminScore- the minimum score for an edge- Returns:
- groups of two or more methods, largest first
-
getSignatures
public List<MethodSignature> getSignatures()
- Returns:
- an unmodifiable view of the indexed signatures
-
getMethodCount
public int getMethodCount()
- Returns:
- the number of indexed methods
-
getSignature
public MethodSignature getSignature(String className, String methodName, String descriptor)
Looks up an indexed signature by method reference.- Parameters:
className- the owning class namemethodName- the method namedescriptor- the method descriptor- Returns:
- the matching signature, or null if not indexed
-
-