Package javax.validation

Examples of javax.validation.ConstraintDefinitionException


    try {
      final Method method = ReflectionHelper.getMethod( annotation.annotationType(), "payload" );
      if ( method == null ) {
        String msg = annotation.annotationType().getName() + " contains Constraint annotation, but does " +
            "not contain a payload parameter.";
        throw new ConstraintDefinitionException( msg );
      }
      Class<?>[] defaultPayload = ( Class<?>[] ) method.getDefaultValue();
      if ( defaultPayload.length != 0 ) {
        String msg = annotation.annotationType()
            .getName() + " contains Constraint annotation, but the payload " +
            "paramter default value is not the empty array.";
        throw new ConstraintDefinitionException( msg );
      }
    }
    catch ( ClassCastException e ) {
      String msg = annotation.annotationType().getName() + " contains Constraint annotation, but the " +
          "payload parameter is of wrong type.";
      throw new ConstraintDefinitionException( msg );
    }
  }
View Full Code Here


    try {
      final Method method = ReflectionHelper.getMethod( annotation.annotationType(), "groups" );
      if ( method == null ) {
        String msg = annotation.annotationType().getName() + " contains Constraint annotation, but does " +
            "not contain a groups parameter.";
        throw new ConstraintDefinitionException( msg );
      }
      Class<?>[] defaultGroups = ( Class<?>[] ) method.getDefaultValue();
      if ( defaultGroups.length != 0 ) {
        String msg = annotation.annotationType()
            .getName() + " contains Constraint annotation, but the groups " +
            "paramter default value is not the empty array.";
        throw new ConstraintDefinitionException( msg );
      }
    }
    catch ( ClassCastException e ) {
      String msg = annotation.annotationType().getName() + " contains Constraint annotation, but the " +
          "groups parameter is of wrong type.";
      throw new ConstraintDefinitionException( msg );
    }
  }
View Full Code Here

      ReflectionHelper.getAnnotationParameter( annotation, "message", String.class );
    }
    catch ( Exception e ) {
      String msg = annotation.annotationType().getName() + " contains Constraint annotation, but does " +
          "not contain a message parameter.";
      throw new ConstraintDefinitionException( msg );
    }
  }
View Full Code Here

      //not 100% sure what to do here at this point, need to go over the JSR-303 and implement
    } else {
      if(this.descriptor.getAttributes().containsKey("message")) {
        templateString = (String)descriptor.getAttributes().get("message");
      } else {
        throw new ConstraintDefinitionException("An @Constraint annotation must have a message defined.");
      }
    }   
    return templateString;
  }
View Full Code Here

         
          //otherwise, continue validation
          try {
            cValidator.initialize(descriptor.getAnnotation());
          } catch (ClassCastException ccex) {
            throw new ConstraintDefinitionException("Could not cast constraint definition.",ccex);
          } catch (Exception ex) {
            throw new ValidationException("An exception occured while intializing constraint description.", ex);
          }
          ConstraintValidatorContextImpl validatorContext = new ConstraintValidatorContextImpl(descriptor);
          try {
View Full Code Here

                        }
                    } catch (final RuntimeException re) {
                        if (ValidationException.class.isInstance(re)) {
                            throw re;
                        }
                        throw new ConstraintDefinitionException(re);
                    }
                }
            }
        }
View Full Code Here

            fillAssignableTypes(type, validatorTypes.keySet(), assignableTypes);
            reduceAssignableTypes(assignableTypes);
            checkOneType(assignableTypes, type, owner, annotation, access);

            if ((type == Object.class || type == Object[].class) && validatorTypes.containsKey(Object.class) && validatorTypes.containsKey(Object[].class)) {
                throw new ConstraintDefinitionException("Only a validator for Object or Object[] should be provided for cross parameter validators");
            }

            final Collection<Class<? extends ConstraintValidator<A, ?>>> key = validatorTypes.get(assignableTypes.get(0));
            if (key.size() > 1) {
                final String message = "Factory returned " + key.size() + " validators";
                if (ParametersAccess.class.isInstance(access)) { // cross parameter
                    throw new ConstraintDefinitionException(message);
                }
                throw new UnexpectedTypeException(message);
            }

            @SuppressWarnings("unchecked")
View Full Code Here

        if (types.isEmpty()) {
            final String message = "No validator could be found for type " + stringForType(targetType)
                + ". See: @" + anno.annotationType().getSimpleName() + " at " + stringForLocation(owner, access);
            if (Object[].class.equals(targetType)) { // cross parameter
                throw new ConstraintDefinitionException(message);
            }
            throw new UnexpectedTypeException(message);
        } else if (types.size() > 1) {
            StringBuilder buf = new StringBuilder();
            buf.append("Ambiguous validators for type ");
View Full Code Here

                validator.initialize(annotation);
            } catch (RuntimeException e) {
                // Either a "legit" problem initializing the validator or a
                // ClassCastException if the validator associated annotation is
                // not a supertype of the validated annotation.
                throw new ConstraintDefinitionException("Incorrect validator ["
                    + validator.getClass().getCanonicalName() + "] for annotation "
                    + annotation.annotationType().getCanonicalName(), e);
            }
        }
    }
View Full Code Here

                        foundGroups = true;
                    } else if (ConstraintAnnotationAttributes.VALIDATION_APPLIES_TO.getAttributeName().equals(name)) {
                        buildValidationAppliesTo(method);
                        validationAppliesTo = method;
                    } else if (name.startsWith("valid")) {
                        throw new ConstraintDefinitionException("constraints parameters can't start with valid: " + name);
                    } else {
                        if (ConstraintAnnotationAttributes.MESSAGE.getAttributeName().equals(name)) {
                            foundMessage = true;
                            if (!TypeUtils.isAssignable(method.getReturnType(), ConstraintAnnotationAttributes.MESSAGE.getType())) {
                                throw new ConstraintDefinitionException("Return type for message() must be of type " + ConstraintAnnotationAttributes.MESSAGE.getType());
                            }
                        }
                        constraintValidation.getAttributes().put(name, method.invoke(constraintValidation.getAnnotation()));
                    }
                } catch (final ConstraintDefinitionException cde) {
                    throw cde;
                } catch (final Exception e) { // do nothing
                    log.log(Level.WARNING, String.format("Error processing annotation: %s ", constraintValidation.getAnnotation()), e);
                }
            }
        }

        if (!foundMessage) {
            throw new ConstraintDefinitionException("Annotation " + annotationType.getName() + " has no message method");
        }
        if (!foundPayload) {
            throw new ConstraintDefinitionException("Annotation " + annotationType.getName() + " has no payload method");
        }
        if (!foundGroups) {
            throw new ConstraintDefinitionException("Annotation " + annotationType.getName() + " has no groups method");
        }
        if (validationAppliesTo != null && !ConstraintTarget.IMPLICIT.equals(validationAppliesTo.getDefaultValue())) {
            throw new ConstraintDefinitionException("validationAppliesTo default value should be IMPLICIT");
        }

        // valid validationAppliesTo
        final Constraint annotation = annotationType.getAnnotation(Constraint.class);
        if (annotation == null) {
            return;
        }

        final Pair validationTarget = computeValidationTarget(annotation.validatedBy());
        for (final Annotation a : annotationType.getAnnotations()) {
            final Class<? extends Annotation> aClass = a.annotationType();
            if (aClass.getName().startsWith("java.lang.annotation.")) {
                continue;
            }

            final Constraint inheritedConstraint = aClass.getAnnotation(Constraint.class);
            if (inheritedConstraint != null && !aClass.getName().startsWith("javax.validation.constraints.")) {
                final Pair validationTargetInherited = computeValidationTarget(inheritedConstraint.validatedBy());
                if ((validationTarget.a > 0 && validationTargetInherited.b > 0 && validationTarget.b == 0)
                        || (validationTarget.b > 0 && validationTargetInherited.a > 0 && validationTarget.a == 0)) {
                    throw new ConstraintDefinitionException("Parent and child constraint have different targets");
                }
            }
        }
    }
View Full Code Here

TOP

Related Classes of javax.validation.ConstraintDefinitionException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.