Search in sources :

Example 16 with ClassOrInterfaceType

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

the class DrlxParseUtil method getExpressionType.

public static java.lang.reflect.Type getExpressionType(RuleContext context, TypeResolver typeResolver, Expression expr, Collection<String> usedDeclarations) {
    if (expr instanceof LiteralExpr) {
        return getLiteralExpressionType((LiteralExpr) expr);
    }
    if (expr instanceof UnaryExpr) {
        return getExpressionType(context, typeResolver, expr.asUnaryExpr().getExpression(), usedDeclarations);
    }
    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 MapCreationLiteralExpression) {
        return Map.class;
    }
    if (expr instanceof ListCreationLiteralExpression) {
        return List.class;
    }
    if (expr instanceof NameExpr) {
        return expressionTypeNameExpr(context, usedDeclarations, ((NameExpr) expr).getNameAsString());
    }
    if (expr instanceof DrlNameExpr) {
        return expressionTypeNameExpr(context, usedDeclarations, ((DrlNameExpr) expr).getNameAsString());
    }
    if (expr instanceof BinaryExpr) {
        return boolean.class;
    }
    if (expr instanceof MethodCallExpr) {
        MethodCallExpr methodCallExpr = (MethodCallExpr) expr;
        Optional<Expression> scopeExpression = methodCallExpr.getScope();
        if (scopeExpression.isPresent()) {
            java.lang.reflect.Type scopeType = getExpressionType(context, typeResolver, scopeExpression.get(), usedDeclarations);
            return returnTypeOfMethodCallExpr(context, typeResolver, methodCallExpr, scopeType, usedDeclarations);
        } else {
            throw new IllegalStateException("Scope expression is not present for " + ((MethodCallExpr) expr).getNameAsString() + "!");
        }
    }
    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);
        }
    }
    if (expr instanceof ConditionalExpr) {
        ConditionalExpr ternaryExpr = ((ConditionalExpr) expr);
        java.lang.reflect.Type conditionType = getExpressionType(context, typeResolver, ternaryExpr.getCondition(), usedDeclarations);
        if (conditionType != Boolean.class && conditionType != boolean.class) {
            context.addCompilationError(new InvalidExpressionErrorResult("Condtion used in ternary expression '" + expr + "' isn't boolean"));
            return Object.class;
        }
        java.lang.reflect.Type leftType = getExpressionType(context, typeResolver, ternaryExpr.getThenExpr(), usedDeclarations);
        java.lang.reflect.Type rightType = getExpressionType(context, typeResolver, ternaryExpr.getElseExpr(), usedDeclarations);
        Class<?> leftClass = toRawClass(leftType);
        Class<?> rightClass = toRawClass(rightType);
        if (leftClass.isAssignableFrom(rightClass)) {
            return leftType;
        }
        if (rightClass.isAssignableFrom(leftClass)) {
            return rightType;
        }
        return Object.class;
    }
    if (expr.isClassExpr()) {
        return Class.class;
    }
    throw new RuntimeException("Unknown expression type: " + PrintUtil.printNode(expr));
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) BigDecimalLiteralExpr(org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) CharLiteralExpr(com.github.javaparser.ast.expr.CharLiteralExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) BigIntegerLiteralExpr(org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Collectors.toList(java.util.stream.Collectors.toList) ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) ArrayAccessExpr(com.github.javaparser.ast.expr.ArrayAccessExpr) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) ConditionalExpr(com.github.javaparser.ast.expr.ConditionalExpr) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) Expression(com.github.javaparser.ast.expr.Expression) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) ClassUtil.toRawClass(org.drools.modelcompiler.util.ClassUtil.toRawClass) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 17 with ClassOrInterfaceType

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

the class ConsequenceDSLGenerator method generateMethod.

private static void generateMethod(int arity) {
    String template = "    public static <A, B> ConsequenceBuilder._2<A, B> on(Variable<A> decl1, Variable<B> decl2) {\n" + "        return new ConsequenceBuilder._2(decl1, decl2);\n" + "    }";
    MethodDeclaration bodyDeclaration = (MethodDeclaration) StaticJavaParser.parseBodyDeclaration(template);
    ClassOrInterfaceType arityType = toClassOrInterfaceType(arityName(arity));
    List<Parameter> parameters = genericTypeStream(arity, genericTypeIndex -> {
        ClassOrInterfaceType type = toClassOrInterfaceType(String.format("Variable<%s>", argumentTypeName(genericTypeIndex)));
        return new Parameter(type, argName(genericTypeIndex));
    }).collect(Collectors.toList());
    bodyDeclaration.setParameters(nodeList(parameters));
    ClassOrInterfaceType arityTypeWithNameSpace = toClassOrInterfaceType("ConsequenceBuilder." + arityName(arity));
    List<Expression> argumentCall = genericTypeStream(arity, genericTypeIndex -> new NameExpr(argName(genericTypeIndex))).collect(Collectors.toList());
    bodyDeclaration.setParameters(nodeList(parameters));
    Expression expr = new ObjectCreationExpr(null, arityTypeWithNameSpace, nodeList(argumentCall));
    bodyDeclaration.setBody(new BlockStmt(nodeList(new ReturnStmt(expr))));
    List<TypeParameter> genericTypeParameterList = genericTypeStream(arity, ConsequenceDSLGenerator::createTypeParameter).collect(Collectors.toList());
    List<Type> genericTypeList = genericTypeStream(arity, ConsequenceDSLGenerator::parseType).collect(Collectors.toList());
    ClassOrInterfaceType newType = toClassOrInterfaceType("ConsequenceBuilder." + arityName(arity));
    newType.setTypeArguments(nodeList(genericTypeList));
    bodyDeclaration.setType(newType);
    arityType.setTypeArguments(nodeList(genericTypeList));
    bodyDeclaration.setTypeParameters(nodeList(genericTypeParameterList));
    System.out.println(bodyDeclaration.toString());
    System.out.println();
}
Also used : IntStream(java.util.stream.IntStream) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) TypeParameter(com.github.javaparser.ast.type.TypeParameter) Parameter(com.github.javaparser.ast.body.Parameter) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) 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) Type(com.github.javaparser.ast.type.Type) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) IntFunction(java.util.function.IntFunction) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) TypeParameter(com.github.javaparser.ast.type.TypeParameter) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) 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) Type(com.github.javaparser.ast.type.Type) Expression(com.github.javaparser.ast.expr.Expression) TypeParameter(com.github.javaparser.ast.type.TypeParameter) Parameter(com.github.javaparser.ast.body.Parameter) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt)

Example 18 with ClassOrInterfaceType

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

the class CommonCodegenUtils method getTypedClassOrInterfaceTypeByTypes.

/**
 * Returns
 * <pre>
 *     (<i>className</i>)<(<i>comma-separated list of types</i>)>
 * </pre>
 * <p>
 * e.g
 * <pre>
 *     CLASS_NAME<TypeA, TypeB>
 * </pre>
 * a <b>typed</b> <code>ClassOrInterfaceType</code>
 * @param className
 * @param types
 * @return
 */
public static ClassOrInterfaceType getTypedClassOrInterfaceTypeByTypes(final String className, final List<Type> types) {
    ClassOrInterfaceType toReturn = parseClassOrInterfaceType(className);
    toReturn.setTypeArguments(NodeList.nodeList(types));
    return toReturn;
}
Also used : StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 19 with ClassOrInterfaceType

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

the class KiePMMLFactoryFactory method getInstantiationExpression.

public static Expression getInstantiationExpression(String kiePMMLModelClass, boolean isInterpreted) {
    ClassOrInterfaceType classOrInterfaceType = parseClassOrInterfaceType(kiePMMLModelClass);
    if (isInterpreted) {
        MethodCallExpr methodCallExpr = new MethodCallExpr();
        methodCallExpr.setScope(new NameExpr(kiePMMLModelClass));
        methodCallExpr.setName(GET_MODEL);
        return methodCallExpr;
    } else {
        ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
        objectCreationExpr.setType(classOrInterfaceType);
        return objectCreationExpr;
    }
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 20 with ClassOrInterfaceType

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

the class CommonCodegenUtilsTest method getParamMethodDeclaration.

@Test
public void getParamMethodDeclaration() {
    String methodName = "METHOD_NAME";
    final Map<String, ClassOrInterfaceType> parameterNameTypeMap = new HashMap<>();
    parameterNameTypeMap.put("stringParam", parseClassOrInterfaceType(String.class.getName()));
    parameterNameTypeMap.put("kiePMMLNameValueParam", parseClassOrInterfaceType(KiePMMLNameValue.class.getName()));
    parameterNameTypeMap.put("listParam", new ClassOrInterfaceType(null, new SimpleName(List.class.getName()), NodeList.nodeList(parseClassOrInterfaceType(KiePMMLNameValue.class.getName()))));
    MethodDeclaration retrieved = CommonCodegenUtils.getMethodDeclaration(methodName, parameterNameTypeMap);
    commonValidateMethodDeclaration(retrieved, methodName);
    commonValidateMethodDeclarationParams(retrieved, parameterNameTypeMap);
}
Also used : HashMap(java.util.HashMap) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SimpleName(com.github.javaparser.ast.expr.SimpleName) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) ArrayList(java.util.ArrayList) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Test(org.junit.Test)

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