Search in sources :

Example 6 with CharacterLiteral

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

the class ASTNodes method getEscapedCharacterLiteral.

/**
 * Escapes a character value to a literal that can be used in Java source.
 *
 * @param ch the character value
 * @return the escaped string
 * @see CharacterLiteral#getEscapedValue()
 */
public static String getEscapedCharacterLiteral(char ch) {
    CharacterLiteral characterLiteral = AST.newAST(ASTProvider.SHARED_AST_LEVEL).newCharacterLiteral();
    characterLiteral.setCharValue(ch);
    return characterLiteral.getEscapedValue();
}
Also used : CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Example 7 with CharacterLiteral

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

the class ObsoleteCharacterParameterRatherThanStringCleanUp method refactorWithCharacter.

private void refactorWithCharacter(final StringLiteral stringLiteral, final String value) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteCharacterParameterRatherThanStringCleanUp_description);
    CharacterLiteral replacement = ast.newCharacterLiteral();
    replacement.setCharValue(value.charAt(0));
    rewrite.replace(stringLiteral, 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) CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Example 8 with CharacterLiteral

use of org.eclipse.jdt.core.dom.CharacterLiteral in project eclipse-pmd by acanda.

the class UseIndexOfCharQuickFix method apply.

/**
 * Replaces the string literal <code>"a"</code> in <code>s.indexOf("a")</code> with the character literal
 * <code>'a'</code>.
 */
@Override
protected boolean apply(final MethodInvocation node) {
    @SuppressWarnings("unchecked") final List<Expression> arguments = node.arguments();
    if (arguments.size() == 1 && arguments.get(0) instanceof StringLiteral) {
        final CharacterLiteral character = node.getAST().newCharacterLiteral();
        final StringLiteral string = (StringLiteral) arguments.get(0);
        character.setEscapedValue(toCharValue(string.getEscapedValue()));
        replace(string, character);
        return true;
    }
    return false;
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Example 9 with CharacterLiteral

use of org.eclipse.jdt.core.dom.CharacterLiteral in project eclipse-pmd by acanda.

the class AddEmptyStringQuickFix method apply.

@Override
@SuppressWarnings("unchecked")
protected boolean apply(final InfixExpression node) {
    final Expression rightOperand = node.getRightOperand();
    // "" + x.toString() -> x.toString()
    if (isString(rightOperand)) {
        return replace(node, copy(rightOperand));
    }
    // "" + 'a' -> "a"
    if (isCharacterLiteral(rightOperand)) {
        final AST ast = node.getAST();
        final StringLiteral stringLiteral = ast.newStringLiteral();
        final String escapedCharacter = ((CharacterLiteral) rightOperand).getEscapedValue();
        stringLiteral.setEscapedValue(convertToEscapedString(escapedCharacter));
        return replace(node, stringLiteral);
    }
    // "" + x -> String.valueOf(x)
    final AST ast = node.getAST();
    final MethodInvocation toString = ast.newMethodInvocation();
    toString.setExpression(ast.newSimpleName("String"));
    toString.setName(ast.newSimpleName("valueOf"));
    toString.arguments().add(copy(rightOperand));
    return replace(node, toString);
}
Also used : AST(org.eclipse.jdt.core.dom.AST) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Example 10 with CharacterLiteral

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

the class CodeGenerator method createPlainInitStmt.

@SuppressWarnings("unchecked")
private void createPlainInitStmt(final int logRecNo, final Block methodBlock, final AST ast) {
    // NOTE: PLAIN INIT: has always one non-null param
    // TODO: use primitives
    final int oid = this.log.objectIds.get(logRecNo);
    if (this.oidToVarMapping.containsKey(oid)) {
        // TODO this might happen because of Integer.valueOf o.รค. . Is this approach ok?
        return;
    }
    final String type = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
    final Object value = this.log.params.get(logRecNo)[0];
    final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
    vd.setName(ast.newSimpleName(this.createNewVarName(oid, value.getClass().getName())));
    final VariableDeclarationStatement stmt = ast.newVariableDeclarationStatement(vd);
    if (value instanceof Class) {
        stmt.setType(ast.newSimpleType(ast.newSimpleName("Class")));
    } else {
        stmt.setType(this.createAstType(type, ast));
    }
    if (value instanceof Number) {
        if (value instanceof Long) {
            vd.setInitializer(ast.newNumberLiteral(String.valueOf(value) + 'l'));
        } else if (value instanceof Double) {
            vd.setInitializer(ast.newNumberLiteral(String.valueOf(value) + 'd'));
        } else {
            vd.setInitializer(ast.newNumberLiteral(String.valueOf(value)));
        }
    } else if (value instanceof String) {
        final StringLiteral literal = ast.newStringLiteral();
        literal.setLiteralValue((String) value);
        vd.setInitializer(literal);
    } else if (value instanceof Character) {
        final CharacterLiteral literal = ast.newCharacterLiteral();
        literal.setCharValue((Character) value);
        vd.setInitializer(literal);
    } else if (value instanceof Boolean) {
        final BooleanLiteral literal = ast.newBooleanLiteral((Boolean) value);
        vd.setInitializer(literal);
    } else if (value instanceof Class) {
        final TypeLiteral clazz = ast.newTypeLiteral();
        clazz.setType(ast.newSimpleType(ast.newName(((Class<?>) value).getName())));
        vd.setInitializer(clazz);
    } else {
        throw new IllegalStateException("An error occurred while creating a plain statement: unsupported type: " + value.getClass().getName());
    }
    methodBlock.statements().add(stmt);
}
Also used : BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) TypeLiteral(org.eclipse.jdt.core.dom.TypeLiteral) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Aggregations

CharacterLiteral (org.eclipse.jdt.core.dom.CharacterLiteral)11 StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)6 BooleanLiteral (org.eclipse.jdt.core.dom.BooleanLiteral)3 Expression (org.eclipse.jdt.core.dom.Expression)3 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)3 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)3 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)3 AST (org.eclipse.jdt.core.dom.AST)2 TypeLiteral (org.eclipse.jdt.core.dom.TypeLiteral)2 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)2 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)1 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)1 ArrayAccess (org.eclipse.jdt.core.dom.ArrayAccess)1 ArrayCreation (org.eclipse.jdt.core.dom.ArrayCreation)1 Assignment (org.eclipse.jdt.core.dom.Assignment)1 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)1 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)1 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1