Search in sources :

Example 1 with ClassOrInterfaceType

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

the class DrlxParseUtil method getExpressionType.

public static Class<?> getExpressionType(RuleContext context, TypeResolver typeResolver, Expression expr, Collection<String> usedDeclarations) {
    if (expr instanceof LiteralExpr) {
        return getLiteralExpressionType((LiteralExpr) expr);
    }
    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 NameExpr) {
        String name = ((NameExpr) expr).getNameAsString();
        if (usedDeclarations != null) {
            usedDeclarations.add(name);
        }
        return context.getDeclarationById(name).map(DeclarationSpec::getDeclarationClass).get();
    }
    if (expr instanceof MethodCallExpr) {
        MethodCallExpr methodCallExpr = (MethodCallExpr) expr;
        Class<?> scopeType = getExpressionType(context, typeResolver, methodCallExpr.getScope().get(), usedDeclarations);
        return returnTypeOfMethodCallExpr(context, typeResolver, methodCallExpr, scopeType, usedDeclarations);
    }
    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);
        }
    }
    throw new RuntimeException("Unknown expression type: " + expr);
}
Also used : ObjectCreationExpr(org.drools.javaparser.ast.expr.ObjectCreationExpr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ArrayAccessExpr(org.drools.javaparser.ast.expr.ArrayAccessExpr) ExpressionTyper.findLeftLeafOfNameExpr(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper.findLeftLeafOfNameExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) LiteralExpr(org.drools.javaparser.ast.expr.LiteralExpr) LongLiteralExpr(org.drools.javaparser.ast.expr.LongLiteralExpr) CharLiteralExpr(org.drools.javaparser.ast.expr.CharLiteralExpr) NullLiteralExpr(org.drools.javaparser.ast.expr.NullLiteralExpr) BooleanLiteralExpr(org.drools.javaparser.ast.expr.BooleanLiteralExpr) IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) DoubleLiteralExpr(org.drools.javaparser.ast.expr.DoubleLiteralExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) ArrayCreationExpr(org.drools.javaparser.ast.expr.ArrayCreationExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 2 with ClassOrInterfaceType

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

the class POJOGenerator method generateEqualsForField.

private static Statement generateEqualsForField(MethodDeclaration getter, String fieldName) {
    Type type = getter.getType();
    Statement statement;
    if (type instanceof ClassOrInterfaceType) {
        statement = parseStatement(" if( __fieldName != null ? !__fieldName.equals(that.__fieldName) : that.__fieldName != null) { return false; }");
    } else if (type instanceof PrimitiveType) {
        statement = parseStatement(" if( __fieldName != that.__fieldName) { return false; }");
    } else {
        throw new RuntimeException("Unknown type");
    }
    return replaceFieldName(statement, fieldName);
}
Also used : ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) PrimitiveType(org.drools.javaparser.ast.type.PrimitiveType) Type(org.drools.javaparser.ast.type.Type) JavaParser.parseStatement(org.drools.javaparser.JavaParser.parseStatement) Statement(org.drools.javaparser.ast.stmt.Statement) PrimitiveType(org.drools.javaparser.ast.type.PrimitiveType) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType)

Example 3 with ClassOrInterfaceType

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

the class PackageModel method buildRulesField.

private MethodCallExpr buildRulesField(ClassOrInterfaceDeclaration rulesClass) {
    MethodCallExpr rulesInit = new MethodCallExpr(null, "Arrays.asList");
    ClassOrInterfaceType rulesType = new ClassOrInterfaceType(null, new SimpleName("List"), new NodeList<Type>(new ClassOrInterfaceType(null, "Rule")));
    VariableDeclarator rulesVar = new VariableDeclarator(rulesType, "rulesList", rulesInit);
    rulesClass.addMember(new FieldDeclaration(EnumSet.of(Modifier.PUBLIC, Modifier.STATIC), rulesVar));
    return rulesInit;
}
Also used : ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Type(org.drools.javaparser.ast.type.Type) SimpleName(org.drools.javaparser.ast.expr.SimpleName) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) FieldDeclaration(org.drools.javaparser.ast.body.FieldDeclaration) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) VariableDeclarator(org.drools.javaparser.ast.body.VariableDeclarator)

Example 4 with ClassOrInterfaceType

use of org.drools.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 = JavaParser.parseClassOrInterfaceType(Global.class.getCanonicalName());
    varType.setTypeArguments(DrlxParseUtil.classToReferenceType(globalClass));
    Type declType = DrlxParseUtil.classToReferenceType(globalClass);
    MethodCallExpr declarationOfCall = new MethodCallExpr(null, "globalOf");
    declarationOfCall.addArgument(new ClassExpr(declType));
    declarationOfCall.addArgument(new StringLiteralExpr(packageName));
    declarationOfCall.addArgument(new StringLiteralExpr(globalName));
    FieldDeclaration field = classDeclaration.addField(varType, toVar(globalName), Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL);
    field.getVariables().get(0).setInitializer(declarationOfCall);
}
Also used : ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Type(org.drools.javaparser.ast.type.Type) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Global(org.drools.model.Global) FieldDeclaration(org.drools.javaparser.ast.body.FieldDeclaration) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 5 with ClassOrInterfaceType

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

the class ModelGenerator method addUnitData.

private static void addUnitData(String unitVar, Class<?> type, BlockStmt ruleBlock) {
    Type declType = classToReferenceType(type);
    ClassOrInterfaceType varType = JavaParser.parseClassOrInterfaceType(UnitData.class.getCanonicalName());
    varType.setTypeArguments(declType);
    VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, toVar(unitVar), Modifier.FINAL);
    MethodCallExpr unitDataCall = new MethodCallExpr(null, UNIT_DATA_CALL);
    unitDataCall.addArgument(new ClassExpr(declType));
    unitDataCall.addArgument(new StringLiteralExpr(unitVar));
    AssignExpr var_assign = new AssignExpr(var_, unitDataCall, AssignExpr.Operator.ASSIGN);
    ruleBlock.addStatement(var_assign);
}
Also used : ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Type(org.drools.javaparser.ast.type.Type) DrlxParseUtil.classToReferenceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.classToReferenceType) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) UnitData(org.drools.model.UnitData) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr)

Aggregations

ClassOrInterfaceType (org.drools.javaparser.ast.type.ClassOrInterfaceType)8 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)6 Type (org.drools.javaparser.ast.type.Type)6 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)5 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)4 FieldDeclaration (org.drools.javaparser.ast.body.FieldDeclaration)2 AssignExpr (org.drools.javaparser.ast.expr.AssignExpr)2 VariableDeclarationExpr (org.drools.javaparser.ast.expr.VariableDeclarationExpr)2 DrlxParseUtil.classToReferenceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.classToReferenceType)2 BehaviorDescr (org.drools.compiler.lang.descr.BehaviorDescr)1 JavaParser.parseStatement (org.drools.javaparser.JavaParser.parseStatement)1 VariableDeclarator (org.drools.javaparser.ast.body.VariableDeclarator)1 ArrayAccessExpr (org.drools.javaparser.ast.expr.ArrayAccessExpr)1 ArrayCreationExpr (org.drools.javaparser.ast.expr.ArrayCreationExpr)1 BooleanLiteralExpr (org.drools.javaparser.ast.expr.BooleanLiteralExpr)1 CharLiteralExpr (org.drools.javaparser.ast.expr.CharLiteralExpr)1 DoubleLiteralExpr (org.drools.javaparser.ast.expr.DoubleLiteralExpr)1 IntegerLiteralExpr (org.drools.javaparser.ast.expr.IntegerLiteralExpr)1 LiteralExpr (org.drools.javaparser.ast.expr.LiteralExpr)1 LongLiteralExpr (org.drools.javaparser.ast.expr.LongLiteralExpr)1