use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class ConstraintParser method toBigDecimalExpression.
// TODO luca this logic should be moved in Constraint compiler?
private Expression toBigDecimalExpression(TypedExpression typedExpression) {
MethodCallExpr toBigDecimalMethod = new MethodCallExpr(null, "org.drools.modelcompiler.util.EvaluationUtil.toBigDecimal");
Expression arg = typedExpression.getExpression();
Optional<Class<?>> originalPatternType = typedExpression.getOriginalPatternType();
ConstraintCompiler constraintCompiler = createConstraintCompiler(context, originalPatternType);
CompiledExpressionResult compiledBlockResult = constraintCompiler.compileExpression(PrintUtil.printNode(arg));
arg = compiledBlockResult.getExpression();
if (arg.isEnclosedExpr()) {
arg = arg.asEnclosedExpr().getInner();
}
if (arg instanceof BigIntegerLiteralExpr) {
arg = new ObjectCreationExpr(null, toClassOrInterfaceType(BigInteger.class), NodeList.nodeList(new StringLiteralExpr(((BigIntegerLiteralExpr) arg).asBigInteger().toString())));
} else if (arg instanceof BigDecimalLiteralExpr) {
arg = new ObjectCreationExpr(null, toClassOrInterfaceType(BigDecimal.class), NodeList.nodeList(new StringLiteralExpr(((BigDecimalLiteralExpr) arg).asBigDecimal().toString())));
}
toBigDecimalMethod.addArgument(arg);
return toBigDecimalMethod;
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class ObjectCreationExpressionT method toJavaExpression.
@Override
public Node toJavaExpression() {
ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
objectCreationExpr.setType(type);
List<Expression> arguments = this.constructorArguments.stream().map(typedExpression -> (Expression) typedExpression.toJavaExpression()).collect(Collectors.toList());
objectCreationExpr.setArguments(NodeList.nodeList(arguments));
return objectCreationExpr;
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class ParserUtil method objectCreationExprToValue.
public static Object objectCreationExprToValue(ObjectCreationExpr objectCreationExpr, RuleContext context) {
// Only a few classes/constructors are handled. Otherwise, value becomes null so a link would be UNKNOWN. To be enhanced : DROOLS-6711
ClassOrInterfaceType type = objectCreationExpr.getType();
Class<?> clazz = getClassFromType(context.getTypeResolver(), type);
if (clazz.equals(BigDecimal.class)) {
NodeList<Expression> arguments = objectCreationExpr.getArguments();
Optional<Object> opt = arguments.stream().findFirst().filter(StringLiteralExpr.class::isInstance).map(literalExpr -> literalExpr.asStringLiteralExpr().asString()).map(BigDecimal::new);
return opt.orElse(null);
} else if (clazz.equals(BigInteger.class)) {
NodeList<Expression> arguments = objectCreationExpr.getArguments();
Optional<Object> opt = arguments.stream().findFirst().filter(StringLiteralExpr.class::isInstance).map(literalExpr -> literalExpr.asStringLiteralExpr().asString()).map(BigInteger::new);
return opt.orElse(null);
}
return null;
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class FlowDSLQueryGenerator method queryNameClass.
private void queryNameClass(ClassOrInterfaceDeclaration clazz) {
MethodDeclaration query1 = clazz.addMethod(QUERY, Modifier.Keyword.PUBLIC, Modifier.Keyword.STATIC);
query1.addParameter(STRINGLITERAL, "name");
BlockStmt stmts = new BlockStmt();
NodeList<Type> typeArguments = nodeList();
NodeList<TypeParameter> typeParameters = nodeList();
NodeList<Expression> arguments = nodeList();
ClassOrInterfaceType queryCallViewItemImpl = queryDefImpl();
ObjectCreationExpr objCreationExpr = new ObjectCreationExpr(null, queryCallViewItemImpl, arguments);
objCreationExpr.setDiamondOperator();
stmts.addStatement(new ReturnStmt(objCreationExpr));
objCreationExpr.addArgument(new NameExpr(VIEWBUILDER));
objCreationExpr.addArgument("name");
rangeArity().forEach(i -> {
String genericTypeName = stringWithIndex("T", i);
String type = stringWithIndex("type", i);
typeArguments.add(parseType(genericTypeName));
arguments.add(new NameExpr(type));
String classGenericType = genericType(genericTypeName);
Type genericType = parseType(classGenericType);
query1.addParameter(genericType, type);
typeParameters.add(new TypeParameter(genericTypeName));
});
query1.setTypeParameters(typeParameters);
query1.setBody(stmts);
ClassOrInterfaceType type = queryDef();
type.setTypeArguments(typeArguments);
query1.setType(type);
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class FlowDSLQueryGenerator method queryPkgNameClass.
private void queryPkgNameClass(ClassOrInterfaceDeclaration clazz) {
MethodDeclaration query1 = clazz.addMethod(QUERY, Modifier.Keyword.PUBLIC, Modifier.Keyword.STATIC);
query1.addParameter(STRINGLITERAL, "pkg");
query1.addParameter(STRINGLITERAL, "name");
BlockStmt stmts = new BlockStmt();
NodeList<Type> typeArguments = nodeList();
NodeList<TypeParameter> typeParameters = nodeList();
NodeList<Expression> arguments = nodeList();
ClassOrInterfaceType queryCallViewItemImpl = queryDefImpl();
ObjectCreationExpr objCreationExpr = new ObjectCreationExpr(null, queryCallViewItemImpl, arguments);
objCreationExpr.setDiamondOperator();
stmts.addStatement(new ReturnStmt(objCreationExpr));
objCreationExpr.addArgument(new NameExpr(VIEWBUILDER));
objCreationExpr.addArgument("pkg");
objCreationExpr.addArgument("name");
rangeArity().forEach(i -> {
String genericTypeName = stringWithIndex("T", i);
String type = stringWithIndex("type", i);
typeArguments.add(parseType(genericTypeName));
arguments.add(new NameExpr(type));
String classGenericType = genericType(genericTypeName);
Type genericType = parseType(classGenericType);
query1.addParameter(genericType, type);
typeParameters.add(new TypeParameter(genericTypeName));
});
query1.setTypeParameters(typeParameters);
query1.setBody(stmts);
ClassOrInterfaceType type = queryDef();
type.setTypeArguments(typeArguments);
query1.setType(type);
}
Aggregations