Search in sources :

Example 1 with CharLiteralExpr

use of com.github.javaparser.ast.expr.CharLiteralExpr in project checker-framework by typetools.

the class StubParser method getValueOfExpressionInAnnotation.

/**
 * Returns the value of {@code expr}, or null if some problem occurred getting the value.
 */
private Object getValueOfExpressionInAnnotation(String name, Expression expr, TypeKind valueKind) {
    if (expr instanceof FieldAccessExpr || expr instanceof NameExpr) {
        VariableElement elem;
        if (expr instanceof NameExpr) {
            elem = findVariableElement((NameExpr) expr);
        } else {
            elem = findVariableElement((FieldAccessExpr) expr);
        }
        if (elem == null) {
            stubWarn("Field not found: " + expr);
            return null;
        }
        Object value = elem.getConstantValue() != null ? elem.getConstantValue() : elem;
        if (value instanceof Number) {
            return convert((Number) value, valueKind);
        } else {
            return value;
        }
    } else if (expr instanceof StringLiteralExpr) {
        return ((StringLiteralExpr) expr).asString();
    } else if (expr instanceof BooleanLiteralExpr) {
        return ((BooleanLiteralExpr) expr).getValue();
    } else if (expr instanceof CharLiteralExpr) {
        return convert((int) ((CharLiteralExpr) expr).asChar(), valueKind);
    } else if (expr instanceof DoubleLiteralExpr) {
        // double, too.
        return ((DoubleLiteralExpr) expr).asDouble();
    } else if (expr instanceof IntegerLiteralExpr) {
        return convert(((IntegerLiteralExpr) expr).asInt(), valueKind);
    } else if (expr instanceof LongLiteralExpr) {
        return convert(((LongLiteralExpr) expr).asLong(), valueKind);
    } else if (expr instanceof ClassExpr) {
        ClassExpr classExpr = (ClassExpr) expr;
        String className = classExpr.getType().toString();
        if (importedTypes.containsKey(className)) {
            return importedTypes.get(className).asType();
        }
        TypeElement typeElement = findTypeOfName(className);
        if (typeElement == null) {
            stubWarn("StubParser: unknown class name " + className);
            return null;
        }
        return typeElement.asType();
    } else if (expr instanceof NullLiteralExpr) {
        stubWarn("Null found as value for %s. Null isn't allowed as an annotation value", name);
        return null;
    } else {
        stubWarn("Unexpected annotation expression: " + expr);
        return null;
    }
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) TypeElement(javax.lang.model.element.TypeElement) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) CharLiteralExpr(com.github.javaparser.ast.expr.CharLiteralExpr) VariableElement(javax.lang.model.element.VariableElement) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr)

Example 2 with CharLiteralExpr

use of com.github.javaparser.ast.expr.CharLiteralExpr in project checker-framework by typetools.

the class AnnotationFileParser method getValueOfExpressionInAnnotation.

/**
 * Returns the value of {@code expr}.
 *
 * @param name the name of an annotation element/argument, used for diagnostic messages
 * @param expr the expression to determine the value of
 * @param valueKind the type of the result
 * @return the value of {@code expr}
 * @throws AnnotationFileParserException if a problem occurred getting the value
 */
private Object getValueOfExpressionInAnnotation(String name, Expression expr, TypeKind valueKind) throws AnnotationFileParserException {
    if (expr instanceof FieldAccessExpr || expr instanceof NameExpr) {
        VariableElement elem;
        if (expr instanceof NameExpr) {
            elem = findVariableElement((NameExpr) expr);
        } else {
            elem = findVariableElement((FieldAccessExpr) expr);
        }
        if (elem == null) {
            throw new AnnotationFileParserException(String.format("variable %s not found", expr));
        }
        Object value = elem.getConstantValue() != null ? elem.getConstantValue() : elem;
        if (value instanceof Number) {
            return convert((Number) value, valueKind);
        } else {
            return value;
        }
    } else if (expr instanceof StringLiteralExpr) {
        return ((StringLiteralExpr) expr).asString();
    } else if (expr instanceof BooleanLiteralExpr) {
        return ((BooleanLiteralExpr) expr).getValue();
    } else if (expr instanceof CharLiteralExpr) {
        return convert((int) ((CharLiteralExpr) expr).asChar(), valueKind);
    } else if (expr instanceof DoubleLiteralExpr) {
        // double, too.
        return ((DoubleLiteralExpr) expr).asDouble();
    } else if (expr instanceof IntegerLiteralExpr) {
        return convert(((IntegerLiteralExpr) expr).asNumber(), valueKind);
    } else if (expr instanceof LongLiteralExpr) {
        return convert(((LongLiteralExpr) expr).asNumber(), valueKind);
    } else if (expr instanceof UnaryExpr) {
        switch(expr.toString()) {
            // the smallest member of an integral type is larger than the largest value.
            case "-9223372036854775808L":
            case "-9223372036854775808l":
                return convert(Long.MIN_VALUE, valueKind, false);
            case "-2147483648":
                return convert(Integer.MIN_VALUE, valueKind, false);
            default:
                if (((UnaryExpr) expr).getOperator() == UnaryExpr.Operator.MINUS) {
                    Object value = getValueOfExpressionInAnnotation(name, ((UnaryExpr) expr).getExpression(), valueKind);
                    if (value instanceof Number) {
                        return convert((Number) value, valueKind, true);
                    }
                }
                throw new AnnotationFileParserException("unexpected Unary annotation expression: " + expr);
        }
    } else if (expr instanceof ClassExpr) {
        ClassExpr classExpr = (ClassExpr) expr;
        // Type.toString(): @FullyQualifiedName
        @SuppressWarnings("signature") @FullyQualifiedName String className = classExpr.getType().toString();
        if (importedTypes.containsKey(className)) {
            return importedTypes.get(className).asType();
        }
        TypeElement typeElement = findTypeOfName(className);
        if (typeElement == null) {
            throw new AnnotationFileParserException("unknown class name " + className);
        }
        return typeElement.asType();
    } else if (expr instanceof NullLiteralExpr) {
        throw new AnnotationFileParserException("Illegal annotation value null, for " + name);
    } else {
        throw new AnnotationFileParserException("Unexpected annotation expression: " + expr);
    }
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) TypeElement(javax.lang.model.element.TypeElement) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) FullyQualifiedName(org.checkerframework.checker.signature.qual.FullyQualifiedName) CharLiteralExpr(com.github.javaparser.ast.expr.CharLiteralExpr) VariableElement(javax.lang.model.element.VariableElement) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr)

Example 3 with CharLiteralExpr

use of com.github.javaparser.ast.expr.CharLiteralExpr in project drools by kiegroup.

the class CoercedExpression method coerceToString.

private static TypedExpression coerceToString(TypedExpression typedExpression) {
    final Expression expression = typedExpression.getExpression();
    TypedExpression coercedExpression;
    if (expression instanceof CharLiteralExpr) {
        coercedExpression = typedExpression.cloneWithNewExpression(toStringLiteral(((CharLiteralExpr) expression).getValue()));
    } else if (typedExpression.isPrimitive()) {
        coercedExpression = typedExpression.cloneWithNewExpression(new MethodCallExpr(new NameExpr("String"), "valueOf", NodeList.nodeList(expression)));
    } else if (typedExpression.getType() == Object.class) {
        coercedExpression = typedExpression.cloneWithNewExpression(new MethodCallExpr(expression, "toString"));
    } else if (expression instanceof NameExpr) {
        coercedExpression = typedExpression.cloneWithNewExpression(new CastExpr(toClassOrInterfaceType(String.class), expression));
    } else {
        coercedExpression = typedExpression.cloneWithNewExpression(toStringLiteral(expression.toString()));
    }
    return coercedExpression.setType(String.class);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) NameExpr(com.github.javaparser.ast.expr.NameExpr) CastExpr(com.github.javaparser.ast.expr.CastExpr) CharLiteralExpr(com.github.javaparser.ast.expr.CharLiteralExpr) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

CharLiteralExpr (com.github.javaparser.ast.expr.CharLiteralExpr)3 NameExpr (com.github.javaparser.ast.expr.NameExpr)3 BooleanLiteralExpr (com.github.javaparser.ast.expr.BooleanLiteralExpr)2 ClassExpr (com.github.javaparser.ast.expr.ClassExpr)2 DoubleLiteralExpr (com.github.javaparser.ast.expr.DoubleLiteralExpr)2 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)2 IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)2 LongLiteralExpr (com.github.javaparser.ast.expr.LongLiteralExpr)2 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)2 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)2 TypeElement (javax.lang.model.element.TypeElement)2 VariableElement (javax.lang.model.element.VariableElement)2 CastExpr (com.github.javaparser.ast.expr.CastExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 UnaryExpr (com.github.javaparser.ast.expr.UnaryExpr)1 FullyQualifiedName (org.checkerframework.checker.signature.qual.FullyQualifiedName)1 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)1 UnificationTypedExpression (org.drools.modelcompiler.builder.generator.UnificationTypedExpression)1