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));
}
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();
}
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;
}
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;
}
}
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);
}
Aggregations