Search in sources :

Example 11 with TypeNameMatch

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

the class ImportOrganizeTest method createQuery.

protected IChooseImportQuery createQuery(final String name, final String[] choices, final int[] nEntries) {
    return new IChooseImportQuery() {

        public TypeNameMatch[] chooseImports(TypeNameMatch[][] openChoices, ISourceRange[] ranges) {
            assertTrue(name + "-query-nchoices1", choices.length == openChoices.length);
            assertTrue(name + "-query-nchoices2", nEntries.length == openChoices.length);
            for (int i = 0; i < nEntries.length; i++) {
                assertTrue(name + "-query-cnt" + i, openChoices[i].length == nEntries[i]);
            }
            TypeNameMatch[] res = new TypeNameMatch[openChoices.length];
            for (int i = 0; i < openChoices.length; i++) {
                TypeNameMatch[] selection = openChoices[i];
                assertNotNull(name + "-query-setset" + i, selection);
                assertTrue(name + "-query-setlen" + i, selection.length > 0);
                TypeNameMatch found = null;
                for (int k = 0; k < selection.length; k++) {
                    if (selection[k].getFullyQualifiedName().equals(choices[i])) {
                        found = selection[k];
                    }
                }
                assertNotNull(name + "-query-notfound" + i, found);
                res[i] = found;
            }
            return res;
        }
    };
}
Also used : TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) IChooseImportQuery(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)

Example 12 with TypeNameMatch

use of org.eclipse.jdt.core.search.TypeNameMatch in project AutoRefactor by JnRouvignac.

the class ReplaceQualifiedNamesBySimpleNamesRefactoring method importTypesFromPackage.

private void importTypesFromPackage(final String pkgName, ASTNode node) {
    final TypeNameMatchRequestor importTypeCollector = new TypeNameMatchRequestor() {

        @Override
        public void acceptTypeNameMatch(TypeNameMatch typeNameMatch) {
            final boolean isTopLevelType = typeNameMatch.getType().getDeclaringType() == null;
            if (isTopLevelType) {
                if (!pkgName.equals(typeNameMatch.getPackageName())) {
                    // sanity check failed
                    throw new IllegalStateException("Expected package '" + typeNameMatch.getPackageName() + "' to be equal to '" + pkgName + "'");
                }
                QName qname = QName.valueOf(typeNameMatch.getFullyQualifiedName());
                types.addName(FQN.fromImport(qname, true));
            }
        }
    };
    try {
        final SearchEngine searchEngine = new SearchEngine();
        searchEngine.searchAllTypeNames(// search in this package
        pkgName.toCharArray(), // search in this package
        R_EXACT_MATCH, // do not filter by type name
        null, // do not filter by type name
        R_EXACT_MATCH, // look for all java types (class, interfaces, enums, etc.)
        TYPE, // search everywhere
        createWorkspaceScope(), importTypeCollector, // wait in case the indexer is indexing
        WAIT_UNTIL_READY_TO_SEARCH, ctx.getProgressMonitor());
    } catch (JavaModelException e) {
        throw new UnhandledException(node, e);
    }
}
Also used : UnhandledException(org.autorefactor.util.UnhandledException) JavaModelException(org.eclipse.jdt.core.JavaModelException) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor)

Example 13 with TypeNameMatch

use of org.eclipse.jdt.core.search.TypeNameMatch in project bndtools by bndtools.

the class SuperInterfaceSelectionDialog method addSelectedInterfaces.

/*
     * Adds selected interfaces to the list.
     */
private void addSelectedInterfaces() {
    StructuredSelection selection = getSelectedItems();
    if (selection == null)
        return;
    for (Iterator<?> iter = selection.iterator(); iter.hasNext(); ) {
        Object obj = iter.next();
        if (obj instanceof TypeNameMatch) {
            accessedHistoryItem(obj);
            TypeNameMatch type = (TypeNameMatch) obj;
            String qualifiedName = getNameWithTypeParameters(type.getType());
            String message;
            if (fTypeWizardPage.addSuperInterface(qualifiedName)) {
                message = Messages.format(NewWizardMessages.SuperInterfaceSelectionDialog_interfaceadded_info, BasicElementLabels.getJavaElementName(qualifiedName));
            } else {
                message = Messages.format(NewWizardMessages.SuperInterfaceSelectionDialog_interfacealreadyadded_info, BasicElementLabels.getJavaElementName(qualifiedName));
            }
            updateStatus(new StatusInfo(IStatus.INFO, message));
        }
    }
}
Also used : TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) StatusInfo(org.eclipse.jdt.internal.ui.dialogs.StatusInfo) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection)

Example 14 with TypeNameMatch

use of org.eclipse.jdt.core.search.TypeNameMatch in project sts4 by spring-projects.

the class OpenFullyQualifiedNameInEditor method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    String fqName = event.getParameter(FQ_NAME);
    String projectName = event.getParameter(PROJECT_NAME);
    String packageName = "";
    String typeName = fqName;
    int idx = fqName.lastIndexOf('.');
    if (idx >= 0) {
        packageName = fqName.substring(0, idx);
        typeName = idx < fqName.length() - 1 ? fqName.substring(idx + 1) : "";
    }
    if (!typeName.isEmpty()) {
        SearchEngine engine = new SearchEngine((WorkingCopyOwner) null);
        List<TypeNameMatch> matches = new ArrayList<>();
        String searchedTypeName = getSearchedTypeName(typeName);
        final boolean isInnerType = searchedTypeName != typeName;
        TypeNameMatchRequestor requestor = new TypeNameMatchRequestor() {

            @Override
            public void acceptTypeNameMatch(TypeNameMatch match) {
                if (isInnerType) {
                    if (match.getFullyQualifiedName().equals(fqName.replace("$", "."))) {
                        matches.add(match);
                    }
                } else {
                    matches.add(match);
                }
            }
        };
        try {
            IJavaSearchScope searchScope = createSearchScope(projectName);
            engine.searchAllTypeNames(packageName.toCharArray(), SearchPattern.R_EXACT_MATCH, searchedTypeName.toCharArray(), SearchPattern.R_EXACT_MATCH, IJavaSearchConstants.TYPE, searchScope, requestor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor());
            if (matches.isEmpty()) {
                BootLanguageServerPlugin.getDefault().getLog().log(new Status(IStatus.WARNING, BootLanguageServerPlugin.ID, "Cannot find type: " + fqName));
            } else {
                if (matches.size() > 1) {
                    BootLanguageServerPlugin.getDefault().getLog().log(new Status(IStatus.WARNING, BootLanguageServerPlugin.ID, "More than one type is defined for: " + fqName));
                }
                try {
                    IType type = matches.get(0).getType();
                    IEditorPart editorPart = JavaUI.openInEditor(type);
                    JavaUI.revealInEditor(editorPart, (IJavaElement) type);
                } catch (JavaModelException ex) {
                    // $NON-NLS-1$
                    throw new ExecutionException("Error opening java element in editor", ex);
                } catch (PartInitException ex) {
                    // $NON-NLS-1$
                    throw new ExecutionException("Error opening java element in editor", ex);
                }
            }
        } catch (JavaModelException e) {
            BootLanguageServerPlugin.getDefault().getLog().log(e.getStatus());
        }
    }
    return null;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) ArrayList(java.util.ArrayList) IEditorPart(org.eclipse.ui.IEditorPart) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor) IType(org.eclipse.jdt.core.IType) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) PartInitException(org.eclipse.ui.PartInitException) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 15 with TypeNameMatch

use of org.eclipse.jdt.core.search.TypeNameMatch in project AutoRefactor by JnRouvignac.

the class ObsoleteSimpleNameRatherThanQualifiedNameCleanUp method importTypesFromPackage.

private void importTypesFromPackage(final String pkgName, final ASTNode visited) {
    TypeNameMatchRequestor importTypeCollector = new TypeNameMatchRequestor() {

        @Override
        public void acceptTypeNameMatch(final TypeNameMatch typeNameMatch) {
            boolean isTopLevelType = typeNameMatch.getType().getDeclaringType() == null;
            if (isTopLevelType) {
                if (!pkgName.equals(typeNameMatch.getPackageName())) {
                    // Sanity check failed
                    throw new IllegalStateException(// $NON-NLS-1$
                    "Expected package '" + typeNameMatch.getPackageName() + "' to be equal to '" + pkgName + // $NON-NLS-1$ //$NON-NLS-2$
                    "'");
                }
                QName qname = QName.valueOf(typeNameMatch.getFullyQualifiedName());
                types.addName(FQN.fromImport(qname, true));
            }
        }
    };
    try {
        SearchEngine searchEngine = new SearchEngine();
        // search in this package
        searchEngine.searchAllTypeNames(// search in this package
        pkgName.toCharArray(), // search in this package
        SearchPattern.R_EXACT_MATCH, // do not filter by type name
        null, // do not filter by type name
        SearchPattern.R_EXACT_MATCH, // look for all java types (class, interfaces, enums, etc.)
        IBinding.TYPE, // search everywhere
        SearchEngine.createWorkspaceScope(), // wait in case the indexer is indexing
        importTypeCollector, // wait in case the indexer is indexing
        IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, cuRewrite.getProgressMonitor());
    } catch (JavaModelException e) {
        throw new UnhandledException(visited, e);
    }
}
Also used : UnhandledException(org.autorefactor.util.UnhandledException) JavaModelException(org.eclipse.jdt.core.JavaModelException) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor)

Aggregations

TypeNameMatch (org.eclipse.jdt.core.search.TypeNameMatch)15 ArrayList (java.util.ArrayList)9 SearchEngine (org.eclipse.jdt.core.search.SearchEngine)8 JavaModelException (org.eclipse.jdt.core.JavaModelException)6 TypeNameMatchRequestor (org.eclipse.jdt.core.search.TypeNameMatchRequestor)6 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)3 List (java.util.List)2 UnhandledException (org.autorefactor.util.UnhandledException)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)2 IType (org.eclipse.jdt.core.IType)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 IBinding (org.eclipse.jdt.core.dom.IBinding)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)2 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)2