Example usage for org.eclipse.jdt.core.dom Expression getAST

List of usage examples for org.eclipse.jdt.core.dom Expression getAST

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom Expression getAST.

Prototype

public final AST getAST() 

Source Link

Document

Returns this node's AST.

Usage

From source file:ch.acanda.eclipse.pmd.java.resolution.sunsecure.MethodReturnsInternalArrayQuickFix.java

License:Open Source License

@Override
protected boolean apply(final ReturnStatement node) {
    final Expression expression = node.getExpression();
    final AST ast = expression.getAST();
    final MethodInvocation replacement = create(ast, MethodInvocation.class);
    replacement.setExpression(copy(expression));
    final SimpleName name = create(ast, SimpleName.class);
    name.setIdentifier("clone");
    replacement.setName(name);// www  .  j a  v a2  s  .  co  m
    return replace(expression, replacement);
}

From source file:ch.acanda.eclipse.pmd.java.resolution.SuppressWarningsQuickFix.java

License:Open Source License

@SuppressWarnings("unchecked")
private ArrayInitializer createArrayInitializer(final Expression value) {
    final AST ast = value.getAST();
    final ArrayInitializer array;
    if (value instanceof ArrayInitializer) {
        array = createArrayInitializerAndCopyExpressions(ast, (ArrayInitializer) value);

    } else {//from  w  w  w. ja va 2 s  .  com
        array = (ArrayInitializer) ast.createInstance(ArrayInitializer.class);
        array.expressions().add(ASTUtil.copy(value));
    }

    array.expressions().add(createPMDLiteralValue(ast));
    return array;
}

From source file:com.google.devtools.j2cpp.gen.CppStatementGenerator.java

License:Open Source License

private String getRightShiftType(Expression node) {
    ITypeBinding binding = node.resolveTypeBinding();
    AST ast = node.getAST();
    if (binding == null || ast.resolveWellKnownType("int").equals(binding)) {
        return "int";
    } else if (ast.resolveWellKnownType("long").equals(binding)) {
        return "long long";
    } else if (ast.resolveWellKnownType("byte").equals(binding)) {
        return "char";
    } else if (ast.resolveWellKnownType("short").equals(binding)) {
        return "short";
    } else if (ast.resolveWellKnownType("char").equals(binding)) {
        return "unichar";
    } else {/* ww  w .j av a2  s  .c  o  m*/
        throw new AssertionError("invalid right shift expression type: " + binding.getName());
    }
}

From source file:com.google.devtools.j2cpp.gen.CppStatementGenerator.java

License:Open Source License

/**
 * Converts a string concatenation expression into a NSString format string and a
 * list of arguments for it, containing all the non-literal expressions.  If the
 * expression is all literals, then a string concatenation is printed.  If not,
 * then a NSString stringWithFormat: message is output.
 *//*from w w  w.j  a  v  a2s .  c  o  m*/
@SuppressWarnings("fallthrough")
private void printStringConcatenation(Expression leftOperand, Expression rightOperand,
        List<Expression> extendedOperands, boolean needRetainRhs) {
    // Copy all operands into a single list.
    List<Expression> operands = Lists.newArrayList(leftOperand, rightOperand);
    operands.addAll(extendedOperands);

    String format = "@\"";
    List<Expression> args = Lists.newArrayList();
    for (Expression operand : operands) {
        if (operand instanceof BooleanLiteral || operand instanceof CharacterLiteral
                || operand instanceof NullLiteral) {
            format += operand.toString();
        } else if (operand instanceof StringLiteral) {
            StringLiteral literal = (StringLiteral) operand;
            if (isValidCppString(literal)) {
                String s = (((StringLiteral) operand).getEscapedValue());
                s = s.substring(1, s.length() - 1); // remove surrounding double-quotes
                s = UnicodeUtils.escapeUnicodeSequences(s);
                format += s.replace("%", "%%"); // escape % character
            } else {
                // Convert to NSString invocation when printing args.
                format += "%@";
                args.add(operand);
            }
        } else if (operand instanceof NumberLiteral) {
            format += ((NumberLiteral) operand).getToken();
        } else {
            args.add(operand);

            // Append format specifier.
            ITypeBinding operandType = Types.getTypeBinding(operand);
            if (operandType.isPrimitive()) {
                String type = operandType.getBinaryName();
                assert type.length() == 1;
                switch (type.charAt(0)) {
                case 'B': // byte
                case 'I': // int
                case 'S': // short
                    format += "%d";
                    break;
                case 'J': // long
                    format += "%qi";
                    break;
                case 'D': // double
                case 'F': // float
                    format += "%f";
                    break;
                case 'C': // char
                    format += "%c";
                    break;
                case 'Z': // boolean
                    format += "%@";
                    break;
                default:
                    throw new AssertionError("unknown primitive type: " + type);
                }
            } else {
                format += "%@";
            }
        }
    }
    format += '"';

    if (args.isEmpty()) {
        buffer.append(format.replace("%%", "%")); // unescape % character
        return;
    }

    if (needRetainRhs) {
        buffer.append("[[NSString alloc] initWithFormat:");
    } else {
        buffer.append("[NSString stringWithFormat:");
    }
    buffer.append(format);
    buffer.append(", ");
    for (Iterator<Expression> iter = args.iterator(); iter.hasNext();) {
        Expression arg = iter.next();
        if (Types.getTypeBinding(arg).isEqualTo(arg.getAST().resolveWellKnownType("boolean"))) {
            buffer.append("[JavaLangBoolean toStringWithBOOL:");
            arg.accept(this);
            buffer.append(']');
        } else if (arg instanceof StringLiteral) {
            // Strings with all valid C99 characters were previously converted,
            // so this literal needs to be defined with a char array.
            buffer.append(buildStringFromChars(((StringLiteral) arg).getLiteralValue()));
        } else {
            arg.accept(this);
        }
        if (iter.hasNext()) {
            buffer.append(", ");
        }
    }
    buffer.append(']');
}

From source file:com.intel.ide.eclipse.mpt.ast.UnresolvedElementsSubProcessor.java

License:Open Source License

private static ITypeBinding[] getParameterTypes(List<Expression> args) {
    ITypeBinding[] params = new ITypeBinding[args.size()];
    for (int i = 0; i < args.size(); i++) {
        Expression expr = args.get(i);
        ITypeBinding curr = Bindings.normalizeTypeBinding(expr.resolveTypeBinding());
        if (curr != null && curr.isWildcardType()) {
            curr = ASTResolving.normalizeWildcardType(curr, true, expr.getAST());
        }/*from  ww w  .j a  v  a2 s .c o m*/
        if (curr == null) {
            curr = expr.getAST().resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
        }
        params[i] = curr;
    }
    return params;
}

From source file:com.intel.ide.eclipse.mpt.ast.UnresolvedElementsSubProcessor.java

License:Open Source License

private static ChangeDescription[] createSignatureChangeDescription(int[] indexOfDiff, int nDiffs,
        ITypeBinding[] paramTypes, List<Expression> arguments, ITypeBinding[] argTypes) {
    ChangeDescription[] changeDesc = new ChangeDescription[paramTypes.length];
    for (int i = 0; i < nDiffs; i++) {
        int diffIndex = indexOfDiff[i];
        Expression arg = arguments.get(diffIndex);
        String name = getExpressionBaseName(arg);
        ITypeBinding argType = argTypes[diffIndex];
        if (argType.isWildcardType()) {
            argType = ASTResolving.normalizeWildcardType(argType, true, arg.getAST());
            if (argType == null) {
                return null;
            }//from   w  w w  .j  a v  a 2s.co m
        }
        changeDesc[diffIndex] = new EditDescription(argType, name);
    }
    return changeDesc;
}

From source file:com.intel.ide.eclipse.mpt.ast.UnresolvedElementsSubProcessor.java

License:Open Source License

private static ITypeBinding[] getArgumentTypes(List<Expression> arguments) {
    ITypeBinding[] res = new ITypeBinding[arguments.size()];
    for (int i = 0; i < res.length; i++) {
        Expression expression = arguments.get(i);
        ITypeBinding curr = expression.resolveTypeBinding();
        if (curr == null) {
            return null;
        }//from   ww  w  .j a  v a2  s . c o m
        if (!curr.isNullType()) { // don't normalize null type
            curr = Bindings.normalizeTypeBinding(curr);
            if (curr == null) {
                curr = expression.getAST().resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
            }
        }
        res[i] = curr;
    }
    return res;
}

From source file:org.decojer.cavaj.utils.Expressions.java

License:Open Source License

/**
 * New assignment expression./* w  w w.  j av a2 s .com*/
 *
 * @param operator
 *            assignment expression operator
 * @param leftOperand
 *            left operand expression
 * @param rightOperand
 *            right operand expression
 * @param op
 *            originating operation
 * @return expression
 */
@Nonnull
public static Assignment newAssignment(@Nullable final Assignment.Operator operator,
        @Nonnull final Expression leftOperand, @Nonnull final Expression rightOperand, @Nonnull final Op op) {
    final Assignment assignment = setOp(leftOperand.getAST().newAssignment(), op);
    assignment.setOperator(operator);
    final int operatorPriority = Priority.priority(assignment).getPriority();
    assignment.setLeftHandSide(wrap(leftOperand, operatorPriority));
    assignment.setRightHandSide(wrap(rightOperand, operatorPriority));
    return assignment;
}

From source file:org.decojer.cavaj.utils.Expressions.java

License:Open Source License

/**
 * New infix expression.// www .java  2 s. c  om
 *
 * @param operator
 *            infix expression operator
 * @param leftOperand
 *            left operand expression
 * @param rightOperand
 *            right operand expression
 * @param op
 *            originating operation
 * @return expression
 */
@Nonnull
public static InfixExpression newInfixExpression(@Nullable final InfixExpression.Operator operator,
        @Nonnull final Expression leftOperand, @Nonnull final Expression rightOperand, @Nonnull final Op op) {
    final InfixExpression infixExpression = setOp(leftOperand.getAST().newInfixExpression(), op);
    infixExpression.setOperator(operator);
    final int operatorPriority = Priority.priority(infixExpression).getPriority();
    infixExpression.setLeftOperand(wrap(leftOperand, operatorPriority));
    // more operators possible, but PLUS... really necessary here?!
    final boolean assoc = operator == InfixExpression.Operator.PLUS
            || operator == InfixExpression.Operator.CONDITIONAL_AND
            || operator == InfixExpression.Operator.CONDITIONAL_OR;
    infixExpression.setRightOperand(wrap(rightOperand, operatorPriority - (assoc ? 0 : 1)));
    return infixExpression;
}

From source file:org.decojer.cavaj.utils.Expressions.java

License:Open Source License

/**
 * New postfix expression./*  w ww .jav a 2 s . c o m*/
 *
 * @param operator
 *            postfix expression operator
 * @param operand
 *            operand expression
 * @param op
 *            originating operation
 * @return expression
 */
@Nonnull
public static PostfixExpression newPostfixExpression(@Nullable final PostfixExpression.Operator operator,
        @Nonnull final Expression operand, @Nonnull final Op op) {
    final PostfixExpression postfixExpression = setOp(operand.getAST().newPostfixExpression(), op);
    postfixExpression.setOperator(operator);
    postfixExpression.setOperand(wrap(operand, Priority.priority(postfixExpression)));
    return postfixExpression;
}