Search in sources :

Example 6 with TightSourceRangeComputer

use of org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer in project che by eclipse.

the class ChangeSignatureProcessor method checkInitialConditions.

/* (non-Javadoc)
	 * @see org.eclipse.ltk.core.refactoring.Refactoring#checkInitialConditions(org.eclipse.core.runtime.IProgressMonitor)
	 */
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor monitor) throws CoreException {
    try {
        //$NON-NLS-1$
        monitor.beginTask("", 5);
        RefactoringStatus result = Checks.checkIfCuBroken(fMethod);
        if (result.hasFatalError())
            return result;
        if (fMethod == null || !fMethod.exists()) {
            String message = Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_method_deleted, BasicElementLabels.getFileName(getCu()));
            return RefactoringStatus.createFatalErrorStatus(message);
        }
        if (fMethod.getDeclaringType().isInterface()) {
            fTopMethod = MethodChecks.overridesAnotherMethod(fMethod, fMethod.getDeclaringType().newSupertypeHierarchy(new SubProgressMonitor(monitor, 1)));
            monitor.worked(1);
        } else if (MethodChecks.isVirtual(fMethod)) {
            ITypeHierarchy hierarchy = getCachedTypeHierarchy(new SubProgressMonitor(monitor, 1));
            fTopMethod = MethodChecks.isDeclaredInInterface(fMethod, hierarchy, new SubProgressMonitor(monitor, 1));
            if (fTopMethod == null)
                fTopMethod = MethodChecks.overridesAnotherMethod(fMethod, hierarchy);
        }
        if (fTopMethod == null)
            fTopMethod = fMethod;
        if (!fTopMethod.equals(fMethod)) {
            if (fTopMethod.getDeclaringType().isInterface()) {
                RefactoringStatusContext context = JavaStatusContext.create(fTopMethod);
                String message = Messages.format(RefactoringCoreMessages.MethodChecks_implements, new String[] { JavaElementUtil.createMethodSignature(fTopMethod), BasicElementLabels.getJavaElementName(fTopMethod.getDeclaringType().getFullyQualifiedName('.')) });
                return RefactoringStatus.createStatus(RefactoringStatus.FATAL, message, context, Corext.getPluginId(), RefactoringStatusCodes.METHOD_DECLARED_IN_INTERFACE, fTopMethod);
            } else {
                RefactoringStatusContext context = JavaStatusContext.create(fTopMethod);
                String message = Messages.format(RefactoringCoreMessages.MethodChecks_overrides, new String[] { JavaElementUtil.createMethodSignature(fTopMethod), BasicElementLabels.getJavaElementName(fTopMethod.getDeclaringType().getFullyQualifiedName('.')) });
                return RefactoringStatus.createStatus(RefactoringStatus.FATAL, message, context, Corext.getPluginId(), RefactoringStatusCodes.OVERRIDES_ANOTHER_METHOD, fTopMethod);
            }
        }
        if (monitor.isCanceled())
            throw new OperationCanceledException();
        if (fBaseCuRewrite == null || !fBaseCuRewrite.getCu().equals(getCu())) {
            fBaseCuRewrite = new CompilationUnitRewrite(getCu());
            fBaseCuRewrite.getASTRewrite().setTargetSourceRangeComputer(new TightSourceRangeComputer());
        }
        RefactoringStatus[] status = TypeContextChecker.checkMethodTypesSyntax(fMethod, getParameterInfos(), fReturnTypeInfo);
        for (int i = 0; i < status.length; i++) {
            result.merge(status[i]);
        }
        monitor.worked(1);
        result.merge(createExceptionInfoList());
        monitor.worked(1);
        return result;
    } finally {
        monitor.done();
    }
}
Also used : RefactoringStatusContext(org.eclipse.ltk.core.refactoring.RefactoringStatusContext) TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 7 with TightSourceRangeComputer

use of org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer in project che by eclipse.

the class InlineTempRefactoring method removeTemp.

private void removeTemp(CompilationUnitRewrite cuRewrite) {
    VariableDeclaration variableDeclaration = getVariableDeclaration();
    TextEditGroup groupDesc = cuRewrite.createGroupDescription(RefactoringCoreMessages.InlineTempRefactoring_remove_edit_name);
    ASTNode parent = variableDeclaration.getParent();
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TightSourceRangeComputer sourceRangeComputer = new TightSourceRangeComputer();
    rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
    if (parent instanceof VariableDeclarationStatement && ((VariableDeclarationStatement) parent).fragments().size() == 1) {
        sourceRangeComputer.addTightSourceNode(parent);
        rewrite.remove(parent, groupDesc);
    } else {
        sourceRangeComputer.addTightSourceNode(variableDeclaration);
        rewrite.remove(variableDeclaration, groupDesc);
    }
}
Also used : TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 8 with TightSourceRangeComputer

use of org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer in project che by eclipse.

the class ConvertForLoopOperation method rewriteAST.

/**
	 * {@inheritDoc}
	 */
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException {
    TextEditGroup group = createTextEditGroup(FixMessages.Java50Fix_ConvertToEnhancedForLoop_description, cuRewrite);
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TightSourceRangeComputer rangeComputer;
    if (rewrite.getExtendedSourceRangeComputer() instanceof TightSourceRangeComputer) {
        rangeComputer = (TightSourceRangeComputer) rewrite.getExtendedSourceRangeComputer();
    } else {
        rangeComputer = new TightSourceRangeComputer();
    }
    rangeComputer.addTightSourceNode(getForStatement());
    rewrite.setTargetSourceRangeComputer(rangeComputer);
    Statement statement = convert(cuRewrite, group, positionGroups);
    rewrite.replace(getForStatement(), statement, group);
}
Also used : TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) Statement(org.eclipse.jdt.core.dom.Statement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 9 with TightSourceRangeComputer

use of org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer in project che by eclipse.

the class LocalCorrectionsSubProcessor method getUnusedObjectAllocationProposals.

public static void getUnusedObjectAllocationProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    CompilationUnit root = context.getASTRoot();
    AST ast = root.getAST();
    ASTNode selectedNode = problem.getCoveringNode(root);
    if (selectedNode == null) {
        return;
    }
    ASTNode parent = selectedNode.getParent();
    if (parent instanceof ExpressionStatement) {
        ExpressionStatement expressionStatement = (ExpressionStatement) parent;
        Expression expr = expressionStatement.getExpression();
        ITypeBinding exprType = expr.resolveTypeBinding();
        if (exprType != null && Bindings.isSuperType(ast.resolveWellKnownType("java.lang.Throwable"), exprType)) {
            //$NON-NLS-1$
            ASTRewrite rewrite = ASTRewrite.create(ast);
            TightSourceRangeComputer sourceRangeComputer = new TightSourceRangeComputer();
            rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
            ThrowStatement throwStatement = ast.newThrowStatement();
            throwStatement.setExpression((Expression) rewrite.createMoveTarget(expr));
            sourceRangeComputer.addTightSourceNode(expressionStatement);
            rewrite.replace(expressionStatement, throwStatement, null);
            String label = CorrectionMessages.LocalCorrectionsSubProcessor_throw_allocated_description;
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
            LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.THROW_ALLOCATED_OBJECT, image);
            proposal.setEndPosition(rewrite.track(throwStatement));
            proposals.add(proposal);
        }
        MethodDeclaration method = ASTResolving.findParentMethodDeclaration(selectedNode);
        if (method != null && !method.isConstructor()) {
            ASTRewrite rewrite = ASTRewrite.create(ast);
            TightSourceRangeComputer sourceRangeComputer = new TightSourceRangeComputer();
            rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
            ReturnStatement returnStatement = ast.newReturnStatement();
            returnStatement.setExpression((Expression) rewrite.createMoveTarget(expr));
            sourceRangeComputer.addTightSourceNode(expressionStatement);
            rewrite.replace(expressionStatement, returnStatement, null);
            String label = CorrectionMessages.LocalCorrectionsSubProcessor_return_allocated_description;
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
            int relevance;
            ITypeBinding returnTypeBinding = method.getReturnType2().resolveBinding();
            if (returnTypeBinding != null && exprType != null && exprType.isAssignmentCompatible(returnTypeBinding)) {
                relevance = IProposalRelevance.RETURN_ALLOCATED_OBJECT_MATCH;
            } else if (method.getReturnType2() instanceof PrimitiveType && ((PrimitiveType) method.getReturnType2()).getPrimitiveTypeCode() == PrimitiveType.VOID) {
                relevance = IProposalRelevance.RETURN_ALLOCATED_OBJECT_VOID;
            } else {
                relevance = IProposalRelevance.RETURN_ALLOCATED_OBJECT;
            }
            LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, relevance, image);
            proposal.setEndPosition(rewrite.track(returnStatement));
            proposals.add(proposal);
        }
        {
            ASTRewrite rewrite = ASTRewrite.create(ast);
            rewrite.remove(parent, null);
            String label = CorrectionMessages.LocalCorrectionsSubProcessor_remove_allocated_description;
            //JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_TOOL_DELETE);
            ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_UNUSED_ALLOCATED_OBJECT, image);
            proposals.add(proposal);
        }
    }
    QuickAssistProcessor.getAssignToVariableProposals(context, selectedNode, null, proposals);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Image(org.eclipse.swt.graphics.Image) TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 10 with TightSourceRangeComputer

use of org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer in project che by eclipse.

the class QuickAssistProcessor method getConvertToMultiCatchProposals.

private static boolean getConvertToMultiCatchProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    if (!JavaModelUtil.is17OrHigher(context.getCompilationUnit().getJavaProject()))
        return false;
    CatchClause catchClause = (CatchClause) ASTResolving.findAncestor(covering, ASTNode.CATCH_CLAUSE);
    if (catchClause == null) {
        return false;
    }
    Statement statement = ASTResolving.findParentStatement(covering);
    if (statement != catchClause.getParent() && statement != catchClause.getBody()) {
        // selection is in a statement inside the body
        return false;
    }
    Type type1 = catchClause.getException().getType();
    Type selectedMultiCatchType = null;
    if (type1.isUnionType() && covering instanceof Name) {
        Name topMostName = ASTNodes.getTopMostName((Name) covering);
        ASTNode parent = topMostName.getParent();
        if (parent instanceof SimpleType || parent instanceof NameQualifiedType) {
            selectedMultiCatchType = (Type) parent;
        }
    }
    if (selectedMultiCatchType != null)
        return false;
    TryStatement tryStatement = (TryStatement) catchClause.getParent();
    List<CatchClause> catchClauses = tryStatement.catchClauses();
    if (catchClauses.size() <= 1)
        return false;
    String commonSource = null;
    try {
        IBuffer buffer = context.getCompilationUnit().getBuffer();
        for (Iterator<CatchClause> iterator = catchClauses.iterator(); iterator.hasNext(); ) {
            CatchClause catchClause1 = iterator.next();
            Block body = catchClause1.getBody();
            String source = buffer.getText(body.getStartPosition(), body.getLength());
            if (commonSource == null) {
                commonSource = source;
            } else {
                if (!commonSource.equals(source))
                    return false;
            }
        }
    } catch (JavaModelException e) {
        return false;
    }
    if (resultingCollections == null)
        return true;
    AST ast = covering.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    TightSourceRangeComputer sourceRangeComputer = new TightSourceRangeComputer();
    sourceRangeComputer.addTightSourceNode(catchClauses.get(catchClauses.size() - 1));
    rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
    CatchClause firstCatchClause = catchClauses.get(0);
    UnionType newUnionType = ast.newUnionType();
    List<Type> types = newUnionType.types();
    for (Iterator<CatchClause> iterator = catchClauses.iterator(); iterator.hasNext(); ) {
        CatchClause catchClause1 = iterator.next();
        Type type = catchClause1.getException().getType();
        if (type instanceof UnionType) {
            List<Type> types2 = ((UnionType) type).types();
            for (Iterator<Type> iterator2 = types2.iterator(); iterator2.hasNext(); ) {
                types.add((Type) rewrite.createCopyTarget(iterator2.next()));
            }
        } else {
            types.add((Type) rewrite.createCopyTarget(type));
        }
    }
    SingleVariableDeclaration newExceptionDeclaration = ast.newSingleVariableDeclaration();
    newExceptionDeclaration.setType(newUnionType);
    newExceptionDeclaration.setName((SimpleName) rewrite.createCopyTarget(firstCatchClause.getException().getName()));
    rewrite.replace(firstCatchClause.getException(), newExceptionDeclaration, null);
    for (int i = 1; i < catchClauses.size(); i++) {
        rewrite.remove(catchClauses.get(i), null);
    }
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    String label = CorrectionMessages.QuickAssistProcessor_convert_to_single_multicatch_block;
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.COMBINE_CATCH_BLOCKS, image);
    resultingCollections.add(proposal);
    return true;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) Image(org.eclipse.swt.graphics.Image) IBuffer(org.eclipse.jdt.core.IBuffer) TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IType(org.eclipse.jdt.core.IType) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Aggregations

TightSourceRangeComputer (org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer)10 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)7 Image (org.eclipse.swt.graphics.Image)4 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)3 TextEditGroup (org.eclipse.text.edits.TextEditGroup)3 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 IType (org.eclipse.jdt.core.IType)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)2 ForStatement (org.eclipse.jdt.core.dom.ForStatement)2 Statement (org.eclipse.jdt.core.dom.Statement)2 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)2 LinkedCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal)2 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 IBuffer (org.eclipse.jdt.core.IBuffer)1