org.eclipse.jdt.core.search.SearchRequestor Java Examples
The following examples show how to use
org.eclipse.jdt.core.search.SearchRequestor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaReferenceCodeMining.java From jdt-codemining with Eclipse Public License 1.0 | 6 votes |
/** * Return the number of references for the given java element. * * @param element the java element. * @param monitor the monitor * @return he number of references for the given java element. * @throws JavaModelException throws when java error. * @throws CoreException throws when java error. */ private static long countReferences(IJavaElement element, IProgressMonitor monitor) throws JavaModelException, CoreException { if (element == null) { return 0; } final AtomicLong count = new AtomicLong(0); SearchPattern pattern = SearchPattern.createPattern(element, IJavaSearchConstants.REFERENCES); SearchEngine engine = new SearchEngine(); engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, createSearchScope(), new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { Object o = match.getElement(); if (o instanceof IJavaElement && ((IJavaElement) o).getAncestor(IJavaElement.COMPILATION_UNIT) != null) { count.incrementAndGet(); } } }, monitor); return count.get(); }
Example #2
Source File: RenamePackageProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * @param scope search scope * @param pm mrogress monitor * @return all package fragments in <code>scope</code> with same name as <code>fPackage</code>, excluding fPackage * @throws CoreException if search failed */ private IPackageFragment[] getNamesakePackages(IJavaSearchScope scope, IProgressMonitor pm) throws CoreException { SearchPattern pattern= SearchPattern.createPattern(fPackage.getElementName(), IJavaSearchConstants.PACKAGE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE); final HashSet<IPackageFragment> packageFragments= new HashSet<IPackageFragment>(); SearchRequestor requestor= new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { IJavaElement enclosingElement= SearchUtils.getEnclosingJavaElement(match); if (enclosingElement instanceof IPackageFragment) { IPackageFragment pack= (IPackageFragment) enclosingElement; if (! fPackage.equals(pack)) packageFragments.add(pack); } } }; new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm); return packageFragments.toArray(new IPackageFragment[packageFragments.size()]); }
Example #3
Source File: RenameMethodProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private IMethod[] searchForDeclarationsOfClashingMethods(IProgressMonitor pm) throws CoreException { final List<IMethod> results= new ArrayList<IMethod>(); SearchPattern pattern= createNewMethodPattern(); IJavaSearchScope scope= RefactoringScopeFactory.create(getMethod().getJavaProject()); SearchRequestor requestor= new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { Object method= match.getElement(); if (method instanceof IMethod) // check for bug 90138: [refactoring] [rename] Renaming method throws internal exception results.add((IMethod) method); else JavaPlugin.logErrorMessage("Unexpected element in search match: " + match.toString()); //$NON-NLS-1$ } }; new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm); return results.toArray(new IMethod[results.size()]); }
Example #4
Source File: RenameMethodProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private IType[] searchForOuterTypesOfReferences(IMethod[] newNameMethods, IProgressMonitor pm) throws CoreException { final Set<IType> outerTypesOfReferences= new HashSet<IType>(); 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)) return; // e.g. an IImportDeclaration for a static method import 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()]); }
Example #5
Source File: RenamePackageProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
/** * @param scope search scope * @param pm mrogress monitor * @return all package fragments in <code>scope</code> with same name as <code>fPackage</code>, excluding fPackage * @throws CoreException if search failed */ private IPackageFragment[] getNamesakePackages(IJavaSearchScope scope, IProgressMonitor pm) throws CoreException { SearchPattern pattern= SearchPattern.createPattern(fPackage.getElementName(), IJavaSearchConstants.PACKAGE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE); final HashSet<IPackageFragment> packageFragments= new HashSet<>(); SearchRequestor requestor= new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { IJavaElement enclosingElement= SearchUtils.getEnclosingJavaElement(match); if (enclosingElement instanceof IPackageFragment) { IPackageFragment pack= (IPackageFragment) enclosingElement; if (! fPackage.equals(pack)) { packageFragments.add(pack); } } } }; new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm); return packageFragments.toArray(new IPackageFragment[packageFragments.size()]); }
Example #6
Source File: RippleMethodFinder.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
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); }
Example #7
Source File: RenameMethodProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
private IMethod[] searchForDeclarationsOfClashingMethods(IProgressMonitor pm) throws CoreException { final List<IMethod> results= new ArrayList<>(); SearchPattern pattern= createNewMethodPattern(); IJavaSearchScope scope= RefactoringScopeFactory.create(getMethod().getJavaProject()); SearchRequestor requestor= new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { Object method= match.getElement(); if (method instanceof IMethod) { results.add((IMethod) method); } else { JavaLanguageServerPlugin.logError("Unexpected element in search match: " + match.toString()); //$NON-NLS-1$ } } }; new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm); return results.toArray(new IMethod[results.size()]); }
Example #8
Source File: MainMethodSearchEngine.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Searches for all main methods in the given scope. * Valid styles are IJavaElementSearchConstants.CONSIDER_BINARIES and * IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS * @param pm progress monitor * @param scope the search scope * @param style search style constants (see {@link IJavaElementSearchConstants}) * @return the types found * @throws CoreException */ public IType[] searchMainMethods(IProgressMonitor pm, IJavaSearchScope scope, int style) throws CoreException { List<IType> typesFound= new ArrayList<IType>(200); SearchPattern pattern= SearchPattern.createPattern("main(String[]) void", //$NON-NLS-1$ IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE); SearchRequestor requestor= new MethodCollector(typesFound, style); new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm); return typesFound.toArray(new IType[typesFound.size()]); }
Example #9
Source File: RippleMethodFinder2.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void findAllDeclarations(IProgressMonitor monitor, WorkingCopyOwner owner) throws CoreException { fDeclarations= new ArrayList<IMethod>(); class MethodRequestor extends SearchRequestor { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { IMethod method= (IMethod) match.getElement(); boolean isBinary= method.isBinary(); if (fBinaryRefs != null || ! (fExcludeBinaries && isBinary)) { fDeclarations.add(method); } if (isBinary && fBinaryRefs != null) { fDeclarationToMatch.put(method, match); } } } 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); SearchParticipant[] participants= SearchUtils.getDefaultSearchParticipants(); IJavaSearchScope scope= RefactoringScopeFactory.createRelatedProjectsScope(fMethod.getJavaProject(), IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.SYSTEM_LIBRARIES); MethodRequestor requestor= new MethodRequestor(); SearchEngine searchEngine= owner != null ? new SearchEngine(owner) : new SearchEngine(); searchEngine.search(pattern, participants, scope, requestor, monitor); }
Example #10
Source File: RefactoringSearchEngine.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
public static ICompilationUnit[] findAffectedCompilationUnits(SearchPattern pattern, IJavaSearchScope scope, final IProgressMonitor pm, RefactoringStatus status, final boolean tolerateInAccurateMatches) throws JavaModelException { boolean hasNonCuMatches = false; class ResourceSearchRequestor extends SearchRequestor { boolean hasPotentialMatches = false; Set<IResource> resources = new HashSet<>(5); private IResource fLastResource; @Override public void acceptSearchMatch(SearchMatch match) { if (!tolerateInAccurateMatches && match.getAccuracy() == SearchMatch.A_INACCURATE) { hasPotentialMatches = true; } if (fLastResource != match.getResource()) { fLastResource = match.getResource(); resources.add(fLastResource); } } } ResourceSearchRequestor requestor = new ResourceSearchRequestor(); try { new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm); } catch (CoreException e) { throw new JavaModelException(e); } List<IJavaElement> result = new ArrayList<>(requestor.resources.size()); for (Iterator<IResource> iter = requestor.resources.iterator(); iter.hasNext();) { IResource resource = iter.next(); IJavaElement element = JavaCore.create(resource); if (element instanceof ICompilationUnit) { result.add(element); } else { hasNonCuMatches = true; } } addStatusErrors(status, requestor.hasPotentialMatches, hasNonCuMatches); return result.toArray(new ICompilationUnit[result.size()]); }
Example #11
Source File: RippleMethodFinder2.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
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 (fBinaryRefs != null || ! (fExcludeBinaries && isBinary)) { fDeclarations.add(method); } if (isBinary && fBinaryRefs != null) { fDeclarationToMatch.put(method, match); } } } 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); SearchParticipant[] participants= SearchUtils.getDefaultSearchParticipants(); IJavaSearchScope scope= RefactoringScopeFactory.createRelatedProjectsScope(fMethod.getJavaProject(), IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.SYSTEM_LIBRARIES); MethodRequestor requestor= new MethodRequestor(); SearchEngine searchEngine= owner != null ? new SearchEngine(owner) : new SearchEngine(); searchEngine.search(pattern, participants, scope, requestor, monitor); }
Example #12
Source File: RenameMethodProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
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)) { return; // e.g. an IImportDeclaration for a static method import } 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()]); }
Example #13
Source File: JdtBasedConstructorScope.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
@Override protected Iterable<IEObjectDescription> internalGetAllElements() { IJavaProject javaProject = getTypeProvider().getJavaProject(); if (javaProject == null) return Collections.emptyList(); final List<IEObjectDescription> allScopedElements = Lists.newArrayListWithExpectedSize(25000); try { IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaProject }); SearchRequestor searchRequestor = new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { Object element = match.getElement(); if (element instanceof IMethod) { IMethod constructor = (IMethod) element; allScopedElements.add(createScopedElement(constructor)); } else if (element instanceof IType) { allScopedElements.add(createScopedElement((IType)element)); } } }; collectContents(searchScope, searchRequestor); } catch (CoreException e) { logger.error("CoreException when searching for constructors.", e); } return allScopedElements; }
Example #14
Source File: Utils.java From CogniCrypt with Eclipse Public License 2.0 | 5 votes |
/** * This method searches the passed project for the class that contains the main method. * * @param project Project that is searched * @param requestor Object that handles the search results */ public static void findMainMethodInCurrentProject(final IJavaProject project, final SearchRequestor requestor) { final SearchPattern sp = SearchPattern.createPattern("main", IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH); final SearchEngine se = new SearchEngine(); final SearchParticipant[] searchParticipants = new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()}; final IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project}); try { se.search(sp, searchParticipants, scope, requestor, null); } catch (final CoreException e) { Activator.getDefault().logError(e); } }
Example #15
Source File: CallerFinder.java From lapse-plus with GNU General Public License v3.0 | 5 votes |
public static Collection/*<MethodUnitPair>*/ findDeclarations(IProgressMonitor progressMonitor, String methodName, IJavaProject project, boolean isConstructor) { try { SearchRequestor searchRequestor = new MethodSearchRequestor.MethodDeclarationsSearchRequestor(); SearchEngine searchEngine = new SearchEngine(); IProgressMonitor monitor = new SubProgressMonitor( progressMonitor, 5, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK); monitor.beginTask("Searching for calls to " + methodName + (project != null ? " in " + project.getProject().getName() : ""), 100); IJavaSearchScope searchScope = getSearchScope(project); // This is kind of hacky: we need to make up a string name for the search to work right log("Looking for " + methodName); int matchType = !isConstructor ? IJavaSearchConstants.METHOD : IJavaSearchConstants.CONSTRUCTOR; SearchPattern pattern = SearchPattern.createPattern( methodName, matchType, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE ); searchEngine.search( pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, searchScope, searchRequestor, monitor ); if(searchRequestor instanceof MethodSearchRequestor.MethodDeclarationsSearchRequestor){ return ((MethodSearchRequestor.MethodDeclarationsSearchRequestor)searchRequestor).getMethodUnitPairs(); }else{ return ((MethodSearchRequestor.MethodReferencesSearchRequestor)searchRequestor).getMethodUnitPairs(); } } catch (CoreException e) { JavaPlugin.log(e); return new LinkedList(); } }
Example #16
Source File: JDTUtils.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
public static IJavaElement[] findElementsAtSelection(ITypeRoot unit, int line, int column, PreferenceManager preferenceManager, IProgressMonitor monitor) throws JavaModelException { if (unit == null || monitor.isCanceled()) { return null; } int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column); if (offset > -1) { return unit.codeSelect(offset, 0); } if (unit instanceof IClassFile) { IClassFile classFile = (IClassFile) unit; ContentProviderManager contentProvider = JavaLanguageServerPlugin.getContentProviderManager(); String contents = contentProvider.getSource(classFile, monitor); if (contents != null) { IDocument document = new Document(contents); try { offset = document.getLineOffset(line) + column; if (offset > -1) { String name = parse(contents, offset); if (name == null) { return null; } SearchPattern pattern = SearchPattern.createPattern(name, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_FULL_MATCH); IJavaSearchScope scope = createSearchScope(unit.getJavaProject(), preferenceManager); List<IJavaElement> elements = new ArrayList<>(); SearchRequestor requestor = new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) { if (match.getElement() instanceof IJavaElement) { elements.add((IJavaElement) match.getElement()); } } }; SearchEngine searchEngine = new SearchEngine(); searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, null); return elements.toArray(new IJavaElement[0]); } } catch (BadLocationException | CoreException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e); } } } return null; }
Example #17
Source File: JdtBasedConstructorScope.java From xtext-eclipse with Eclipse Public License 2.0 | 4 votes |
public void collectContents(IJavaSearchScope searchScope, SearchRequestor searchRequestor) throws CoreException { SearchPattern pattern = new ConstructorDeclarationPattern(null, null, SearchPattern.R_PREFIX_MATCH); new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), searchScope, searchRequestor, new NullProgressMonitor()); }
Example #18
Source File: ImplementationCollector.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private List<T> findMethodImplementations(IProgressMonitor monitor) throws CoreException { IMethod method = (IMethod) javaElement; try { if (cannotBeOverriddenMethod(method)) { return null; } } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Find method implementations failure ", e); return null; } CompilationUnit ast = CoreASTProvider.getInstance().getAST(typeRoot, CoreASTProvider.WAIT_YES, monitor); if (ast == null) { return null; } ASTNode node = NodeFinder.perform(ast, region.getOffset(), region.getLength()); ITypeBinding parentTypeBinding = null; if (node instanceof SimpleName) { ASTNode parent = node.getParent(); if (parent instanceof MethodInvocation) { Expression expression = ((MethodInvocation) parent).getExpression(); if (expression == null) { parentTypeBinding= Bindings.getBindingOfParentType(node); } else { parentTypeBinding = expression.resolveTypeBinding(); } } else if (parent instanceof SuperMethodInvocation) { // Directly go to the super method definition return Collections.singletonList(mapper.convert(method, 0, 0)); } else if (parent instanceof MethodDeclaration) { parentTypeBinding = Bindings.getBindingOfParentType(node); } } final IType receiverType = getType(parentTypeBinding); if (receiverType == null) { return null; } final List<T> results = new ArrayList<>(); try { String methodLabel = JavaElementLabelsCore.getElementLabel(method, JavaElementLabelsCore.DEFAULT_QUALIFIED); monitor.beginTask(Messages.format(JavaElementImplementationHyperlink_search_method_implementors, methodLabel), 10); SearchRequestor requestor = new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { if (match.getAccuracy() == SearchMatch.A_ACCURATE) { Object element = match.getElement(); if (element instanceof IMethod) { IMethod methodFound = (IMethod) element; if (!JdtFlags.isAbstract(methodFound)) { T result = mapper.convert(methodFound, match.getOffset(), match.getLength()); if (result != null) { results.add(result); } } } } } }; IJavaSearchScope hierarchyScope; if (receiverType.isInterface()) { hierarchyScope = SearchEngine.createHierarchyScope(method.getDeclaringType()); } else { if (isFullHierarchyNeeded(new SubProgressMonitor(monitor, 3), method, receiverType)) { hierarchyScope = SearchEngine.createHierarchyScope(receiverType); } else { boolean isMethodAbstract = JdtFlags.isAbstract(method); hierarchyScope = SearchEngine.createStrictHierarchyScope(null, receiverType, true, isMethodAbstract, null); } } int limitTo = IJavaSearchConstants.DECLARATIONS | IJavaSearchConstants.IGNORE_DECLARING_TYPE | IJavaSearchConstants.IGNORE_RETURN_TYPE; SearchPattern pattern = SearchPattern.createPattern(method, limitTo); Assert.isNotNull(pattern); SearchParticipant[] participants = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }; SearchEngine engine = new SearchEngine(); engine.search(pattern, participants, hierarchyScope, requestor, new SubProgressMonitor(monitor, 7)); if (monitor.isCanceled()) { throw new OperationCanceledException(); } } finally { monitor.done(); } return results; }
Example #19
Source File: ReferencesHandler.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
public List<Location> findReferences(ReferenceParams param, IProgressMonitor monitor) { final List<Location> locations = new ArrayList<>(); try { IJavaElement elementToSearch = JDTUtils.findElementAtSelection(JDTUtils.resolveTypeRoot(param.getTextDocument().getUri()), param.getPosition().getLine(), param.getPosition().getCharacter(), this.preferenceManager, monitor); if (elementToSearch == null) { return locations; } boolean includeClassFiles = preferenceManager.isClientSupportsClassFileContent(); SearchEngine engine = new SearchEngine(); SearchPattern pattern = SearchPattern.createPattern(elementToSearch, IJavaSearchConstants.REFERENCES); engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, createSearchScope(), new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { Object o = match.getElement(); if (o instanceof IJavaElement) { IJavaElement element = (IJavaElement) o; ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT); Location location = null; if (compilationUnit != null) { location = JDTUtils.toLocation(compilationUnit, match.getOffset(), match.getLength()); } else if (includeClassFiles) { IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE); if (cf != null && cf.getSourceRange() != null) { location = JDTUtils.toLocation(cf, match.getOffset(), match.getLength()); } } if (location != null) { locations.add(location); } } } }, monitor); } catch (CoreException e) { JavaLanguageServerPlugin.logException("Find references failure ", e); } return locations; }
Example #20
Source File: ResolveClasspathsHandler.java From java-debug with Eclipse Public License 1.0 | 4 votes |
/** * Try to find the associated java element with the main class from the test folders. * * @param project the java project containing the main class * @param mainClass the main class name * @return the associated java element */ private static IJavaElement findMainClassInTestFolders(IJavaProject project, String mainClass) { if (project == null || StringUtils.isBlank(mainClass)) { return null; } // get a list of test folders and check whether main class is here int constraints = IJavaSearchScope.SOURCES; IJavaElement[] testFolders = JdtUtils.getTestPackageFragmentRoots(project); if (testFolders.length > 0) { try { List<IJavaElement> mainClassesInTestFolder = new ArrayList<>(); SearchPattern pattern = SearchPattern.createPattern(mainClass, IJavaSearchConstants.CLASS, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_EXACT_MATCH); SearchEngine searchEngine = new SearchEngine(); IJavaSearchScope scope = SearchEngine.createJavaSearchScope(testFolders, constraints); SearchRequestor requestor = new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) { Object element = match.getElement(); if (element instanceof IJavaElement) { mainClassesInTestFolder.add((IJavaElement) element); } } }; searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, null /* progress monitor */); if (!mainClassesInTestFolder.isEmpty()) { return mainClassesInTestFolder.get(0); } } catch (Exception e) { logger.log(Level.SEVERE, String.format("Searching the main class failure: %s", e.toString()), e); } } return null; }
Example #21
Source File: ResolveClasspathsHandler.java From java-debug with Eclipse Public License 1.0 | 4 votes |
/** * Get java project from type. * * @param fullyQualifiedTypeName * fully qualified name of type * @return java project * @throws CoreException * CoreException */ public static List<IJavaProject> getJavaProjectFromType(String fullyQualifiedTypeName) throws CoreException { // If only one Java project exists in the whole workspace, return the project directly. List<IJavaProject> javaProjects = JdtUtils.listJavaProjects(ResourcesPlugin.getWorkspace().getRoot()); if (javaProjects.size() <= 1) { return javaProjects; } String[] splitItems = fullyQualifiedTypeName.split("/"); // If the main class name contains the module name, should trim the module info. if (splitItems.length == 2) { fullyQualifiedTypeName = splitItems[1]; } final String moduleName = splitItems.length == 2 ? splitItems[0] : null; final String className = fullyQualifiedTypeName; SearchPattern pattern = SearchPattern.createPattern( fullyQualifiedTypeName, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH); IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); ArrayList<IJavaProject> projects = new ArrayList<>(); SearchRequestor requestor = new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) { Object element = match.getElement(); if (element instanceof IType) { IType type = (IType) element; IJavaProject project = type.getJavaProject(); if (className.equals(type.getFullyQualifiedName()) && (moduleName == null || moduleName.equals(JdtUtils.getModuleName(project)))) { projects.add(project); } } } }; SearchEngine searchEngine = new SearchEngine(); searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, null /* progress monitor */); return projects.stream().distinct().collect(Collectors.toList()); }
Example #22
Source File: RefactoringSearchEngine.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
public static ICompilationUnit[] findAffectedCompilationUnits(SearchPattern pattern, IJavaSearchScope scope, final IProgressMonitor pm, RefactoringStatus status, final boolean tolerateInAccurateMatches) throws JavaModelException { boolean hasNonCuMatches= false; class ResourceSearchRequestor extends SearchRequestor{ boolean hasPotentialMatches= false ; Set<IResource> resources= new HashSet<IResource>(5); private IResource fLastResource; @Override public void acceptSearchMatch(SearchMatch match) { if (!tolerateInAccurateMatches && match.getAccuracy() == SearchMatch.A_INACCURATE) { hasPotentialMatches= true; } if (fLastResource != match.getResource()) { fLastResource= match.getResource(); resources.add(fLastResource); } } } ResourceSearchRequestor requestor = new ResourceSearchRequestor(); try { new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm); } catch (CoreException e) { throw new JavaModelException(e); } List<IJavaElement> result= new ArrayList<IJavaElement>(requestor.resources.size()); for (Iterator<IResource> iter= requestor.resources.iterator(); iter.hasNext(); ) { IResource resource= iter.next(); IJavaElement element= JavaCore.create(resource); if (element instanceof ICompilationUnit) { result.add(element); } else { hasNonCuMatches= true; } } addStatusErrors(status, requestor.hasPotentialMatches, hasNonCuMatches); return result.toArray(new ICompilationUnit[result.size()]); }
Example #23
Source File: CallerFinder.java From lapse-plus with GNU General Public License v3.0 | 4 votes |
public static Collection/*<MethodUnitPair>*/ findCallers(IProgressMonitor progressMonitor, String methodName, IJavaProject project, boolean isConstructor) { try { MethodSearchRequestor.initializeParserMap(); SearchRequestor searchRequestor = (SearchRequestor)new MethodSearchRequestor.MethodReferencesSearchRequestor(); SearchEngine searchEngine = new SearchEngine(); IProgressMonitor monitor = new SubProgressMonitor(progressMonitor, 5, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK); monitor.beginTask("Searching for calls to " + methodName + (project != null ? " in " + project.getProject().getName() : ""), 100); IJavaSearchScope searchScope = getSearchScope(project); // This is kind of hacky: we need to make up a string name for the search to work right log("Looking for calls to " + methodName); int matchType = !isConstructor ? IJavaSearchConstants.METHOD : IJavaSearchConstants.CONSTRUCTOR; SearchPattern pattern = SearchPattern.createPattern( methodName, matchType, IJavaSearchConstants.REFERENCES, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE); searchEngine.search( pattern, new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()}, searchScope, searchRequestor, monitor ); if(searchRequestor instanceof MethodSearchRequestor.MethodDeclarationsSearchRequestor){ return ((MethodSearchRequestor.MethodDeclarationsSearchRequestor)searchRequestor).getMethodUnitPairs(); }else{ return ((MethodSearchRequestor.MethodReferencesSearchRequestor)searchRequestor).getMethodUnitPairs(); } } catch (CoreException e) { JavaPlugin.log(e); return new LinkedList(); } }