Java Code Examples for org.eclipse.jdt.core.search.SearchPattern#createPattern()

The following examples show how to use org.eclipse.jdt.core.search.SearchPattern#createPattern() . 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: RenamePackageProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @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 2
Source File: RenamePackageProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @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: PushDownRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static IJavaElement[] getReferencingElementsFromSameClass(IMember member, IProgressMonitor pm, RefactoringStatus status) throws JavaModelException {
	Assert.isNotNull(member);
	final RefactoringSearchEngine2 engine= new RefactoringSearchEngine2(SearchPattern.createPattern(member, IJavaSearchConstants.REFERENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE));
	engine.setFiltering(true, true);
	engine.setScope(SearchEngine.createJavaSearchScope(new IJavaElement[] { member.getDeclaringType() }));
	engine.setStatus(status);
	engine.searchPattern(new SubProgressMonitor(pm, 1));
	SearchResultGroup[] groups= (SearchResultGroup[]) engine.getResults();
	Set<IJavaElement> result= new HashSet<IJavaElement>(3);
	for (int i= 0; i < groups.length; i++) {
		SearchResultGroup group= groups[i];
		SearchMatch[] results= group.getSearchResults();
		for (int j= 0; j < results.length; j++) {
			SearchMatch searchResult= results[j];
			result.add(SearchUtils.getEnclosingJavaElement(searchResult));
		}
	}
	return result.toArray(new IJavaElement[result.size()]);
}
 
Example 4
Source File: RenameFieldProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void addAccessorOccurrences(IProgressMonitor pm, IMethod accessor, String editName, String newAccessorName, RefactoringStatus status) throws CoreException {
	Assert.isTrue(accessor.exists());

	IJavaSearchScope scope= RefactoringScopeFactory.create(accessor);
	SearchPattern pattern= SearchPattern.createPattern(accessor, IJavaSearchConstants.ALL_OCCURRENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
	SearchResultGroup[] groupedResults= RefactoringSearchEngine.search(
		pattern, scope, new MethodOccurenceCollector(accessor.getElementName()), pm, status);

	for (int i= 0; i < groupedResults.length; i++) {
		ICompilationUnit cu= groupedResults[i].getCompilationUnit();
		if (cu == null) {
			continue;
		}
		SearchMatch[] results= groupedResults[i].getSearchResults();
		for (int j= 0; j < results.length; j++){
			SearchMatch searchResult= results[j];
			TextEdit edit= new ReplaceEdit(searchResult.getOffset(), searchResult.getLength(), newAccessorName);
			addTextEdit(fChangeManager.get(cu), editName, edit);
		}
	}
}
 
Example 5
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 6
Source File: RenameMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private SearchPattern createNewMethodPattern() {
	StringBuffer stringPattern= new StringBuffer(getNewElementName()).append('(');
	int paramCount= getMethod().getNumberOfParameters();
	for (int i= 0; i < paramCount; i++) {
		if (i > 0)
			stringPattern.append(',');
		stringPattern.append('*');
	}
	stringPattern.append(')');

	return SearchPattern.createPattern(stringPattern.toString(), IJavaSearchConstants.METHOD,
			IJavaSearchConstants.DECLARATIONS, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
}
 
Example 7
Source File: TargetProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public ICompilationUnit[] getAffectedCompilationUnits(final RefactoringStatus status, ReferencesInBinaryContext binaryRefs, IProgressMonitor pm) throws CoreException {
	IMethod method= (IMethod)fMethodBinding.getJavaElement();
	Assert.isTrue(method != null);

	SearchPattern pattern= SearchPattern.createPattern(method, IJavaSearchConstants.REFERENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
	IJavaSearchScope scope= RefactoringScopeFactory.create(method, true, false);
	final HashSet<ICompilationUnit> affectedCompilationUnits= new HashSet<ICompilationUnit>();
	CollectingSearchRequestor requestor= new CollectingSearchRequestor(binaryRefs) {
		private ICompilationUnit fLastCU;
		@Override
		public void acceptSearchMatch(SearchMatch match) throws CoreException {
			if (filterMatch(match))
				return;
			if (match.isInsideDocComment())
				return; // TODO: should warn user (with something like a ReferencesInBinaryContext)

			ICompilationUnit unit= SearchUtils.getCompilationUnit(match);
			if (match.getAccuracy() == SearchMatch.A_INACCURATE) {
				if (unit != null) {
					status.addError(RefactoringCoreMessages.TargetProvider_inaccurate_match,
						JavaStatusContext.create(unit, new SourceRange(match.getOffset(), match.getLength())));
				} else {
					status.addError(RefactoringCoreMessages.TargetProvider_inaccurate_match);
				}
			} else if (unit != null) {
				if (! unit.equals(fLastCU)) {
					fLastCU= unit;
					affectedCompilationUnits.add(unit);
				}
			}
		}
	};
	new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, new SubProgressMonitor(pm, 1));
	return affectedCompilationUnits.toArray(new ICompilationUnit[affectedCompilationUnits.size()]);
}
 
Example 8
Source File: RippleMethodFinder2.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
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 9
Source File: RenameMethodProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private SearchPattern createNewMethodPattern() {
	StringBuffer stringPattern= new StringBuffer(getNewElementName()).append('(');
	int paramCount= getMethod().getNumberOfParameters();
	for (int i= 0; i < paramCount; i++) {
		if (i > 0) {
			stringPattern.append(',');
		}
		stringPattern.append('*');
	}
	stringPattern.append(')');

	return SearchPattern.createPattern(stringPattern.toString(), IJavaSearchConstants.METHOD,
			IJavaSearchConstants.DECLARATIONS, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
}
 
Example 10
Source File: CreateCopyOfCompilationUnitChange.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static SearchPattern createSearchPattern(IType type) throws JavaModelException {
	SearchPattern pattern= SearchPattern.createPattern(type, IJavaSearchConstants.ALL_OCCURRENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
	IMethod[] constructors= JavaElementUtil.getAllConstructors(type);
	if (constructors.length == 0)
		return pattern;
	SearchPattern constructorDeclarationPattern= RefactoringSearchEngine.createOrPattern(constructors, IJavaSearchConstants.DECLARATIONS);
	return SearchPattern.createOrPattern(pattern, constructorDeclarationPattern);
}
 
Example 11
Source File: JavaSearchPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isValidSearchPattern() {
	if (getPattern().length() == 0) {
		return false;
	}
	if (fJavaElement != null) {
		return true;
	}
	return SearchPattern.createPattern(getPattern(), getSearchFor(), getLimitTo(), SearchPattern.R_EXACT_MATCH) != null;
}
 
Example 12
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 13
Source File: CallerFinder.java    From lapse-plus with GNU General Public License v3.0 5 votes vote down vote up
public static Collection/*<MethodDeclarationUnitPair>*/ 
		findCallees(IProgressMonitor progressMonitor, String methodName, IJavaProject project, boolean isConstructor) 
{
	try {
           MethodSearchRequestor.MethodDeclarationsSearchRequestor searchRequestor = 
           	new MethodSearchRequestor.MethodDeclarationsSearchRequestor();
           SearchEngine searchEngine = new SearchEngine();

           IProgressMonitor monitor = new SubProgressMonitor(
           		progressMonitor, 5, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL);
           monitor.beginTask("Searching for declaration of " + methodName +
           		(project != null ? " in " + project.getProject().getName() : ""), 100);
           IJavaSearchScope searchScope = getSearchScope(project);
           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
				);
           monitor.done();

           return searchRequestor.getMethodUnitPairs();
       } catch (CoreException e) {
           JavaPlugin.log(e);

           return new LinkedList();
       }
}
 
Example 14
Source File: MainMethodSearchEngine.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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 15
Source File: RefactoringSearchEngine2.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Sets the search pattern to be used during search.
 * <p>
 * This method must be called before {@link RefactoringSearchEngine2#searchPattern(IProgressMonitor)}
 *
 * @param elements the set of elements
 * @param limitTo determines the nature of the expected matches. This is a combination of {@link org.eclipse.jdt.core.search.IJavaSearchConstants}.
 */
public final void setPattern(final IJavaElement[] elements, final int limitTo) {
	Assert.isNotNull(elements);
	Assert.isTrue(elements.length > 0);
	SearchPattern pattern= SearchPattern.createPattern(elements[0], limitTo, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
	IJavaElement element= null;
	for (int index= 1; index < elements.length; index++) {
		element= elements[index];
		pattern= SearchPattern.createOrPattern(pattern, SearchPattern.createPattern(element, limitTo, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE));
	}
	setPattern(pattern);
}
 
Example 16
Source File: RenameTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
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) == '.') {
							continue cus; // explicit import from another package => no problem
						}
					}
				}
				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;
}
 
Example 17
Source File: RenameFieldProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private SearchPattern createSearchPattern(){
	return SearchPattern.createPattern(fField, IJavaSearchConstants.REFERENCES);
}
 
Example 18
Source File: ResolveClasspathsHandler.java    From java-debug with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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 19
Source File: RenamePackageProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private SearchResultGroup[] getReferences(IProgressMonitor pm, ReferencesInBinaryContext binaryRefs, RefactoringStatus status) throws CoreException {
	IJavaSearchScope scope= RefactoringScopeFactory.create(fPackage, true, false);
	SearchPattern pattern= SearchPattern.createPattern(fPackage, IJavaSearchConstants.REFERENCES);
	CollectingSearchRequestor requestor= new CuCollectingSearchRequestor(binaryRefs);
	return RefactoringSearchEngine.search(pattern, scope, requestor, pm, status);
}
 
Example 20
Source File: RenameTypeProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
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) == '.') {
							continue cus; // explicit import from another package => no problem
						}
					}
				}
				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;
}