Search in sources :

Example 11 with CompilationUnitRewrite

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

the class IntroduceIndirectionRefactoring method updateReferences.

private RefactoringStatus updateReferences(IProgressMonitor monitor) throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    //$NON-NLS-1$
    monitor.beginTask("", 90);
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    IMethod[] ripple = RippleMethodFinder2.getRelatedMethods(fTargetMethod, false, new NoOverrideProgressMonitor(monitor, 10), null);
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    SearchResultGroup[] references = Checks.excludeCompilationUnits(getReferences(ripple, new NoOverrideProgressMonitor(monitor, 10), result), result);
    if (result.hasFatalError())
        return result;
    result.merge(Checks.checkCompileErrorsInAffectedFiles(references));
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    int ticksPerCU = references.length == 0 ? 0 : 70 / references.length;
    for (int i = 0; i < references.length; i++) {
        SearchResultGroup group = references[i];
        SearchMatch[] searchResults = group.getSearchResults();
        CompilationUnitRewrite currentCURewrite = getCachedCURewrite(group.getCompilationUnit());
        for (int j = 0; j < searchResults.length; j++) {
            SearchMatch match = searchResults[j];
            if (match.isInsideDocComment())
                continue;
            IMember enclosingMember = (IMember) match.getElement();
            ASTNode target = getSelectedNode(group.getCompilationUnit(), currentCURewrite.getRoot(), match.getOffset(), match.getLength());
            if (target instanceof SuperMethodInvocation) {
                // Cannot retarget calls to super - add a warning
                result.merge(createWarningAboutCall(enclosingMember, target, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_super_keyword));
                continue;
            }
            //$NON-NLS-1$
            Assert.isTrue(target instanceof MethodInvocation, "Element of call should be a MethodInvocation.");
            MethodInvocation invocation = (MethodInvocation) target;
            ITypeBinding typeBinding = getExpressionType(invocation);
            if (fIntermediaryFirstParameterType == null) {
                // no highest type yet
                fIntermediaryFirstParameterType = typeBinding.getTypeDeclaration();
            } else {
                // check if current type is higher
                result.merge(findCommonParent(typeBinding.getTypeDeclaration()));
            }
            if (result.hasFatalError())
                return result;
            // create an edit for this particular call
            result.merge(updateMethodInvocation(invocation, enclosingMember, currentCURewrite));
            // does call see the intermediary method?
            // => increase visibility of the type of the intermediary method.
            result.merge(adjustVisibility(fIntermediaryType, enclosingMember.getDeclaringType(), new NoOverrideProgressMonitor(monitor, 0)));
            if (monitor.isCanceled())
                throw new OperationCanceledException();
        }
        if (!isRewriteKept(group.getCompilationUnit()))
            createChangeAndDiscardRewrite(group.getCompilationUnit());
        monitor.worked(ticksPerCU);
    }
    monitor.done();
    return result;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IMember(org.eclipse.jdt.core.IMember) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IMethod(org.eclipse.jdt.core.IMethod)

Example 12 with CompilationUnitRewrite

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

the class InlineConstantRefactoring method findDeclaration.

private RefactoringStatus findDeclaration() throws JavaModelException {
    fDeclarationSelectedChecked = true;
    fDeclarationSelected = false;
    ASTNode parent = fSelectedConstantName.getParent();
    if (parent instanceof VariableDeclarationFragment) {
        VariableDeclarationFragment parentDeclaration = (VariableDeclarationFragment) parent;
        if (parentDeclaration.getName() == fSelectedConstantName) {
            fDeclarationSelected = true;
            fDeclarationCuRewrite = fSelectionCuRewrite;
            fDeclaration = (VariableDeclarationFragment) fSelectedConstantName.getParent();
            return null;
        }
    }
    VariableDeclarationFragment declaration = (VariableDeclarationFragment) fSelectionCuRewrite.getRoot().findDeclaringNode(fSelectedConstantName.resolveBinding());
    if (declaration != null) {
        fDeclarationCuRewrite = fSelectionCuRewrite;
        fDeclaration = declaration;
        return null;
    }
    if (fField.getCompilationUnit() == null)
        return RefactoringStatus.createStatus(RefactoringStatus.FATAL, RefactoringCoreMessages.InlineConstantRefactoring_binary_file, null, Corext.getPluginId(), RefactoringStatusCodes.DECLARED_IN_CLASSFILE, null);
    fDeclarationCuRewrite = new CompilationUnitRewrite(fField.getCompilationUnit());
    fDeclaration = ASTNodeSearchUtil.getFieldDeclarationFragmentNode(fField, fDeclarationCuRewrite.getRoot());
    return null;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 13 with CompilationUnitRewrite

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

the class InlineTempRefactoring method createChange.

//----- changes
@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
    try {
        pm.beginTask(RefactoringCoreMessages.InlineTempRefactoring_preview, 2);
        final Map<String, String> arguments = new HashMap<String, String>();
        String project = null;
        IJavaProject javaProject = fCu.getJavaProject();
        if (javaProject != null)
            project = javaProject.getElementName();
        final IVariableBinding binding = getVariableDeclaration().resolveBinding();
        String text = null;
        final IMethodBinding method = binding.getDeclaringMethod();
        if (method != null)
            text = BindingLabelProvider.getBindingLabel(method, JavaElementLabels.ALL_FULLY_QUALIFIED);
        else
            text = BasicElementLabels.getJavaElementName('{' + JavaElementLabels.ELLIPSIS_STRING + '}');
        final String description = Messages.format(RefactoringCoreMessages.InlineTempRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(binding.getName()));
        final String header = Messages.format(RefactoringCoreMessages.InlineTempRefactoring_descriptor_description, new String[] { BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED), text });
        final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
        comment.addSetting(Messages.format(RefactoringCoreMessages.InlineTempRefactoring_original_pattern, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED)));
        final InlineLocalVariableDescriptor descriptor = RefactoringSignatureDescriptorFactory.createInlineLocalVariableDescriptor(project, description, comment.asString(), arguments, RefactoringDescriptor.NONE);
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCu));
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, String.valueOf(fSelectionStart) + ' ' + String.valueOf(fSelectionLength));
        CompilationUnitRewrite cuRewrite = new CompilationUnitRewrite(fCu, fASTRoot);
        inlineTemp(cuRewrite);
        removeTemp(cuRewrite);
        final CompilationUnitChange result = cuRewrite.createChange(RefactoringCoreMessages.InlineTempRefactoring_inline, false, new SubProgressMonitor(pm, 1));
        result.setDescriptor(new RefactoringChangeDescriptor(descriptor));
        return result;
    } finally {
        pm.done();
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) IJavaProject(org.eclipse.jdt.core.IJavaProject) HashMap(java.util.HashMap) InlineLocalVariableDescriptor(org.eclipse.jdt.core.refactoring.descriptors.InlineLocalVariableDescriptor) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange) RefactoringChangeDescriptor(org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor)

Example 14 with CompilationUnitRewrite

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

the class InlineConstantRefactoring method initialize.

private void initialize(ICompilationUnit cu, CompilationUnit node) {
    fSelectionCuRewrite = new CompilationUnitRewrite(cu, node);
    fSelectedConstantName = findConstantNameNode();
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite)

Example 15 with CompilationUnitRewrite

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

the class ExtractTempRefactoring method checkFinalConditions.

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
    try {
        pm.beginTask(RefactoringCoreMessages.ExtractTempRefactoring_checking_preconditions, 4);
        fCURewrite = new CompilationUnitRewrite(fCu, fCompilationUnitNode);
        fCURewrite.getASTRewrite().setTargetSourceRangeComputer(new NoCommentSourceRangeComputer());
        doCreateChange(new SubProgressMonitor(pm, 2));
        fChange = fCURewrite.createChange(RefactoringCoreMessages.ExtractTempRefactoring_change_name, true, new SubProgressMonitor(pm, 1));
        RefactoringStatus result = new RefactoringStatus();
        if (Arrays.asList(getExcludedVariableNames()).contains(fTempName))
            result.addWarning(Messages.format(RefactoringCoreMessages.ExtractTempRefactoring_another_variable, BasicElementLabels.getJavaElementName(fTempName)));
        result.merge(checkMatchingFragments());
        fChange.setKeepPreviewEdits(true);
        if (fCheckResultForCompileProblems) {
            checkNewSource(new SubProgressMonitor(pm, 1), result);
        }
        return result;
    } finally {
        pm.done();
    }
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) NoCommentSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.NoCommentSourceRangeComputer) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

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