Search in sources :

Example 11 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.

the class PackageModel method buildRulesField.

private MethodCallExpr buildRulesField(ClassOrInterfaceDeclaration rulesClass) {
    MethodCallExpr rulesInit = new MethodCallExpr(null, "java.util.Arrays.asList");
    ClassOrInterfaceType rulesType = new ClassOrInterfaceType(null, new SimpleName("java.util.List"), new NodeList<Type>(toClassOrInterfaceType(Rule.class)));
    MethodDeclaration rulesGetter = new MethodDeclaration(NodeList.nodeList(publicModifier()), rulesType, "getRulesList");
    rulesGetter.createBody().addStatement(new ReturnStmt(rulesInit));
    rulesClass.addMember(rulesGetter);
    return rulesInit;
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SimpleName(com.github.javaparser.ast.expr.SimpleName) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 12 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.

the class PackageModel method addGlobalField.

private static void addGlobalField(ClassOrInterfaceDeclaration classDeclaration, String packageName, String globalName, Class<?> globalClass) {
    ClassOrInterfaceType varType = toClassOrInterfaceType(Global.class);
    varType.setTypeArguments(DrlxParseUtil.classToReferenceType(globalClass));
    Type declType = DrlxParseUtil.classToReferenceType(globalClass);
    MethodCallExpr declarationOfCall = createDslTopLevelMethod(GLOBAL_OF_CALL);
    declarationOfCall.addArgument(new ClassExpr(declType));
    declarationOfCall.addArgument(toStringLiteral(packageName));
    declarationOfCall.addArgument(toStringLiteral(globalName));
    FieldDeclaration field = classDeclaration.addField(varType, toVar(globalName), publicModifier().getKeyword(), staticModifier().getKeyword(), finalModifier().getKeyword());
    field.getVariables().get(0).setInitializer(declarationOfCall);
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 13 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.

the class ExecModelLambdaPostProcessor method replaceLambda.

private void replaceLambda(LambdaExpr lambdaExpr, Function<Optional<String>, MaterializedLambda> lambdaExtractor, Optional<String> exprId) {
    try {
        CreatedClass aClass = lambdaExtractor.apply(exprId).create(lambdaExpr.clone(), imports, staticImports);
        lambdaClasses.put(aClass.getClassNameWithPackage(), aClass);
        ClassOrInterfaceType type = toClassOrInterfaceType(aClass.getClassNameWithPackage());
        FieldAccessExpr lambdaInstance = lambdaInstance(type);
        toBeReplacedLambdas.add(() -> lambdaExpr.replace(lambdaInstance));
    } catch (DoNotConvertLambdaException e) {
        logger.debug("Cannot externalize lambdas {}", e.getMessage());
    }
}
Also used : FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)

Example 14 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.

the class MaterializedLambdaExtractor method createImplementedTypes.

@Override
protected NodeList<ClassOrInterfaceType> createImplementedTypes() {
    ClassOrInterfaceType functionType = functionType();
    NodeList<Type> typeArguments = lambdaParametersToTypeArguments();
    typeArguments.add(returnType);
    functionType.setTypeArguments(typeArguments);
    return nodeList(functionType, lambdaExtractorType());
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)

Example 15 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType 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();
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Expression(com.github.javaparser.ast.expr.Expression) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) AssignExpr(com.github.javaparser.ast.expr.AssignExpr)

Aggregations

ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)76 Expression (com.github.javaparser.ast.expr.Expression)33 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)29 Type (com.github.javaparser.ast.type.Type)24 NameExpr (com.github.javaparser.ast.expr.NameExpr)23 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)21 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)21 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)17 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)17 NodeList (com.github.javaparser.ast.NodeList)14 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)13 TypeParameter (com.github.javaparser.ast.type.TypeParameter)13 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)12 Test (org.junit.Test)11 StaticJavaParser.parseType (com.github.javaparser.StaticJavaParser.parseType)9 List (java.util.List)9 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)8 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)7 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)7 HashMap (java.util.HashMap)7