Search in sources :

Example 6 with BooleanLiteral

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

the class PrimitiveWrapperCreationRefactoring method replaceWithBooleanLiteral.

private boolean replaceWithBooleanLiteral(QualifiedName node, boolean val) {
    final BooleanLiteral booleanLiteral = this.ctx.getASTBuilder().boolean0(val);
    this.ctx.getRefactorings().replace(node, booleanLiteral);
    return DO_NOT_VISIT_SUBTREE;
}
Also used : BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral)

Example 7 with BooleanLiteral

use of org.eclipse.jdt.core.dom.BooleanLiteral in project xtext-xtend by eclipse.

the class JavaASTFlattener method isBooleanType.

public boolean isBooleanType(final Expression expression) {
    if ((expression instanceof BooleanLiteral)) {
        return true;
    }
    if ((expression instanceof SimpleName)) {
        final Type declType = this._aSTFlattenerUtils.findDeclaredType(((SimpleName) expression));
        if ((declType != null)) {
            boolean _matched = false;
            boolean _isPrimitiveType = declType.isPrimitiveType();
            if (_isPrimitiveType) {
                _matched = true;
                PrimitiveType.Code _primitiveTypeCode = ((PrimitiveType) declType).getPrimitiveTypeCode();
                return Objects.equal(_primitiveTypeCode, PrimitiveType.BOOLEAN);
            }
        }
    }
    return false;
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) SimpleName(org.eclipse.jdt.core.dom.SimpleName) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType)

Example 8 with BooleanLiteral

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

the class LiteralRatherThanBooleanConstantRefactoring method replaceWithBooleanLiteral.

private boolean replaceWithBooleanLiteral(final QualifiedName node, final boolean val) {
    final BooleanLiteral booleanLiteral = this.ctx.getASTBuilder().boolean0(val);
    this.ctx.getRefactorings().replace(node, booleanLiteral);
    return DO_NOT_VISIT_SUBTREE;
}
Also used : BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral)

Example 9 with BooleanLiteral

use of org.eclipse.jdt.core.dom.BooleanLiteral 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)

Example 10 with BooleanLiteral

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

the class JUnitCodeGenerator method createPlainInitStmt.

@SuppressWarnings("unchecked")
@Override
public void createPlainInitStmt(CaptureLog log, int logRecNo) {
    PostProcessor.notifyRecentlyProcessedLogRecNo(logRecNo);
    // NOTE: PLAIN INIT: has always one non-null param
    // TODO: use primitives
    final int oid = 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 = log.oidClassNames.get(log.oidRecMapping.get(oid));
    final Object value = 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

BooleanLiteral (org.eclipse.jdt.core.dom.BooleanLiteral)13 Expression (org.eclipse.jdt.core.dom.Expression)6 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)6 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)5 CastExpression (org.eclipse.jdt.core.dom.CastExpression)4 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)4 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4 StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)4 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)4 CharacterLiteral (org.eclipse.jdt.core.dom.CharacterLiteral)3 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)3 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)3 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)3 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)3 AST (org.eclipse.jdt.core.dom.AST)2 ArrayAccess (org.eclipse.jdt.core.dom.ArrayAccess)2 ArrayType (org.eclipse.jdt.core.dom.ArrayType)2 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 NullLiteral (org.eclipse.jdt.core.dom.NullLiteral)2