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