Search in sources :

Example 6 with IImportDeclaration

use of org.eclipse.jdt.core.IImportDeclaration 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;
}
Also used : RefactoringStatusContext(org.eclipse.ltk.core.refactoring.RefactoringStatusContext) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration)

Aggregations

IImportDeclaration (org.eclipse.jdt.core.IImportDeclaration)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)2 ImportDeclaration (org.eclipse.che.ide.ext.java.shared.dto.model.ImportDeclaration)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)1 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)1 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)1 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)1 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)1 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)1 ISourceImport (org.eclipse.jdt.internal.compiler.env.ISourceImport)1 ISourceType (org.eclipse.jdt.internal.compiler.env.ISourceType)1 Parser (org.eclipse.jdt.internal.compiler.parser.Parser)1 CompilationUnitElementInfo (org.eclipse.jdt.internal.core.CompilationUnitElementInfo)1 ImportDeclaration (org.eclipse.jdt.internal.core.ImportDeclaration)1 PackageFragment (org.eclipse.jdt.internal.core.PackageFragment)1 SourceType (org.eclipse.jdt.internal.core.SourceType)1 SourceTypeElementInfo (org.eclipse.jdt.internal.core.SourceTypeElementInfo)1