Search in sources :

Example 1 with AnnotationValuePairDefinitionImpl

use of org.kie.workbench.common.services.datamodeller.core.impl.AnnotationValuePairDefinitionImpl in project kie-wb-common by kiegroup.

the class DriverUtils method buildAnnotationDefinition.

public static AnnotationDefinition buildAnnotationDefinition(Class cls) {
    if (!cls.isAnnotation())
        return null;
    AnnotationDefinitionImpl annotationDefinition = new AnnotationDefinitionImpl(NamingUtils.normalizeClassName(cls.getName()));
    // set retention and target.
    DriverUtils.copyAnnotationRetention(cls, annotationDefinition);
    DriverUtils.copyAnnotationTarget(cls, annotationDefinition);
    Method[] methods = cls.getMethods();
    Method method;
    AnnotationValuePairDefinitionImpl valuePairDefinition;
    Class returnType;
    boolean isArray = false;
    for (int i = 0; methods != null && i < methods.length; i++) {
        method = methods[i];
        if (DriverUtils.isAnnotationMember(cls, method)) {
            returnType = method.getReturnType();
            if ((isArray = returnType.isArray()))
                returnType = returnType.getComponentType();
            valuePairDefinition = new AnnotationValuePairDefinitionImpl(method.getName(), NamingUtils.normalizeClassName(returnType.getName()), DriverUtils.buildValuePairType(returnType), isArray, // TODO, review this default value assignment, when we have annotations the default value should be an AnnotationInstance
            method.getDefaultValue() != null ? method.getDefaultValue().toString() : null);
            if (valuePairDefinition.isAnnotation()) {
                valuePairDefinition.setAnnotationDefinition(buildAnnotationDefinition(returnType));
            }
            if (valuePairDefinition.isEnum()) {
                Object[] enumConstants = returnType.getEnumConstants();
                if (enumConstants != null) {
                    String[] strEnumConstants = new String[enumConstants.length];
                    for (int j = 0; j < enumConstants.length; j++) {
                        strEnumConstants[j] = enumConstants[j].toString();
                    }
                    valuePairDefinition.setEnumValues(strEnumConstants);
                }
            }
            annotationDefinition.addValuePair(valuePairDefinition);
        }
    }
    return annotationDefinition;
}
Also used : AnnotationDefinitionImpl(org.kie.workbench.common.services.datamodeller.core.impl.AnnotationDefinitionImpl) Method(java.lang.reflect.Method) AnnotationValuePairDefinitionImpl(org.kie.workbench.common.services.datamodeller.core.impl.AnnotationValuePairDefinitionImpl)

Aggregations

Method (java.lang.reflect.Method)1 AnnotationDefinitionImpl (org.kie.workbench.common.services.datamodeller.core.impl.AnnotationDefinitionImpl)1 AnnotationValuePairDefinitionImpl (org.kie.workbench.common.services.datamodeller.core.impl.AnnotationValuePairDefinitionImpl)1