Search in sources :

Example 21 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression in project che by eclipse.

the class AssociativeInfixExpressionFragment method createCopyTarget.

public Expression createCopyTarget(ASTRewrite rewrite, boolean removeSurroundingParenthesis) throws JavaModelException {
    List<Expression> allOperands = findGroupMembersInOrderFor(fGroupRoot);
    if (allOperands.size() == fOperands.size()) {
        return (Expression) rewrite.createCopyTarget(fGroupRoot);
    }
    CompilationUnit root = (CompilationUnit) fGroupRoot.getRoot();
    ICompilationUnit cu = (ICompilationUnit) root.getJavaElement();
    String source = cu.getBuffer().getText(getStartPosition(), getLength());
    return (Expression) rewrite.createStringPlaceholder(source, ASTNode.INFIX_EXPRESSION);
//		//Todo: see whether we could copy bigger chunks of the original selection
//		// (probably only possible from extendedOperands list or from nested InfixExpressions)
//		InfixExpression result= rewrite.getAST().newInfixExpression();
//		result.setOperator(getOperator());
//		Expression first= (Expression) fOperands.get(0);
//		Expression second= (Expression) fOperands.get(1);
//		result.setLeftOperand((Expression) rewrite.createCopyTarget(first));
//		result.setRightOperand((Expression) rewrite.createCopyTarget(second));
//		for (int i= 2; i < fOperands.size(); i++) {
//			Expression next= (Expression) fOperands.get(i);
//			result.extendedOperands().add(rewrite.createCopyTarget(next));
//		}
//		return result;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression)

Example 22 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression in project che by eclipse.

the class AssociativeInfixExpressionFragment method createFragmentForFullSubtree.

public static IExpressionFragment createFragmentForFullSubtree(InfixExpression node) {
    Assert.isNotNull(node);
    if (!isAssociativeInfix(node))
        return null;
    InfixExpression groupRoot = findGroupRoot(node);
    Assert.isTrue(isAGroupRoot(groupRoot));
    List<Expression> groupMembers = AssociativeInfixExpressionFragment.findGroupMembersInOrderFor(node);
    return new AssociativeInfixExpressionFragment(groupRoot, groupMembers);
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 23 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression in project Main by SpartanRefactoring.

the class MultiplicationToCast method replacement.

@Override
public ASTNode replacement(final InfixExpression x) {
    if (x.getOperator() != Operator.TIMES)
        return null;
    int i = 0;
    boolean found = false;
    final CastExpression $ = x.getAST().newCastExpression();
    for (final Expression e : extract.allOperands(x)) {
        if (iz.hexaDecimal(e))
            continue;
        if (iz.literal(e, 1.)) {
            $.setType(x.getAST().newPrimitiveType(PrimitiveType.DOUBLE));
            found = true;
        }
        if (iz.literal(e, 1L)) {
            $.setType(x.getAST().newPrimitiveType(PrimitiveType.LONG));
            found = true;
        }
        if (found) {
            if (x.hasExtendedOperands()) {
                final List<Expression> xs = extract.allOperands(x);
                xs.remove(i);
                $.setExpression(subject.operands(xs).to(x.getOperator()));
            } else {
                if (i == 0) {
                    $.setExpression(copy.of(x.getRightOperand()));
                    return $;
                }
                $.setExpression(copy.of(x.getLeftOperand()));
            }
            return $;
        }
        ++i;
    }
    return null;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 24 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression in project Main by SpartanRefactoring.

the class $EvaluateInfixExpression method replacement.

@Override
public final ASTNode replacement(final InfixExpression x) {
    try {
        if (iz.validForEvaluation(x)) {
            final String $ = opportunisticReplacement(x);
            if ($ != null && $.length() < (x + "").length())
                return x.getAST().newNumberLiteral($);
        }
        if (indexForLeftEvaluation(x) > 1) {
            final int index = indexForLeftEvaluation(x);
            final InfixExpression cuttedExpression = subject.operands(extract.allOperands(x).subList(0, index)).to(operator());
            final List<Expression> afterExpressionOperands = extract.allOperands(x).subList(index, extract.allOperands(x).size());
            if (iz.validForEvaluation(cuttedExpression)) {
                final String str = opportunisticReplacement(cuttedExpression);
                if (str != null)
                    return subject.pair(az.expression(x.getAST().newNumberLiteral(str)), afterExpressionOperands.size() == 1 ? the.firstOf(afterExpressionOperands) : subject.operands(afterExpressionOperands).to(operator())).to(operator());
            }
        }
        if (indexForRightEvaluation(x) > 1 && operator() != DIVIDE && operator() != REMAINDER) {
            final int index = indexForRightEvaluation(x);
            final InfixExpression cuttedExpression = subject.operands(extract.allOperands(x).subList(extract.allOperands(x).size() - index, extract.allOperands(x).size())).to(operator());
            final List<Expression> beforeExpressionOperands = extract.allOperands(x).subList(0, extract.allOperands(x).size() - index);
            if (iz.validForEvaluation(cuttedExpression)) {
                final String s = opportunisticReplacement(cuttedExpression);
                if (s != null)
                    return subject.pair(beforeExpressionOperands.size() == 1 ? the.firstOf(beforeExpressionOperands) : subject.operands(beforeExpressionOperands).to(operator()), az.expression(x.getAST().newNumberLiteral(s))).to(operator());
            }
        }
    } catch (@SuppressWarnings("unused") final IllegalArgumentException __) {
        // /* Logging Java code */ monitor.logEvaluationError(this,e);
        return null;
    }
    return null;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 25 with InfixExpression

use of org.eclipse.jdt.core.dom.InfixExpression in project Main by SpartanRefactoring.

the class InfixComparisonSizeToZero method replacement.

@Override
public ASTNode replacement(final InfixExpression x) {
    final Operator $ = x.getOperator();
    if (!iz.comparison($))
        return null;
    final Expression right = right(x), left = left(x);
    return !validTypes(right, left) ? null : iz.methodInvocation(left) ? replacement($, az.methodInvocation(left), right) : replacement(op.conjugate($), az.methodInvocation(right), left);
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Aggregations

InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)95 Expression (org.eclipse.jdt.core.dom.Expression)65 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)51 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)39 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)33 CastExpression (org.eclipse.jdt.core.dom.CastExpression)31 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)26 AST (org.eclipse.jdt.core.dom.AST)24 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)20 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)19 ASTNode (org.eclipse.jdt.core.dom.ASTNode)17 Operator (org.eclipse.jdt.core.dom.InfixExpression.Operator)14 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)14 Assignment (org.eclipse.jdt.core.dom.Assignment)12 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)12 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)12 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)12 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)11 SimpleName (org.eclipse.jdt.core.dom.SimpleName)10 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)10