Search in sources :

Example 16 with CompilationUnitRewrite

use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.

the class ExtractConstantRefactoring method checkInitialConditions.

@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
    try {
        //$NON-NLS-1$
        pm.beginTask("", 7);
        RefactoringStatus result = Checks.validateEdit(fCu, getValidationContext());
        if (result.hasFatalError())
            return result;
        pm.worked(1);
        if (fCuRewrite == null) {
            CompilationUnit cuNode = RefactoringASTParser.parseWithASTProvider(fCu, true, new SubProgressMonitor(pm, 3));
            fCuRewrite = new CompilationUnitRewrite(fCu, cuNode);
        } else {
            pm.worked(3);
        }
        result.merge(checkSelection(new SubProgressMonitor(pm, 3)));
        if (result.hasFatalError())
            return result;
        if (isLiteralNodeSelected())
            fReplaceAllOccurrences = false;
        if (isInTypeDeclarationAnnotation(getSelectedExpression().getAssociatedNode())) {
            fVisibility = JdtFlags.VISIBILITY_STRING_PACKAGE;
        }
        ITypeBinding targetType = getContainingTypeBinding();
        if (targetType.isInterface()) {
            fTargetIsInterface = true;
            fVisibility = JdtFlags.VISIBILITY_STRING_PUBLIC;
        }
        return result;
    } finally {
        pm.done();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 17 with CompilationUnitRewrite

use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.

the class DeleteChangeCreator method createDeleteChange.

/*
	 * List<IJavaElement> javaElements
	 */
private static Change createDeleteChange(ICompilationUnit cu, List<IJavaElement> javaElements, TextChangeManager manager) throws CoreException {
    CompilationUnit cuNode = RefactoringASTParser.parseWithASTProvider(cu, false, null);
    CompilationUnitRewrite rewriter = new CompilationUnitRewrite(cu, cuNode);
    IJavaElement[] elements = javaElements.toArray(new IJavaElement[javaElements.size()]);
    ASTNodeDeleteUtil.markAsDeleted(elements, rewriter, null);
    return addTextEditFromRewrite(manager, cu, rewriter.getASTRewrite());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) IJavaElement(org.eclipse.jdt.core.IJavaElement)

Example 18 with CompilationUnitRewrite

use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.

the class RenameTypeParameterProcessor method createRenameChanges.

/**
	 * Creates the necessary changes for the renaming of the type parameter.
	 *
	 * @param monitor
	 *            the progress monitor to display progress
	 * @return the status of the operation
	 * @throws CoreException
	 *             if the change could not be generated
	 */
private RefactoringStatus createRenameChanges(IProgressMonitor monitor) throws CoreException {
    Assert.isNotNull(monitor);
    RefactoringStatus status = new RefactoringStatus();
    try {
        monitor.beginTask(RefactoringCoreMessages.RenameTypeParameterRefactoring_searching, 2);
        ICompilationUnit cu = fTypeParameter.getDeclaringMember().getCompilationUnit();
        CompilationUnit root = RefactoringASTParser.parseWithASTProvider(cu, true, null);
        CompilationUnitRewrite rewrite = new CompilationUnitRewrite(cu, root);
        IMember member = fTypeParameter.getDeclaringMember();
        ASTNode declaration = null;
        if (member instanceof IMethod) {
            declaration = ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) member, root);
        } else if (member instanceof IType) {
            declaration = ASTNodeSearchUtil.getAbstractTypeDeclarationNode((IType) member, root);
        } else {
            //$NON-NLS-1$
            JavaPlugin.logErrorMessage("Unexpected sub-type of IMember: " + member.getClass().getName());
            Assert.isTrue(false);
        }
        monitor.worked(1);
        RenameTypeParameterVisitor visitor = new RenameTypeParameterVisitor(rewrite, fTypeParameter.getNameRange(), status);
        if (declaration != null)
            declaration.accept(visitor);
        fChange = visitor.getResult();
    } finally {
        monitor.done();
    }
    return status;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMethod(org.eclipse.jdt.core.IMethod) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType)

Example 19 with CompilationUnitRewrite

use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.

the class IntroduceIndirectionRefactoring method createChangeAndDiscardRewrite.

private void createChangeAndDiscardRewrite(ICompilationUnit compilationUnit) throws CoreException {
    CompilationUnitRewrite rewrite = fRewrites.get(compilationUnit);
    if (rewrite != null) {
        fTextChangeManager.manage(compilationUnit, rewrite.createChange(true));
        fRewrites.remove(compilationUnit);
    }
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite)

Example 20 with CompilationUnitRewrite

use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.

the class IntroduceIndirectionRefactoring method checkFinalConditions.

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    RefactoringStatus result = new RefactoringStatus();
    fTextChangeManager = new TextChangeManager();
    fIntermediaryFirstParameterType = null;
    fIntermediaryTypeBinding = null;
    for (Iterator<CompilationUnitRewrite> iter = fRewrites.values().iterator(); iter.hasNext(); ) iter.next().clearASTAndImportRewrites();
    int startupTicks = 5;
    int hierarchyTicks = 5;
    int visibilityTicks = 5;
    int referenceTicks = fUpdateReferences ? 30 : 5;
    int creationTicks = 5;
    //$NON-NLS-1$
    pm.beginTask("", startupTicks + hierarchyTicks + visibilityTicks + referenceTicks + creationTicks);
    pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions);
    result.merge(Checks.checkMethodName(fIntermediaryMethodName, fIntermediaryType));
    if (result.hasFatalError())
        return result;
    if (fIntermediaryType == null)
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_run_without_intermediary_type);
    // intermediary type is already non binary/non-enum
    CompilationUnitRewrite imRewrite = getCachedCURewrite(fIntermediaryType.getCompilationUnit());
    fIntermediaryTypeBinding = typeToBinding(fIntermediaryType, imRewrite.getRoot());
    fAdjustor = new MemberVisibilityAdjustor(fIntermediaryType, fIntermediaryType);
    fIntermediaryAdjustments = new HashMap<IMember, IncomingMemberVisibilityAdjustment>();
    // check static method in non-static nested type
    if (fIntermediaryTypeBinding.isNested() && !Modifier.isStatic(fIntermediaryTypeBinding.getModifiers()))
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_create_in_nested_nonstatic, JavaStatusContext.create(fIntermediaryType));
    pm.worked(startupTicks);
    if (pm.isCanceled())
        throw new OperationCanceledException();
    if (fUpdateReferences) {
        //$NON-NLS-1$
        pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions + " " + RefactoringCoreMessages.IntroduceIndirectionRefactoring_looking_for_references);
        result.merge(updateReferences(new NoOverrideProgressMonitor(pm, referenceTicks)));
        pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions);
    } else {
        // only update the declaration and/or a selected method invocation
        if (fSelectionMethodInvocation != null) {
            fIntermediaryFirstParameterType = getExpressionType(fSelectionMethodInvocation);
            final IMember enclosing = getEnclosingInitialSelectionMember();
            // create an edit for this particular call
            result.merge(updateMethodInvocation(fSelectionMethodInvocation, enclosing, getCachedCURewrite(fSelectionCompilationUnit)));
            if (!isRewriteKept(fSelectionCompilationUnit))
                createChangeAndDiscardRewrite(fSelectionCompilationUnit);
            // does call see the intermediary method?
            // => increase visibility of the type of the intermediary method.
            result.merge(adjustVisibility(fIntermediaryType, enclosing.getDeclaringType(), new NoOverrideProgressMonitor(pm, 0)));
        }
        pm.worked(referenceTicks);
    }
    if (pm.isCanceled())
        throw new OperationCanceledException();
    if (fIntermediaryFirstParameterType == null)
        fIntermediaryFirstParameterType = fTargetMethodBinding.getDeclaringClass();
    // The target type and method may have changed - update them
    IType actualTargetType = (IType) fIntermediaryFirstParameterType.getJavaElement();
    if (!fTargetMethod.getDeclaringType().equals(actualTargetType)) {
        IMethod actualTargetMethod = new MethodOverrideTester(actualTargetType, actualTargetType.newSupertypeHierarchy(null)).findOverriddenMethodInHierarchy(actualTargetType, fTargetMethod);
        fTargetMethod = actualTargetMethod;
        fTargetMethodBinding = findMethodBindingInHierarchy(fIntermediaryFirstParameterType, actualTargetMethod);
        Assert.isNotNull(fTargetMethodBinding);
    }
    result.merge(checkCanCreateIntermediaryMethod());
    createIntermediaryMethod();
    pm.worked(creationTicks);
    //$NON-NLS-1$
    pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions + " " + RefactoringCoreMessages.IntroduceIndirectionRefactoring_adjusting_visibility);
    result.merge(updateTargetVisibility(new NoOverrideProgressMonitor(pm, 0)));
    result.merge(updateIntermediaryVisibility(new NoOverrideProgressMonitor(pm, 0)));
    pm.worked(visibilityTicks);
    pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions);
    createChangeAndDiscardRewrite(fIntermediaryType.getCompilationUnit());
    result.merge(Checks.validateModifiesFiles(getAllFilesToModify(), getValidationContext()));
    pm.done();
    return result;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) IncomingMemberVisibilityAdjustment(org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor.IncomingMemberVisibilityAdjustment) MethodOverrideTester(org.eclipse.jdt.internal.corext.util.MethodOverrideTester) MemberVisibilityAdjustor(org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType) IMethod(org.eclipse.jdt.core.IMethod) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager)

Aggregations

CompilationUnitRewrite (org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite)22 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)10 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)9 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)6 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)5 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)5 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)4 IMember (org.eclipse.jdt.core.IMember)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)4 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)4 IMethod (org.eclipse.jdt.core.IMethod)3 IType (org.eclipse.jdt.core.IType)3 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)3 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)2 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)2