use of org.drools.javaparser.ast.body.MethodDeclaration in project drools by kiegroup.
the class PackageModel method getRulesSource.
public RuleSourceResult getRulesSource() {
CompilationUnit cu = new CompilationUnit();
cu.setPackageDeclaration(name);
manageImportForCompilationUnit(cu);
ClassOrInterfaceDeclaration rulesClass = cu.addClass(rulesFileName);
rulesClass.addImplementedType(Model.class);
BodyDeclaration<?> dateFormatter = JavaParser.parseBodyDeclaration("public final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DateUtils.getDateFormatMask());\n");
rulesClass.addMember(dateFormatter);
BodyDeclaration<?> getNameMethod = JavaParser.parseBodyDeclaration(" @Override\n" + " public String getName() {\n" + " return \"" + name + "\";\n" + " }\n");
rulesClass.addMember(getNameMethod);
BodyDeclaration<?> getRulesMethod = JavaParser.parseBodyDeclaration(" @Override\n" + " public List<Rule> getRules() {\n" + " return rules;\n" + " }\n");
rulesClass.addMember(getRulesMethod);
StringBuilder sb = new StringBuilder("\n");
sb.append("With the following expression ID:\n");
sb.append(exprIdGenerator.toString());
sb.append("\n");
JavadocComment exprIdComment = new JavadocComment(sb.toString());
getRulesMethod.setComment(exprIdComment);
BodyDeclaration<?> getGlobalsMethod = JavaParser.parseBodyDeclaration(" @Override\n" + " public List<Global> getGlobals() {\n" + " return globals;\n" + " }\n");
rulesClass.addMember(getGlobalsMethod);
BodyDeclaration<?> getQueriesMethod = JavaParser.parseBodyDeclaration(" @Override\n" + " public List<Query> getQueries() {\n" + " return queries;\n" + " }\n");
rulesClass.addMember(getQueriesMethod);
BodyDeclaration<?> getTypeMetaDataMethod = JavaParser.parseBodyDeclaration(" @Override\n" + " public List<TypeMetaData> getTypeMetaDatas() {\n" + " return typeMetaDatas;\n" + " }\n");
rulesClass.addMember(getTypeMetaDataMethod);
for (Map.Entry<String, MethodCallExpr> windowReference : windowReferences.entrySet()) {
FieldDeclaration f = rulesClass.addField(WINDOW_REFERENCE_TYPE, windowReference.getKey(), Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL);
f.getVariables().get(0).setInitializer(windowReference.getValue());
}
for (Map.Entry<String, Class<?>> g : getGlobals().entrySet()) {
addGlobalField(rulesClass, getName(), g.getKey(), g.getValue());
}
for (Map.Entry<String, QueryGenerator.QueryDefWithType> queryDef : queryDefWithType.entrySet()) {
FieldDeclaration field = rulesClass.addField(queryDef.getValue().getQueryType(), queryDef.getKey(), Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL);
field.getVariables().get(0).setInitializer(queryDef.getValue().getMethodCallExpr());
}
for (Map.Entry<String, MethodDeclaration> methodName : queryMethods.entrySet()) {
FieldDeclaration field = rulesClass.addField(methodName.getValue().getType(), methodName.getKey(), Modifier.FINAL);
field.getVariables().get(0).setInitializer(new MethodCallExpr(null, methodName.getKey()));
}
// instance initializer block.
// add to `rules` list the result of invoking each method for rule
InitializerDeclaration rulesListInitializer = new InitializerDeclaration();
BlockStmt rulesListInitializerBody = new BlockStmt();
rulesListInitializer.setBody(rulesListInitializerBody);
queryMethods.values().forEach(rulesClass::addMember);
buildArtifactsDeclaration(queryMethods.keySet(), rulesClass, rulesListInitializerBody, "Query", "queries", false);
buildArtifactsDeclaration(windowReferences.keySet(), rulesClass, rulesListInitializerBody, "WindowReference", "windowReferences", false);
buildArtifactsDeclaration(getGlobals().keySet(), rulesClass, rulesListInitializerBody, "Global", "globals", true);
if (!typeMetaDataExpressions.isEmpty()) {
BodyDeclaration<?> typeMetaDatasList = JavaParser.parseBodyDeclaration("List<TypeMetaData> typeMetaDatas = new ArrayList<>();");
rulesClass.addMember(typeMetaDatasList);
for (Expression expr : typeMetaDataExpressions) {
addInitStatement(rulesListInitializerBody, expr, "typeMetaDatas");
}
} else {
BodyDeclaration<?> typeMetaDatasList = JavaParser.parseBodyDeclaration("List<TypeMetaData> typeMetaDatas = Collections.emptyList();");
rulesClass.addMember(typeMetaDatasList);
}
functions.forEach(rulesClass::addMember);
RuleSourceResult results = new RuleSourceResult(cu);
int ruleCount = ruleMethods.size();
boolean requiresMultipleRulesLists = ruleCount >= RULES_DECLARATION_PER_CLASS - 1;
MethodCallExpr rules = buildRulesField(rulesClass);
if (requiresMultipleRulesLists) {
addRulesList(rulesListInitializerBody, "rulesList");
}
// each method per Drlx parser result
int count = -1;
Map<Integer, ClassOrInterfaceDeclaration> splitted = new LinkedHashMap<>();
for (Entry<String, MethodDeclaration> ruleMethodKV : ruleMethods.entrySet()) {
ClassOrInterfaceDeclaration rulesMethodClass = splitted.computeIfAbsent(++count / RULES_PER_CLASS, i -> {
CompilationUnit cuRulesMethod = new CompilationUnit();
results.with(cuRulesMethod);
cuRulesMethod.setPackageDeclaration(name);
manageImportForCompilationUnit(cuRulesMethod);
cuRulesMethod.addImport(JavaParser.parseImport("import static " + name + "." + rulesFileName + ".*;"));
String currentRulesMethodClassName = rulesFileName + "RuleMethods" + i;
return cuRulesMethod.addClass(currentRulesMethodClassName);
});
rulesMethodClass.addMember(ruleMethodKV.getValue());
if (count % RULES_DECLARATION_PER_CLASS == RULES_DECLARATION_PER_CLASS - 1) {
int index = count / RULES_DECLARATION_PER_CLASS;
rules = buildRulesField(results, index);
addRulesList(rulesListInitializerBody, rulesFileName + "Rules" + index + ".rulesList");
}
// manage in main class init block:
rules.addArgument(new MethodCallExpr(new NameExpr(rulesMethodClass.getNameAsString()), ruleMethodKV.getKey()));
}
BodyDeclaration<?> rulesList = requiresMultipleRulesLists ? JavaParser.parseBodyDeclaration("List<Rule> rules = new ArrayList<>(" + ruleCount + ");") : JavaParser.parseBodyDeclaration("List<Rule> rules = rulesList;");
rulesClass.addMember(rulesList);
if (!rulesListInitializer.getBody().getStatements().isEmpty()) {
rulesClass.addMember(rulesListInitializer);
}
return results;
}
use of org.drools.javaparser.ast.body.MethodDeclaration in project drools by kiegroup.
the class ModelGenerator method processRule.
private static void processRule(KnowledgeBuilderImpl kbuilder, TypeResolver typeResolver, PackageModel packageModel, PackageDescr packageDescr, RuleDescr ruleDescr, boolean isPattern) {
RuleContext context = new RuleContext(kbuilder, packageModel, ruleDescr, typeResolver, isPattern);
for (Entry<String, Object> kv : ruleDescr.getNamedConsequences().entrySet()) {
context.addNamedConsequence(kv.getKey(), kv.getValue().toString());
}
new ModelGeneratorVisitor(context, packageModel).visit(getExtendedLhs(packageDescr, ruleDescr));
final String ruleMethodName = "rule_" + toId(ruleDescr.getName());
MethodDeclaration ruleMethod = new MethodDeclaration(EnumSet.of(Modifier.PUBLIC, Modifier.STATIC), RULE_TYPE, ruleMethodName);
ruleMethod.setJavadocComment(" Rule name: " + ruleDescr.getName() + " ");
VariableDeclarationExpr ruleVar = new VariableDeclarationExpr(RULE_TYPE, RULE_CALL);
MethodCallExpr ruleCall = new MethodCallExpr(null, RULE_CALL);
if (!ruleDescr.getNamespace().isEmpty()) {
ruleCall.addArgument(new StringLiteralExpr(ruleDescr.getNamespace()));
}
ruleCall.addArgument(new StringLiteralExpr(ruleDescr.getName()));
RuleUnitDescr ruleUnitDescr = context.getRuleUnitDescr();
MethodCallExpr buildCallScope = ruleUnitDescr != null ? new MethodCallExpr(ruleCall, UNIT_CALL).addArgument(new ClassExpr(classToReferenceType(ruleUnitDescr.getRuleUnitClass()))) : ruleCall;
for (MethodCallExpr attributeExpr : ruleAttributes(context, ruleDescr)) {
attributeExpr.setScope(buildCallScope);
buildCallScope = attributeExpr;
}
for (MethodCallExpr metaAttributeExpr : ruleMetaAttributes(context, ruleDescr)) {
metaAttributeExpr.setScope(buildCallScope);
buildCallScope = metaAttributeExpr;
}
MethodCallExpr buildCall = new MethodCallExpr(buildCallScope, BUILD_CALL, NodeList.nodeList(context.getExpressions()));
BlockStmt ruleVariablesBlock = new BlockStmt();
createUnitData(ruleUnitDescr, ruleVariablesBlock);
createVariables(kbuilder, ruleVariablesBlock, packageModel, context);
ruleMethod.setBody(ruleVariablesBlock);
MethodCallExpr executeCall = new Consequence(context).createCall(ruleDescr, ruleDescr.getConsequence().toString(), ruleVariablesBlock, false);
buildCall.addArgument(executeCall);
ruleVariablesBlock.addStatement(new AssignExpr(ruleVar, buildCall, AssignExpr.Operator.ASSIGN));
ruleVariablesBlock.addStatement(new ReturnStmt(RULE_CALL));
packageModel.putRuleMethod(ruleMethodName, ruleMethod);
}
use of org.drools.javaparser.ast.body.MethodDeclaration in project drools by kiegroup.
the class POJOGenerator method toClassDeclaration.
/**
* @param packageDescr
*/
public static ClassOrInterfaceDeclaration toClassDeclaration(TypeDeclarationDescr typeDeclaration, PackageDescr packageDescr) {
EnumSet<Modifier> classModifiers = EnumSet.of(Modifier.PUBLIC);
String generatedClassName = typeDeclaration.getTypeName();
ClassOrInterfaceDeclaration generatedClass = new ClassOrInterfaceDeclaration(classModifiers, false, generatedClassName);
generatedClass.addImplementedType(GeneratedFact.class.getName());
// Ref: {@link org.drools.core.factmodel.DefaultBeanClassBuilder} by default always receive is Serializable.
generatedClass.addImplementedType(Serializable.class.getName());
boolean hasSuper = typeDeclaration.getSuperTypeName() != null;
if (hasSuper) {
generatedClass.addExtendedType(typeDeclaration.getSuperTypeName());
}
List<AnnotationDescr> softAnnotations = new ArrayList<>();
for (AnnotationDescr ann : typeDeclaration.getAnnotations()) {
final String annFqn = Optional.ofNullable(ann.getFullyQualifiedName()).orElse(Optional.ofNullable(predefinedClassLevelAnnotation.get(ann.getName())).map(Class::getCanonicalName).orElse(null));
if (annFqn != null) {
NormalAnnotationExpr annExpr = generatedClass.addAndGetAnnotation(annFqn);
ann.getValueMap().forEach((k, v) -> annExpr.addPair(k, getAnnotationValue(annFqn, k, v.toString())));
} else {
softAnnotations.add(ann);
}
}
if (softAnnotations.size() > 0) {
String softAnnDictionary = softAnnotations.stream().map(a -> "<dt>" + a.getName() + "</dt><dd>" + a.getValuesAsString() + "</dd>").collect(Collectors.joining());
JavadocComment generatedClassJavadoc = new JavadocComment("<dl>" + softAnnDictionary + "</dl>");
generatedClass.setJavadocComment(generatedClassJavadoc);
}
// No-args ctor
generatedClass.addConstructor(Modifier.PUBLIC);
List<Statement> equalsFieldStatement = new ArrayList<>();
List<Statement> hashCodeFieldStatement = new ArrayList<>();
List<String> toStringFieldStatement = new ArrayList<>();
List<TypeFieldDescr> keyFields = new ArrayList<>();
Collection<TypeFieldDescr> inheritedFields = findInheritedDeclaredFields(typeDeclaration, packageDescr);
Collection<TypeFieldDescr> typeFields = typeDeclaration.getFields().values();
if (!inheritedFields.isEmpty() || !typeDeclaration.getFields().isEmpty()) {
ConstructorDeclaration fullArgumentsCtor = generatedClass.addConstructor(Modifier.PUBLIC);
NodeList<Statement> ctorFieldStatement = NodeList.nodeList();
MethodCallExpr superCall = new MethodCallExpr(null, "super");
for (TypeFieldDescr typeFieldDescr : inheritedFields) {
String fieldName = typeFieldDescr.getFieldName();
addCtorArg(fullArgumentsCtor, typeFieldDescr.getPattern().getObjectType(), fieldName);
superCall.addArgument(fieldName);
if (typeFieldDescr.getAnnotation("key") != null) {
keyFields.add(typeFieldDescr);
}
}
ctorFieldStatement.add(new ExpressionStmt(superCall));
int position = inheritedFields.size();
for (TypeFieldDescr typeFieldDescr : typeFields) {
String fieldName = typeFieldDescr.getFieldName();
Type returnType = addCtorArg(fullArgumentsCtor, typeFieldDescr.getPattern().getObjectType(), fieldName);
FieldDeclaration field = generatedClass.addField(returnType, fieldName, Modifier.PRIVATE);
field.createSetter();
field.addAndGetAnnotation(Position.class.getName()).addPair("value", "" + position++);
MethodDeclaration getter = field.createGetter();
equalsFieldStatement.add(generateEqualsForField(getter, fieldName));
hashCodeFieldStatement.addAll(generateHashCodeForField(getter, fieldName));
ctorFieldStatement.add(replaceFieldName(parseStatement("this.__fieldName = __fieldName;"), fieldName));
toStringFieldStatement.add(format("+ {0}+{1}", quote(fieldName + "="), fieldName));
if (typeFieldDescr.getAnnotation("key") != null) {
keyFields.add(typeFieldDescr);
}
}
fullArgumentsCtor.setBody(new BlockStmt(ctorFieldStatement));
if (!keyFields.isEmpty() && keyFields.size() != inheritedFields.size() + typeFields.size()) {
ConstructorDeclaration keyArgumentsCtor = generatedClass.addConstructor(Modifier.PUBLIC);
NodeList<Statement> ctorKeyFieldStatement = NodeList.nodeList();
MethodCallExpr keySuperCall = new MethodCallExpr(null, "super");
ctorKeyFieldStatement.add(new ExpressionStmt(keySuperCall));
for (TypeFieldDescr typeFieldDescr : keyFields) {
String fieldName = typeFieldDescr.getFieldName();
addCtorArg(keyArgumentsCtor, typeFieldDescr.getPattern().getObjectType(), fieldName);
if (typeDeclaration.getFields().get(fieldName) != null) {
ctorKeyFieldStatement.add(replaceFieldName(parseStatement("this.__fieldName = __fieldName;"), fieldName));
} else {
keySuperCall.addArgument(fieldName);
}
}
keyArgumentsCtor.setBody(new BlockStmt(ctorKeyFieldStatement));
}
if (hasSuper) {
generatedClass.addMember(generateEqualsMethod(generatedClassName, equalsFieldStatement));
generatedClass.addMember(generateHashCodeMethod(hashCodeFieldStatement));
}
}
generatedClass.addMember(generateToStringMethod(generatedClassName, toStringFieldStatement));
return generatedClass;
}
use of org.drools.javaparser.ast.body.MethodDeclaration in project drools by kiegroup.
the class POJOGenerator method generateToStringMethod.
private static MethodDeclaration generateToStringMethod(String generatedClassName, List<String> toStringFieldStatement) {
final String header = format("return {0} + {1}", quote(generatedClassName), quote("( "));
final String body = String.join(format("+ {0}", quote(", ")), toStringFieldStatement);
final String close = format("+{0};", quote(" )"));
final Statement toStringStatement = parseStatement(header + body + close);
final Type returnType = JavaParser.parseType(String.class.getSimpleName());
final MethodDeclaration equals = new MethodDeclaration(EnumSet.of(Modifier.PUBLIC), returnType, TO_STRING);
equals.addAnnotation("Override");
equals.setBody(new BlockStmt(NodeList.nodeList(toStringStatement)));
return equals;
}
use of org.drools.javaparser.ast.body.MethodDeclaration in project drools by kiegroup.
the class ConstraintParser method getDrlxParseResult.
private DrlxParseResult getDrlxParseResult(Class<?> patternType, String bindingId, String expression, DrlxExpression drlx, boolean isPositional) {
Expression drlxExpr = drlx.getExpr();
boolean isEnclosed = false;
while (drlxExpr instanceof EnclosedExpr) {
drlxExpr = ((EnclosedExpr) drlxExpr).getInner();
isEnclosed = true;
}
if (drlxExpr instanceof MethodCallExpr && !((MethodCallExpr) drlxExpr).getScope().isPresent() && ((MethodCallExpr) drlxExpr).getNameAsString().equals("eval")) {
drlxExpr = ((MethodCallExpr) drlxExpr).getArgument(0);
}
String exprId;
if (GENERATE_EXPR_ID) {
exprId = context.getExprId(patternType, expression);
}
if (drlxExpr instanceof BinaryExpr) {
BinaryExpr binaryExpr = (BinaryExpr) drlxExpr;
BinaryExpr.Operator operator = binaryExpr.getOperator();
IndexUtil.ConstraintType decodeConstraintType = DrlxParseUtil.toConstraintType(operator);
final ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
final ExpressionTyper expressionTyper = new ExpressionTyper(context, patternType, bindingId, isPositional, expressionTyperContext);
TypedExpressionResult leftTypedExpressionResult = expressionTyper.toTypedExpression(binaryExpr.getLeft());
Optional<TypedExpression> optLeft = leftTypedExpressionResult.getTypedExpression();
if (!optLeft.isPresent()) {
return new DrlxParseFail();
}
List<String> usedDeclarationsOnLeft = drlx.getBind() == null ? null : new ArrayList<>(expressionTyperContext.getUsedDeclarations());
TypedExpressionResult rightExpressionResult = expressionTyper.toTypedExpression(binaryExpr.getRight());
Optional<TypedExpression> optRight = rightExpressionResult.getTypedExpression();
if (!optRight.isPresent()) {
context.addCompilationError(new ParseExpressionErrorResult(drlxExpr));
return new DrlxParseFail();
}
TypedExpression left = optLeft.get();
TypedExpression right = optRight.get();
Expression combo;
if (left.isPrimitive()) {
if (!right.getType().isPrimitive() && !Number.class.isAssignableFrom(right.getType()) && !Boolean.class.isAssignableFrom(right.getType()) && !String.class.isAssignableFrom(right.getType())) {
context.addCompilationError(new InvalidExpressionErrorResult("Comparison operation requires compatible types. Found " + left.getType() + " and " + right.getType()));
return new DrlxParseFail();
}
if (right.getExpression() instanceof StringLiteralExpr) {
right.setExpression(new IntegerLiteralExpr(((StringLiteralExpr) right.getExpression()).asString()));
} else if (right.getExpression() instanceof LiteralStringValueExpr) {
right.setExpression(coerceLiteralExprToType((LiteralStringValueExpr) right.getExpression(), left.getType()));
}
combo = new BinaryExpr(left.getExpression(), right.getExpression(), operator);
} else {
coerceRightExpression(left, right);
switch(operator) {
case EQUALS:
case NOT_EQUALS:
combo = getEqualityExpression(left, right, operator);
break;
default:
if (left.getExpression() == null || right.getExpression() == null) {
context.addCompilationError(new ParseExpressionErrorResult(drlxExpr));
return new DrlxParseFail();
}
combo = handleSpecialComparisonCases(operator, left, right);
}
}
for (Expression e : leftTypedExpressionResult.getPrefixExpressions()) {
combo = new BinaryExpr(e, combo, BinaryExpr.Operator.AND);
}
boolean isBetaNode = false;
if (right.getExpression() instanceof BinaryExpr) {
if (((BinaryExpr) right.getExpression()).getRight() instanceof MethodCallExpr) {
isBetaNode = true;
}
} else if (right.getExpression() instanceof NameExpr) {
isBetaNode = true;
}
if (isEnclosed) {
combo = new EnclosedExpr(combo);
}
return new DrlxParseSuccess(patternType, exprId, bindingId, combo, left.getType()).setDecodeConstraintType(decodeConstraintType).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setUsedDeclarationsOnLeft(usedDeclarationsOnLeft).setReactOnProperties(expressionTyperContext.getReactOnProperties()).setLeft(left).setRight(right).setBetaNode(isBetaNode);
}
if (drlxExpr instanceof UnaryExpr) {
UnaryExpr unaryExpr = (UnaryExpr) drlxExpr;
TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, patternType, bindingId, isPositional).toTypedExpression(unaryExpr);
return typedExpressionResult.getTypedExpression().<DrlxParseResult>map(left -> {
return new DrlxParseSuccess(patternType, exprId, bindingId, left.getExpression(), left.getType()).setUsedDeclarations(typedExpressionResult.getUsedDeclarations()).setReactOnProperties(typedExpressionResult.getReactOnProperties()).setLeft(left);
}).orElse(new DrlxParseFail());
}
if (drlxExpr instanceof PointFreeExpr) {
PointFreeExpr pointFreeExpr = (PointFreeExpr) drlxExpr;
TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, patternType, bindingId, isPositional).toTypedExpression(pointFreeExpr);
final Optional<TypedExpression> optTypedExpression = typedExpressionResult.getTypedExpression();
return optTypedExpression.<DrlxParseResult>map(typedExpression -> {
final Expression returnExpression = typedExpression.getExpression();
final Class<?> returnType = typedExpression.getType();
return new DrlxParseSuccess(patternType, exprId, bindingId, returnExpression, returnType).setUsedDeclarations(typedExpressionResult.getUsedDeclarations()).setReactOnProperties(typedExpressionResult.getReactOnProperties()).setLeft(typedExpression.getLeft()).setStatic(typedExpression.isStatic()).setValidExpression(true);
}).orElse(new DrlxParseFail());
}
if (drlxExpr instanceof MethodCallExpr) {
MethodCallExpr methodCallExpr = (MethodCallExpr) drlxExpr;
// when the methodCallExpr will be placed in the model/DSL, any parameter being a "this" need to be implemented as _this by convention.
List<ThisExpr> rewriteThisExprs = recurseCollectArguments(methodCallExpr).stream().filter(ThisExpr.class::isInstance).map(ThisExpr.class::cast).collect(Collectors.toList());
for (ThisExpr t : rewriteThisExprs) {
methodCallExpr.replace(t, new NameExpr("_this"));
}
Optional<MethodDeclaration> functionCall = packageModel.getFunctions().stream().filter(m -> m.getName().equals(methodCallExpr.getName())).findFirst();
if (functionCall.isPresent()) {
Class<?> returnType = DrlxParseUtil.getClassFromContext(context.getTypeResolver(), functionCall.get().getType().asString());
NodeList<Expression> arguments = methodCallExpr.getArguments();
List<String> usedDeclarations = new ArrayList<>();
for (Expression arg : arguments) {
if (arg instanceof NameExpr && !arg.toString().equals("_this")) {
usedDeclarations.add(arg.toString());
} else if (arg instanceof MethodCallExpr) {
TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, null, bindingId, isPositional).toTypedExpression(arg);
usedDeclarations.addAll(typedExpressionResult.getUsedDeclarations());
}
}
return new DrlxParseSuccess(patternType, exprId, bindingId, methodCallExpr, returnType).setUsedDeclarations(usedDeclarations);
} else if (methodCallExpr.getScope().isPresent() && methodCallExpr.getScope().get() instanceof StringLiteralExpr) {
TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, String.class, bindingId, isPositional).toTypedExpression(methodCallExpr);
Optional<TypedExpression> optConverted = typedExpressionResult.getTypedExpression();
return optConverted.<DrlxParseResult>map(converted -> {
return new DrlxParseSuccess(String.class, exprId, bindingId, converted.getExpression(), converted.getType()).setLeft(converted).setUsedDeclarations(typedExpressionResult.getUsedDeclarations());
}).orElse(new DrlxParseFail());
} else if (patternType != null) {
NameExpr _this = new NameExpr("_this");
TypedExpression converted = DrlxParseUtil.toMethodCallWithClassCheck(context, methodCallExpr, bindingId, patternType, context.getTypeResolver());
Expression withThis = DrlxParseUtil.prepend(_this, converted.getExpression());
return new DrlxParseSuccess(patternType, exprId, bindingId, withThis, converted.getType()).setLeft(converted);
} else {
return new DrlxParseSuccess(patternType, exprId, bindingId, methodCallExpr, null);
}
}
if (drlxExpr instanceof FieldAccessExpr) {
FieldAccessExpr fieldCallExpr = (FieldAccessExpr) drlxExpr;
NameExpr _this = new NameExpr("_this");
TypedExpression converted = DrlxParseUtil.toMethodCallWithClassCheck(context, fieldCallExpr, bindingId, patternType, context.getTypeResolver());
Expression withThis = DrlxParseUtil.prepend(_this, converted.getExpression());
return new DrlxParseSuccess(patternType, exprId, bindingId, withThis, converted.getType()).setLeft(converted);
}
if (drlxExpr instanceof NameExpr) {
NameExpr nameExpr = (NameExpr) drlxExpr;
NameExpr _this = new NameExpr("_this");
TypedExpression converted = DrlxParseUtil.toMethodCallWithClassCheck(context, nameExpr, bindingId, patternType, context.getTypeResolver());
Expression withThis = DrlxParseUtil.prepend(_this, converted.getExpression());
if (drlx.getBind() != null) {
return new DrlxParseSuccess(patternType, exprId, bindingId, null, converted.getType()).setLeft(new TypedExpression(withThis, converted.getType())).addReactOnProperty(lcFirst(nameExpr.getNameAsString()));
} else {
return new DrlxParseSuccess(patternType, exprId, bindingId, withThis, converted.getType()).addReactOnProperty(nameExpr.getNameAsString());
}
}
if (drlxExpr instanceof OOPathExpr) {
return new DrlxParseSuccess(patternType, exprId, bindingId, drlxExpr, null);
}
if (drlxExpr instanceof LiteralExpr) {
return new DrlxParseSuccess(patternType, exprId, bindingId, drlxExpr, getLiteralExpressionType(((LiteralExpr) drlxExpr)));
}
// TODO
throw new UnsupportedOperationException("Unknown expression: " + toDrlx(drlxExpr));
}
Aggregations