Search in sources :

Example 1 with Constraint

use of javax.validation.Constraint in project podam by devopsfolks.

the class TypeManufacturerUtil method findAttributeStrategy.

/**
     * It returns a {@link AttributeStrategy} if one was specified in
     * annotations, or {@code null} otherwise.
     *
     * @param strategy
     *            The data provider strategy
     * @param annotations
     *            The list of annotations, irrelevant annotations will be removed
     * @param attributeType
     *            Type of attribute expected to be returned
     * @return {@link AttributeStrategy}, if {@link PodamStrategyValue} or bean
     *         validation constraint annotation was found among annotations
     * @throws IllegalAccessException
     *         if attribute strategy cannot be instantiated
     * @throws InstantiationException
     *         if attribute strategy cannot be instantiated
     * @throws SecurityException 
     *         if access security is violated
     * @throws InvocationTargetException
     *         if invocation failed
     * @throws IllegalArgumentException 
     *         if illegal argument provided to a constructor
     */
public static AttributeStrategy<?> findAttributeStrategy(DataProviderStrategy strategy, List<Annotation> annotations, Class<?> attributeType) throws InstantiationException, IllegalAccessException, SecurityException, IllegalArgumentException, InvocationTargetException {
    List<Annotation> localAnnotations = new ArrayList<Annotation>(annotations);
    Iterator<Annotation> iter = localAnnotations.iterator();
    while (iter.hasNext()) {
        Annotation annotation = iter.next();
        if (annotation instanceof PodamStrategyValue) {
            PodamStrategyValue strategyAnnotation = (PodamStrategyValue) annotation;
            return strategyAnnotation.value().newInstance();
        }
        /* Podam annotation is present, this will be handled later by type manufacturers */
        if (annotation.annotationType().getAnnotation(PodamAnnotation.class) != null) {
            return null;
        }
        /* Find real class out of proxy */
        Class<? extends Annotation> annotationClass = annotation.getClass();
        if (Proxy.isProxyClass(annotationClass)) {
            Class<?>[] interfaces = annotationClass.getInterfaces();
            if (interfaces.length == 1) {
                @SuppressWarnings("unchecked") Class<? extends Annotation> tmp = (Class<? extends Annotation>) interfaces[0];
                annotationClass = tmp;
            }
        }
        AttributeStrategy<?> attrStrategy = strategy.getStrategyForAnnotation(annotationClass);
        if (null != attrStrategy) {
            return attrStrategy;
        }
        if (annotation.annotationType().getAnnotation(Constraint.class) != null) {
            if (annotation instanceof NotNull || annotation.annotationType().getName().equals("org.hibernate.validator.constraints.NotEmpty") || annotation.annotationType().getName().equals("org.hibernate.validator.constraints.NotBlank")) {
                /* We don't need to do anything for NotNull constraint */
                iter.remove();
            } else if (!NotNull.class.getPackage().equals(annotationClass.getPackage())) {
                LOG.warn("Please, register AttributeStratergy for custom " + "constraint {}, in DataProviderStrategy! Value " + "will be left to null", annotation);
            }
        } else {
            iter.remove();
        }
    }
    AttributeStrategy<?> retValue = null;
    if (!localAnnotations.isEmpty() && !Collection.class.isAssignableFrom(attributeType) && !Map.class.isAssignableFrom(attributeType) && !attributeType.isArray()) {
        retValue = new BeanValidationStrategy(attributeType);
    }
    return retValue;
}
Also used : Constraint(javax.validation.Constraint) NotNull(javax.validation.constraints.NotNull) Annotation(java.lang.annotation.Annotation)

Aggregations

Annotation (java.lang.annotation.Annotation)1 Constraint (javax.validation.Constraint)1 NotNull (javax.validation.constraints.NotNull)1