Search in sources :

Example 1 with TypedCompileTimeConstant

use of org.jetbrains.kotlin.resolve.constants.TypedCompileTimeConstant in project kotlin by JetBrains.

the class CompileTimeConstantUtils method canBeReducedToBooleanConstant.

public static boolean canBeReducedToBooleanConstant(@Nullable KtExpression expression, @NotNull BindingContext context, @Nullable Boolean expectedValue) {
    KtExpression effectiveExpression = KtPsiUtil.deparenthesize(expression);
    if (effectiveExpression == null)
        return false;
    CompileTimeConstant<?> compileTimeConstant = ConstantExpressionEvaluator.getConstant(effectiveExpression, context);
    if (!(compileTimeConstant instanceof TypedCompileTimeConstant) || compileTimeConstant.getUsesVariableAsConstant())
        return false;
    ConstantValue constantValue = ((TypedCompileTimeConstant) compileTimeConstant).getConstantValue();
    if (!(constantValue instanceof BooleanValue))
        return false;
    Boolean value = ((BooleanValue) constantValue).getValue();
    return expectedValue == null || expectedValue.equals(value);
}
Also used : BooleanValue(org.jetbrains.kotlin.resolve.constants.BooleanValue) KtExpression(org.jetbrains.kotlin.psi.KtExpression) TypedCompileTimeConstant(org.jetbrains.kotlin.resolve.constants.TypedCompileTimeConstant) ConstantValue(org.jetbrains.kotlin.resolve.constants.ConstantValue)

Aggregations

KtExpression (org.jetbrains.kotlin.psi.KtExpression)1 BooleanValue (org.jetbrains.kotlin.resolve.constants.BooleanValue)1 ConstantValue (org.jetbrains.kotlin.resolve.constants.ConstantValue)1 TypedCompileTimeConstant (org.jetbrains.kotlin.resolve.constants.TypedCompileTimeConstant)1