Class SearchEngine

    • Constructor Detail

      • SearchEngine

        public SearchEngine()
        Creates a new search engine.
      • SearchEngine

        public SearchEngine​(ICompilationUnit[] workingCopies)
        Creates a new search engine with a list of working copies that will take precedence over their original compilation units in the subsequent search operations.

        Note that passing an empty working copy will be as if the original compilation unit had been deleted.

        Since 3.0 the given working copies take precedence over primary working copies (if any).

        Parameters:
        workingCopies - the working copies that take precedence over their original compilation units
        Since:
        3.0
      • SearchEngine

        public SearchEngine​(IWorkingCopy[] workingCopies)
        Deprecated.
        Creates a new search engine with a list of working copies that will take precedence over their original compilation units in the subsequent search operations.

        Note that passing an empty working copy will be as if the original compilation unit had been deleted.

        Since 3.0 the given working copies take precedence over primary working copies (if any).

        Parameters:
        workingCopies - the working copies that take precedence over their original compilation units
        Since:
        2.0
      • SearchEngine

        public SearchEngine​(WorkingCopyOwner workingCopyOwner)
        Creates a new search engine with the given working copy owner. The working copies owned by this owner will take precedence over the primary compilation units in the subsequent search operations.
        Parameters:
        workingCopyOwner - the owner of the working copies that take precedence over their original compilation units
        Since:
        3.0
    • Method Detail

      • createHierarchyScope

        public static IJavaSearchScope createHierarchyScope​(IType type)
                                                     throws JavaModelException
        Returns a Java search scope limited to the hierarchy of the given type. The Java elements resulting from a search with this scope will be types in this hierarchy, or members of the types in this hierarchy.
        Parameters:
        type - the focus of the hierarchy scope
        Returns:
        a new hierarchy scope
        Throws:
        JavaModelException - if the hierarchy could not be computed on the given type
      • createHierarchyScope

        public static IJavaSearchScope createHierarchyScope​(IType type,
                                                            WorkingCopyOwner owner)
                                                     throws JavaModelException
        Returns a Java search scope limited to the hierarchy of the given type. When the hierarchy is computed, the types defined in the working copies owned by the given owner take precedence over the original compilation units. The Java elements resulting from a search with this scope will be types in this hierarchy, or members of the types in this hierarchy.
        Parameters:
        type - the focus of the hierarchy scope
        owner - the owner of working copies that take precedence over original compilation units
        Returns:
        a new hierarchy scope
        Throws:
        JavaModelException - if the hierarchy could not be computed on the given type
        Since:
        3.0
      • createStrictHierarchyScope

        public static IJavaSearchScope createStrictHierarchyScope​(IJavaProject project,
                                                                  IType type,
                                                                  boolean onlySubtypes,
                                                                  boolean includeFocusType,
                                                                  WorkingCopyOwner owner)
                                                           throws JavaModelException
        Returns a Java search scope limited to the hierarchy of the given type and to a given project. The Java elements resulting from a search with this scope will be types in this hierarchy.

        Unlike the createHierarchyScope methods, this method creates strict scopes that only contain types that actually span the hierarchy of the focus type, but do not include additional enclosing or member types.

        By default, hierarchy scopes include all direct and indirect supertypes and subtypes of the focus type. This method, however, allows to restrict the hierarchy to true subtypes, and exclude supertypes. Also, inclusion of the focus type itself is controlled by a parameter.

        Parameters:
        project - the project to which to constrain the search, or null if search should consider all types in the workspace
        type - the focus of the hierarchy scope
        onlySubtypes - if true only subtypes of type are considered
        includeFocusType - if true the focus type type is included in the resulting scope, otherwise it is excluded
        owner - the owner of working copies that take precedence over original compilation units, or null if the primary working copy owner should be used
        Returns:
        a new hierarchy scope
        Throws:
        JavaModelException - if the hierarchy could not be computed on the given type
        Since:
        3.6
      • createJavaSearchScope

        public static IJavaSearchScope createJavaSearchScope​(IResource[] resources)
        Deprecated.
        Returns a Java search scope limited to the given resources. The Java elements resulting from a search with this scope will have their underlying resource included in or equals to one of the given resources.

        Resources must not overlap, for example, one cannot include a folder and its children.

        Parameters:
        resources - the resources the scope is limited to
        Returns:
        a new Java search scope
      • createJavaSearchScope

        public static IJavaSearchScope createJavaSearchScope​(IJavaElement[] elements)
        Returns a Java search scope limited to the given Java elements. The Java elements resulting from a search with this scope will be children of the given elements.

        If an element is an IJavaProject, then the project's source folders, its jars (external and internal) and its referenced projects (with their source folders and jars, recursively) will be included.

        If an element is an IPackageFragmentRoot, then only the package fragments of this package fragment root will be included.

        If an element is an IPackageFragment, then only the compilation unit and class files of this package fragment will be included. Subpackages will NOT be included.

        In other words, this is equivalent to using SearchEngine.createJavaSearchScope(elements, true).

        Parameters:
        elements - the Java elements the scope is limited to
        Returns:
        a new Java search scope
        Since:
        2.0
      • createJavaSearchScope

        public static IJavaSearchScope createJavaSearchScope​(IJavaElement[] elements,
                                                             boolean includeReferencedProjects)
        Returns a Java search scope limited to the given Java elements. The Java elements resulting from a search with this scope will be children of the given elements.

        If an element is an IJavaProject, then the project's source folders, its jars (external and internal) and - if specified - its referenced projects (with their source folders and jars, recursively) will be included.

        If an element is an IPackageFragmentRoot, then only the package fragments of this package fragment root will be included.

        If an element is an IPackageFragment, then only the compilation unit and class files of this package fragment will be included. Subpackages will NOT be included.

        Parameters:
        elements - the Java elements the scope is limited to
        includeReferencedProjects - a flag indicating if referenced projects must be recursively included
        Returns:
        a new Java search scope
        Since:
        2.0
      • createJavaSearchScope

        public static IJavaSearchScope createJavaSearchScope​(boolean excludeTestCode,
                                                             IJavaElement[] elements,
                                                             boolean includeReferencedProjects)
        Returns a Java search scope limited to the given Java elements. The Java elements resulting from a search with this scope will be children of the given elements.

        If an element is an IJavaProject, then the project's source folders, its jars (external and internal) and - if specified - its referenced projects (with their source folders and jars, recursively) will be included.

        If an element is an IPackageFragmentRoot, then only the package fragments of this package fragment root will be included.

        If an element is an IPackageFragment, then only the compilation unit and class files of this package fragment will be included. Subpackages will NOT be included.

        Parameters:
        excludeTestCode - if true, test code we be excluded
        elements - the Java elements the scope is limited to
        includeReferencedProjects - a flag indicating if referenced projects must be recursively included
        Returns:
        a new Java search scope
        Since:
        3.14
      • createJavaSearchScope

        public static IJavaSearchScope createJavaSearchScope​(boolean excludeTestCode,
                                                             IJavaElement[] elements,
                                                             int includeMask)
        Returns a Java search scope limited to the given Java elements. The Java elements resulting from a search with this scope will be children of the given elements.

        If an element is an IJavaProject, then it includes:

        • its source folders if IJavaSearchScope.SOURCES is specified,
        • its application libraries (internal and external jars, class folders that are on the raw classpath, or the ones that are coming from a classpath path variable, or the ones that are coming from a classpath container with the K_APPLICATION kind) if IJavaSearchScope.APPLICATION_LIBRARIES is specified
        • its system libraries (internal and external jars, class folders that are coming from an IClasspathContainer with the K_SYSTEM kind) if IJavaSearchScope.SYSTEM_LIBRARIES is specified
        • its referenced projects (with their source folders and jars, recursively) if IJavaSearchScope.REFERENCED_PROJECTS is specified.

        If an element is an IPackageFragmentRoot, then only the package fragments of this package fragment root will be included.

        If an element is an IPackageFragment, then only the compilation unit and class files of this package fragment will be included. Subpackages will NOT be included.

        Parameters:
        excludeTestCode -
        elements - the Java elements the scope is limited to
        includeMask - the bit-wise OR of all include types of interest
        Returns:
        a new Java search scope
        Since:
        3.14
        See Also:
        IJavaSearchScope.SOURCES, IJavaSearchScope.APPLICATION_LIBRARIES, IJavaSearchScope.SYSTEM_LIBRARIES, IJavaSearchScope.REFERENCED_PROJECTS
      • createSearchPattern

        public static ISearchPattern createSearchPattern​(IJavaElement element,
                                                         int limitTo)
        Returns a search pattern based on a given Java element. The pattern is used to trigger the appropriate search, and can be parameterized as follows:
        Parameters:
        element - the Java element the search pattern is based on
        limitTo - determines the nature of the expected matches
        Returns:
        a search pattern for a Java element or null if the given element is ill-formed
      • createTypeNameMatch

        public static TypeNameMatch createTypeNameMatch​(IType type,
                                                        int modifiers)
        Create a type name match on a given type with specific modifiers.
        Parameters:
        type - The java model handle of the type
        modifiers - Modifiers of the type
        Returns:
        A non-null match on the given type.
        Since:
        3.3
      • createMethodNameMatch

        public static MethodNameMatch createMethodNameMatch​(IMethod method,
                                                            int modifiers)
        Create a method name match on a given method with specific modifiers.

        NOTE: this is a first-cut version and might have noticeable delay times in some cases. Targeted for optimization during 4.7. It is recommended to contact JDT Team if anyone plans to use this.

        Parameters:
        method - The Java model handle of the method
        modifiers - Modifiers of the method
        Returns:
        A non-null match on the given method.
        Since:
        3.12
      • createWorkspaceScope

        public static IJavaSearchScope createWorkspaceScope()
        Returns a Java search scope with the workspace as the only limit.
        Returns:
        a new workspace scope
      • getDefaultSearchParticipant

        public static SearchParticipant getDefaultSearchParticipant()
        Returns a new default Java search participant.
        Returns:
        a new default Java search participant
        Since:
        3.0
      • search

        public void search​(SearchPattern pattern,
                           SearchParticipant[] participants,
                           IJavaSearchScope scope,
                           SearchRequestor requestor,
                           IProgressMonitor monitor)
                    throws CoreException
        Searches for matches of a given search pattern. Search patterns can be created using helper methods (from a String pattern or a Java element) and encapsulate the description of what is being searched (for example, search method declarations in a case sensitive way).
        Parameters:
        pattern - the pattern to search
        participants - the participants in the search
        scope - the search scope
        requestor - the requestor to report the matches to
        monitor - the progress monitor used to report progress
        Throws:
        CoreException - if the search failed. Reasons include:
        • the classpath is incorrectly set
        Since:
        3.0
      • searchAllMethodNames

        public void searchAllMethodNames​(char[] packageName,
                                         int pkgMatchRule,
                                         char[] declaringQualification,
                                         int declQualificationMatchRule,
                                         char[] declaringSimpleName,
                                         int declSimpleNameMatchRule,
                                         char[] methodName,
                                         int methodMatchRule,
                                         IJavaSearchScope scope,
                                         MethodNameRequestor nameRequestor,
                                         int waitingPolicy,
                                         IProgressMonitor progressMonitor)
                                  throws JavaModelException
        Searches for all method declarations in the given scope. Accepted matches will be returned by MethodNameRequestor.acceptMethod(char[], int, char[], char[], int, char[], char[], char[][], char[][], char[], int, java.lang.String, int).

        NOTE: this is a first-cut version and might have noticeable delay times in some cases. Targeted for optimization during 4.7. It is recommended to contact JDT Team if anyone plans to use this.

        Parameters:
        packageName - the full name of the package of the searched types, or a prefix for this package, or a wild-carded string for this package. May be null, then any package name is accepted.
        pkgMatchRule - match rule for package.
        declaringQualification - Qualification of the declaring type.
        declQualificationMatchRule - match rule for declaring qualifier of parent of the type.
        declaringSimpleName - simple name of the declaring type.
        declSimpleNameMatchRule - match rule for the simple name of the enclosing type.
        methodName - the method name searched for.
        methodMatchRule - match rule for the method name.
        scope - the scope to search in
        nameRequestor - the requestor that collects the results of the search.
        waitingPolicy - one of
        progressMonitor - the progress monitor to report progress to, or null if no progress monitor is provided
        Throws:
        JavaModelException - if the search failed.
        Since:
        3.12
      • searchAllMethodNames

        public void searchAllMethodNames​(char[] packageName,
                                         int pkgMatchRule,
                                         char[] declaringQualification,
                                         int declQualificationMatchRule,
                                         char[] declaringSimpleName,
                                         int declSimpleNameMatchRule,
                                         char[] methodName,
                                         int methodMatchRule,
                                         IJavaSearchScope scope,
                                         MethodNameMatchRequestor nameRequestor,
                                         int waitingPolicy,
                                         IProgressMonitor progressMonitor)
                                  throws JavaModelException
        Searches for all method declarations in the given scope.

        Provided MethodNameMatchRequestor requestor will collect the MethodNameMatch matches found during the search.

        NOTE: this is a first-cut version and might have noticeable delay times in some cases. Targeted for optimization during 4.7. It is recommended to contact JDT Team if anyone plans to use this.

        Parameters:
        packageName - the full name of the package of the searched types, or a prefix for this package, or a wild-carded string for this package. May be null, then any package name is accepted.
        pkgMatchRule - match rule for package.
        declaringQualification - Qualification of the declaring type.
        declQualificationMatchRule - match rule for declaring qualifier of parent of the type.
        declaringSimpleName - simple name of the declaring type.
        declSimpleNameMatchRule - match rule for the simple name of the enclosing type.
        methodName - the method name searched for.
        methodMatchRule - match rule for the method name.
        scope - the scope to search in
        nameRequestor - the MethodNameMatchRequestor
        waitingPolicy - one of
        progressMonitor - the progress monitor to report progress to, or null if no progress monitor is provided
        Throws:
        JavaModelException - if the search failed.
        Since:
        3.12
      • searchAllTypeNames

        public void searchAllTypeNames​(char[][] qualifications,
                                       char[][] typeNames,
                                       IJavaSearchScope scope,
                                       TypeNameRequestor nameRequestor,
                                       int waitingPolicy,
                                       IProgressMonitor progressMonitor)
                                throws JavaModelException
        Searches for all top-level types and member types in the given scope matching any of the given qualifications and type names in a case sensitive way.
        Parameters:
        qualifications - the qualified name of the package/enclosing type of the searched types. May be null, then any package name is accepted.
        typeNames - the simple names of the searched types. If this parameter is null, then no type will be found.
        scope - the scope to search in
        nameRequestor - the requestor that collects the results of the search
        waitingPolicy - one of
        progressMonitor - the progress monitor to report progress to, or null if no progress monitor is provided
        Throws:
        JavaModelException - if the search failed. Reasons include:
        • the classpath is incorrectly set
        Since:
        3.1
      • searchAllTypeNames

        public void searchAllTypeNames​(char[][] qualifications,
                                       char[][] typeNames,
                                       IJavaSearchScope scope,
                                       TypeNameMatchRequestor nameMatchRequestor,
                                       int waitingPolicy,
                                       IProgressMonitor progressMonitor)
                                throws JavaModelException
        Searches for all top-level types and member types in the given scope matching any of the given qualifications and type names in a case sensitive way.

        Provided TypeNameMatchRequestor requestor will collect TypeNameMatch matches found during the search.

        Parameters:
        qualifications - the qualified name of the package/enclosing type of the searched types. May be null, then any package name is accepted.
        typeNames - the simple names of the searched types. If this parameter is null, then no type will be found.
        scope - the scope to search in
        nameMatchRequestor - the requestor that collects matches of the search.
        waitingPolicy - one of
        progressMonitor - the progress monitor to report progress to, or null if no progress monitor is provided
        Throws:
        JavaModelException - if the search failed. Reasons include:
        • the classpath is incorrectly set
        Since:
        3.3
      • searchDeclarationsOfAccessedFields

        public void searchDeclarationsOfAccessedFields​(IJavaElement enclosingElement,
                                                       SearchRequestor requestor,
                                                       IProgressMonitor monitor)
                                                throws JavaModelException
        Searches for all declarations of the fields accessed in the given element. The element can be a compilation unit or a source type/method/field. Reports the field declarations using the given requestor.

        Consider the following code:

         
                        class A {
                                int field1;
                        }
                        class B extends A {
                                String value;
                        }
                        class X {
                                void test() {
                                        B b = new B();
                                        System.out.println(b.value + b.field1);
                                };
                        }
         
         

        then searching for declarations of accessed fields in method X.test() would collect the fields B.value and A.field1.

        Parameters:
        enclosingElement - the field, method, type, or compilation unit to be searched in
        requestor - a callback object to which each match is reported
        monitor - the progress monitor used to report progress
        Throws:
        JavaModelException - if the search failed. Reasons include:
        • the element doesn't exist
        • the classpath is incorrectly set
        IllegalArgumentException - if the given java element has not the right type
        Since:
        3.0
      • searchDeclarationsOfAccessedFields

        public void searchDeclarationsOfAccessedFields​(IWorkspace workspace,
                                                       IJavaElement enclosingElement,
                                                       IJavaSearchResultCollector resultCollector)
                                                throws JavaModelException
        Searches for all declarations of the fields accessed in the given element. The element can be a compilation unit, a source type, or a source method. Reports the field declarations using the given collector.

        Consider the following code:

         
                        class A {
                                int field1;
                        }
                        class B extends A {
                                String value;
                        }
                        class X {
                                void test() {
                                        B b = new B();
                                        System.out.println(b.value + b.field1);
                                };
                        }
         
         

        then searching for declarations of accessed fields in method X.test() would collect the fields B.value and A.field1.

        Parameters:
        workspace - the workspace
        enclosingElement - the method, type, or compilation unit to be searched in
        resultCollector - a callback object to which each match is reported
        Throws:
        JavaModelException - if the search failed. Reasons include:
        • the element doesn't exist
        • the classpath is incorrectly set
      • searchDeclarationsOfReferencedTypes

        public void searchDeclarationsOfReferencedTypes​(IJavaElement enclosingElement,
                                                        SearchRequestor requestor,
                                                        IProgressMonitor monitor)
                                                 throws JavaModelException
        Searches for all declarations of the types referenced in the given element. The element can be a compilation unit or a source type/method/field. Reports the type declarations using the given requestor.

        Consider the following code:

         
                        class A {
                        }
                        class B extends A {
                        }
                        interface I {
                          int VALUE = 0;
                        }
                        class X {
                                void test() {
                                        B b = new B();
                                        this.foo(b, I.VALUE);
                                };
                        }
         
         

        then searching for declarations of referenced types in method X.test() would collect the class B and the interface I.

        Parameters:
        enclosingElement - the field, method, type, or compilation unit to be searched in
        requestor - a callback object to which each match is reported
        monitor - the progress monitor used to report progress
        Throws:
        JavaModelException - if the search failed. Reasons include:
        • the element doesn't exist
        • the classpath is incorrectly set
        IllegalArgumentException - if the given java element has not the right type
        Since:
        3.0
      • searchDeclarationsOfReferencedTypes

        public void searchDeclarationsOfReferencedTypes​(IWorkspace workspace,
                                                        IJavaElement enclosingElement,
                                                        IJavaSearchResultCollector resultCollector)
                                                 throws JavaModelException
        Searches for all declarations of the types referenced in the given element. The element can be a compilation unit, a source type, or a source method. Reports the type declarations using the given collector.

        Consider the following code:

         
                        class A {
                        }
                        class B extends A {
                        }
                        interface I {
                          int VALUE = 0;
                        }
                        class X {
                                void test() {
                                        B b = new B();
                                        this.foo(b, I.VALUE);
                                };
                        }
         
         

        then searching for declarations of referenced types in method X.test() would collect the class B and the interface I.

        Parameters:
        workspace - the workspace
        enclosingElement - the method, type, or compilation unit to be searched in
        resultCollector - a callback object to which each match is reported
        Throws:
        JavaModelException - if the search failed. Reasons include:
        • the element doesn't exist
        • the classpath is incorrectly set
      • searchDeclarationsOfSentMessages

        public void searchDeclarationsOfSentMessages​(IJavaElement enclosingElement,
                                                     SearchRequestor requestor,
                                                     IProgressMonitor monitor)
                                              throws JavaModelException
        Searches for all declarations of the methods invoked in the given element. The element can be a compilation unit or a source type/method/field. Reports the method declarations using the given requestor.

        Consider the following code:

         
                        class A {
                                void foo() {};
                                void bar() {};
                        }
                        class B extends A {
                                void foo() {};
                        }
                        class X {
                                void test() {
                                        A a = new B();
                                        a.foo();
                                        B b = (B)a;
                                        b.bar();
                                };
                        }
         
         

        then searching for declarations of sent messages in method X.test() would collect the methods A.foo(), B.foo(), and A.bar().

        Parameters:
        enclosingElement - the field, method, type or compilation unit to be searched in
        requestor - a callback object to which each match is reported
        monitor - the progress monitor used to report progress
        Throws:
        JavaModelException - if the search failed. Reasons include:
        • the element doesn't exist
        • the classpath is incorrectly set
        IllegalArgumentException - if the given java element has not the right type
        Since:
        3.0
      • searchDeclarationsOfSentMessages

        public void searchDeclarationsOfSentMessages​(IWorkspace workspace,
                                                     IJavaElement enclosingElement,
                                                     IJavaSearchResultCollector resultCollector)
                                              throws JavaModelException
        Searches for all declarations of the methods invoked in the given element. The element can be a compilation unit, a source type, or a source method. Reports the method declarations using the given collector.

        Consider the following code:

         
                        class A {
                                void foo() {};
                                void bar() {};
                        }
                        class B extends A {
                                void foo() {};
                        }
                        class X {
                                void test() {
                                        A a = new B();
                                        a.foo();
                                        B b = (B)a;
                                        b.bar();
                                };
                        }
         
         

        then searching for declarations of sent messages in method X.test() would collect the methods A.foo(), B.foo(), and A.bar().

        Parameters:
        workspace - the workspace
        enclosingElement - the method, type, or compilation unit to be searched in
        resultCollector - a callback object to which each match is reported
        Throws:
        JavaModelException - if the search failed. Reasons include:
        • the element doesn't exist
        • the classpath is incorrectly set