Search in sources :

Example 21 with NumberLiteral

use of org.eclipse.jdt.core.dom.NumberLiteral in project AutoRefactor by JnRouvignac.

the class BigNumberCleanUp method visit.

@Override
public boolean visit(final MethodInvocation visited) {
    if (visited.getExpression() == null) {
        return true;
    }
    if (getJavaMinorVersion() >= 5 && (ASTNodes.usesGivenSignature(visited, BigInteger.class.getCanonicalName(), "valueOf", // $NON-NLS-1$
    long.class.getSimpleName()) || // $NON-NLS-1$
    ASTNodes.usesGivenSignature(// $NON-NLS-1$
    visited, // $NON-NLS-1$
    BigDecimal.class.getCanonicalName(), // $NON-NLS-1$
    "valueOf", long.class.getSimpleName()) || // $NON-NLS-1$
    ASTNodes.usesGivenSignature(// $NON-NLS-1$
    visited, // $NON-NLS-1$
    BigDecimal.class.getCanonicalName(), // $NON-NLS-1$
    "valueOf", double.class.getSimpleName()))) {
        ITypeBinding typeBinding = visited.getExpression().resolveTypeBinding();
        Expression arg0 = (Expression) visited.arguments().get(0);
        if (arg0 instanceof NumberLiteral) {
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            String token = ((NumberLiteral) arg0).getToken().replaceFirst("[lLfFdD]$", "").replace("_", "");
            if (token.contains(".") && ASTNodes.hasType(typeBinding, BigDecimal.class.getCanonicalName())) {
                // $NON-NLS-1$
                TextEditGroup group = new TextEditGroup(MultiFixMessages.BigNumberCleanUp_description);
                ASTRewrite rewrite = cuRewrite.getASTRewrite();
                rewrite.replace(visited, getClassInstanceCreatorNode(visited.getExpression(), token), group);
            } else if (JavaConstants.ZERO_LONG_LITERAL_RE.matcher(token).matches()) {
                // $NON-NLS-1$
                replaceWithQualifiedName(visited, typeBinding, "ZERO");
            } else if (JavaConstants.ONE_LONG_LITERAL_RE.matcher(token).matches()) {
                // $NON-NLS-1$
                replaceWithQualifiedName(visited, typeBinding, "ONE");
            } else if (JavaConstants.TEN_LONG_LITERAL_RE.matcher(token).matches()) {
                // $NON-NLS-1$
                replaceWithQualifiedName(visited, typeBinding, "TEN");
            } else {
                return true;
            }
            return false;
        }
    }
    return true;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) BigInteger(java.math.BigInteger) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) BigDecimal(java.math.BigDecimal) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 22 with NumberLiteral

use of org.eclipse.jdt.core.dom.NumberLiteral in project AutoRefactor by JnRouvignac.

the class ObsoleteUppercaseNumberSuffixRatherThanLowercaseCleanUp method useUppercase.

private void useUppercase(final NumberLiteral node, final String token) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteUppercaseNumberSuffixRatherThanLowercaseCleanUp_description);
    String newToken = token.substring(0, token.length() - 1) + token.substring(token.length() - 1).toUpperCase();
    NumberLiteral replacement = ast.newNumberLiteral(newToken);
    ASTNodes.replaceButKeepComment(rewrite, node, replacement, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 23 with NumberLiteral

use of org.eclipse.jdt.core.dom.NumberLiteral in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final ArrayAccess node) {
    Expression _index = node.getIndex();
    if ((_index instanceof NumberLiteral)) {
        node.getArray().accept(this);
        this.appendToBuffer(".get(");
        node.getIndex().accept(this);
        this.appendToBuffer(")");
    } else {
        final String arrayname = this.computeArrayName(node);
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("{val _rdIndx_");
        _builder.append(arrayname);
        _builder.append("=");
        this.appendToBuffer(_builder.toString());
        node.getIndex().accept(this);
        this.appendSpaceToBuffer();
        node.getArray().accept(this);
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append(".get(_rdIndx_");
        _builder_1.append(arrayname);
        _builder_1.append(")}");
        this.appendToBuffer(_builder_1.toString());
    }
    return false;
}
Also used : InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 24 with NumberLiteral

use of org.eclipse.jdt.core.dom.NumberLiteral in project evosuite by EvoSuite.

the class TestExtractingVisitor method retrieveTypeClass.

/**
 * <p>
 * retrieveTypeClass
 * </p>
 *
 * @param argument
 *            a {@link java.lang.Object} object.
 * @return a {@link java.lang.Class} object.
 */
protected Class<?> retrieveTypeClass(Object argument) {
    assert argument != null;
    if (argument instanceof SimpleType) {
        SimpleType simpleType = (SimpleType) argument;
        return retrieveTypeClass(simpleType);
    }
    if (argument instanceof ITypeBinding) {
        ITypeBinding binding = (ITypeBinding) argument;
        return retrieveTypeClass(binding);
    }
    if (argument instanceof IVariableBinding) {
        IVariableBinding variableBinding = (IVariableBinding) argument;
        return retrieveTypeClass(variableBinding.getType());
    }
    if (argument instanceof SimpleName) {
        SimpleName simpleName = (SimpleName) argument;
        return retrieveTypeClass(simpleName.resolveBinding());
    }
    if (argument instanceof StringLiteral) {
        return String.class;
    }
    if (argument instanceof NumberLiteral) {
        return retrieveTypeClass((NumberLiteral) argument);
    }
    if (argument instanceof PrimitiveType) {
        PrimitiveType primitiveType = (PrimitiveType) argument;
        String typeCode = primitiveType.getPrimitiveTypeCode().toString();
        Class<?> result = PRIMITIVE_TYPECODE_MAPPING.get(typeCode);
        assert result != null : "Could not resolve typecode " + typeCode + ".";
        return result;
    }
    if (argument instanceof ArrayType) {
        ArrayType arrayType = (ArrayType) argument;
        return retrieveTypeClass(arrayType);
    }
    if (argument instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) argument;
        return retrieveTypeClass(parameterizedType.getType());
    }
    if (argument instanceof VariableDeclarationFragment) {
        VariableDeclarationFragment varDeclFrgmnt = (VariableDeclarationFragment) argument;
        return retrieveTypeClass(varDeclFrgmnt.resolveBinding());
    }
    if (argument instanceof InfixExpression) {
        InfixExpression infixExpr = (InfixExpression) argument;
        ITypeBinding refTypeBinding = infixExpr.resolveTypeBinding();
        if (refTypeBinding != null) {
            return retrieveTypeClass(refTypeBinding);
        } else {
            throw new RuntimeException("Could not determine type class of infix expression '" + infixExpr + "'.");
        }
    }
    if (argument instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) argument;
        ITypeBinding typeBinding = methodInvocation.resolveTypeBinding();
        if (typeBinding != null) {
            return retrieveTypeClass(typeBinding);
        }
        Expression typeExpression = methodInvocation.getExpression();
        if (typeExpression instanceof MethodInvocation) {
            MethodInvocation parentMethodInvocation = (MethodInvocation) typeExpression;
            IMethodBinding parentMethodBinding = parentMethodInvocation.resolveMethodBinding();
            return retrieveTypeClass(parentMethodBinding.getDeclaringClass());
        } else {
            return retrieveTypeClass(typeExpression);
        }
    }
    if (argument instanceof ArrayAccess) {
        ArrayAccess arrayAccess = (ArrayAccess) argument;
        return retrieveTypeClass(arrayAccess.getArray());
    }
    if (argument instanceof Class<?>) {
        return (Class<?>) argument;
    }
    if (argument instanceof ClassInstanceCreation) {
        return retrieveTypeClass(((ClassInstanceCreation) argument).resolveTypeBinding());
    }
    if (argument instanceof BooleanLiteral) {
        return Boolean.TYPE;
    }
    throw new UnsupportedOperationException("Retrieval of type " + argument.getClass() + " not implemented yet!");
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) PrimitiveExpression(org.evosuite.testcase.PrimitiveExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) GenericClass(org.evosuite.utils.GenericClass) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 25 with NumberLiteral

use of org.eclipse.jdt.core.dom.NumberLiteral in project evosuite by EvoSuite.

the class TestExtractingVisitor method retrieveVariableReference.

private VariableReference retrieveVariableReference(PrefixExpression prefixExpr, Class<?> numberClass) {
    if (prefixExpr.getOperator() == org.eclipse.jdt.core.dom.PrefixExpression.Operator.MINUS) {
        NumberLiteral numberLiteral = (NumberLiteral) prefixExpr.getOperand();
        Number value = (Number) numberLiteral.resolveConstantExpressionValue();
        value = negate(value);
        PrimitiveStatement<?> numberAssignment = createPrimitiveStatement(numberClass, value);
        testCase.addStatement(numberAssignment);
        return numberAssignment.getReturnValue();
    }
    throw new UnsupportedOperationException("Prefix " + prefixExpr + " not implemented!");
}
Also used : NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Aggregations

NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)28 Expression (org.eclipse.jdt.core.dom.Expression)11 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)11 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)10 CastExpression (org.eclipse.jdt.core.dom.CastExpression)9 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)8 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)6 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)6 StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)6 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)6 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)6 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)5 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)5 Name (org.eclipse.jdt.core.dom.Name)5 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)4 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)4 SimpleType (org.eclipse.jdt.core.dom.SimpleType)4 TextEditGroup (org.eclipse.text.edits.TextEditGroup)4 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)3 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)3