use of org.eclipse.jdt.core.search.SearchPattern in project che by eclipse.
the class CreateCopyOfCompilationUnitChange method getReferences.
private static SearchResultGroup getReferences(final ICompilationUnit copy, IProgressMonitor monitor) throws JavaModelException {
final ICompilationUnit[] copies = new ICompilationUnit[] { copy };
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(copies);
final IType type = copy.findPrimaryType();
if (type == null)
return null;
SearchPattern pattern = createSearchPattern(type);
final RefactoringSearchEngine2 engine = new RefactoringSearchEngine2(pattern);
engine.setScope(scope);
engine.setWorkingCopies(copies);
engine.setRequestor(new IRefactoringSearchRequestor() {
TypeOccurrenceCollector fTypeOccurrenceCollector = new TypeOccurrenceCollector(type);
public SearchMatch acceptSearchMatch(SearchMatch match) {
try {
return fTypeOccurrenceCollector.acceptSearchMatch2(copy, match);
} catch (CoreException e) {
JavaPlugin.log(e);
return null;
}
}
});
engine.searchPattern(monitor);
final Object[] results = engine.getResults();
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=106127)
for (int index = 0; index < results.length; index++) {
SearchResultGroup group = (SearchResultGroup) results[index];
if (group.getCompilationUnit().equals(copy))
return group;
}
return null;
}
use of org.eclipse.jdt.core.search.SearchPattern in project che by eclipse.
the class IntroduceFactoryRefactoring method findNonPrimaryType.
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;
}
use of org.eclipse.jdt.core.search.SearchPattern in project che by eclipse.
the class IntroduceIndirectionRefactoring method getReferences.
private SearchResultGroup[] getReferences(IMethod[] methods, IProgressMonitor pm, RefactoringStatus status) throws CoreException {
SearchPattern pattern = RefactoringSearchEngine.createOrPattern(methods, IJavaSearchConstants.REFERENCES);
IJavaSearchScope scope = RefactoringScopeFactory.create(fIntermediaryType, false);
return RefactoringSearchEngine.search(pattern, scope, pm, status);
}
use of org.eclipse.jdt.core.search.SearchPattern in project che by eclipse.
the class RefactoringSearchEngine method createOrPattern.
public static SearchPattern createOrPattern(IJavaElement[] elements, int limitTo) {
if (elements == null || elements.length == 0)
return null;
Set<IJavaElement> set = new HashSet<IJavaElement>(Arrays.asList(elements));
Iterator<IJavaElement> iter = set.iterator();
IJavaElement first = iter.next();
SearchPattern pattern = SearchPattern.createPattern(first, limitTo, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
if (// check for bug 90138
pattern == null)
//$NON-NLS-1$ //$NON-NLS-2$
throw new IllegalArgumentException("Invalid java element: " + first.getHandleIdentifier() + "\n" + first.toString());
while (iter.hasNext()) {
IJavaElement each = iter.next();
SearchPattern nextPattern = SearchPattern.createPattern(each, limitTo, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
if (// check for bug 90138
nextPattern == null)
//$NON-NLS-1$ //$NON-NLS-2$
throw new IllegalArgumentException("Invalid java element: " + each.getHandleIdentifier() + "\n" + each.toString());
pattern = SearchPattern.createOrPattern(pattern, nextPattern);
}
return pattern;
}
use of org.eclipse.jdt.core.search.SearchPattern in project che by eclipse.
the class RenameTypeProcessor method checkConflictingTypes.
private RefactoringStatus checkConflictingTypes(IProgressMonitor pm) throws CoreException {
RefactoringStatus result = new RefactoringStatus();
IJavaSearchScope scope = RefactoringScopeFactory.create(fType);
SearchPattern pattern = SearchPattern.createPattern(getNewElementName(), IJavaSearchConstants.TYPE, IJavaSearchConstants.ALL_OCCURRENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
ICompilationUnit[] cusWithReferencesToConflictingTypes = RefactoringSearchEngine.findAffectedCompilationUnits(pattern, scope, pm, result);
if (cusWithReferencesToConflictingTypes.length == 0)
return result;
ICompilationUnit[] cusWithReferencesToRenamedType = getCus(fReferences);
Set<ICompilationUnit> conflicts = getIntersection(cusWithReferencesToRenamedType, cusWithReferencesToConflictingTypes);
if (cusWithReferencesToConflictingTypes.length > 0) {
cus: for (ICompilationUnit cu : cusWithReferencesToConflictingTypes) {
String packageName = fType.getPackageFragment().getElementName();
if (((IPackageFragment) cu.getParent()).getElementName().equals(packageName)) {
boolean hasOnDemandImport = false;
IImportDeclaration[] imports = cu.getImports();
for (IImportDeclaration importDecl : imports) {
if (importDecl.isOnDemand()) {
hasOnDemandImport = true;
} else {
String importName = importDecl.getElementName();
int packageLength = importName.length() - getNewElementName().length() - 1;
if (packageLength > 0 && importName.endsWith(getNewElementName()) && importName.charAt(packageLength) == '.') {
// explicit import from another package => no problem
continue cus;
}
}
}
if (hasOnDemandImport) {
// the renamed type in the same package will shadow the *-imported type
conflicts.add(cu);
}
}
}
}
for (ICompilationUnit conflict : conflicts) {
RefactoringStatusContext context = JavaStatusContext.create(conflict);
String message = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_another_type, new String[] { getNewElementLabel(), BasicElementLabels.getFileName(conflict) });
result.addError(message, context);
}
return result;
}
Aggregations