Search in sources :

Example 6 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class RenameTypeProcessor method addConstructorRenames.

private void addConstructorRenames(TextChangeManager manager) throws CoreException {
    ICompilationUnit cu = fType.getCompilationUnit();
    IMethod[] methods = fType.getMethods();
    int typeNameLength = fType.getElementName().length();
    for (int i = 0; i < methods.length; i++) {
        if (methods[i].isConstructor()) {
            /*
				 * constructor declarations cannot be fully qualified so we can use simple replace here
				 *
				 * if (methods[i].getNameRange() == null), then it's a binary file so it's wrong anyway
				 * (checked as a precondition)
				 */
            String name = RefactoringCoreMessages.RenameTypeRefactoring_rename_constructor;
            TextChangeCompatibility.addTextEdit(manager.get(cu), name, new ReplaceEdit(methods[i].getNameRange().getOffset(), typeNameLength, getNewElementName()));
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IMethod(org.eclipse.jdt.core.IMethod)

Example 7 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class RenameTypeProcessor method initializeSimilarElementsRenameProcessors.

// --------- Similar names
/**
	 * Creates and initializes the refactoring processors for similarly named elements
	 * @param progressMonitor progress monitor
	 * @param context context
	 * @return status
	 * @throws CoreException should not happen
	 */
private RefactoringStatus initializeSimilarElementsRenameProcessors(IProgressMonitor progressMonitor, CheckConditionsContext context) throws CoreException {
    Assert.isNotNull(fPreloadedElementToName);
    Assert.isNotNull(fPreloadedElementToSelection);
    final RefactoringStatus status = new RefactoringStatus();
    final Set<IMethod> handledTopLevelMethods = new HashSet<IMethod>();
    final Set<Warning> warnings = new HashSet<Warning>();
    final List<RefactoringProcessor> processors = new ArrayList<RefactoringProcessor>();
    fFinalSimilarElementToName = new HashMap<IJavaElement, String>();
    CompilationUnit currentResolvedCU = null;
    ICompilationUnit currentCU = null;
    int current = 0;
    final int max = fPreloadedElementToName.size();
    //$NON-NLS-1$
    progressMonitor.beginTask("", max * 3);
    progressMonitor.setTaskName(RefactoringCoreMessages.RenameTypeProcessor_checking_similarly_named_declarations_refactoring_conditions);
    for (Iterator<IJavaElement> iter = fPreloadedElementToName.keySet().iterator(); iter.hasNext(); ) {
        final IJavaElement element = iter.next();
        current++;
        progressMonitor.worked(3);
        // not selected? -> skip
        if (!(fPreloadedElementToSelection.get(element)).booleanValue())
            continue;
        // already registered? (may happen with overridden methods) -> skip
        if (fFinalSimilarElementToName.containsKey(element))
            continue;
        // CompilationUnit changed? (note: fPreloadedElementToName is sorted by CompilationUnit)
        ICompilationUnit newCU = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
        if (!newCU.equals(currentCU)) {
            checkCUCompleteConditions(status, currentResolvedCU, currentCU, processors);
            if (status.hasFatalError())
                return status;
            // reset values
            currentResolvedCU = null;
            currentCU = newCU;
            processors.clear();
        }
        final String newName = fPreloadedElementToName.get(element);
        RefactoringProcessor processor = null;
        if (element instanceof ILocalVariable) {
            final ILocalVariable currentLocal = (ILocalVariable) element;
            if (currentResolvedCU == null)
                currentResolvedCU = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(currentCU, true);
            processor = createLocalRenameProcessor(currentLocal, newName, currentResolvedCU);
            if (status.hasFatalError())
                return status;
            fFinalSimilarElementToName.put(currentLocal, newName);
        }
        if (element instanceof IField) {
            final IField currentField = (IField) element;
            processor = createFieldRenameProcessor(currentField, newName);
            status.merge(checkForConflictingRename(currentField, newName));
            if (status.hasFatalError())
                return status;
            fFinalSimilarElementToName.put(currentField, newName);
        }
        if (element instanceof IMethod) {
            IMethod currentMethod = (IMethod) element;
            if (MethodChecks.isVirtual(currentMethod)) {
                final IType declaringType = currentMethod.getDeclaringType();
                ITypeHierarchy hierarchy = null;
                if (!declaringType.isInterface())
                    hierarchy = declaringType.newTypeHierarchy(new NullProgressMonitor());
                final IMethod topmost = MethodChecks.getTopmostMethod(currentMethod, hierarchy, new NullProgressMonitor());
                if (topmost != null)
                    currentMethod = topmost;
                if (handledTopLevelMethods.contains(currentMethod))
                    continue;
                handledTopLevelMethods.add(currentMethod);
                final IMethod[] ripples = RippleMethodFinder2.getRelatedMethods(currentMethod, new NullProgressMonitor(), null);
                if (checkForWarnings(warnings, newName, ripples))
                    continue;
                status.merge(checkForConflictingRename(ripples, newName));
                if (status.hasFatalError())
                    return status;
                processor = createVirtualMethodRenameProcessor(currentMethod, newName, ripples, hierarchy);
                fFinalSimilarElementToName.put(currentMethod, newName);
                for (int i = 0; i < ripples.length; i++) {
                    fFinalSimilarElementToName.put(ripples[i], newName);
                }
            } else {
                status.merge(checkForConflictingRename(new IMethod[] { currentMethod }, newName));
                if (status.hasFatalError())
                    break;
                fFinalSimilarElementToName.put(currentMethod, newName);
                processor = createNonVirtualMethodRenameProcessor(currentMethod, newName);
            }
        }
        progressMonitor.subTask(Messages.format(RefactoringCoreMessages.RenameTypeProcessor_progress_current_total, new Object[] { String.valueOf(current), String.valueOf(max) }));
        status.merge(processor.checkInitialConditions(new NoOverrideProgressMonitor(progressMonitor, 1)));
        if (status.hasFatalError())
            return status;
        status.merge(processor.checkFinalConditions(new NoOverrideProgressMonitor(progressMonitor, 1), context));
        if (status.hasFatalError())
            return status;
        processors.add(processor);
        progressMonitor.worked(1);
        if (progressMonitor.isCanceled())
            throw new OperationCanceledException();
    }
    // check last CU
    checkCUCompleteConditions(status, currentResolvedCU, currentCU, processors);
    status.merge(addWarnings(warnings));
    progressMonitor.done();
    return status;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IType(org.eclipse.jdt.core.IType) ILocalVariable(org.eclipse.jdt.core.ILocalVariable) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) IMethod(org.eclipse.jdt.core.IMethod) HashSet(java.util.HashSet) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IField(org.eclipse.jdt.core.IField) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) RefactoringProcessor(org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor)

Example 8 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class RenameTypeProcessor method computeRenameModifications.

@Override
protected RenameModifications computeRenameModifications() {
    RenameModifications result = new RenameModifications();
    result.rename(fType, new RenameTypeArguments(getNewElementName(), getUpdateReferences(), getUpdateSimilarDeclarations(), getSimilarElements()), createParticipantDescriptorFilter());
    if (isPrimaryType()) {
        ICompilationUnit cu = fType.getCompilationUnit();
        String newCUName = getNewCompilationUnit().getElementName();
        result.rename(cu, new RenameArguments(newCUName, getUpdateReferences()));
    }
    return result;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RenameTypeArguments(org.eclipse.jdt.core.refactoring.RenameTypeArguments) RenameArguments(org.eclipse.ltk.core.refactoring.participants.RenameArguments)

Example 9 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class RenameTypeProcessor method checkTypesImportedInCu.

private RefactoringStatus checkTypesImportedInCu() throws CoreException {
    IImportDeclaration imp = getImportedType(fType.getCompilationUnit(), getNewElementName());
    if (imp == null)
        return null;
    String msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_imported, new Object[] { getNewElementLabel(), BasicElementLabels.getPathLabel(fType.getCompilationUnit().getResource().getFullPath(), false) });
    IJavaElement grandParent = imp.getParent().getParent();
    if (grandParent instanceof ICompilationUnit)
        return RefactoringStatus.createErrorStatus(msg, JavaStatusContext.create(imp));
    return null;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration)

Example 10 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class RenameTypeProcessor method doCheckFinalConditions.

@Override
protected RefactoringStatus doCheckFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException {
    //$NON-NLS-1$
    Assert.isNotNull(fType, "type");
    //$NON-NLS-1$
    Assert.isNotNull(getNewElementName(), "newName");
    RefactoringStatus result = new RefactoringStatus();
    int referenceSearchTicks = fUpdateReferences || fUpdateSimilarElements ? 15 : 0;
    int affectedCusTicks = fUpdateReferences || fUpdateSimilarElements ? 10 : 1;
    int similarElementTicks = fUpdateSimilarElements ? 85 : 0;
    int createChangeTicks = 5;
    int qualifiedNamesTicks = fUpdateQualifiedNames ? 50 : 0;
    try {
        //$NON-NLS-1$
        pm.beginTask("", 12 + referenceSearchTicks + affectedCusTicks + similarElementTicks + createChangeTicks + qualifiedNamesTicks);
        pm.setTaskName(RefactoringCoreMessages.RenameTypeRefactoring_checking);
        fChangeManager = new TextChangeManager(true);
        result.merge(checkNewElementName(getNewElementName()));
        if (result.hasFatalError())
            return result;
        result.merge(Checks.checkIfCuBroken(fType));
        if (result.hasFatalError())
            return result;
        pm.worked(1);
        result.merge(checkTypesInCompilationUnit());
        pm.worked(1);
        result.merge(checkForMethodsWithConstructorNames());
        pm.worked(1);
        result.merge(checkImportedTypes());
        pm.worked(1);
        if (Checks.isTopLevel(fType)) {
            ICompilationUnit cu = fType.getCompilationUnit();
            String newCUName = JavaModelUtil.getRenamedCUName(cu, getNewElementName());
            if (!newCUName.equals(cu.getElementName()))
                result.merge(Checks.checkCompilationUnitNewName(cu, getNewElementName()));
        }
        pm.worked(1);
        if (isPrimaryType())
            result.merge(checkNewPathValidity());
        pm.worked(1);
        result.merge(checkEnclosingTypes());
        pm.worked(1);
        result.merge(checkEnclosedTypes());
        pm.worked(1);
        result.merge(checkTypesInPackage());
        pm.worked(1);
        result.merge(checkTypesImportedInCu());
        pm.worked(1);
        result.merge(Checks.checkForMainAndNativeMethods(fType));
        pm.worked(1);
        // before doing any expensive analysis
        if (result.hasFatalError())
            return result;
        result.merge(analyseEnclosedTypes());
        pm.worked(1);
        // before doing _the really_ expensive analysis
        if (result.hasFatalError())
            return result;
        // Load references, including similarly named elements
        if (fUpdateReferences || fUpdateSimilarElements) {
            pm.setTaskName(RefactoringCoreMessages.RenameTypeRefactoring_searching);
            result.merge(initializeReferences(new SubProgressMonitor(pm, referenceSearchTicks)));
        } else {
            fReferences = new SearchResultGroup[0];
        }
        pm.setTaskName(RefactoringCoreMessages.RenameTypeRefactoring_checking);
        if (pm.isCanceled())
            throw new OperationCanceledException();
        if (fUpdateReferences || fUpdateSimilarElements) {
            result.merge(analyzeAffectedCompilationUnits(new SubProgressMonitor(pm, affectedCusTicks)));
        } else {
            Checks.checkCompileErrorsInAffectedFile(result, fType.getResource());
            pm.worked(affectedCusTicks);
        }
        if (result.hasFatalError())
            return result;
        if (fUpdateSimilarElements) {
            result.merge(initializeSimilarElementsRenameProcessors(new SubProgressMonitor(pm, similarElementTicks), context));
            if (result.hasFatalError())
                return result;
        }
        createChanges(new SubProgressMonitor(pm, createChangeTicks));
        if (fUpdateQualifiedNames)
            computeQualifiedNameMatches(new SubProgressMonitor(pm, qualifiedNamesTicks));
        return result;
    } finally {
        pm.done();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1975 Test (org.junit.Test)1482 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1455 ArrayList (java.util.ArrayList)837 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)548 List (java.util.List)476 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)385 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)284 Hashtable (java.util.Hashtable)143 IType (org.eclipse.jdt.core.IType)135 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)103 ASTNode (org.eclipse.jdt.core.dom.ASTNode)97 JavaModelException (org.eclipse.jdt.core.JavaModelException)82 IJavaElement (org.eclipse.jdt.core.IJavaElement)81 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)70 IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)70 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)65 IJavaProject (org.eclipse.jdt.core.IJavaProject)64 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)62 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)56