Java Code Examples for org.eclipse.jdt.core.search.SearchEngine#getDefaultSearchParticipant()

The following examples show how to use org.eclipse.jdt.core.search.SearchEngine#getDefaultSearchParticipant() . 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: Utils.java    From CogniCrypt with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 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 2
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 3
Source File: MoveCuUpdateCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private TypeReference(IJavaElement enclosingElement, int accuracy, int start, int length,
		boolean insideDocComment, IResource resource, int simpleNameStart, String simpleName) {
	super(enclosingElement, accuracy, start, length,
			insideDocComment, SearchEngine.getDefaultSearchParticipant(), resource);
	fSimpleNameStart= simpleNameStart;
	fSimpleTypeName= simpleName;
}
 
Example 4
Source File: DefaultJavaIndexer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void generateIndexForJar(String pathToJar, String pathToIndexFile) throws IOException {
	File f = new File(pathToJar);
	if (!f.exists()) {
		throw new FileNotFoundException(pathToJar + " not found"); //$NON-NLS-1$
	}
	IndexLocation indexLocation = new FileIndexLocation(new File(pathToIndexFile));
	Index index = new Index(indexLocation, pathToJar, false /*reuse index file*/);
	SearchParticipant participant = SearchEngine.getDefaultSearchParticipant();
	index.separator = JAR_SEPARATOR;
	ZipFile zip = new ZipFile(pathToJar);
	try {
		for (Enumeration e = zip.entries(); e.hasMoreElements();) {
			// iterate each entry to index it
			ZipEntry ze = (ZipEntry) e.nextElement();
			String zipEntryName = ze.getName();
			if (Util.isClassFileName(zipEntryName)) {
				final byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getZipEntryByteContent(ze, zip);
				JavaSearchDocument entryDocument = new JavaSearchDocument(ze, new Path(pathToJar), classFileBytes, participant);
				entryDocument.setIndex(index);
				new BinaryIndexer(entryDocument).indexDocument();
			}
		}
		index.save();
	} finally {
		zip.close();
	}
	return;
}
 
Example 5
Source File: SearchUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static SearchParticipant[] getDefaultSearchParticipants() {
	return new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
}
 
Example 6
Source File: MoveCuUpdateCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private TypeReference(IJavaElement enclosingElement, int accuracy, int start, int length, boolean insideDocComment, IResource resource, int simpleNameStart, String simpleName) {
	super(enclosingElement, accuracy, start, length, insideDocComment, SearchEngine.getDefaultSearchParticipant(), resource);
	fSimpleNameStart = simpleNameStart;
	fSimpleTypeName = simpleName;
}
 
Example 7
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;
}
 
Example 8
Source File: NLSSearchQuery.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public IStatus run(IProgressMonitor monitor) {
	monitor.beginTask("", 5 * fWrapperClass.length); //$NON-NLS-1$

	try {
		final AbstractTextSearchResult textResult= (AbstractTextSearchResult) getSearchResult();
		textResult.removeAll();

		for (int i= 0; i < fWrapperClass.length; i++) {
			IJavaElement wrapperClass= fWrapperClass[i];
			IFile propertieFile= fPropertiesFile[i];
			if (! wrapperClass.exists())
				return JavaUIStatus.createError(0, Messages.format(NLSSearchMessages.NLSSearchQuery_wrapperNotExists, JavaElementLabels.getElementLabel(wrapperClass, JavaElementLabels.ALL_DEFAULT)), null);
			if (! propertieFile.exists())
				return JavaUIStatus.createError(0, Messages.format(NLSSearchMessages.NLSSearchQuery_propertiesNotExists, BasicElementLabels.getResourceName(propertieFile)), null);

			SearchPattern pattern= SearchPattern.createPattern(wrapperClass, IJavaSearchConstants.REFERENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
			SearchParticipant[] participants= new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()};

			NLSSearchResultRequestor requestor= new NLSSearchResultRequestor(propertieFile, fResult);
			try {
				SearchEngine engine= new SearchEngine();
				engine.search(pattern, participants, fScope, requestor, new SubProgressMonitor(monitor, 4));
				requestor.reportUnusedPropertyNames(new SubProgressMonitor(monitor, 1));

				ICompilationUnit compilationUnit= ((IType)wrapperClass).getCompilationUnit();
				CompilationUnitEntry groupElement= new CompilationUnitEntry(NLSSearchMessages.NLSSearchResultCollector_unusedKeys, compilationUnit);

				boolean hasUnusedPropertie= false;
				IField[] fields= ((IType)wrapperClass).getFields();
				for (int j= 0; j < fields.length; j++) {
					IField field= fields[j];
					if (isNLSField(field)) {
						ISourceRange sourceRange= field.getSourceRange();
						if (sourceRange != null) {
							String fieldName= field.getElementName();
							if (!requestor.hasPropertyKey(fieldName)) {
								fResult.addMatch(new Match(compilationUnit, sourceRange.getOffset(), sourceRange.getLength()));
							}
							if (!requestor.isUsedPropertyKey(fieldName)) {
								hasUnusedPropertie= true;
								fResult.addMatch(new Match(groupElement, sourceRange.getOffset(), sourceRange.getLength()));
							}
						}
					}
				}
				if (hasUnusedPropertie)
					fResult.addCompilationUnitGroup(groupElement);

			} catch (CoreException e) {
				return new Status(e.getStatus().getSeverity(), JavaPlugin.getPluginId(), IStatus.OK, NLSSearchMessages.NLSSearchQuery_error, e);
			}
		}
	} finally {
		monitor.done();
	}
	return 	Status.OK_STATUS;
}
 
Example 9
Source File: SearchUtils.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static SearchParticipant[] getDefaultSearchParticipants() {
	return new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
}