Search in sources :

Example 6 with NumberLiteral

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

the class CapitalizeLongLiteralRefactoring method replaceLong.

private void replaceLong(final NumberLiteral node, final String token) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final NumberLiteral replacement = b.numberLiteral();
    final String newToken = token.substring(0, token.length() - 1) + "L";
    replacement.setToken(newToken);
    ctx.getRefactorings().replace(node, replacement);
}
Also used : ASTBuilder(org.autorefactor.refactoring.ASTBuilder) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 7 with NumberLiteral

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

the class GenerateForLoopAssistProposal method getForInitializer.

/**
	 * Generates a {@link VariableDeclarationExpression}, which initializes the loop variable to
	 * iterate over an array.
	 * 
	 * @param ast the current {@link AST} instance
	 * @param loopVariableName the name of the variable which should be initialized
	 * @return a filled {@link VariableDeclarationExpression}, declaring a int variable, which is
	 *         initializes with 0
	 */
private VariableDeclarationExpression getForInitializer(AST ast, SimpleName loopVariableName) {
    // initializing fragment
    VariableDeclarationFragment firstDeclarationFragment = ast.newVariableDeclarationFragment();
    firstDeclarationFragment.setName(loopVariableName);
    NumberLiteral startIndex = ast.newNumberLiteral();
    firstDeclarationFragment.setInitializer(startIndex);
    // declaration
    VariableDeclarationExpression variableDeclaration = ast.newVariableDeclarationExpression(firstDeclarationFragment);
    PrimitiveType variableType = ast.newPrimitiveType(PrimitiveType.INT);
    variableDeclaration.setType(variableType);
    return variableDeclaration;
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 8 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 9 with NumberLiteral

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

the class GetterSetterUtil method createInfixInvocationFromPostPrefixExpression.

private static Expression createInfixInvocationFromPostPrefixExpression(InfixExpression.Operator operator, Expression getterExpression, AST ast, ITypeBinding variableType, boolean is50OrHigher) {
    InfixExpression infix = ast.newInfixExpression();
    infix.setLeftOperand(getterExpression);
    infix.setOperator(operator);
    NumberLiteral number = ast.newNumberLiteral();
    //$NON-NLS-1$
    number.setToken("1");
    infix.setRightOperand(number);
    ITypeBinding infixType = infix.resolveTypeBinding();
    return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher);
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 10 with NumberLiteral

use of org.eclipse.jdt.core.dom.NumberLiteral in project che 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)

Aggregations

NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)13 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)5 Expression (org.eclipse.jdt.core.dom.Expression)4 Name (org.eclipse.jdt.core.dom.Name)4 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)4 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)3 CastExpression (org.eclipse.jdt.core.dom.CastExpression)3 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)3 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)3 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)3 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)3 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)2 IBinding (org.eclipse.jdt.core.dom.IBinding)2 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)2 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)2 StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)2 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)2 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)2 ArrayList (java.util.ArrayList)1 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)1