Search in sources :

Example 6 with SwitchEntry

use of com.github.javaparser.ast.stmt.SwitchEntry in project drools by kiegroup.

the class AccessibleMethod method setterSwitchStatement.

private Statement setterSwitchStatement() {
    SwitchStmt switchStmt = switchOnFieldName();
    NodeList<SwitchEntry> entries = switchStmt.getEntries();
    for (DescrFieldDefinition field : fields) {
        if (!field.isOverride()) {
            entries.add(setValueFromField(field));
        }
    }
    Optional<Class<?>> abstractResolvedClass = descrTypeDefinition.getAbstractResolvedClass();
    if (abstractResolvedClass.isPresent()) {
        entries.addAll(superClassSetterEntries(abstractResolvedClass.get()).collect(toList()));
    } else if (descrTypeDefinition.getDeclaredAbstractClass().isPresent()) {
        entries.add(switchEntry(SUPER_SET_VALUE));
    }
    return new BlockStmt(nodeList(switchStmt));
}
Also used : SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry)

Example 7 with SwitchEntry

use of com.github.javaparser.ast.stmt.SwitchEntry in project drools by kiegroup.

the class PropagatorCompilerHandler method startHashedAlphaNode.

@Override
public void startHashedAlphaNode(AlphaNode hashedAlpha, Object hashedValue) {
    SwitchEntry newSwitchEntry = new SwitchEntry();
    if (canInlineValue(fieldType)) {
        final Expression quotedHashedValue;
        if (hashedValue instanceof String) {
            quotedHashedValue = new StringLiteralExpr((String) hashedValue);
        } else if (hashedValue instanceof Long) {
            quotedHashedValue = new LongLiteralExpr((Long) hashedValue);
        } else {
            quotedHashedValue = new IntegerLiteralExpr((Integer) hashedValue);
        }
        newSwitchEntry.setLabels(nodeList(quotedHashedValue));
    } else {
        newSwitchEntry.setLabels(nodeList(new IntegerLiteralExpr(hashedAlpha.getId())));
    }
    addNewSwitchEntryToStack(newSwitchEntry);
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry)

Example 8 with SwitchEntry

use of com.github.javaparser.ast.stmt.SwitchEntry in project drools by kiegroup.

the class FEELPropertyAccessibleImplementation method setFeelPropertyDefinition.

private MethodDefinition setFeelPropertyDefinition() {
    MethodDeclaration setFEELProperty = cloneMethodTemplate("setFEELProperty");
    SwitchStmt firstSwitch = setFEELProperty.findFirst(SwitchStmt.class).orElseThrow(() -> new InvalidTemplateException("Missing switch statement in setFEELProperty"));
    firstSwitch.setComment(null);
    List<SwitchEntry> collect = fields.stream().map(this::toSetPropertySwitchEntry).collect(Collectors.toList());
    firstSwitch.setEntries(nodeList(collect));
    BlockStmt body = setFEELProperty.getBody().orElseThrow(() -> new InvalidTemplateException("Empty body in setFEELProperty"));
    if (typeDefinition instanceof AbstractDMNSetType) {
        body.addStatement(0, new ExpressionStmt(StaticJavaParser.parseExpression("definedKeySet.add(property)")));
    }
    MethodWithStringBody setFeelPropertyDefinition = new MethodWithStringBody("setFEELProperty", "void", body.toString()).addParameter(String.class.getCanonicalName(), "property").addParameter(Object.class.getCanonicalName(), "value");
    addOverrideAnnotation(setFeelPropertyDefinition);
    return setFeelPropertyDefinition;
}
Also used : SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) MethodWithStringBody(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodWithStringBody) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt)

Example 9 with SwitchEntry

use of com.github.javaparser.ast.stmt.SwitchEntry in project drools by kiegroup.

the class FEELPropertyAccessibleImplementation method toGetPropertySwitchEntry.

private SwitchEntry toGetPropertySwitchEntry(DMNDeclaredField fieldDefinition) {
    ReturnStmt returnStmt = new ReturnStmt();
    MethodCallExpr mc = StaticJavaParser.parseExpression(EvalHelper.PropertyValueResult.class.getCanonicalName() + ".ofValue()");
    String accessorName = fieldDefinition.overriddenGetterName().orElse(getAccessorName(fieldDefinition, "get"));
    mc.addArgument(new MethodCallExpr(new ThisExpr(), accessorName));
    returnStmt.setExpression(mc);
    return new SwitchEntry(nodeList(new StringLiteralExpr(fieldDefinition.getOriginalMapKey())), SwitchEntry.Type.STATEMENT_GROUP, nodeList(returnStmt));
}
Also used : EvalHelper(org.kie.dmn.feel.util.EvalHelper) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 10 with SwitchEntry

use of com.github.javaparser.ast.stmt.SwitchEntry in project drools by kiegroup.

the class FEELPropertyAccessibleImplementation method toSetPropertySwitchEntry.

private SwitchEntry toSetPropertySwitchEntry(DMNDeclaredField fieldDefinition) {
    String accessorName = fieldDefinition.overriddenSetterName().orElse(getAccessorName(fieldDefinition, "set"));
    MethodCallExpr setMethod = new MethodCallExpr(new ThisExpr(), accessorName);
    setMethod.addArgument(new CastExpr(StaticJavaParser.parseType(fieldDefinition.getObjectType()), new NameExpr("value")));
    ExpressionStmt setStatement = new ExpressionStmt();
    setStatement.setExpression(setMethod);
    NodeList<Expression> labels = nodeList(new StringLiteralExpr(fieldDefinition.getOriginalMapKey()));
    NodeList<Statement> statements = nodeList(setStatement, new ReturnStmt());
    return new SwitchEntry(labels, SwitchEntry.Type.STATEMENT_GROUP, statements);
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

SwitchEntry (com.github.javaparser.ast.stmt.SwitchEntry)12 SwitchStmt (com.github.javaparser.ast.stmt.SwitchStmt)5 Expression (com.github.javaparser.ast.expr.Expression)4 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)4 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)4 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)4 Statement (com.github.javaparser.ast.stmt.Statement)4 IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)3 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)3 ThisExpr (com.github.javaparser.ast.expr.ThisExpr)3 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)3 NodeList (com.github.javaparser.ast.NodeList)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 CastExpr (com.github.javaparser.ast.expr.CastExpr)2 NameExpr (com.github.javaparser.ast.expr.NameExpr)2 StaticJavaParser (com.github.javaparser.StaticJavaParser)1 StaticJavaParser.parseExpression (com.github.javaparser.StaticJavaParser.parseExpression)1 StaticJavaParser.parseStatement (com.github.javaparser.StaticJavaParser.parseStatement)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 NodeList.nodeList (com.github.javaparser.ast.NodeList.nodeList)1