Search in sources :

Example 16 with NumberLiteral

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

the class BigNumberRefactoring method visit.

@Override
public boolean visit(MethodInvocation node) {
    if (node.getExpression() == null) {
        return VISIT_SUBTREE;
    }
    if (getJavaMinorVersion() >= 5 && (isMethod(node, "java.math.BigInteger", "valueOf", "long") || isMethod(node, "java.math.BigDecimal", "valueOf", "long") || isMethod(node, "java.math.BigDecimal", "valueOf", "double"))) {
        final ITypeBinding typeBinding = node.getExpression().resolveTypeBinding();
        final Expression arg0 = arg0(node);
        if (arg0 instanceof NumberLiteral) {
            final String token = ((NumberLiteral) arg0).getToken().replaceFirst("[lLfFdD]$", "");
            if (token.contains(".") && hasType(typeBinding, "java.math.BigDecimal")) {
                this.ctx.getRefactorings().replace(node, getClassInstanceCreatorNode((Name) node.getExpression(), token));
            } else if (ZERO_LONG_LITERAL_RE.matcher(token).matches()) {
                replaceWithQualifiedName(node, typeBinding, "ZERO");
            } else if (ONE_LONG_LITERAL_RE.matcher(token).matches()) {
                replaceWithQualifiedName(node, typeBinding, "ONE");
            } else if (TEN_LONG_LITERAL_RE.matcher(token).matches()) {
                replaceWithQualifiedName(node, typeBinding, "TEN");
            } else {
                return VISIT_SUBTREE;
            }
            return DO_NOT_VISIT_SUBTREE;
        }
    } else if (!(node.getParent() instanceof PrefixExpression) || !hasOperator((PrefixExpression) node.getParent(), NOT)) {
        return maybeReplaceEquals(true, node, node);
    }
    return VISIT_SUBTREE;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral) Name(org.eclipse.jdt.core.dom.Name)

Example 17 with NumberLiteral

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

the class StubUtility method getBaseNameFromExpression.

private static String getBaseNameFromExpression(IJavaProject project, Expression assignedExpression, int variableKind) {
    String name = null;
    if (assignedExpression instanceof CastExpression) {
        assignedExpression = ((CastExpression) assignedExpression).getExpression();
    }
    if (assignedExpression instanceof Name) {
        Name simpleNode = (Name) assignedExpression;
        IBinding binding = simpleNode.resolveBinding();
        if (binding instanceof IVariableBinding)
            return getBaseName((IVariableBinding) binding, project);
        return ASTNodes.getSimpleNameIdentifier(simpleNode);
    } else if (assignedExpression instanceof MethodInvocation) {
        name = ((MethodInvocation) assignedExpression).getName().getIdentifier();
    } else if (assignedExpression instanceof SuperMethodInvocation) {
        name = ((SuperMethodInvocation) assignedExpression).getName().getIdentifier();
    } else if (assignedExpression instanceof FieldAccess) {
        return ((FieldAccess) assignedExpression).getName().getIdentifier();
    } else if (variableKind == NamingConventions.VK_STATIC_FINAL_FIELD && (assignedExpression instanceof StringLiteral || assignedExpression instanceof NumberLiteral)) {
        String string = assignedExpression instanceof StringLiteral ? ((StringLiteral) assignedExpression).getLiteralValue() : ((NumberLiteral) assignedExpression).getToken();
        StringBuffer res = new StringBuffer();
        boolean needsUnderscore = false;
        for (int i = 0; i < string.length(); i++) {
            char ch = string.charAt(i);
            if (Character.isJavaIdentifierPart(ch)) {
                if (res.length() == 0 && !Character.isJavaIdentifierStart(ch) || needsUnderscore) {
                    res.append('_');
                }
                res.append(ch);
                needsUnderscore = false;
            } else {
                needsUnderscore = res.length() > 0;
            }
        }
        if (res.length() > 0) {
            return res.toString();
        }
    }
    if (name != null) {
        for (int i = 0; i < KNOWN_METHOD_NAME_PREFIXES.length; i++) {
            String curr = KNOWN_METHOD_NAME_PREFIXES[i];
            if (name.startsWith(curr)) {
                if (name.equals(curr)) {
                    // don't suggest 'get' as variable name
                    return null;
                } else if (Character.isUpperCase(name.charAt(curr.length()))) {
                    return name.substring(curr.length());
                }
            }
        }
    }
    return name;
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Name(org.eclipse.jdt.core.dom.Name) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) CastExpression(org.eclipse.jdt.core.dom.CastExpression) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 18 with NumberLiteral

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

the class ObsoleteLambdaCleanUp method visit.

@Override
public boolean visit(final LambdaExpression node) {
    if (node.hasParentheses() && node.parameters().size() == 1 && node.parameters().get(0) instanceof VariableDeclarationFragment) {
        // TODO it should also be possible to deal with a SingleVariableDeclaration
        // when the type matches the expected inferred type
        // To do this, we should visit the whole block and check the target type
        removeParamParentheses(node);
        return false;
    }
    if (node.getBody() instanceof Block) {
        List<Statement> statements = ASTNodes.asList((Block) node.getBody());
        if (statements.size() == 1 && statements.get(0) instanceof ReturnStatement) {
            removeReturnAndBrackets(node, statements);
            return false;
        }
    } else if (node.getBody() instanceof ClassInstanceCreation) {
        ClassInstanceCreation ci = (ClassInstanceCreation) node.getBody();
        List<Expression> arguments = ci.arguments();
        if (ci.resolveTypeBinding() != null && ci.getAnonymousClassDeclaration() == null && node.parameters().size() == arguments.size() && areSameIdentifiers(node, arguments)) {
            replaceByCreationReference(node, ci);
            return false;
        }
    } else if (node.getBody() instanceof SuperMethodInvocation) {
        SuperMethodInvocation smi = (SuperMethodInvocation) node.getBody();
        List<Expression> arguments = smi.arguments();
        if (node.parameters().size() == arguments.size() && areSameIdentifiers(node, arguments)) {
            replaceBySuperMethodReference(node, smi);
            return false;
        }
    } else if (node.getBody() instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) node.getBody();
        Expression calledExpression = methodInvocation.getExpression();
        ITypeBinding calledType = ASTNodes.getCalledType(methodInvocation);
        List<Expression> arguments = methodInvocation.arguments();
        if (node.parameters().size() == arguments.size()) {
            if (!areSameIdentifiers(node, arguments)) {
                return true;
            }
            if (isStaticMethod(methodInvocation)) {
                if (!arguments.isEmpty()) {
                    String[] remainingParams = new String[arguments.size() - 1];
                    for (int i = 0; i < arguments.size() - 1; i++) {
                        ITypeBinding argumentBinding = arguments.get(i + 1).resolveTypeBinding();
                        if (argumentBinding == null) {
                            return true;
                        }
                        remainingParams[i] = argumentBinding.getQualifiedName();
                    }
                    for (IMethodBinding methodBinding : calledType.getDeclaredMethods()) {
                        if ((methodBinding.getModifiers() & Modifier.STATIC) == 0 && ASTNodes.usesGivenSignature(methodBinding, calledType.getQualifiedName(), methodInvocation.getName().getIdentifier(), remainingParams)) {
                            return true;
                        }
                    }
                }
                replaceByTypeReference(node, methodInvocation);
                return false;
            }
            if (calledExpression == null || calledExpression instanceof StringLiteral || calledExpression instanceof NumberLiteral || calledExpression instanceof ThisExpression) {
                replaceByMethodReference(node, methodInvocation);
                return false;
            }
            if (calledExpression instanceof FieldAccess) {
                FieldAccess fieldAccess = (FieldAccess) calledExpression;
                if (fieldAccess.resolveFieldBinding().isEffectivelyFinal()) {
                    replaceByMethodReference(node, methodInvocation);
                    return false;
                }
            } else if (calledExpression instanceof SuperFieldAccess) {
                SuperFieldAccess fieldAccess = (SuperFieldAccess) calledExpression;
                if (fieldAccess.resolveFieldBinding().isEffectivelyFinal()) {
                    replaceByMethodReference(node, methodInvocation);
                    return false;
                }
            }
        } else if (calledExpression instanceof SimpleName && node.parameters().size() == arguments.size() + 1) {
            SimpleName calledObject = (SimpleName) calledExpression;
            if (isSameIdentifier(node, 0, calledObject)) {
                for (int i = 0; i < arguments.size(); i++) {
                    SimpleName expression = ASTNodes.as(arguments.get(i), SimpleName.class);
                    if (expression == null || !isSameIdentifier(node, i + 1, expression)) {
                        return true;
                    }
                }
                ITypeBinding klass = calledExpression.resolveTypeBinding();
                if (klass == null) {
                    return true;
                }
                String[] remainingParams = new String[arguments.size() + 1];
                remainingParams[0] = klass.getQualifiedName();
                for (int i = 0; i < arguments.size(); i++) {
                    ITypeBinding argumentBinding = arguments.get(i).resolveTypeBinding();
                    if (argumentBinding == null) {
                        return true;
                    }
                    remainingParams[i + 1] = argumentBinding.getQualifiedName();
                }
                for (IMethodBinding methodBinding : klass.getDeclaredMethods()) {
                    if ((methodBinding.getModifiers() & Modifier.STATIC) != 0 && ASTNodes.usesGivenSignature(methodBinding, klass.getQualifiedName(), methodInvocation.getName().getIdentifier(), remainingParams)) {
                        return true;
                    }
                }
                replaceByTypeReference(node, methodInvocation);
                return false;
            }
        }
    }
    return true;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) List(java.util.List) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 19 with NumberLiteral

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

the class ObsoleteRemoveUnnecessaryCastCleanUp method createPrimitive.

private void createPrimitive(final CastExpression visited, final PrefixExpression.Operator prefixOperator, final NumberLiteral literal, final char postfix) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRemoveUnnecessaryCastCleanUp_description);
    NumberLiteral numberLiteral = ast.newNumberLiteral(literal.getToken() + postfix);
    if (prefixOperator != null) {
        PrefixExpression prefixExpression = ast.newPrefixExpression();
        prefixExpression.setOperator(prefixOperator);
        prefixExpression.setOperand(numberLiteral);
        ASTNodes.replaceButKeepComment(rewrite, visited, prefixExpression, group);
    } else {
        ASTNodes.replaceButKeepComment(rewrite, visited, numberLiteral, group);
    }
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 20 with NumberLiteral

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

the class ObsoleteRemoveUnnecessaryCastCleanUp method visit.

@Override
public boolean visit(final CastExpression visited) {
    Expression expression = visited.getExpression();
    PrefixExpression prefixExpression = ASTNodes.as(expression, PrefixExpression.class);
    NumberLiteral literal = ASTNodes.as(expression, NumberLiteral.class);
    PrefixExpression.Operator prefixOperator = null;
    if (prefixExpression != null) {
        if (ASTNodes.hasOperator(prefixExpression, PrefixExpression.Operator.MINUS)) {
            prefixOperator = PrefixExpression.Operator.MINUS;
            literal = ASTNodes.as(prefixExpression.getOperand(), NumberLiteral.class);
        } else if (ASTNodes.hasOperator(prefixExpression, PrefixExpression.Operator.PLUS)) {
            prefixOperator = PrefixExpression.Operator.PLUS;
            literal = ASTNodes.as(prefixExpression.getOperand(), NumberLiteral.class);
        } else {
            literal = null;
        }
    }
    if (literal != null && (literal.getToken().matches(".*[^lLdDfF]") || literal.getToken().matches("0x.*[^lL]"))) {
        // $NON-NLS-1$ //$NON-NLS-2$
        if (ASTNodes.hasType(visited.getType().resolveBinding(), long.class.getSimpleName())) {
            createPrimitive(visited, prefixOperator, literal, 'L');
            return false;
        }
        if (ASTNodes.hasType(visited.getType().resolveBinding(), float.class.getSimpleName())) {
            createPrimitive(visited, prefixOperator, literal, 'F');
            return false;
        }
        if (ASTNodes.hasType(visited.getType().resolveBinding(), double.class.getSimpleName())) {
            createPrimitive(visited, prefixOperator, literal, 'D');
            return false;
        }
    }
    if (canRemoveCast(visited)) {
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRemoveUnnecessaryCastCleanUp_description);
        ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodes.createMoveTarget(rewrite, visited.getExpression()), group);
        return false;
    }
    return true;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) 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