Search in sources :

Example 86 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project liferay-ide by liferay.

the class JSPMarkerResolutionGenerator method _findTypes.

private List<IType> _findTypes(IJavaProject javaProject, String typeName) {
    List<IType> retval = Collections.emptyList();
    try {
        IType type = javaProject.findType(typeName);
        if (type != null) {
            TypeInProjectRequestor requestor = new TypeInProjectRequestor();
            IJavaSearchScope scope = SearchEngine.createStrictHierarchyScope(javaProject, type, true, false, null);
            SearchPattern search = SearchPattern.createPattern("*", IJavaSearchConstants.CLASS, IJavaSearchConstants.DECLARATIONS, 0);
            new SearchEngine().search(search, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, new NullProgressMonitor());
            retval = requestor.getResults();
        }
    } catch (Exception e) {
    }
    return retval;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) CoreException(org.eclipse.core.runtime.CoreException) IType(org.eclipse.jdt.core.IType)

Example 87 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project liferay-ide by liferay.

the class HierarchyTypeClassNameExtractor method doExtractClassNames.

@Override
protected String[] doExtractClassNames(Node node, IFile file, String pathForClass, String findByAttrName, boolean findByParentNode, String xpathFactoryProviderId, NamespaceInfos namespaceInfo) throws XPathExpressionException {
    String[] retval = null;
    IJavaProject project = JavaCore.create(file.getProject());
    try {
        IType type = project.findType(_typeName);
        if (type != null) {
            TypeNameRequestor requestor = new TypeNameRequestor();
            IJavaSearchScope scope = SearchEngine.createStrictHierarchyScope(project, type, true, false, null);
            SearchPattern search = SearchPattern.createPattern("*", IJavaSearchConstants.CLASS, IJavaSearchConstants.DECLARATIONS, 0);
            new SearchEngine().search(search, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, new NullProgressMonitor());
            retval = requestor.getResults().toArray(new String[0]);
        }
    } catch (Exception e) {
    }
    return retval;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IJavaProject(org.eclipse.jdt.core.IJavaProject) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) XPathExpressionException(javax.xml.xpath.XPathExpressionException) CoreException(org.eclipse.core.runtime.CoreException) IType(org.eclipse.jdt.core.IType)

Example 88 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project ch.hsr.ifs.cdttesting by IFS-HSR.

the class JumpToMostSpecificClassHandler method findType.

protected final IType findType(final IJavaProject project, String className) {
    final IType[] result = { null };
    final String dottedName = className.replace('$', '.');
    try {
        PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    if (project != null) {
                        result[0] = internalFindType(project, dottedName, new HashSet<IJavaProject>(), monitor);
                    }
                    if (result[0] == null) {
                        int lastDot = dottedName.lastIndexOf('.');
                        TypeNameMatchRequestor nameMatchRequestor = new TypeNameMatchRequestor() {

                            @Override
                            public void acceptTypeNameMatch(TypeNameMatch match) {
                                result[0] = match.getType();
                            }
                        };
                        new SearchEngine().searchAllTypeNames(lastDot >= 0 ? dottedName.substring(0, lastDot).toCharArray() : null, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE, (lastDot >= 0 ? dottedName.substring(lastDot + 1) : dottedName).toCharArray(), SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE, IJavaSearchConstants.TYPE, SearchEngine.createWorkspaceScope(), nameMatchRequestor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
                    }
                } catch (JavaModelException e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InvocationTargetException e) {
        JUnitPlugin.log(e);
    } catch (InterruptedException e) {
    // user cancelled
    }
    return result[0];
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor) InvocationTargetException(java.lang.reflect.InvocationTargetException) IType(org.eclipse.jdt.core.IType) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IJavaProject(org.eclipse.jdt.core.IJavaProject) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch)

Example 89 with SearchEngine

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

the class JavaTypeQuickfixes method getImportedTypesScope.

protected IScope getImportedTypesScope(EObject model, final String misspelled, final IScope actualScope, IJavaSearchScope scope) {
    if (scope == null) {
        return IScope.NULLSCOPE;
    }
    try {
        final Set<String> visiblePackages = importsConfiguration.getImplicitlyImportedPackages((XtextResource) model.eResource());
        final Set<String> importedTypes = Sets.newHashSet();
        final Set<String> seen = Sets.newHashSet();
        XImportSection importSection = importsConfiguration.getImportSection((XtextResource) model.eResource());
        if (importSection != null) {
            parseImportSection(importSection, new IAcceptor<String>() {

                @Override
                public void accept(String t) {
                    visiblePackages.add(t);
                }
            }, new IAcceptor<String>() {

                @Override
                public void accept(String t) {
                    importedTypes.add(t);
                }
            });
        }
        SearchEngine searchEngine = new SearchEngine();
        final List<IEObjectDescription> validProposals = Lists.newArrayList();
        for (String importedType : importedTypes) {
            if (validProposals.size() <= 5 && seen.add(importedType)) {
                int dot = importedType.lastIndexOf('.');
                if (dot != -1) {
                    importedType = importedType.substring(dot + 1);
                }
                if (isSimilarTypeName(misspelled, importedType)) {
                    QualifiedName qualifiedName = qualifiedNameConverter.toQualifiedName(importedType);
                    for (IEObjectDescription element : actualScope.getElements(qualifiedName)) {
                        validProposals.add(new AliasedEObjectDescription(qualifiedName, element));
                        break;
                    }
                }
            }
        }
        try {
            for (String visiblePackage : visiblePackages) {
                if (validProposals.size() <= 5) {
                    searchEngine.searchAllTypeNames(visiblePackage.toCharArray(), SearchPattern.R_EXACT_MATCH, null, SearchPattern.R_EXACT_MATCH, IJavaSearchConstants.TYPE, scope, new TypeNameRequestor() {

                        @Override
                        public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) {
                            StringBuilder typeNameBuilder = new StringBuilder(simpleTypeName.length);
                            for (char[] enclosingType : enclosingTypeNames) {
                                typeNameBuilder.append(enclosingType);
                                typeNameBuilder.append('.');
                            }
                            typeNameBuilder.append(simpleTypeName);
                            String typeName = typeNameBuilder.toString();
                            if (isSimilarTypeName(misspelled, typeName)) {
                                String fqNameAsString = getQualifiedTypeName(packageName, enclosingTypeNames, simpleTypeName);
                                if (seen.add(fqNameAsString)) {
                                    QualifiedName qualifiedName = qualifiedNameConverter.toQualifiedName(typeName);
                                    for (IEObjectDescription element : actualScope.getElements(qualifiedName)) {
                                        validProposals.add(new AliasedEObjectDescription(qualifiedName, element));
                                        break;
                                    }
                                }
                            }
                        }
                    }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor() {

                        @Override
                        public boolean isCanceled() {
                            return validProposals.size() > 5;
                        }
                    });
                }
            }
        } catch (OperationCanceledException exc) {
        // enough proposals
        }
        return new SimpleScope(validProposals);
    } catch (JavaModelException jme) {
        return IScope.NULLSCOPE;
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) AliasedEObjectDescription(org.eclipse.xtext.resource.impl.AliasedEObjectDescription) JavaModelException(org.eclipse.jdt.core.JavaModelException) XImportSection(org.eclipse.xtext.xtype.XImportSection) SimpleScope(org.eclipse.xtext.scoping.impl.SimpleScope) QualifiedName(org.eclipse.xtext.naming.QualifiedName) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameRequestor(org.eclipse.jdt.core.search.TypeNameRequestor)

Example 90 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project egradle by de-jcup.

the class JDTDataAccess method scanForJavaType.

/**
 * Scan for package names
 *
 * @param type
 * @param scope
 * @param packageNames - can be <code>null</code>
 * @return list with java types
 */
public List<String> scanForJavaType(String type, IJavaSearchScope scope, String... packageNames) {
    SearchEngine engine = new SearchEngine();
    List<String> foundList = new ArrayList<>();
    try {
        // int packageFlags=0;
        // String packPattern = "org.gradle.api";
        // String typePattern = "Project";
        // int matchRule;
        // int elementKind;
        // IJavaSearchScope searchScope;
        // TypeNameRequestor requestor;
        // IProgressMonitor progressMonitor;
        // engine.searchAllTypeNames((packPattern == null) ? null :
        // packPattern.toCharArray(),
        // packageFlags,
        // typePattern.toCharArray(), matchRule,
        // IJavaElement.TYPE, searchScope, requestor, 3,
        // progressMonitor);
        List<char[]> groovyAutomaticImports = new ArrayList<>();
        if ("BigDecimal".equals(type) || "BigInteger".equals(type)) {
            addImport(groovyAutomaticImports, "java.math");
        } else {
            addImport(groovyAutomaticImports, "java.lang");
            addImport(groovyAutomaticImports, "java.util");
            addImport(groovyAutomaticImports, "java.net");
            addImport(groovyAutomaticImports, "java.io");
            if (packageNames != null && packageNames.length > 0) {
                for (String packageName : packageNames) {
                    if (packageName != null && !packageName.isEmpty()) {
                        addImport(groovyAutomaticImports, packageName);
                    }
                }
            }
        }
        char[][] qualifications = new char[groovyAutomaticImports.size()][];
        for (int i = 0; i < groovyAutomaticImports.size(); i++) {
            qualifications[i] = groovyAutomaticImports.get(i);
        }
        char[][] typeNames = new char[1][];
        typeNames[0] = type.toCharArray();
        TypeNameRequestor nameRequestor = new TypeNameRequestor() {

            @Override
            public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) {
                String simpleName = new String(simpleTypeName);
                String simplePackageName = new String(packageName);
                foundList.add(simplePackageName + "." + simpleName);
            }
        };
        int policiy = IJavaSearchConstants.FORCE_IMMEDIATE_SEARCH;
        IProgressMonitor progressMonitor = new NullProgressMonitor();
        engine.searchAllTypeNames(qualifications, typeNames, scope, nameRequestor, policiy, progressMonitor);
    } catch (JavaModelException e) {
        EditorUtil.INSTANCE.logError("Was not able to search all type names", e);
    }
    return foundList;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) ArrayList(java.util.ArrayList) TypeNameRequestor(org.eclipse.jdt.core.search.TypeNameRequestor)

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