Class MethodSimilarityAnalyzer


  • public class MethodSimilarityAnalyzer
    extends Object
    Pairwise method-similarity analyzer over a ClassPool, used to find duplicates and renamed copies.
    • 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 with
        minScore - 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 name
        methodName - the method name
        descriptor - the method descriptor
        metric - the metric to score with
        minScore - 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 against
        metric - the metric to score with
        minScore - 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 signature
        sig2 - the second signature
        primaryMetric - 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 with
        minScore - 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 name
        methodName - the method name
        descriptor - the method descriptor
        Returns:
        the matching signature, or null if not indexed