Search in sources :

Example 1 with CastVariable2

use of org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CastVariable2 in project che by eclipse.

the class InferTypeArgumentsRefactoring method rewriteDeclarations.

private void rewriteDeclarations(InferTypeArgumentsUpdate update, IProgressMonitor pm) throws CoreException {
    HashMap<ICompilationUnit, CuUpdate> updates = update.getUpdates();
    Set<Entry<ICompilationUnit, CuUpdate>> entrySet = updates.entrySet();
    //$NON-NLS-1$
    pm.beginTask("", entrySet.size());
    pm.setTaskName(RefactoringCoreMessages.InferTypeArgumentsRefactoring_creatingChanges);
    for (Iterator<Entry<ICompilationUnit, CuUpdate>> iter = entrySet.iterator(); iter.hasNext(); ) {
        if (pm.isCanceled())
            throw new OperationCanceledException();
        Entry<ICompilationUnit, CuUpdate> entry = iter.next();
        ICompilationUnit cu = entry.getKey();
        pm.worked(1);
        pm.subTask(BasicElementLabels.getFileName(cu));
        CompilationUnitRewrite rewrite = new CompilationUnitRewrite(cu);
        rewrite.setResolveBindings(false);
        CuUpdate cuUpdate = entry.getValue();
        for (Iterator<CollectionElementVariable2> cvIter = cuUpdate.getDeclarations().iterator(); cvIter.hasNext(); ) {
            ConstraintVariable2 cv = cvIter.next();
            rewriteConstraintVariable(cv, rewrite, fTCModel, fLeaveUnconstrainedRaw, null);
        }
        for (Iterator<CastVariable2> castsIter = cuUpdate.getCastsToRemove().iterator(); castsIter.hasNext(); ) {
            CastVariable2 castCv = castsIter.next();
            rewriteCastVariable(castCv, rewrite, fTCModel);
        }
        CompilationUnitChange change = rewrite.createChange(true);
        if (change != null) {
            fChangeManager.manage(cu, change);
        }
    }
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) Entry(java.util.Map.Entry) CastVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CastVariable2) CuUpdate(org.eclipse.jdt.internal.corext.refactoring.generics.InferTypeArgumentsUpdate.CuUpdate) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 2 with CastVariable2

use of org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CastVariable2 in project che by eclipse.

the class InferTypeArgumentsRefactoring method rewriteCastVariable.

private static ASTNode rewriteCastVariable(CastVariable2 castCv, CompilationUnitRewrite rewrite, InferTypeArgumentsTCModel tCModel) {
    //, List positionGroups) {
    ASTNode node = castCv.getRange().getNode(rewrite.getRoot());
    ConstraintVariable2 expressionVariable = castCv.getExpressionVariable();
    ConstraintVariable2 methodReceiverCv = tCModel.getMethodReceiverCv(expressionVariable);
    if (methodReceiverCv != null) {
        TType chosenReceiverType = InferTypeArgumentsConstraintsSolver.getChosenType(methodReceiverCv);
        if (chosenReceiverType == null)
            return null;
        else if (!InferTypeArgumentsTCModel.isAGenericType(chosenReceiverType))
            return null;
        else if (hasUnboundElement(methodReceiverCv, tCModel))
            return null;
    }
    CastExpression castExpression = (CastExpression) node;
    Expression expression = castExpression.getExpression();
    ASTNode nodeToReplace;
    if (castExpression.getParent() instanceof ParenthesizedExpression)
        nodeToReplace = castExpression.getParent();
    else
        nodeToReplace = castExpression;
    Expression newExpression = (Expression) rewrite.getASTRewrite().createMoveTarget(expression);
    rewrite.getASTRewrite().replace(nodeToReplace, newExpression, rewrite.createGroupDescription(RefactoringCoreMessages.InferTypeArgumentsRefactoring_removeCast));
    rewrite.getImportRemover().registerRemovedNode(nodeToReplace);
    return newExpression;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 3 with CastVariable2

use of org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CastVariable2 in project che by eclipse.

the class InferTypeArgumentsTCModel method makeCastVariable.

public CastVariable2 makeCastVariable(CastExpression castExpression, ConstraintVariable2 expressionCv) {
    ITypeBinding typeBinding = castExpression.resolveTypeBinding();
    ICompilationUnit cu = RefactoringASTParser.getCompilationUnit(castExpression);
    CompilationUnitRange range = new CompilationUnitRange(cu, castExpression);
    CastVariable2 castCv = new CastVariable2(createTType(typeBinding), range, expressionCv);
    fCastVariables.add(castCv);
    return castCv;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnitRange(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.CompilationUnitRange) CastVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CastVariable2) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 4 with CastVariable2

use of org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CastVariable2 in project che by eclipse.

the class InferTypeArgumentsConstraintsSolver method findCastsToRemove.

private void findCastsToRemove(CastVariable2[] castVariables) {
    for (int i = 0; i < castVariables.length; i++) {
        CastVariable2 castCv = castVariables[i];
        ConstraintVariable2 expressionVariable = castCv.getExpressionVariable();
        TType chosenType = InferTypeArgumentsConstraintsSolver.getChosenType(expressionVariable);
        TType castType = castCv.getType();
        TType expressionType = expressionVariable.getType();
        if (chosenType != null && TTypes.canAssignTo(chosenType, castType)) {
            if (chosenType.equals(expressionType))
                // The type has not changed. Don't remove the cast, since it could be
                continue;
            // there to get access to default-visible members or to
            // unify types of conditional expressions.
            fUpdate.addCastToRemove(castCv);
        } else if (expressionVariable instanceof ArrayTypeVariable2 && castType.isArrayType()) {
            // bug 97258
            ArrayElementVariable2 arrayElementCv = fTCModel.getArrayElementVariable(expressionVariable);
            if (arrayElementCv == null)
                continue;
            TType chosenArrayElementType = InferTypeArgumentsConstraintsSolver.getChosenType(arrayElementCv);
            if (chosenArrayElementType != null && TTypes.canAssignTo(chosenArrayElementType, ((ArrayType) castType).getComponentType())) {
                if (expressionType instanceof ArrayType && chosenArrayElementType.equals(((ArrayType) expressionType).getComponentType()))
                    // The type has not changed. Don't remove the cast, since it could be
                    continue;
                // there to unify types of conditional expressions.
                fUpdate.addCastToRemove(castCv);
            }
        }
    }
}
Also used : ArrayType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.ArrayType) CastVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CastVariable2) ArrayTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ArrayTypeVariable2) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2) ArrayElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ArrayElementVariable2)

Aggregations

CastVariable2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CastVariable2)3 ConstraintVariable2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 TType (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType)2 Entry (java.util.Map.Entry)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1 Expression (org.eclipse.jdt.core.dom.Expression)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)1 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)1 CuUpdate (org.eclipse.jdt.internal.corext.refactoring.generics.InferTypeArgumentsUpdate.CuUpdate)1 CompilationUnitRewrite (org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite)1 CompilationUnitRange (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.CompilationUnitRange)1 ArrayType (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.ArrayType)1 ArrayElementVariable2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ArrayElementVariable2)1 ArrayTypeVariable2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ArrayTypeVariable2)1 CollectionElementVariable2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2)1