Search in sources :

Example 36 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project quarkus-ls by redhat-developer.

the class JavaTypesSearch method collectClassesAndInterfaces.

private void collectClassesAndInterfaces(IProgressMonitor monitor, List<JavaTypeInfo> javaTypes) throws JavaModelException {
    // Collect classes and interfaces according to the type name
    SearchEngine engine = new SearchEngine();
    // 
    engine.searchAllTypeNames(// 
    packageName == null ? null : packageName.toCharArray(), // 
    SearchPattern.R_EXACT_MATCH, // 
    typeName.toCharArray(), // 
    SearchPattern.R_PATTERN_MATCH | SearchPattern.R_CASE_SENSITIVE, // 
    IJavaSearchConstants.CLASS_AND_INTERFACE, // 
    scope, new TypeNameMatchRequestor() {

        @Override
        public void acceptTypeNameMatch(TypeNameMatch match) {
            IType type = (IType) match.getType();
            String typeSignature = AbstractTypeResolver.resolveJavaTypeSignature(type);
            JavaTypeInfo classInfo = new JavaTypeInfo();
            classInfo.setSignature(typeSignature);
            javaTypes.add(classInfo);
            try {
                classInfo.setKind(type.isInterface() ? JavaTypeKind.Interface : JavaTypeKind.Class);
            } catch (JavaModelException e) {
                LOGGER.log(Level.SEVERE, "Error while collecting Java Types for '" + packageName + " package and Java type '" + typeName + "'.", e);
            }
        }
    }, // 
    IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) BasicSearchEngine(org.eclipse.jdt.internal.core.search.BasicSearchEngine) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) JavaTypeInfo(com.redhat.qute.commons.JavaTypeInfo) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor) IType(org.eclipse.jdt.core.IType)

Example 37 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project quarkus-ls by redhat-developer.

the class TemplateDataSupport method search.

private static void search(IMember fieldOrMethod, TemplateDataVisitor visitor, IProgressMonitor monitor) throws CoreException {
    boolean searchInJavaProject = isSearchInJavaProject(fieldOrMethod);
    SearchEngine engine = new SearchEngine();
    SearchPattern pattern = SearchPattern.createPattern(fieldOrMethod, IJavaSearchConstants.REFERENCES);
    int searchScope = IJavaSearchScope.SOURCES;
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(true, new IJavaElement[] { searchInJavaProject ? fieldOrMethod.getJavaProject() : fieldOrMethod.getCompilationUnit() }, searchScope);
    engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object o = match.getElement();
            if (o instanceof IMethod) {
                IMethod method = (IMethod) o;
                visitor.setMethod(method);
                // Get the AST of the method declaration where template field of CheckedTemplate
                // method is referenced.
                CompilationUnit cu = getASTRoot(method.getCompilationUnit());
                ASTNode methodDeclarationAST = new NodeFinder(cu, method.getSourceRange().getOffset(), method.getSourceRange().getLength()).getCoveringNode();
                // Visit the body of the method declaration to collect method invocation of
                // temlate.data(param-name, param-type);
                methodDeclarationAST.accept(visitor);
            }
        }
    }, monitor);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) NodeFinder(org.eclipse.jdt.core.dom.NodeFinder) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IMethod(org.eclipse.jdt.core.IMethod)

Example 38 with SearchEngine

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

the class ClassSelectionDialog method findTypes.

private IType[] findTypes(String patternText, IProgressMonitor progressMonitor) {
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaProject[] { javaProject }, IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS | IJavaSearchScope.SYSTEM_LIBRARIES | IJavaSearchScope.APPLICATION_LIBRARIES);
    SearchPattern pattern = createSearchPattern(patternText);
    SearchParticipant[] participants = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
    ClassCollector collector = new ClassCollector();
    try {
        new SearchEngine().search(pattern, participants, scope, collector, progressMonitor);
    } catch (CoreException e) {
        logError(e);
        return new IType[0];
    }
    IType[] foundTypes = collector.getTypes().toArray(new IType[collector.getTypes().size()]);
    return foundTypes;
}
Also used : SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) SearchParticipant(org.eclipse.jdt.core.search.SearchParticipant) IType(org.eclipse.jdt.core.IType)

Example 39 with SearchEngine

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

the class AddImportsOperation method findAllTypes.

/*
	 * Finds a type by the simple name.
	 */
private TypeNameMatch[] findAllTypes(String simpleTypeName, IJavaSearchScope searchScope, SimpleName nameNode, IProgressMonitor monitor) throws JavaModelException {
    boolean is50OrHigher = JavaModelUtil.is50OrHigher(fCompilationUnit.getJavaProject());
    int typeKinds = SimilarElementsRequestor.ALL_TYPES;
    if (nameNode != null) {
        typeKinds = ASTResolving.getPossibleTypeKinds(nameNode, is50OrHigher);
    }
    ArrayList<TypeNameMatch> typeInfos = new ArrayList<TypeNameMatch>();
    TypeNameMatchCollector requestor = new TypeNameMatchCollector(typeInfos);
    int matchMode = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
    new SearchEngine().searchAllTypeNames(null, matchMode, simpleTypeName.toCharArray(), matchMode, getSearchForConstant(typeKinds), searchScope, requestor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
    ArrayList<TypeNameMatch> typeRefsFound = new ArrayList<TypeNameMatch>(typeInfos.size());
    for (int i = 0, len = typeInfos.size(); i < len; i++) {
        TypeNameMatch curr = typeInfos.get(i);
        if (curr.getPackageName().length() > 0) {
            // do not suggest imports from the default package
            if (isOfKind(curr, typeKinds, is50OrHigher) && isVisible(curr)) {
                typeRefsFound.add(curr);
            }
        }
    }
    return typeRefsFound.toArray(new TypeNameMatch[typeRefsFound.size()]);
}
Also used : SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) ArrayList(java.util.ArrayList) TypeNameMatchCollector(org.eclipse.jdt.internal.corext.util.TypeNameMatchCollector)

Example 40 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project che 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<ICompilationUnit>();
    HashSet<ICompilationUnit> declaringCUs = new HashSet<ICompilationUnit>(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.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)

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