Java Code Examples for org.eclipse.jdt.core.search.SearchMatch#A_ACCURATE

The following examples show how to use org.eclipse.jdt.core.search.SearchMatch#A_ACCURATE . 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: IntroduceFactoryRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IType findNonPrimaryType(String fullyQualifiedName, IProgressMonitor pm, RefactoringStatus status) throws JavaModelException {
	SearchPattern p= SearchPattern.createPattern(fullyQualifiedName, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
	final RefactoringSearchEngine2 engine= new RefactoringSearchEngine2(p);

	engine.setFiltering(true, true);
	engine.setScope(RefactoringScopeFactory.create(fCtorBinding.getJavaElement().getJavaProject()));
	engine.setStatus(status);
	engine.searchPattern(new SubProgressMonitor(pm, 1));

	SearchResultGroup[] groups= (SearchResultGroup[]) engine.getResults();

	if (groups.length != 0) {
		for(int i= 0; i < groups.length; i++) {
			SearchMatch[] matches= groups[i].getSearchResults();
			for(int j= 0; j < matches.length; j++) {
				if (matches[j].getAccuracy() == SearchMatch.A_ACCURATE)
					return (IType) matches[j].getElement();
			}
		}
	}
	return null;
}
 
Example 2
Source File: MethodReferencesSearchRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void acceptSearchMatch(SearchMatch match) {
       if (fRequireExactMatch && (match.getAccuracy() != SearchMatch.A_ACCURATE)) {
           return;
       }

       if (match.isInsideDocComment()) {
           return;
       }

       if (match.getElement() != null && match.getElement() instanceof IMember) {
           IMember member= (IMember) match.getElement();
           switch (member.getElementType()) {
               case IJavaElement.METHOD:
               case IJavaElement.TYPE:
               case IJavaElement.FIELD:
               case IJavaElement.INITIALIZER:
                   fSearchResults.addMember(member, member, match.getOffset(), match.getOffset()+match.getLength());
                   break;
           }
       }
   }
 
Example 3
Source File: CollectingSearchRequestor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns whether the given match should be filtered out.
 * The default implementation filters out matches in binaries iff
 * {@link #CollectingSearchRequestor(ReferencesInBinaryContext)} has been called with a
 * non-<code>null</code> argument. Accurate binary matches are added to the {@link ReferencesInBinaryContext}.
 *
 * @param match the match to test
 * @return <code>true</code> iff the given match should <em>not</em> be collected
 * @throws CoreException
 */
public boolean filterMatch(SearchMatch match) throws CoreException {
	if (fBinaryRefs == null) {
		return false;
	}

	if (match.getAccuracy() == SearchMatch.A_ACCURATE && isBinaryElement(match.getElement())) {
		// binary classpaths are often incomplete -> avoiding false positives from inaccurate matches
		fBinaryRefs.add(match);
		return true;
	}

	return false;
}
 
Example 4
Source File: ConstructorReferenceFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static SearchMatch createSearchResult(ASTNode superCall, IMethod constructor) {
	int start= superCall.getStartPosition();
	int end= ASTNodes.getInclusiveEnd(superCall); //TODO: why inclusive?
	IResource resource= constructor.getResource();
	return new SearchMatch(constructor, SearchMatch.A_ACCURATE, start, end - start,
			SearchEngine.getDefaultSearchParticipant(), resource);
}
 
Example 5
Source File: CollectingSearchRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns whether the given match should be filtered out.
 * The default implementation filters out matches in binaries iff
 * {@link #CollectingSearchRequestor(ReferencesInBinaryContext)} has been called with a
 * non-<code>null</code> argument. Accurate binary matches are added to the {@link ReferencesInBinaryContext}.
 *
 * @param match the match to test
 * @return <code>true</code> iff the given match should <em>not</em> be collected
 * @throws CoreException
 */
public boolean filterMatch(SearchMatch match) throws CoreException {
	if (fBinaryRefs == null)
		return false;

	if (match.getAccuracy() == SearchMatch.A_ACCURATE && isBinaryElement(match.getElement())) {
		// binary classpaths are often incomplete -> avoiding false positives from inaccurate matches
		fBinaryRefs.add(match);
		return true;
	}

	return false;
}
 
Example 6
Source File: ImplementationCollector.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
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;
}