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