Search in sources :

Example 1 with NumberLiteral

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

the class ConvertForLoopOperation method getIndexBindingFromFragment.

/*
	 * Must be one of:
	 * <ul>
	 * <li>[result]= 0</li>
	 * </ul>
	 */
private IVariableBinding getIndexBindingFromFragment(VariableDeclarationFragment fragment) {
    Expression initializer = fragment.getInitializer();
    if (!(initializer instanceof NumberLiteral))
        return null;
    NumberLiteral number = (NumberLiteral) initializer;
    if (!LITERAL_0.equals(number.getToken()))
        return null;
    return (IVariableBinding) fragment.getName().resolveBinding();
}
Also used : PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 2 with NumberLiteral

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

the class BigDecimalRefactoring method visit.

@Override
public boolean visit(MethodInvocation node) {
    if (node.getExpression() == null) {
        return VISIT_SUBTREE;
    }
    if (getJavaMinorVersion() >= 5 && (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 NumberLiteral nb = (NumberLiteral) arg0;
            if (nb.getToken().contains(".")) {
                this.ctx.getRefactorings().replace(node, getClassInstanceCreatorNode((Name) node.getExpression(), nb.getToken()));
            } else if (ZERO_LONG_LITERAL_RE.matcher(nb.getToken()).matches()) {
                replaceWithQualifiedName(node, typeBinding, "ZERO");
            } else if (ONE_LONG_LITERAL_RE.matcher(nb.getToken()).matches()) {
                replaceWithQualifiedName(node, typeBinding, "ONE");
            } else if (TEN_LONG_LITERAL_RE.matcher(nb.getToken()).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 3 with NumberLiteral

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

the class SourceUtils method preprocessAST.

public static List<Edit> preprocessAST(CompilationUnit cu) {
    final List<Edit> edits = new ArrayList<>();
    // Walk the tree
    cu.accept(new ASTVisitor() {

        @Override
        public boolean visit(SimpleType node) {
            // replace "color" with "int"
            if ("color".equals(node.getName().toString())) {
                edits.add(Edit.replace(node.getStartPosition(), node.getLength(), "int"));
            }
            return super.visit(node);
        }

        @Override
        public boolean visit(NumberLiteral node) {
            // add 'f' to floats
            String s = node.getToken().toLowerCase();
            if (FLOATING_POINT_LITERAL_VERIFIER.matcher(s).matches() && !s.endsWith("f") && !s.endsWith("d")) {
                edits.add(Edit.insert(node.getStartPosition() + node.getLength(), "f"));
            }
            return super.visit(node);
        }

        @Override
        public boolean visit(MethodDeclaration node) {
            // add 'public' to methods with default visibility
            int accessModifiers = node.getModifiers() & ACCESS_MODIFIERS_MASK;
            if (accessModifiers == 0) {
                edits.add(Edit.insert(node.getStartPosition(), "public "));
            }
            return super.visit(node);
        }
    });
    return edits;
}
Also used : SimpleType(org.eclipse.jdt.core.dom.SimpleType) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ArrayList(java.util.ArrayList) Edit(processing.mode.java.pdex.TextTransform.Edit) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 4 with NumberLiteral

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

the class RemoveUnnecessaryCastRefactoring method visit.

@Override
public boolean visit(CastExpression node) {
    final NumberLiteral literal = as(node.getExpression(), NumberLiteral.class);
    if (literal != null && (literal.getToken().matches(".*[^lLdDfF]") || literal.getToken().matches("0x.*[^lL]"))) {
        if (hasType(node.getType().resolveBinding(), "long")) {
            createPrimitive(node, literal, 'L');
            return DO_NOT_VISIT_SUBTREE;
        }
        if (hasType(node.getType().resolveBinding(), "float")) {
            createPrimitive(node, literal, 'f');
            return DO_NOT_VISIT_SUBTREE;
        }
        if (hasType(node.getType().resolveBinding(), "double")) {
            createPrimitive(node, literal, 'd');
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    if (canRemoveCast(node)) {
        final ASTBuilder b = ctx.getASTBuilder();
        ctx.getRefactorings().replace(node, b.move(node.getExpression()));
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : ASTBuilder(org.autorefactor.refactoring.ASTBuilder) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 5 with NumberLiteral

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

the class RemoveUnnecessaryCastRefactoring method createPrimitive.

private void createPrimitive(final CastExpression node, final NumberLiteral literal, final char postfix) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final NumberLiteral numberLiteral = b.numberLiteral();
    numberLiteral.setToken(literal.getToken() + postfix);
    ctx.getRefactorings().replace(node, numberLiteral);
}
Also used : ASTBuilder(org.autorefactor.refactoring.ASTBuilder) 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