use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class QueryDefImplGenerator method callMethod.
private void callMethod(ClassOrInterfaceDeclaration clazz) {
MethodDeclaration method = clazz.addMethod("call", Modifier.Keyword.PUBLIC);
addOverride(method);
method.addParameter("boolean", "open");
method.setType(toClassOrInterfaceType(QueryCallViewItem.class));
BlockStmt statements = new BlockStmt();
NodeList<Expression> arguments = nodeList();
ClassOrInterfaceType queryCallViewItemImpl = toClassOrInterfaceType(QueryCallViewItemImpl.class);
ObjectCreationExpr objCreationExpr = new ObjectCreationExpr(null, queryCallViewItemImpl, arguments);
statements.addStatement(new ReturnStmt(objCreationExpr));
objCreationExpr.addArgument("this");
objCreationExpr.addArgument("open");
rangeArity().forEach(i -> {
String varWithIndex = stringWithIndex("var", i);
String genericTypeName = genericTypeName(i);
method.addParameter(genericType("Argument", genericTypeName), varWithIndex);
objCreationExpr.addArgument(varWithIndex);
});
method.setBody(statements);
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class PreprocessPhase method addTypeToInitialization.
private Optional<Expression> addTypeToInitialization(WithStatement withStatement, Deque<Statement> allNewStatements) {
if (withStatement.getWithObject().isAssignExpr()) {
AssignExpr assignExpr = withStatement.getWithObject().asAssignExpr();
Expression assignExprValue = assignExpr.getValue();
Expression assignExprTarget = assignExpr.getTarget();
if (assignExprValue.isObjectCreationExpr() && assignExprTarget instanceof DrlNameExpr) {
ObjectCreationExpr constructor = assignExprValue.asObjectCreationExpr();
ClassOrInterfaceType ctorType = constructor.getType();
String targetVariableName = ((DrlNameExpr) assignExprTarget).getNameAsString();
VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(ctorType, targetVariableName);
AssignExpr withTypeAssignmentExpr = new AssignExpr(variableDeclarationExpr, assignExprValue, assignExpr.getOperator());
allNewStatements.add(new ExpressionStmt(withTypeAssignmentExpr));
return of(new DrlNameExpr(targetVariableName));
}
}
return empty();
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class RhsParser method processModify.
private ModifyAction processModify(RuleContext context, MethodCallExpr consequenceExpr, MethodCallExpr statement, BlockStmt ruleVariablesBlock) {
String modifiedId = statement.getArgument(0).toString();
Class<?> modifiedClass = context.getDeclarationById(modifiedId).orElseThrow(() -> new RuntimeException("Unknown declaration: " + modifiedId)).getDeclarationClass();
ModifyAction action = new ModifyAction(modifiedClass);
if (statement.getArguments().size() > 1) {
String maskId = statement.getArgument(1).toString();
AssignExpr maskAssignExpr = ruleVariablesBlock.findAll(AssignExpr.class).stream().filter(assign -> ((VariableDeclarationExpr) assign.getTarget()).getVariable(0).toString().equals(maskId)).findFirst().orElseThrow(() -> new RuntimeException("Unknown mask: " + maskId));
MethodCallExpr maskMethod = ((MethodCallExpr) maskAssignExpr.getValue());
List<MethodCallExpr> modifyingExprs = consequenceExpr.findAll(MethodCallExpr.class).stream().filter(m -> m.getScope().map(s -> s.toString().equals(modifiedId) || s.toString().equals("(" + modifiedId + ")")).orElse(false)).collect(Collectors.toList());
for (int i = 1; i < maskMethod.getArguments().size(); i++) {
String property = maskMethod.getArgument(i).asStringLiteralExpr().asString();
String setter = "set" + ucFirst(property);
MethodCallExpr setterExpr = modifyingExprs.stream().filter(m -> m.getNameAsString().equals(setter)).filter(m -> m.getArguments().size() == 1).findFirst().orElse(null);
Object value = null;
if (setterExpr != null) {
Expression arg = setterExpr.getArgument(0);
arg = stripEnclosedAndCast(arg);
if (arg.isLiteralExpr()) {
value = literalToValue(arg.asLiteralExpr());
} else if (arg.isNameExpr()) {
value = ((ImpactAnalysisRuleContext) context).getBindVariableLiteralMap().get(arg.asNameExpr().getName().asString());
} else if (arg.isObjectCreationExpr()) {
value = objectCreationExprToValue((ObjectCreationExpr) arg, context);
}
}
Method accessor = ClassUtils.getAccessor(modifiedClass, property);
if (accessor != null && Map.class.isAssignableFrom(accessor.getReturnType())) {
String mapName = property;
List<MethodCallExpr> mapPutExprs = consequenceExpr.findAll(MethodCallExpr.class).stream().filter(m -> isMapPutExpr(m, modifiedId, accessor.getName())).collect(Collectors.toList());
mapPutExprs.stream().forEach(expr -> {
String mapKey = getLiteralString(context, expr.getArgument(0));
Object mapValue = getLiteralValue(context, expr.getArgument(1));
action.addModifiedProperty(new ModifiedMapProperty(mapName, mapKey, mapValue));
});
} else {
action.addModifiedProperty(new ModifiedProperty(property, value));
}
}
}
return action;
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class DrlxParseUtil method getExpressionType.
public static java.lang.reflect.Type getExpressionType(RuleContext context, TypeResolver typeResolver, Expression expr, Collection<String> usedDeclarations) {
if (expr instanceof LiteralExpr) {
return getLiteralExpressionType((LiteralExpr) expr);
}
if (expr instanceof UnaryExpr) {
return getExpressionType(context, typeResolver, expr.asUnaryExpr().getExpression(), usedDeclarations);
}
if (expr instanceof ArrayAccessExpr) {
return getClassFromContext(typeResolver, ((ArrayCreationExpr) ((ArrayAccessExpr) expr).getName()).getElementType().asString());
}
if (expr instanceof ArrayCreationExpr) {
return getClassFromContext(typeResolver, ((ArrayCreationExpr) expr).getElementType().asString());
}
if (expr instanceof MapCreationLiteralExpression) {
return Map.class;
}
if (expr instanceof ListCreationLiteralExpression) {
return List.class;
}
if (expr instanceof NameExpr) {
return expressionTypeNameExpr(context, usedDeclarations, ((NameExpr) expr).getNameAsString());
}
if (expr instanceof DrlNameExpr) {
return expressionTypeNameExpr(context, usedDeclarations, ((DrlNameExpr) expr).getNameAsString());
}
if (expr instanceof BinaryExpr) {
return boolean.class;
}
if (expr instanceof MethodCallExpr) {
MethodCallExpr methodCallExpr = (MethodCallExpr) expr;
Optional<Expression> scopeExpression = methodCallExpr.getScope();
if (scopeExpression.isPresent()) {
java.lang.reflect.Type scopeType = getExpressionType(context, typeResolver, scopeExpression.get(), usedDeclarations);
return returnTypeOfMethodCallExpr(context, typeResolver, methodCallExpr, scopeType, usedDeclarations);
} else {
throw new IllegalStateException("Scope expression is not present for " + ((MethodCallExpr) expr).getNameAsString() + "!");
}
}
if (expr instanceof ObjectCreationExpr) {
final ClassOrInterfaceType type = ((ObjectCreationExpr) expr).getType();
return getClassFromContext(typeResolver, type.asString());
}
if (expr.isCastExpr()) {
String typeName = expr.asCastExpr().getType().toString();
try {
return typeResolver.resolveType(expr.asCastExpr().getType().toString());
} catch (ClassNotFoundException e) {
context.addCompilationError(new InvalidExpressionErrorResult("Unknown type in cast expression: " + typeName));
throw new RuntimeException("Unknown type in cast expression: " + typeName);
}
}
if (expr instanceof ConditionalExpr) {
ConditionalExpr ternaryExpr = ((ConditionalExpr) expr);
java.lang.reflect.Type conditionType = getExpressionType(context, typeResolver, ternaryExpr.getCondition(), usedDeclarations);
if (conditionType != Boolean.class && conditionType != boolean.class) {
context.addCompilationError(new InvalidExpressionErrorResult("Condtion used in ternary expression '" + expr + "' isn't boolean"));
return Object.class;
}
java.lang.reflect.Type leftType = getExpressionType(context, typeResolver, ternaryExpr.getThenExpr(), usedDeclarations);
java.lang.reflect.Type rightType = getExpressionType(context, typeResolver, ternaryExpr.getElseExpr(), usedDeclarations);
Class<?> leftClass = toRawClass(leftType);
Class<?> rightClass = toRawClass(rightType);
if (leftClass.isAssignableFrom(rightClass)) {
return leftType;
}
if (rightClass.isAssignableFrom(leftClass)) {
return rightType;
}
return Object.class;
}
if (expr.isClassExpr()) {
return Class.class;
}
throw new RuntimeException("Unknown expression type: " + PrintUtil.printNode(expr));
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class FromVisitor method createSingleFrom.
private Optional<Expression> createSingleFrom(String expression) {
final Expression parsedExpression = DrlxParseUtil.parseExpression(expression).getExpr();
if (parsedExpression instanceof FieldAccessExpr || parsedExpression instanceof NameExpr || parsedExpression instanceof DrlNameExpr) {
return fromFieldOrName(expression);
}
if (parsedExpression instanceof ObjectCreationExpr) {
return fromConstructorExpr(expression, (ObjectCreationExpr) parsedExpression);
}
if (parsedExpression instanceof LiteralExpr) {
MethodCallExpr fromCall = createDslTopLevelMethod(FROM_CALL);
fromCall.addArgument(parsedExpression);
return of(fromCall);
}
return fromExpression(expression, parsedExpression);
}
Aggregations