use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.
the class InlineFieldReferenceInitHandler method generateInitBody.
private void generateInitBody(BlockStmt initBodyPerPartition, List<NetworkNode> subNodes, int partitionIndex) {
CompilationUnit partitionedCompilationUnit = partitionedNodeInitialisationClasses.get(partitionIndex);
PackageDeclaration partitionedClassPackage = (PackageDeclaration) partitionedCompilationUnit.getChildNodes().get(0);
ClassOrInterfaceDeclaration partitionedClass = (ClassOrInterfaceDeclaration) partitionedCompilationUnit.getChildNodes().get(1);
for (NetworkNode n : subNodes) {
if (n instanceof CanInlineInANC) {
String variableName;
final ClassOrInterfaceType initMethodReturnType;
if (n instanceof AlphaNode) {
variableName = getVariableName((AlphaNode) n);
initMethodReturnType = StaticJavaParser.parseClassOrInterfaceType(getVariableType((AlphaNode) n).getCanonicalName());
} else {
variableName = getVariableName((Sink) n);
initMethodReturnType = StaticJavaParser.parseClassOrInterfaceType(getVariableType((Sink) n).getCanonicalName());
}
String initMethodName = String.format("init%s", variableName);
// We'll need to partition the lambda creation as with too many the compiler crashes
// We'll move it to a separated class
MethodDeclaration initMethod = new MethodDeclaration(nodeList(Modifier.publicModifier(), Modifier.staticModifier()), initMethodName, initMethodReturnType, parametersFromAdditionalFields());
Expression initExpressionForNode = ((CanInlineInANC) n).toANCInlinedForm();
partitionedClass.addMember(initMethod);
BlockStmt initMethodBlock = new BlockStmt();
initMethodBlock.addStatement(new ReturnStmt(initExpressionForNode));
initMethod.setBody(initMethodBlock);
String callInitMethodAndAssignToFieldString = String.format("%s = %s.%s.%s(ctx);", variableName, partitionedClassPackage.getNameAsString(), partitionedClass.getNameAsString(), initMethodName);
initBodyPerPartition.addStatement(parseStatement(callInitMethodAndAssignToFieldString));
}
}
}
use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.
the class ParserUtil method objectCreationExprToValue.
public static Object objectCreationExprToValue(ObjectCreationExpr objectCreationExpr, RuleContext context) {
// Only a few classes/constructors are handled. Otherwise, value becomes null so a link would be UNKNOWN. To be enhanced : DROOLS-6711
ClassOrInterfaceType type = objectCreationExpr.getType();
Class<?> clazz = getClassFromType(context.getTypeResolver(), type);
if (clazz.equals(BigDecimal.class)) {
NodeList<Expression> arguments = objectCreationExpr.getArguments();
Optional<Object> opt = arguments.stream().findFirst().filter(StringLiteralExpr.class::isInstance).map(literalExpr -> literalExpr.asStringLiteralExpr().asString()).map(BigDecimal::new);
return opt.orElse(null);
} else if (clazz.equals(BigInteger.class)) {
NodeList<Expression> arguments = objectCreationExpr.getArguments();
Optional<Object> opt = arguments.stream().findFirst().filter(StringLiteralExpr.class::isInstance).map(literalExpr -> literalExpr.asStringLiteralExpr().asString()).map(BigInteger::new);
return opt.orElse(null);
}
return null;
}
use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.
the class QueryDefImplGenerator method classDeclaration.
private ClassOrInterfaceDeclaration classDeclaration(CompilationUnit cu) {
ClassOrInterfaceDeclaration clazz = cu.addClass(className, Modifier.Keyword.PUBLIC);
clazz.addExtendedType("QueryDefImpl");
clazz.addImplementedType("ModelComponent");
ClassOrInterfaceType implement = new ClassOrInterfaceType(null, String.format("Query%dDef", arity));
NodeList<Type> typeArguments = nodeList();
rangeArity().forEach(i -> {
String genericTypeName = genericTypeName(i);
typeArguments.add(parseType(genericTypeName));
clazz.addTypeParameter(new TypeParameter(genericTypeName));
clazz.addField(parseType(genericType("Variable", genericTypeName)), argIndex(i), Modifier.Keyword.PRIVATE, Modifier.Keyword.FINAL);
});
implement.setTypeArguments(typeArguments);
clazz.addImplementedType(implement);
return clazz;
}
use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.
the class FlowDSLQueryGenerator method queryNameClass.
private void queryNameClass(ClassOrInterfaceDeclaration clazz) {
MethodDeclaration query1 = clazz.addMethod(QUERY, Modifier.Keyword.PUBLIC, Modifier.Keyword.STATIC);
query1.addParameter(STRINGLITERAL, "name");
BlockStmt stmts = new BlockStmt();
NodeList<Type> typeArguments = nodeList();
NodeList<TypeParameter> typeParameters = nodeList();
NodeList<Expression> arguments = nodeList();
ClassOrInterfaceType queryCallViewItemImpl = queryDefImpl();
ObjectCreationExpr objCreationExpr = new ObjectCreationExpr(null, queryCallViewItemImpl, arguments);
objCreationExpr.setDiamondOperator();
stmts.addStatement(new ReturnStmt(objCreationExpr));
objCreationExpr.addArgument(new NameExpr(VIEWBUILDER));
objCreationExpr.addArgument("name");
rangeArity().forEach(i -> {
String genericTypeName = stringWithIndex("T", i);
String type = stringWithIndex("type", i);
typeArguments.add(parseType(genericTypeName));
arguments.add(new NameExpr(type));
String classGenericType = genericType(genericTypeName);
Type genericType = parseType(classGenericType);
query1.addParameter(genericType, type);
typeParameters.add(new TypeParameter(genericTypeName));
});
query1.setTypeParameters(typeParameters);
query1.setBody(stmts);
ClassOrInterfaceType type = queryDef();
type.setTypeArguments(typeArguments);
query1.setType(type);
}
use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.
the class FlowDSLQueryGenerator method queryPkgNameClass.
private void queryPkgNameClass(ClassOrInterfaceDeclaration clazz) {
MethodDeclaration query1 = clazz.addMethod(QUERY, Modifier.Keyword.PUBLIC, Modifier.Keyword.STATIC);
query1.addParameter(STRINGLITERAL, "pkg");
query1.addParameter(STRINGLITERAL, "name");
BlockStmt stmts = new BlockStmt();
NodeList<Type> typeArguments = nodeList();
NodeList<TypeParameter> typeParameters = nodeList();
NodeList<Expression> arguments = nodeList();
ClassOrInterfaceType queryCallViewItemImpl = queryDefImpl();
ObjectCreationExpr objCreationExpr = new ObjectCreationExpr(null, queryCallViewItemImpl, arguments);
objCreationExpr.setDiamondOperator();
stmts.addStatement(new ReturnStmt(objCreationExpr));
objCreationExpr.addArgument(new NameExpr(VIEWBUILDER));
objCreationExpr.addArgument("pkg");
objCreationExpr.addArgument("name");
rangeArity().forEach(i -> {
String genericTypeName = stringWithIndex("T", i);
String type = stringWithIndex("type", i);
typeArguments.add(parseType(genericTypeName));
arguments.add(new NameExpr(type));
String classGenericType = genericType(genericTypeName);
Type genericType = parseType(classGenericType);
query1.addParameter(genericType, type);
typeParameters.add(new TypeParameter(genericTypeName));
});
query1.setTypeParameters(typeParameters);
query1.setBody(stmts);
ClassOrInterfaceType type = queryDef();
type.setTypeArguments(typeArguments);
query1.setType(type);
}
Aggregations