Class PatternSearch


  • public class PatternSearch
    extends Object
    Fluent search API over a ClassPool for code patterns, optionally backed by call-graph, dependency, and type-inference analyses.
    • Constructor Detail

      • PatternSearch

        public PatternSearch​(ClassPool classPool)
        Creates a search over the given pool with an empty scope and no analyses attached.
        Parameters:
        classPool - the pool of classes to search
    • Method Detail

      • inAllClasses

        public PatternSearch inAllClasses()
        Clears the scope so the search covers every class in the pool.
        Returns:
        this search
      • inClass

        public PatternSearch inClass​(String className)
        Adds one class to the scope, ignoring names the pool cannot resolve.
        Parameters:
        className - name of the class to look up in the pool
        Returns:
        this search
      • inPackage

        public PatternSearch inPackage​(String packagePrefix)
        Adds the classes under a package prefix to the scope.
        Parameters:
        packagePrefix - package prefix to match
        Returns:
        this search
      • inMethod

        public PatternSearch inMethod​(MethodEntry method)
        Adds one method to the scope; an explicit method scope overrides any class scope.
        Parameters:
        method - method to search
        Returns:
        this search
      • inAllMethodsOf

        public PatternSearch inAllMethodsOf​(ClassFile classFile)
        Adds every method of a class to the scope.
        Parameters:
        classFile - class whose methods are searched
        Returns:
        this search
      • limit

        public PatternSearch limit​(int maxResults)
        Caps how many results any subsequent query returns.
        Parameters:
        maxResults - result ceiling
        Returns:
        this search
      • withCallGraph

        public PatternSearch withCallGraph​(CallGraph callGraph)
        Attaches an already-built call graph for the caller and callee queries.
        Parameters:
        callGraph - call graph to use
        Returns:
        this search
      • withCallGraph

        public PatternSearch withCallGraph()
        Builds a call graph over the pool and attaches it.
        Returns:
        this search
      • withDependencies

        public PatternSearch withDependencies​(DependencyAnalyzer analyzer)
        Attaches an already-built dependency analyzer for the dependency queries.
        Parameters:
        analyzer - dependency analyzer to use
        Returns:
        this search
      • withDependencies

        public PatternSearch withDependencies()
        Builds a dependency analyzer over the pool and attaches it.
        Returns:
        this search
      • findMethodCalls

        public List<SearchResult> findMethodCalls​(PatternMatcher pattern)
        Finds call sites accepted by a matcher.
        Parameters:
        pattern - matcher applied to every lifted instruction in scope
        Returns:
        matching instructions, up to the result limit
      • findMethodCalls

        public List<SearchResult> findMethodCalls​(String ownerClass)
        Finds calls to any method declared by one owner class.
        Parameters:
        ownerClass - owner class of the callee
        Returns:
        matching instructions, up to the result limit
      • findMethodCalls

        public List<SearchResult> findMethodCalls​(String ownerClass,
                                                  String methodName)
        Finds calls to one named method on one owner class.
        Parameters:
        ownerClass - owner class of the callee
        methodName - callee name
        Returns:
        matching instructions, up to the result limit
      • findFieldAccesses

        public List<SearchResult> findFieldAccesses​(String ownerClass)
        Finds field reads and writes against one owner class.
        Parameters:
        ownerClass - owner class declaring the field
        Returns:
        matching instructions, up to the result limit
      • findFieldsByName

        public List<SearchResult> findFieldsByName​(String fieldName)
        Finds field accesses by field name, whatever the owner.
        Parameters:
        fieldName - field name to match
        Returns:
        matching instructions, up to the result limit
      • findInstanceOfChecks

        public List<SearchResult> findInstanceOfChecks()
        Finds every instanceof check in scope.
        Returns:
        matching instructions, up to the result limit
      • findInstanceOfChecks

        public List<SearchResult> findInstanceOfChecks​(String typeName)
        Finds instanceof checks against one type.
        Parameters:
        typeName - type being tested
        Returns:
        matching instructions, up to the result limit
      • findCasts

        public List<SearchResult> findCasts()
        Finds every checked cast in scope.
        Returns:
        matching instructions, up to the result limit
      • findCastsTo

        public List<SearchResult> findCastsTo​(String typeName)
        Finds casts to one type.
        Parameters:
        typeName - cast target type
        Returns:
        matching instructions, up to the result limit
      • findAllocations

        public List<SearchResult> findAllocations()
        Finds every object allocation in scope.
        Returns:
        matching instructions, up to the result limit
      • findAllocations

        public List<SearchResult> findAllocations​(String className)
        Finds allocations of one class.
        Parameters:
        className - allocated class
        Returns:
        matching instructions, up to the result limit
      • findNullChecks

        public List<SearchResult> findNullChecks()
        Finds every null comparison in scope.
        Returns:
        matching instructions, up to the result limit
      • findThrows

        public List<SearchResult> findThrows()
        Finds every athrow in scope.
        Returns:
        matching instructions, up to the result limit
      • findPattern

        public List<SearchResult> findPattern​(PatternMatcher pattern)
        Finds instructions accepted by a caller-supplied matcher.
        Parameters:
        pattern - matcher applied to every lifted instruction in scope
        Returns:
        matching instructions, up to the result limit
      • findCallersOf

        public List<SearchResult> findCallersOf​(String owner,
                                                String name,
                                                String descriptor)
        Finds the methods that call one target, building the call graph first if none is attached.
        Parameters:
        owner - declaring class of the target
        name - target method name
        descriptor - target method descriptor
        Returns:
        one result per caller resolvable in the pool, up to the result limit
      • findCalleesOf

        public List<SearchResult> findCalleesOf​(String owner,
                                                String name,
                                                String descriptor)
        Finds the methods one caller invokes, building the call graph first if none is attached.
        Parameters:
        owner - declaring class of the caller
        name - caller method name
        descriptor - caller method descriptor
        Returns:
        one result per callee, up to the result limit
      • findDependentsOf

        public List<SearchResult> findDependentsOf​(String className)
        Finds the classes that depend on one class, building the dependency analysis if none is attached.
        Parameters:
        className - class depended upon
        Returns:
        one result per dependent class, up to the result limit
      • findDependenciesOf

        public List<SearchResult> findDependenciesOf​(String className)
        Finds the classes one class depends on, building the dependency analysis if none is attached.
        Parameters:
        className - class whose dependencies are listed
        Returns:
        one result per dependency, up to the result limit
      • findPotentialNullDereferences

        public List<SearchResult> findPotentialNullDereferences()
        Finds calls and field reads whose receiver type inference cannot prove non-null.
        Returns:
        one result per suspect dereference, up to the result limit