List of usage examples for org.eclipse.jdt.internal.core.search BasicSearchEngine BasicSearchEngine
public BasicSearchEngine()
From source file:org.eclipse.jdt.core.search.SearchEngine.java
License:Open Source License
/** * Creates a new search engine. */ public SearchEngine() { this.basicEngine = new BasicSearchEngine(); }
From source file:org.eclipse.jdt.internal.core.JavaModelManager.java
License:Open Source License
private Map secondaryTypesSearching(IJavaProject project, boolean waitForIndexes, IProgressMonitor monitor, final PerProjectInfo projectInfo) throws JavaModelException { if (VERBOSE || BasicSearchEngine.VERBOSE) { StringBuffer buffer = new StringBuffer("JavaModelManager.secondaryTypesSearch("); //$NON-NLS-1$ buffer.append(project.getElementName()); buffer.append(','); buffer.append(waitForIndexes);/*from w ww . j a va 2 s. c o m*/ buffer.append(')'); Util.verbose(buffer.toString()); } final Hashtable secondaryTypes = new Hashtable(3); IRestrictedAccessTypeRequestor nameRequestor = new IRestrictedAccessTypeRequestor() { public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, AccessRestriction access) { String key = packageName == null ? "" : new String(packageName); //$NON-NLS-1$ HashMap types = (HashMap) secondaryTypes.get(key); if (types == null) types = new HashMap(3); types.put(new String(simpleTypeName), path); secondaryTypes.put(key, types); } }; // Build scope using prereq projects but only source folders IPackageFragmentRoot[] allRoots = project.getAllPackageFragmentRoots(); int length = allRoots.length, size = 0; IPackageFragmentRoot[] allSourceFolders = new IPackageFragmentRoot[length]; for (int i = 0; i < length; i++) { if (allRoots[i].getKind() == IPackageFragmentRoot.K_SOURCE) { allSourceFolders[size++] = allRoots[i]; } } if (size < length) { System.arraycopy(allSourceFolders, 0, allSourceFolders = new IPackageFragmentRoot[size], 0, size); } // Search all secondary types on scope new BasicSearchEngine().searchAllSecondaryTypeNames(allSourceFolders, nameRequestor, waitForIndexes, monitor); // Build types from paths Iterator packages = secondaryTypes.values().iterator(); while (packages.hasNext()) { HashMap types = (HashMap) packages.next(); Iterator names = types.entrySet().iterator(); while (names.hasNext()) { Map.Entry entry = (Map.Entry) names.next(); String typeName = (String) entry.getKey(); String path = (String) entry.getValue(); if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(path)) { IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(path)); ICompilationUnit unit = JavaModelManager.createCompilationUnitFrom(file, null); IType type = unit.getType(typeName); types.put(typeName, type); // replace stored path with type itself } else { names.remove(); } } } // Store result in per project info cache if still null or there's still an indexing cache (may have been set by another thread...) if (projectInfo.secondaryTypes == null || projectInfo.secondaryTypes.get(INDEXED_SECONDARY_TYPES) != null) { projectInfo.secondaryTypes = secondaryTypes; if (VERBOSE || BasicSearchEngine.VERBOSE) { System.out.print(Thread.currentThread() + " -> secondary paths stored in cache: "); //$NON-NLS-1$ System.out.println(); Iterator entries = secondaryTypes.entrySet().iterator(); while (entries.hasNext()) { Map.Entry entry = (Map.Entry) entries.next(); String qualifiedName = (String) entry.getKey(); Util.verbose(" - " + qualifiedName + '-' + entry.getValue()); //$NON-NLS-1$ } } } return projectInfo.secondaryTypes; }
From source file:org.eclipse.xtext.common.types.xtext.ui.JdtTypesProposalProvider.java
License:Open Source License
protected void searchAndCreateProposals(IJavaSearchScope scope, final ICompletionProposalFactory proposalFactory, ContentAssistContext context, EReference typeReference, final Filter filter, final IValueConverter<String> valueConverter, final ICompletionProposalAcceptor acceptor) throws JavaModelException { String prefix = context.getPrefix(); List<String> split = Strings.split(prefix, '.'); char[] typeName = null; char[] packageName = null; if (prefix.length() > 0 && !split.isEmpty()) { CharMatcher dotMatcher = CharMatcher.is('.'); if (Character.isUpperCase(split.get(split.size() - 1).charAt(0))) { typeName = split.get(split.size() - 1).toCharArray(); if (split.size() > 1) packageName = ("*" + dotMatcher .replaceFrom(prefix.substring(0, prefix.length() - (typeName.length + 1)), "*.") + "*") .toCharArray(); } else {/*ww w .j ava 2s .c o m*/ if (prefix.endsWith(".")) prefix = prefix.substring(0, prefix.length() - 1); packageName = ("*" + dotMatcher.replaceFrom(prefix, "*.") + "*").toCharArray(); } } IScope typeScope = null; if (context.getCurrentModel() != null) { typeScope = scopeProvider.getScope(context.getCurrentModel(), typeReference); } final IReplacementTextApplier textApplier = createTextApplier(context, typeScope, qualifiedNameConverter, valueConverter); final ICompletionProposalAcceptor scopeAware = textApplier != null ? new ICompletionProposalAcceptor.Delegate(acceptor) { @Override public void accept(ICompletionProposal proposal) { if (proposal instanceof ConfigurableCompletionProposal) { ((ConfigurableCompletionProposal) proposal).setTextApplier(textApplier); } super.accept(proposal); } } : acceptor; Builder contextBuilder = context.copy(); final PrefixMatcher original = context.getMatcher(); contextBuilder.setMatcher(new PrefixMatcher() { @Override public boolean isCandidateMatchingPrefix(String name, String prefix) { if (original.isCandidateMatchingPrefix(name, prefix)) return true; String nameWithoutDollars = name.replace('$', '.'); String prefixWithoutDollars = prefix.replace('$', '.'); final boolean nameOrPrefixHasDollars = (nameWithoutDollars != name) || (prefixWithoutDollars != prefix); if (nameOrPrefixHasDollars && original.isCandidateMatchingPrefix(nameWithoutDollars, prefixWithoutDollars)) return true; String sub = nameWithoutDollars; int delimiter = sub.indexOf('.'); while (delimiter != -1) { sub = sub.substring(delimiter + 1); delimiter = sub.indexOf('.'); if (delimiter == -1 || prefixWithoutDollars.length() > 0 && Character.isLowerCase(prefixWithoutDollars.charAt(0))) { if (original.isCandidateMatchingPrefix(sub, prefixWithoutDollars)) return true; } } return false; } }); final ContentAssistContext myContext = contextBuilder.toContext(); final IJvmTypeProvider jvmTypeProvider = jdtTypeProviderFactory .findOrCreateTypeProvider(context.getResource().getResourceSet()); final Set<String> filteredTypeNames = getDirtyTypeNames(); final Filter dirtyTypenameFilter = new ITypesProposalProvider.Filter() { @Override public boolean accept(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) { if (path == null || path.endsWith(".class") || path.endsWith(".java")) { // Java index match String identifier = getIdentifier(packageName, simpleTypeName, enclosingTypeNames); if (filteredTypeNames.contains(identifier)) { return false; } } return true; } @Override public int getSearchFor() { return filter.getSearchFor(); } }; BasicSearchEngine searchEngine = new BasicSearchEngine(); searchEngine.searchAllTypeNames(packageName, SearchPattern.R_PATTERN_MATCH, typeName, SearchPattern.R_PREFIX_MATCH | SearchPattern.R_CAMELCASE_MATCH, filter.getSearchFor(), scope, new IRestrictedAccessTypeRequestor() { @Override public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, AccessRestriction access) { if (dirtyTypenameFilter.accept(modifiers, packageName, simpleTypeName, enclosingTypeNames, path) && filter.accept(modifiers, packageName, simpleTypeName, enclosingTypeNames, path) && (!checkAccessRestriction() || (access == null || access.getProblemId() != IProblem.ForbiddenReference && !access.ignoreIfBetter()))) { StringBuilder fqName = new StringBuilder( packageName.length + simpleTypeName.length + 1); if (packageName.length != 0) { fqName.append(packageName); fqName.append('.'); } for (char[] enclosingType : enclosingTypeNames) { /* * the JDT index sometimes yields enclosingTypeNames in the form * char[][] { { Outer$Middle } } * rather than * char[][] { { Outer }, { Middle } } * thus we create the fqName as the binary name and post process the proposal later on */ fqName.append(enclosingType); fqName.append('$'); } fqName.append(simpleTypeName); String fqNameAsString = fqName.toString(); createTypeProposal(fqNameAsString, modifiers, enclosingTypeNames.length > 0, proposalFactory, myContext, scopeAware, jvmTypeProvider, valueConverter); } } }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor() { @Override public boolean isCanceled() { return !acceptor.canAcceptMoreProposals(); } }); if (acceptor.canAcceptMoreProposals()) { Iterable<IEObjectDescription> allDirtyTypes = dirtyStateManager .getExportedObjectsByType(TypesPackage.Literals.JVM_TYPE); for (IEObjectDescription description : allDirtyTypes) { QualifiedName qualifiedName = description.getQualifiedName(); final int modifiers = getDirtyStateModifiers(context, description); if (filter.accept(modifiers, qualifiedName.skipLast(1).toString().toCharArray(), qualifiedName.getLastSegment().toCharArray(), new char[0][0], description.getEObjectURI().toPlatformString(true))) { String fqName = description.getQualifiedName().toString(); createTypeProposal(fqName, modifiers, fqName.indexOf('$') > 0, proposalFactory, myContext, scopeAware, jvmTypeProvider, valueConverter); } } } }
From source file:org.eclipse.xtext.xbase.ui.imports.InteractiveUnresolvedTypeResolver.java
License:Open Source License
protected void findCandidateTypes(final JvmDeclaredType contextType, final String typeSimpleName, IJavaSearchScope searchScope, final IAcceptor<JvmDeclaredType> acceptor) throws JavaModelException { BasicSearchEngine searchEngine = new BasicSearchEngine(); final IVisibilityHelper contextualVisibilityHelper = new ContextualVisibilityHelper(visibilityHelper, contextType);/*from www . ja v a 2 s . c o m*/ searchEngine.searchAllTypeNames(null, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE, typeSimpleName.toCharArray(), SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE, IJavaSearchConstants.TYPE, searchScope, new IRestrictedAccessTypeRequestor() { @Override public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, AccessRestriction access) { final String qualifiedTypeName = getQualifiedTypeName(packageName, enclosingTypeNames, simpleTypeName); if ((access == null || (access.getProblemId() != IProblem.ForbiddenReference && !access.ignoreIfBetter())) && !TypeFilter.isFiltered(packageName, simpleTypeName)) { JvmType importType = typeRefs.findDeclaredType(qualifiedTypeName, contextType.eResource()); if (importType instanceof JvmDeclaredType && contextualVisibilityHelper.isVisible((JvmDeclaredType) importType)) { acceptor.accept((JvmDeclaredType) importType); } } } }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor()); }
From source file:org.summer.dsl.model.types.xtext.ui.JdtTypesProposalProvider.java
License:Open Source License
protected void searchAndCreateProposals(IJavaSearchScope scope, final ICompletionProposalFactory proposalFactory, ContentAssistContext context, EReference typeReference, final Filter filter, final IValueConverter<String> valueConverter, final ICompletionProposalAcceptor acceptor) throws JavaModelException { String prefix = context.getPrefix(); List<String> split = Strings.split(prefix, '.'); char[] typeName = null; char[] packageName = null; if (prefix.length() > 0 && !split.isEmpty()) { CharMatcher dotMatcher = CharMatcher.is('.'); if (Character.isUpperCase(split.get(split.size() - 1).charAt(0))) { typeName = split.get(split.size() - 1).toCharArray(); if (split.size() > 1) packageName = ("*" + dotMatcher .replaceFrom(prefix.substring(0, prefix.length() - (typeName.length + 1)), "*.") + "*") .toCharArray(); } else {// ww w . java 2s. c o m if (prefix.endsWith(".")) prefix = prefix.substring(0, prefix.length() - 1); packageName = ("*" + dotMatcher.replaceFrom(prefix, "*.") + "*").toCharArray(); } } IScope typeScope = null; if (context.getCurrentModel() != null) { typeScope = scopeProvider.getScope(context.getCurrentModel(), typeReference); } final IReplacementTextApplier textApplier = createTextApplier(context, typeScope, qualifiedNameConverter, valueConverter); final ICompletionProposalAcceptor scopeAware = new ICompletionProposalAcceptor.Delegate(acceptor) { @Override public void accept(ICompletionProposal proposal) { if (proposal instanceof ConfigurableCompletionProposal) { ((ConfigurableCompletionProposal) proposal).setTextApplier(textApplier); } super.accept(proposal); } }; Builder contextBuilder = context.copy(); final PrefixMatcher original = context.getMatcher(); contextBuilder.setMatcher(new PrefixMatcher() { @Override public boolean isCandidateMatchingPrefix(String name, String prefix) { if (original.isCandidateMatchingPrefix(name, prefix)) return true; String withoutDollars = name.replace('$', '.'); String prefixWithoutDollars = prefix.replace('$', '.'); if ((withoutDollars != name || prefixWithoutDollars != prefix) && original.isCandidateMatchingPrefix(withoutDollars, prefixWithoutDollars)) return true; String sub = withoutDollars; int delimiter = sub.indexOf('.'); while (delimiter != -1) { sub = sub.substring(delimiter + 1); delimiter = sub.indexOf('.'); if (delimiter == -1 || prefixWithoutDollars.length() > 0 && Character.isLowerCase(prefixWithoutDollars.charAt(0))) { if (original.isCandidateMatchingPrefix(sub, prefixWithoutDollars)) return true; } } return false; } }); final ContentAssistContext myContext = contextBuilder.toContext(); final IJvmTypeProvider jvmTypeProvider = jdtTypeProviderFatory .findOrCreateTypeProvider(context.getResource().getResourceSet()); BasicSearchEngine searchEngine = new BasicSearchEngine(); searchEngine.searchAllTypeNames(packageName, SearchPattern.R_PATTERN_MATCH, typeName, SearchPattern.R_PREFIX_MATCH | SearchPattern.R_CAMELCASE_MATCH, filter.getSearchFor(), scope, new IRestrictedAccessTypeRequestor() { public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, AccessRestriction access) { if (filter.accept(modifiers, packageName, simpleTypeName, enclosingTypeNames, path) && (!checkAccessRestriction() || (access == null || access.getProblemId() != IProblem.ForbiddenReference && !access.ignoreIfBetter()))) { StringBuilder fqName = new StringBuilder( packageName.length + simpleTypeName.length + 1); if (packageName.length != 0) { fqName.append(packageName); fqName.append('.'); } for (char[] enclosingType : enclosingTypeNames) { /* * the JDT index sometimes yields enclosingTypeNames in the form * char[][] { { Outer$Middle } } * rather than * char[][] { { Outer }, { Middle } } * thus we create the fqName as the binary name and post process the proposal later on */ fqName.append(enclosingType); fqName.append('$'); } fqName.append(simpleTypeName); String fqNameAsString = fqName.toString(); createTypeProposal(fqNameAsString, modifiers, enclosingTypeNames.length > 0, proposalFactory, myContext, scopeAware, jvmTypeProvider, valueConverter); } } }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor() { @Override public boolean isCanceled() { return !acceptor.canAcceptMoreProposals(); } }); if (acceptor.canAcceptMoreProposals()) { Iterable<IEObjectDescription> allDirtyTypes = dirtyStateManager .getExportedObjectsByType(TypesPackage.Literals.JVM_TYPE); for (IEObjectDescription description : allDirtyTypes) { QualifiedName qualifiedName = description.getQualifiedName(); if (filter.accept(Flags.AccPublic, qualifiedName.skipLast(1).toString().toCharArray(), qualifiedName.getLastSegment().toCharArray(), new char[0][0], description.getEObjectURI().toPlatformString(true))) { String fqName = description.getQualifiedName().toString(); createTypeProposal(fqName, Flags.AccPublic, false, proposalFactory, myContext, scopeAware, jvmTypeProvider, valueConverter); } } } }
From source file:org.summer.dsl.xbase.ui.imports.InteractiveUnresolvedTypeResolver.java
License:Open Source License
protected void findCandidateTypes(final JvmDeclaredType contextType, final String typeSimpleName, IJavaSearchScope searchScope, final IAcceptor<JvmDeclaredType> acceptor) throws JavaModelException { BasicSearchEngine searchEngine = new BasicSearchEngine(); searchEngine.searchAllTypeNames(null, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE, typeSimpleName.toCharArray(), SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE, IJavaSearchConstants.TYPE, searchScope, new IRestrictedAccessTypeRequestor() { public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, AccessRestriction access) { final String qualifiedTypeName = getQualifiedTypeName(packageName, enclosingTypeNames, simpleTypeName); if (access == null || (access.getProblemId() != IProblem.ForbiddenReference && !access.ignoreIfBetter())) { JvmType importType = typeRefs.findDeclaredType(qualifiedTypeName, contextType.eResource()); if (importType instanceof JvmDeclaredType && visibilityService.isVisible((JvmDeclaredType) importType, contextType)) { acceptor.accept((JvmDeclaredType) importType); }/*from ww w . j a va2 s. c om*/ } } }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor()); }
From source file:org.summer.dsl.xbase.ui.quickfix.JavaTypeQuickfixes.java
License:Open Source License
protected void createImportProposals(final JvmDeclaredType contextType, final Issue issue, String typeName, IJavaSearchScope searchScope, final IssueResolutionAcceptor acceptor) throws JavaModelException { if (contextType != null) { final IVisibilityHelper visibilityHelper = getVisibilityHelper(contextType); final Pair<String, String> packageAndType = typeNameGuesser.guessPackageAndTypeName(contextType, typeName);/*from w w w. ja va2 s . c o m*/ final String wantedPackageName = packageAndType.getFirst(); BasicSearchEngine searchEngine = new BasicSearchEngine(); final char[] wantedPackageChars = (isEmpty(wantedPackageName)) ? null : wantedPackageName.toCharArray(); final String wantedTypeName = packageAndType.getSecond(); searchEngine.searchAllTypeNames(wantedPackageChars, SearchPattern.R_EXACT_MATCH, wantedTypeName.toCharArray(), SearchPattern.R_EXACT_MATCH, IJavaSearchConstants.TYPE, searchScope, new IRestrictedAccessTypeRequestor() { public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, AccessRestriction access) { final String qualifiedTypeName = getQualifiedTypeName(packageName, enclosingTypeNames, simpleTypeName); if (access == null || (access.getProblemId() != IProblem.ForbiddenReference && !access.ignoreIfBetter())) { JvmType importType = services.getTypeReferences() .findDeclaredType(qualifiedTypeName, contextType); if (importType instanceof JvmDeclaredType && visibilityHelper.isVisible((JvmDeclaredType) importType)) { StringBuilder label = new StringBuilder("Import '"); label.append(simpleTypeName); label.append("' ("); label.append(packageName); if (enclosingTypeNames != null && enclosingTypeNames.length > 0) { for (char[] enclosingTypeName : enclosingTypeNames) { label.append("."); label.append(enclosingTypeName); } } label.append(")"); acceptor.accept(issue, label.toString(), label.toString(), "impc_obj.gif", new ISemanticModification() { public void apply(EObject element, IModificationContext context) throws Exception { ReplacingAppendable appendable = appendableFactory.create( context.getXtextDocument(), (XtextResource) element.eResource(), 0, 0); appendable.append(services.getTypeReferences() .findDeclaredType(qualifiedTypeName, element)); appendable.insertNewImports(); } }, jdtTypeRelevance.getRelevance(qualifiedTypeName, wantedTypeName) + 100); } } } }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor()); } }
From source file:org.summer.dsl.xbase.ui.quickfix.JavaTypeQuickfixes.java
License:Open Source License
protected boolean createConstructorProposals(final JvmDeclaredType contextType, final Issue issue, String typeName, IJavaSearchScope searchScope, final IssueResolutionAcceptor acceptor) throws JavaModelException { final boolean[] result = new boolean[] { false }; if (contextType != null) { final IVisibilityHelper visibilityHelper = getVisibilityHelper(contextType); final Pair<String, String> packageAndType = typeNameGuesser.guessPackageAndTypeName(contextType, typeName);/*from w w w. j av a2 s .co m*/ final String wantedPackageName = packageAndType.getFirst(); final String wantedTypeName = packageAndType.getSecond(); BasicSearchEngine searchEngine = new BasicSearchEngine(); final char[] wantedPackageChars = (isEmpty(wantedPackageName)) ? null : wantedPackageName.toCharArray(); searchEngine.searchAllTypeNames(wantedPackageChars, SearchPattern.R_EXACT_MATCH, wantedTypeName.toCharArray(), SearchPattern.R_EXACT_MATCH, IJavaSearchConstants.TYPE, searchScope, new IRestrictedAccessTypeRequestor() { public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, AccessRestriction access) { final String qualifiedTypeName = getQualifiedTypeName(packageName, enclosingTypeNames, simpleTypeName); if (access == null || (access.getProblemId() != IProblem.ForbiddenReference && !access.ignoreIfBetter())) { JvmType importType = services.getTypeReferences() .findDeclaredType(qualifiedTypeName, contextType); if (importType instanceof JvmDeclaredType && visibilityHelper.isVisible((JvmDeclaredType) importType)) { result[0] = true; StringBuilder label = new StringBuilder("Change to constructor call 'new "); label.append(simpleTypeName); label.append("()'"); if (!equal(wantedPackageName, new String(packageName))) { label.append(" ("); label.append(packageName); if (enclosingTypeNames != null && enclosingTypeNames.length > 0) { for (char[] enclosingTypeName : enclosingTypeNames) { label.append("."); label.append(enclosingTypeName); } } label.append(")"); } acceptor.accept(issue, label.toString(), label.toString(), "impc_obj.gif", new ISemanticModification() { public void apply(EObject element, IModificationContext context) throws Exception { IXtextDocument document = context.getXtextDocument(); DocumentRewriter rewriter = rewriterFactory.create(document, (XtextResource) element.eResource()); final int typeEndOffset = document.get().indexOf(wantedTypeName, issue.getOffset() + wantedPackageName.length()) + wantedTypeName.length(); final Section section = rewriter.newSection(issue.getOffset(), typeEndOffset - issue.getOffset()); section.append(services.getTypeReferences() .findDeclaredType(qualifiedTypeName, element)); section.append("()"); final TextEdit textEdit = replaceConverter .convertToTextEdit(rewriter.getChanges()); textEdit.apply(document); } }, jdtTypeRelevance.getRelevance(qualifiedTypeName, wantedTypeName) + 100); } } } }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor()); } return result[0]; }