Search in sources :

Example 56 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.

the class RefactoringSearchEngine2 method searchReferencedTypes.

/**
 * Performs the search of referenced types.
 *
 * @param element the java element whose referenced types have to be found
 * @param monitor the progress monitor, or <code>null</code>
 * @throws JavaModelException if an error occurs during search
 */
public final void searchReferencedTypes(final IJavaElement element, IProgressMonitor monitor) throws JavaModelException {
    Assert.isNotNull(element);
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    try {
        // $NON-NLS-1$
        monitor.beginTask("", 1);
        monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_referenced_types);
        try {
            SearchEngine engine = null;
            if (fOwner != null) {
                engine = new SearchEngine(fOwner);
            } else {
                engine = new SearchEngine(fWorkingCopies);
            }
            engine.searchDeclarationsOfReferencedTypes(element, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
        } catch (CoreException exception) {
            throw new JavaModelException(exception);
        }
    } finally {
        monitor.done();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 57 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.

the class WorkspaceSymbolHandler method search.

public static List<SymbolInformation> search(String query, int maxResults, String projectName, boolean sourceOnly, IProgressMonitor monitor) {
    ArrayList<SymbolInformation> symbols = new ArrayList<>();
    if (StringUtils.isBlank(query)) {
        return symbols;
    }
    try {
        monitor.beginTask("Searching the types...", 100);
        IJavaSearchScope searchScope = createSearchScope(projectName, sourceOnly);
        String tQuery = query.trim();
        String qualifierName = null;
        String typeName = tQuery;
        int qualifierMatchRule = SearchPattern.R_PATTERN_MATCH;
        int qualIndex = tQuery.lastIndexOf('.');
        if (qualIndex != -1) {
            qualifierName = tQuery.substring(0, qualIndex);
            typeName = tQuery.substring(qualIndex + 1);
            qualifierMatchRule = SearchPattern.R_CAMELCASE_MATCH;
            if (qualifierName.contains("*") || qualifierName.contains("?")) {
                qualifierMatchRule = SearchPattern.R_PATTERN_MATCH;
            }
        }
        int typeMatchRule = SearchPattern.R_CAMELCASE_MATCH;
        if (typeName.contains("*") || typeName.contains("?")) {
            typeMatchRule = SearchPattern.R_PATTERN_MATCH;
        }
        PreferenceManager preferenceManager = JavaLanguageServerPlugin.getPreferencesManager();
        new SearchEngine().searchAllTypeNames(qualifierName == null ? null : qualifierName.toCharArray(), qualifierMatchRule, typeName.toCharArray(), typeMatchRule, IJavaSearchConstants.TYPE, searchScope, new TypeNameMatchRequestor() {

            @Override
            public void acceptTypeNameMatch(TypeNameMatch match) {
                try {
                    if (maxResults > 0 && symbols.size() >= maxResults) {
                        return;
                    }
                    Location location = null;
                    try {
                        if (!sourceOnly && match.getType().isBinary()) {
                            location = JDTUtils.toLocation(match.getType().getClassFile());
                        } else if (!match.getType().isBinary()) {
                            location = JDTUtils.toLocation(match.getType());
                        }
                    } catch (Exception e) {
                        JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getSimpleTypeName(), e);
                        return;
                    }
                    if (location != null && match.getSimpleTypeName() != null && !match.getSimpleTypeName().isEmpty()) {
                        SymbolInformation symbolInformation = new SymbolInformation();
                        symbolInformation.setContainerName(match.getTypeContainerName());
                        symbolInformation.setName(match.getSimpleTypeName());
                        symbolInformation.setKind(mapKind(match));
                        if (Flags.isDeprecated(match.getType().getFlags())) {
                            if (preferenceManager != null && preferenceManager.getClientPreferences().isSymbolTagSupported()) {
                                symbolInformation.setTags(List.of(SymbolTag.Deprecated));
                            } else {
                                symbolInformation.setDeprecated(true);
                            }
                        }
                        symbolInformation.setLocation(location);
                        symbols.add(symbolInformation);
                        if (maxResults > 0 && symbols.size() >= maxResults) {
                            monitor.setCanceled(true);
                        }
                    }
                } catch (Exception e) {
                    JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getSimpleTypeName(), e);
                    return;
                }
            }

            private SymbolKind mapKind(TypeNameMatch match) {
                int flags = match.getModifiers();
                if (Flags.isInterface(flags)) {
                    return SymbolKind.Interface;
                }
                if (Flags.isAnnotation(flags)) {
                    return SymbolKind.Property;
                }
                if (Flags.isEnum(flags)) {
                    return SymbolKind.Enum;
                }
                return SymbolKind.Class;
            }
        }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
        if (preferenceManager != null && preferenceManager.getPreferences().isIncludeSourceMethodDeclarations()) {
            monitor.beginTask("Searching methods...", 100);
            IJavaSearchScope nonSourceSearchScope = createSearchScope(projectName, true);
            new SearchEngine().searchAllMethodNames(null, SearchPattern.R_PATTERN_MATCH, query.trim().toCharArray(), typeMatchRule, nonSourceSearchScope, new MethodNameMatchRequestor() {

                @Override
                public void acceptMethodNameMatch(MethodNameMatch match) {
                    try {
                        if (maxResults > 0 && symbols.size() >= maxResults) {
                            return;
                        }
                        Location location = null;
                        try {
                            location = JDTUtils.toLocation(match.getMethod());
                        } catch (Exception e) {
                            JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getMethod().getElementName(), e);
                            return;
                        }
                        if (location != null && match.getMethod().getElementName() != null && !match.getMethod().getElementName().isEmpty()) {
                            SymbolInformation symbolInformation = new SymbolInformation();
                            symbolInformation.setContainerName(match.getMethod().getDeclaringType().getFullyQualifiedName());
                            symbolInformation.setName(match.getMethod().getElementName());
                            symbolInformation.setKind(SymbolKind.Method);
                            if (Flags.isDeprecated(match.getMethod().getFlags())) {
                                if (preferenceManager != null && preferenceManager.getClientPreferences().isSymbolTagSupported()) {
                                    symbolInformation.setTags(List.of(SymbolTag.Deprecated));
                                } else {
                                    symbolInformation.setDeprecated(true);
                                }
                            }
                            symbolInformation.setLocation(location);
                            symbols.add(symbolInformation);
                            if (maxResults > 0 && symbols.size() >= maxResults) {
                                monitor.setCanceled(true);
                            }
                        }
                    } catch (Exception e) {
                        JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getMethod().getElementName(), e);
                        return;
                    }
                }
            }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
        }
    } catch (Exception e) {
        if (e instanceof OperationCanceledException) {
        // ignore.
        } else {
            JavaLanguageServerPlugin.logException("Problem getting search for" + query, e);
        }
    } finally {
        monitor.done();
    }
    return symbols;
}
Also used : MethodNameMatch(org.eclipse.jdt.core.search.MethodNameMatch) SymbolKind(org.eclipse.lsp4j.SymbolKind) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor) JavaModelException(org.eclipse.jdt.core.JavaModelException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) MethodNameMatchRequestor(org.eclipse.jdt.core.search.MethodNameMatchRequestor) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) Location(org.eclipse.lsp4j.Location)

Example 58 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.

the class RippleMethodFinder method findAllDeclarations.

private void findAllDeclarations(IProgressMonitor monitor, WorkingCopyOwner owner) throws CoreException {
    fDeclarations = new ArrayList<>();
    class MethodRequestor extends SearchRequestor {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            IMethod method = (IMethod) match.getElement();
            boolean isBinary = method.isBinary();
            if (!isBinary) {
                fDeclarations.add(method);
            }
        }
    }
    int limitTo = IJavaSearchConstants.DECLARATIONS | IJavaSearchConstants.IGNORE_DECLARING_TYPE | IJavaSearchConstants.IGNORE_RETURN_TYPE;
    int matchRule = SearchPattern.R_ERASURE_MATCH | SearchPattern.R_CASE_SENSITIVE;
    SearchPattern pattern = SearchPattern.createPattern(fMethod, limitTo, matchRule);
    MethodRequestor requestor = new MethodRequestor();
    SearchEngine searchEngine = owner != null ? new SearchEngine(owner) : new SearchEngine();
    searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, createSearchScope(), requestor, monitor);
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMethod(org.eclipse.jdt.core.IMethod)

Example 59 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.

the class RenameMethodProcessor method batchFindNewOccurrences.

// Lower memory footprint than batchFindNewOccurrences. Not used because it is too slow.
// Final solution is maybe to do searches in chunks of ~ 50 CUs.
// private SearchResultGroup[] findNewOccurrences(IMethod[] newMethods, ICompilationUnit[] newDeclarationWCs, IProgressMonitor pm) throws CoreException {
// pm.beginTask("", fOccurrences.length * 2); //$NON-NLS-1$
// 
// SearchPattern refsPattern= RefactoringSearchEngine.createOrPattern(newMethods, IJavaSearchConstants.REFERENCES);
// SearchParticipant[] searchParticipants= SearchUtils.getDefaultSearchParticipants();
// IJavaSearchScope scope= RefactoringScopeFactory.create(newMethods);
// MethodOccurenceCollector requestor= new MethodOccurenceCollector(getNewElementName());
// SearchEngine searchEngine= new SearchEngine(fWorkingCopyOwner);
// 
// //TODO: should process only references
// for (int j= 0; j < fOccurrences.length; j++) { //should be getReferences()
// //cut memory peak by holding only one reference CU at a time in memory
// ICompilationUnit originalCu= fOccurrences[j].getCompilationUnit();
// ICompilationUnit newWc= null;
// try {
// ICompilationUnit wc= RenameAnalyzeUtil.findWorkingCopyForCu(newDeclarationWCs, originalCu);
// if (wc == null) {
// newWc= RenameAnalyzeUtil.createNewWorkingCopy(originalCu, fChangeManager, fWorkingCopyOwner,
// new SubProgressMonitor(pm, 1));
// }
// searchEngine.search(refsPattern, searchParticipants, scope,	requestor, new SubProgressMonitor(pm, 1));
// } finally {
// if (newWc != null)
// newWc.discardWorkingCopy();
// }
// }
// SearchResultGroup[] newResults= RefactoringSearchEngine.groupByResource(requestor.getResults());
// pm.done();
// return newResults;
// }
private SearchResultGroup[] batchFindNewOccurrences(IMethod[] wcNewMethods, final IMethod[] wcOldMethods, ICompilationUnit[] newDeclarationWCs, IProgressMonitor pm, RefactoringStatus status) throws CoreException {
    // $NON-NLS-1$
    pm.beginTask("", 2);
    SearchPattern refsPattern = RefactoringSearchEngine.createOrPattern(wcNewMethods, IJavaSearchConstants.REFERENCES);
    SearchParticipant[] searchParticipants = SearchUtils.getDefaultSearchParticipants();
    IJavaSearchScope scope = RefactoringScopeFactory.create(wcNewMethods);
    MethodOccurenceCollector requestor;
    if (getDelegateUpdating()) {
        // There will be two new matches inside the delegate(s) (the invocation
        // and the javadoc) which are OK and must not be reported.
        // Note that except these ocurrences, the delegate bodies are empty
        // (as they were created this way).
        requestor = new MethodOccurenceCollector(getNewElementName()) {

            @Override
            public void acceptSearchMatch(ICompilationUnit unit, SearchMatch match) throws CoreException {
                for (int i = 0; i < wcOldMethods.length; i++) {
                    if (wcOldMethods[i].equals(match.getElement())) {
                        return;
                    }
                }
                super.acceptSearchMatch(unit, match);
            }
        };
    } else {
        requestor = new MethodOccurenceCollector(getNewElementName());
    }
    SearchEngine searchEngine = new SearchEngine(fWorkingCopyOwner);
    ArrayList<ICompilationUnit> needWCs = new ArrayList<>();
    HashSet<ICompilationUnit> declaringCUs = new HashSet<>(newDeclarationWCs.length);
    for (int i = 0; i < newDeclarationWCs.length; i++) {
        declaringCUs.add(newDeclarationWCs[i].getPrimary());
    }
    for (int i = 0; i < fOccurrences.length; i++) {
        ICompilationUnit cu = fOccurrences[i].getCompilationUnit();
        if (!declaringCUs.contains(cu)) {
            needWCs.add(cu);
        }
    }
    ICompilationUnit[] otherWCs = null;
    try {
        otherWCs = RenameAnalyzeUtil.createNewWorkingCopies(needWCs.toArray(new ICompilationUnit[needWCs.size()]), fChangeManager, fWorkingCopyOwner, new SubProgressMonitor(pm, 1));
        searchEngine.search(refsPattern, searchParticipants, scope, requestor, new SubProgressMonitor(pm, 1));
    } finally {
        pm.done();
        if (otherWCs != null) {
            for (int i = 0; i < otherWCs.length; i++) {
                otherWCs[i].discardWorkingCopy();
            }
        }
    }
    SearchResultGroup[] newResults = RefactoringSearchEngine.groupByCu(requestor.getResults(), status);
    return newResults;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) ArrayList(java.util.ArrayList) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) SearchParticipant(org.eclipse.jdt.core.search.SearchParticipant) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) RefactoringSearchEngine(org.eclipse.jdt.ls.core.internal.corext.refactoring.RefactoringSearchEngine) CoreException(org.eclipse.core.runtime.CoreException) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) HashSet(java.util.HashSet)

Example 60 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.

the class RenameMethodProcessor method searchForOuterTypesOfReferences.

private IType[] searchForOuterTypesOfReferences(IMethod[] newNameMethods, IProgressMonitor pm) throws CoreException {
    final Set<IType> outerTypesOfReferences = new HashSet<>();
    SearchPattern pattern = RefactoringSearchEngine.createOrPattern(newNameMethods, IJavaSearchConstants.REFERENCES);
    IJavaSearchScope scope = createRefactoringScope(getMethod());
    SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object element = match.getElement();
            if (!(element instanceof IMember)) {
                // e.g. an IImportDeclaration for a static method import
                return;
            }
            IMember member = (IMember) element;
            IType declaring = member.getDeclaringType();
            if (declaring == null) {
                return;
            }
            IType outer = declaring.getDeclaringType();
            if (outer != null) {
                outerTypesOfReferences.add(declaring);
            }
        }
    };
    new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm);
    return outerTypesOfReferences.toArray(new IType[outerTypesOfReferences.size()]);
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) RefactoringSearchEngine(org.eclipse.jdt.ls.core.internal.corext.refactoring.RefactoringSearchEngine) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType) HashSet(java.util.HashSet)

Aggregations

SearchEngine (org.eclipse.jdt.core.search.SearchEngine)131 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)73 CoreException (org.eclipse.core.runtime.CoreException)71 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)61 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)61 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)58 ArrayList (java.util.ArrayList)52 JavaModelException (org.eclipse.jdt.core.JavaModelException)48 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)38 IType (org.eclipse.jdt.core.IType)38 IJavaElement (org.eclipse.jdt.core.IJavaElement)37 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)24 SearchParticipant (org.eclipse.jdt.core.search.SearchParticipant)24 TypeNameMatch (org.eclipse.jdt.core.search.TypeNameMatch)21 HashSet (java.util.HashSet)20 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)20 IMethod (org.eclipse.jdt.core.IMethod)20 TypeNameMatchRequestor (org.eclipse.jdt.core.search.TypeNameMatchRequestor)16 IJavaProject (org.eclipse.jdt.core.IJavaProject)15 InvocationTargetException (java.lang.reflect.InvocationTargetException)12