Search in sources :

Example 46 with ClassOrInterfaceType

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

the class ExpressionTyper method listCreationLiteral.

private Optional<TypedExpressionCursor> listCreationLiteral(ListCreationLiteralExpression listCreationLiteralExpression, java.lang.reflect.Type originalTypeCursor) {
    ClassOrInterfaceType arrayListType = (ClassOrInterfaceType) parseType(ArrayList.class.getCanonicalName());
    BlockStmt initializationStmt = new BlockStmt();
    InitializerDeclaration body = new InitializerDeclaration(false, initializationStmt);
    ObjectCreationExpr newArrayListExpr = new ObjectCreationExpr(null, arrayListType, nodeList(), nodeList(), nodeList(body));
    for (Expression e : listCreationLiteralExpression.getExpressions()) {
        ListCreationLiteralExpressionElement expr = (ListCreationLiteralExpressionElement) e;
        Expression value = resolveCreationLiteralNameExpr(originalTypeCursor, expr.getValue());
        initializationStmt.addStatement(new MethodCallExpr(null, "add", nodeList(value)));
    }
    return of(new TypedExpressionCursor(newArrayListExpr, ArrayList.class));
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) InitializerDeclaration(com.github.javaparser.ast.body.InitializerDeclaration) Expression(com.github.javaparser.ast.expr.Expression) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) DrlxParseUtil.isThisExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isThisExpression) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ListCreationLiteralExpressionElement(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpressionElement) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) NullSafeMethodCallExpr(org.drools.mvel.parser.ast.expr.NullSafeMethodCallExpr) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr)

Example 47 with ClassOrInterfaceType

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

the class GeneratedEqualsMethod method generateEqualsForField.

private 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 ArrayType) {
        Type componentType = ((ArrayType) type).getComponentType();
        if (componentType instanceof PrimitiveType) {
            statement = parseStatement(" if( !java.util.Arrays.equals((" + componentType + "[])__fieldName, (" + componentType + "[])that.__fieldName)) { return false; }");
        } else {
            statement = parseStatement(" if( !java.util.Arrays.equals((Object[])__fieldName, (Object[])that.__fieldName)) { 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 : ArrayType(com.github.javaparser.ast.type.ArrayType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ArrayType(com.github.javaparser.ast.type.ArrayType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) Type(com.github.javaparser.ast.type.Type) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) Statement(com.github.javaparser.ast.stmt.Statement) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)

Example 48 with ClassOrInterfaceType

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

the class ConsequenceGenerator method replaceName.

private static void replaceName(int arity, ClassOrInterfaceDeclaration clone, ConstructorDeclaration constructor) {
    ClassOrInterfaceType arityType = toClassOrInterfaceType(arityName(arity));
    clone.findAll(ClassOrInterfaceDeclaration.class, findNodeWithNameArityClassName(ARITY_CLASS_NAME)).forEach(c -> c.setName(arityName(arity)));
    clone.findAll(ClassOrInterfaceType.class, findNodeWithNameArityClassName(ARITY_CLASS_NAME)).forEach(oldType -> oldType.replace(arityType));
    constructor.setName(arityName(arity));
}
Also used : ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)

Example 49 with ClassOrInterfaceType

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

the class ConsequenceGenerator method replaceGenericType.

private static void replaceGenericType(int arity, ClassOrInterfaceDeclaration clone, ConstructorDeclaration constructor) {
    List<TypeParameter> genericTypeParameterList = genericTypeStream(arity, ConsequenceGenerator::createTypeParameter).collect(Collectors.toList());
    clone.setTypeParameters(nodeList(genericTypeParameterList));
    List<Type> genericTypeList = genericTypeStream(arity, ConsequenceGenerator::parseType).collect(Collectors.toList());
    ClassOrInterfaceType extendTypeParameter = toClassOrInterfaceType(arityName(arity));
    extendTypeParameter.setTypeArguments(nodeList(genericTypeList));
    ClassOrInterfaceType extendedType = new ClassOrInterfaceType(null, new SimpleName("AbstractValidBuilder"), nodeList(extendTypeParameter));
    clone.findAll(MethodDeclaration.class, mc -> mc.getType().asString().equals(arityName(arity))).forEach(c -> c.setType(extendTypeParameter));
    clone.setExtendedTypes(nodeList(extendedType));
    List<Parameter> parameters = genericTypeStream(arity, genericTypeIndex -> {
        ClassOrInterfaceType type = toClassOrInterfaceType(String.format("Variable<%s>", argumentTypeName(genericTypeIndex)));
        return new Parameter(type, argName(genericTypeIndex));
    }).collect(Collectors.toList());
    constructor.setParameters(nodeList(parameters));
    constructorBody(arity, constructor);
    ClassOrInterfaceType arityBlockType = toClassOrInterfaceType("Block" + arity);
    arityBlockType.setTypeArguments(nodeList(genericTypeList));
    ClassOrInterfaceType arityBlockTypePlusOne = toClassOrInterfaceType("Block" + (arity + 1));
    List<Type> genericTypeListPlusDrools = new ArrayList<>(genericTypeList);
    genericTypeListPlusDrools.add(0, toClassOrInterfaceType("Drools"));
    arityBlockTypePlusOne.setTypeArguments(nodeList(genericTypeListPlusDrools));
    clone.findAll(ClassOrInterfaceType.class, findNodeWithNameArityClassName(ARITY_CLASS_BLOCK)).forEach(oldType -> oldType.replace(arityBlockType));
    clone.findAll(ClassOrInterfaceType.class, findNodeWithNameArityClassName(ARITY_CLASS_BLOCK_PLUS_ONE)).forEach(oldType -> oldType.replace(arityBlockTypePlusOne));
}
Also used : IntStream(java.util.stream.IntStream) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Parameter(com.github.javaparser.ast.body.Parameter) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) ArrayList(java.util.ArrayList) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) NodeWithSimpleName(com.github.javaparser.ast.nodeTypes.NodeWithSimpleName) Type(com.github.javaparser.ast.type.Type) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) IntFunction(java.util.function.IntFunction) SimpleName(com.github.javaparser.ast.expr.SimpleName) Predicate(java.util.function.Predicate) TypeParameter(com.github.javaparser.ast.type.TypeParameter) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) NameExpr(com.github.javaparser.ast.expr.NameExpr) Collectors(java.util.stream.Collectors) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) Stream(java.util.stream.Stream) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeParameter(com.github.javaparser.ast.type.TypeParameter) 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) NodeWithSimpleName(com.github.javaparser.ast.nodeTypes.NodeWithSimpleName) SimpleName(com.github.javaparser.ast.expr.SimpleName) ArrayList(java.util.ArrayList) Parameter(com.github.javaparser.ast.body.Parameter) TypeParameter(com.github.javaparser.ast.type.TypeParameter) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)

Example 50 with ClassOrInterfaceType

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

the class PropagatorCompilerHandler method startObjectTypeNode.

@Override
public void startObjectTypeNode(ObjectTypeNode objectTypeNode) {
    // we only need to create a reference to the object, not handle, if there is a hashed alpha in the network
    if (alphaNetContainsHashedField) {
        // example of what this will look like
        // ExampleFact fact = (ExampleFact) handle.getObject();
        ClassOrInterfaceType type = StaticJavaParser.parseClassOrInterfaceType(factClassName);
        ExpressionStmt factVariable = localVariableWithCastInitializer(type, LOCAL_FACT_VAR_NAME, new MethodCallExpr(new NameExpr(FACT_HANDLE_PARAM_NAME), "getObject"));
        getCurrentBlockStatement().addStatement(factVariable);
    }
}
Also used : NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

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