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;
}
}
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);
}
}
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);
}
Aggregations