Search in sources :

Example 1 with AnnotationDeclarationError

use of org.drools.compiler.compiler.AnnotationDeclarationError in project drools by kiegroup.

the class KnowledgeBuilderImpl method normalizeAnnotation.

private AnnotationDescr normalizeAnnotation(TypeResolver typeResolver, AnnotationDescr annotationDescr) {
    Class<?> annotationClass = null;
    try {
        annotationClass = typeResolver.resolveType(annotationDescr.getName(), TypeResolver.ONLY_ANNOTATION_CLASS_FILTER);
    } catch (ClassNotFoundException | NoClassDefFoundError e) {
        String className = normalizeAnnotationNonStrictName(annotationDescr.getName());
        try {
            annotationClass = typeResolver.resolveType(className, TypeResolver.ONLY_ANNOTATION_CLASS_FILTER);
        } catch (ClassNotFoundException | NoClassDefFoundError e1) {
        // non-strict annotation, ignore error
        }
    }
    if (annotationClass != null) {
        annotationDescr.setFullyQualifiedName(annotationClass.getCanonicalName());
        for (String key : annotationDescr.getValueMap().keySet()) {
            try {
                Method m = annotationClass.getMethod(key);
                Object val = annotationDescr.getValue(key);
                if (val instanceof Object[] && !m.getReturnType().isArray()) {
                    addBuilderResult(new AnnotationDeclarationError(annotationDescr, "Wrong cardinality on property " + key));
                    return annotationDescr;
                }
                if (m.getReturnType().isArray() && !(val instanceof Object[])) {
                    val = new Object[] { val };
                    annotationDescr.setKeyValue(key, val);
                }
                if (m.getReturnType().isArray()) {
                    int n = Array.getLength(val);
                    for (int j = 0; j < n; j++) {
                        if (Class.class.equals(m.getReturnType().getComponentType())) {
                            String className = Array.get(val, j).toString().replace(".class", "");
                            Array.set(val, j, typeResolver.resolveType(className).getName() + ".class");
                        } else if (m.getReturnType().getComponentType().isAnnotation()) {
                            Array.set(val, j, normalizeAnnotation(typeResolver, (AnnotationDescr) Array.get(val, j)));
                        }
                    }
                } else {
                    if (Class.class.equals(m.getReturnType())) {
                        String className = annotationDescr.getValueAsString(key).replace(".class", "");
                        annotationDescr.setKeyValue(key, typeResolver.resolveType(className));
                    } else if (m.getReturnType().isAnnotation()) {
                        annotationDescr.setKeyValue(key, normalizeAnnotation(typeResolver, (AnnotationDescr) annotationDescr.getValue(key)));
                    }
                }
            } catch (NoSuchMethodException e) {
                addBuilderResult(new AnnotationDeclarationError(annotationDescr, "Unknown annotation property " + key));
            } catch (ClassNotFoundException | NoClassDefFoundError e) {
                addBuilderResult(new AnnotationDeclarationError(annotationDescr, "Unknown class " + annotationDescr.getValue(key) + " used in property " + key + " of annotation " + annotationDescr.getName()));
            }
        }
    }
    return annotationDescr;
}
Also used : AnnotationDeclarationError(org.drools.compiler.compiler.AnnotationDeclarationError) Method(java.lang.reflect.Method)

Example 2 with AnnotationDeclarationError

use of org.drools.compiler.compiler.AnnotationDeclarationError in project drools by kiegroup.

the class KnowledgeBuilderImpl method normalizeAnnotations.

protected void normalizeAnnotations(AnnotatedBaseDescr annotationsContainer, TypeResolver typeResolver, boolean isStrict) {
    for (AnnotationDescr annotationDescr : annotationsContainer.getAnnotations()) {
        annotationDescr.setResource(annotationsContainer.getResource());
        annotationDescr.setStrict(isStrict);
        if (annotationDescr.isDuplicated()) {
            addBuilderResult(new AnnotationDeclarationError(annotationDescr, "Duplicated annotation: " + annotationDescr.getName()));
        }
        if (isStrict) {
            normalizeStrictAnnotation(typeResolver, annotationDescr);
        } else {
            normalizeAnnotation(typeResolver, annotationDescr);
        }
    }
    annotationsContainer.indexByFQN(isStrict);
}
Also used : AnnotationDeclarationError(org.drools.compiler.compiler.AnnotationDeclarationError) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

Aggregations

AnnotationDeclarationError (org.drools.compiler.compiler.AnnotationDeclarationError)2 Method (java.lang.reflect.Method)1 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)1