Search in sources :

Example 1 with AnnotationDefinition

use of org.drools.modelcompiler.builder.generator.declaredtype.api.AnnotationDefinition in project drools by kiegroup.

the class DMNDeclaredField method annotateFieldWithOAS.

private void annotateFieldWithOAS(List<AnnotationDefinition> annotations) {
    if (getObjectType().equals("java.lang.String") && fieldDMNType.getAllowedValues() != null && !fieldDMNType.getAllowedValues().isEmpty()) {
        annotateFieldWithOASEnumValues(annotations);
    } else {
        boolean isTemporal = DMNTypeUtils.isFEELBuiltInType(fieldDMNType) && DMNAllTypesIndex.TEMPORALS.contains(DMNTypeUtils.getFEELBuiltInType(fieldDMNType));
        boolean isTemporalCollection = fieldDMNType.isCollection() && DMNTypeUtils.isFEELBuiltInType(DMNTypeUtils.genericOfCollection(fieldDMNType)) && DMNAllTypesIndex.TEMPORALS.contains(DMNTypeUtils.getFEELBuiltInType(DMNTypeUtils.genericOfCollection(fieldDMNType)));
        if (isTemporal || isTemporalCollection) {
            DMNType temporal = isTemporalCollection ? DMNTypeUtils.genericOfCollection(fieldDMNType) : fieldDMNType;
            Class<?> clazz = index.getJacksonDeserializeAs(temporal).orElseThrow(IllegalStateException::new);
            // intentionally use DMNType name
            String temporalName = temporal.getName();
            if (codeGenConfig.isWithMPOpenApiAnnotation()) {
                AnnotationDefinition annDef = createOASAnnotationForTemporal("org.eclipse.microprofile.openapi.annotations.media.Schema", clazz, temporalName);
                if (isTemporalCollection) {
                    annDef.addValue("type", "org.eclipse.microprofile.openapi.annotations.enums.SchemaType.ARRAY");
                }
                annotations.add(annDef);
            }
            if (codeGenConfig.isWithIOSwaggerOASv3Annotation()) {
                AnnotationDefinition annDef = createOASAnnotationForTemporal("io.swagger.v3.oas.annotations.media.Schema", clazz, temporalName);
                if (isTemporalCollection) {
                    annDef.addValue("type", "\"array\"");
                }
                annotations.add(annDef);
            }
        }
    }
}
Also used : AnnotationDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.AnnotationDefinition) SimpleAnnotationDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.SimpleAnnotationDefinition) DMNType(org.kie.dmn.api.core.DMNType)

Example 2 with AnnotationDefinition

use of org.drools.modelcompiler.builder.generator.declaredtype.api.AnnotationDefinition in project drools by kiegroup.

the class DescrFieldDefinition method addPositionAnnotation.

public void addPositionAnnotation(int position) {
    AnnotationDefinition annotationDefinition = DescrAnnotationDefinition.createPositionAnnotation(position);
    annotations.put(annotationDefinition.getName(), annotationDefinition);
}
Also used : AnnotationDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.AnnotationDefinition)

Example 3 with AnnotationDefinition

use of org.drools.modelcompiler.builder.generator.declaredtype.api.AnnotationDefinition in project drools by kiegroup.

the class GeneratedClassDeclaration method addMethod.

private void addMethod(MethodDefinition methodDefinition) {
    List<Modifier.Keyword> modifiers = new ArrayList<>();
    if (methodDefinition.isStatic()) {
        modifiers.add(Modifier.Keyword.STATIC);
    }
    if (methodDefinition.isPublic()) {
        modifiers.add(Modifier.Keyword.PUBLIC);
    }
    MethodDeclaration methodDeclaration = generatedClass.addMethod(methodDefinition.getMethodName(), modifiers.toArray(new Modifier.Keyword[0]));
    methodDeclaration.setType(methodDefinition.getReturnType());
    for (MethodParameter mp : methodDefinition.parameters()) {
        methodDeclaration.addParameter(mp.getType(), mp.getName());
    }
    for (AnnotationDefinition a : methodDefinition.getAnnotations()) {
        methodDeclaration.addAnnotation(createSimpleAnnotation(a.getName()));
    }
    methodDeclaration.setBody(StaticJavaParser.parseBlock(methodDefinition.getBody()));
}
Also used : AnnotationDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.AnnotationDefinition) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ArrayList(java.util.ArrayList) MethodParameter(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodParameter)

Example 4 with AnnotationDefinition

use of org.drools.modelcompiler.builder.generator.declaredtype.api.AnnotationDefinition in project drools by kiegroup.

the class DMNDeclaredField method getFieldAnnotations.

@Override
public List<AnnotationDefinition> getFieldAnnotations() {
    List<AnnotationDefinition> annotations = new ArrayList<>();
    if (codeGenConfig.isWithJacksonAnnotation()) {
        boolean isCollection = fieldDMNType.isCollection();
        DMNType narrowTypeHint = isCollection ? DMNTypeUtils.getRootBaseTypeOfCollection(fieldDMNType) : fieldDMNType;
        Optional<Class<?>> as = index.getJacksonDeserializeAs(narrowTypeHint);
        as.ifPresent(asClass -> annotations.add(new SimpleAnnotationDefinition("com.fasterxml.jackson.databind.annotation.JsonDeserialize").addValue(isCollection ? "contentAs" : "as", asClass.getCanonicalName() + ".class")));
    }
    if (codeGenConfig.isWithMPOpenApiAnnotation() || codeGenConfig.isWithIOSwaggerOASv3Annotation()) {
        annotateFieldWithOAS(annotations);
    }
    return annotations;
}
Also used : AnnotationDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.AnnotationDefinition) SimpleAnnotationDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.SimpleAnnotationDefinition) ArrayList(java.util.ArrayList) SimpleAnnotationDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.SimpleAnnotationDefinition) DMNType(org.kie.dmn.api.core.DMNType)

Aggregations

AnnotationDefinition (org.drools.modelcompiler.builder.generator.declaredtype.api.AnnotationDefinition)4 ArrayList (java.util.ArrayList)2 SimpleAnnotationDefinition (org.drools.modelcompiler.builder.generator.declaredtype.api.SimpleAnnotationDefinition)2 DMNType (org.kie.dmn.api.core.DMNType)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 MethodParameter (org.drools.modelcompiler.builder.generator.declaredtype.api.MethodParameter)1