Search in sources :

Example 1 with CharacterLiteral

use of org.eclipse.jdt.core.dom.CharacterLiteral in project che 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 org.eclipse.jdt.core.dom.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 2 with CharacterLiteral

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

the class AppendCharacterWithCharQuickFix method apply.

/**
 * Replaces the string literal <code>"a"</code> in <code>buffer.append("a")</code> with the character literal
 * <code>'a'</code>.
 */
@Override
protected boolean apply(final StringLiteral node) {
    final CharacterLiteral character = node.getAST().newCharacterLiteral();
    character.setEscapedValue(toCharValue(node.getEscapedValue()));
    replace(node, character);
    return true;
}
Also used : CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Example 3 with CharacterLiteral

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

the class SimplifyStartsWithQuickFix method apply.

/**
 * Rewrites <code>s.startsWith("a")</code> as <code>s.charAt(0) == 'a'</code>.
 */
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final MethodInvocation node) {
    final AST ast = node.getAST();
    final MethodInvocation charAt = ast.newMethodInvocation();
    charAt.setExpression(copy(node.getExpression()));
    charAt.setName(ast.newSimpleName("charAt"));
    charAt.arguments().add(ast.newNumberLiteral("0"));
    final CharacterLiteral character = ast.newCharacterLiteral();
    final StringLiteral s = (StringLiteral) node.arguments().get(0);
    character.setEscapedValue(s.getEscapedValue().replace('"', '\''));
    final InfixExpression eq = ast.newInfixExpression();
    eq.setOperator(Operator.EQUALS);
    eq.setLeftOperand(charAt);
    eq.setRightOperand(character);
    replace(node, eq);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Example 4 with CharacterLiteral

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

the class CompilationUnitBuilder method newLiteral.

public CharacterLiteral newLiteral(char value) {
    CharacterLiteral str = ast.newCharacterLiteral();
    str.setCharValue(value);
    return str;
}
Also used : CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Example 5 with CharacterLiteral

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

the class TestExtractingVisitor method retrieveVariableReference.

/**
 * <p>
 * retrieveVariableReference
 * </p>
 *
 * @param argument
 *            a {@link java.lang.Object} object.
 * @param varType
 *            a {@link java.lang.Class} object.
 * @return a {@link org.evosuite.testcase.VariableReference} object.
 */
protected VariableReference retrieveVariableReference(Object argument, Class<?> varType) {
    if (argument instanceof ClassInstanceCreation) {
        return retrieveVariableReference((ClassInstanceCreation) argument, varType);
    }
    if (argument instanceof VariableDeclarationFragment) {
        return retrieveVariableReference((VariableDeclarationFragment) argument);
    }
    if (argument instanceof SimpleName) {
        SimpleName simpleName = (SimpleName) argument;
        lineNumber = testReader.getLineNumber(simpleName.getStartPosition());
        return retrieveVariableReference(simpleName.resolveBinding(), varType);
    }
    if (argument instanceof IVariableBinding) {
        return retrieveVariableReference((IVariableBinding) argument, varType);
    }
    if (argument instanceof PrefixExpression) {
        return retrieveVariableReference((PrefixExpression) argument);
    }
    if (argument instanceof InfixExpression) {
        return retrieveVariableReference((InfixExpression) argument, varType);
    }
    if (argument instanceof ExpressionStatement) {
        ExpressionStatement exprStmt = (ExpressionStatement) argument;
        Expression expression = exprStmt.getExpression();
        return retrieveVariableReference(expression, varType);
    }
    if (argument instanceof NullLiteral) {
        return retrieveVariableReference((NullLiteral) argument, varType);
    }
    if (argument instanceof StringLiteral) {
        return retrieveVariableReference((StringLiteral) argument);
    }
    if (argument instanceof NumberLiteral) {
        return retrieveVariableReference((NumberLiteral) argument);
    }
    if (argument instanceof CharacterLiteral) {
        return retrieveVariableReference((CharacterLiteral) argument);
    }
    if (argument instanceof BooleanLiteral) {
        return retrieveVariableReference((BooleanLiteral) argument);
    }
    if (argument instanceof ITypeBinding) {
        if (varType != null) {
            return new ValidVariableReference(testCase.getReference(), varType);
        }
        return new ValidVariableReference(testCase.getReference(), retrieveTypeClass(argument));
    }
    if (argument instanceof QualifiedName) {
        return retrieveVariableReference((QualifiedName) argument);
    }
    if (argument instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) argument;
        VariableReference result = retrieveResultReference(methodInvocation);
        nestedCallResults.push(result);
        return result;
    }
    if (argument instanceof ArrayCreation) {
        return retrieveVariableReference((ArrayCreation) argument);
    }
    if (argument instanceof VariableDeclaration) {
        return retrieveVariableReference((VariableDeclaration) argument);
    }
    if (argument instanceof ArrayAccess) {
        // argument).getArray(), null);
        return retrieveVariableReference((ArrayAccess) argument);
    }
    if (argument instanceof Assignment) {
        return retrieveVariableReference(((Assignment) argument).getLeftHandSide(), null);
    }
    if (argument instanceof CastExpression) {
        CastExpression castExpression = (CastExpression) argument;
        VariableReference result = retrieveVariableReference(castExpression.getExpression(), null);
        Class<?> castClass = retrieveTypeClass(castExpression.resolveTypeBinding());
        assert castClass.isAssignableFrom(toClass(result.getType()));
        result.setType(castClass);
        return result;
    }
    throw new UnsupportedOperationException("Argument type " + argument.getClass() + " not implemented!");
}
Also used : 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) Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) VariableReference(org.evosuite.testcase.VariableReference) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) 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) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) CastExpression(org.eclipse.jdt.core.dom.CastExpression) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral) 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