@SuppressWarnings("unchecked") private void parseConstraintDefinitions( List<ConstraintDefinitionType> constraintDefinitionList, String defaultPackage) { for (ConstraintDefinitionType constraintDefinition : constraintDefinitionList) { String annotationClassName = constraintDefinition.getAnnotation(); Class<?> clazz = getClass(annotationClassName, defaultPackage); if (!clazz.isAnnotation()) { throw log.getIsNotAnAnnotationException(annotationClassName); } Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) clazz; ValidatedByType validatedByType = constraintDefinition.getValidatedBy(); List<Class<? extends ConstraintValidator<? extends Annotation, ?>>> constraintValidatorClasses = newArrayList(); if (validatedByType.getIncludeExistingValidators() != null && validatedByType.getIncludeExistingValidators()) { constraintValidatorClasses.addAll(findConstraintValidatorClasses(annotationClass)); } for (String validatorClassName : validatedByType.getValue()) { Class<? extends ConstraintValidator<?, ?>> validatorClass; validatorClass = (Class<? extends ConstraintValidator<?, ?>>) ReflectionHelper.loadClass(validatorClassName, this.getClass()); if (!ConstraintValidator.class.isAssignableFrom(validatorClass)) { throw log.getIsNotAConstraintValidatorClassException(validatorClass); } constraintValidatorClasses.add(validatorClass); } constraintHelper.addConstraintValidatorDefinition( annotationClass, constraintValidatorClasses); } }
private Object convertStringToReturnType(Class<?> returnType, String value) { Object returnValue; if (returnType.getName().equals(byte.class.getName())) { try { returnValue = Byte.parseByte(value); } catch (NumberFormatException e) { throw log.getInvalidNumberFormatException("byte", e); } } else if (returnType.getName().equals(short.class.getName())) { try { returnValue = Short.parseShort(value); } catch (NumberFormatException e) { throw log.getInvalidNumberFormatException("short", e); } } else if (returnType.getName().equals(int.class.getName())) { try { returnValue = Integer.parseInt(value); } catch (NumberFormatException e) { throw log.getInvalidNumberFormatException("int", e); } } else if (returnType.getName().equals(long.class.getName())) { try { returnValue = Long.parseLong(value); } catch (NumberFormatException e) { throw log.getInvalidNumberFormatException("long", e); } } else if (returnType.getName().equals(float.class.getName())) { try { returnValue = Float.parseFloat(value); } catch (NumberFormatException e) { throw log.getInvalidNumberFormatException("float", e); } } else if (returnType.getName().equals(double.class.getName())) { try { returnValue = Double.parseDouble(value); } catch (NumberFormatException e) { throw log.getInvalidNumberFormatException("double", e); } } else if (returnType.getName().equals(boolean.class.getName())) { returnValue = Boolean.parseBoolean(value); } else if (returnType.getName().equals(char.class.getName())) { if (value.length() != 1) { throw log.getInvalidCharValueException(value); } returnValue = value.charAt(0); } else if (returnType.getName().equals(String.class.getName())) { returnValue = value; } else if (returnType.getName().equals(Class.class.getName())) { returnValue = ReflectionHelper.loadClass(value, this.getClass()); } else { try { @SuppressWarnings("unchecked") Class<Enum> enumClass = (Class<Enum>) returnType; returnValue = Enum.valueOf(enumClass, value); } catch (ClassCastException e) { throw log.getInvalidReturnTypeException(returnType, e); } } return returnValue; }
private <A extends Annotation> Class<?> getAnnotationParameterType( Class<A> annotationClass, String name) { Method m = ReflectionHelper.getMethod(annotationClass, name); if (m == null) { throw log.getAnnotationDoesNotContainAParameterException(annotationClass.getName(), name); } return m.getReturnType(); }
private Class<?> getClass(String clazz, String defaultPackage) { String fullyQualifiedClass; if (isQualifiedClass(clazz)) { fullyQualifiedClass = clazz; } else { fullyQualifiedClass = defaultPackage + PACKAGE_SEPARATOR + clazz; } return ReflectionHelper.loadClass(fullyQualifiedClass, this.getClass()); }
private Schema getMappingSchema() { ClassLoader loader = ReflectionHelper.getClassLoaderFromClass(XmlMappingParser.class); URL schemaUrl = loader.getResource(VALIDATION_MAPPING_XSD); SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = null; try { schema = sf.newSchema(schemaUrl); } catch (SAXException e) { log.unableToCreateSchema(VALIDATION_MAPPING_XSD, e.getMessage()); } return schema; }
private void parsePropertyLevelOverrides( List<GetterType> getters, Class<?> beanClass, String defaultPackage) { List<String> getterNames = newArrayList(); for (GetterType getterType : getters) { String getterName = getterType.getName(); if (getterNames.contains(getterName)) { throw log.getIsDefinedTwiceInMappingXmlForBeanException(getterName, beanClass.getName()); } else { getterNames.add(getterName); } boolean containsMethod = ReflectionHelper.containsMethodWithPropertyName(beanClass, getterName); if (!containsMethod) { throw log.getBeanDoesNotContainThePropertyException(beanClass.getName(), getterName); } final Method method = ReflectionHelper.getMethodFromPropertyName(beanClass, getterName); // ignore annotations boolean ignoreGetterAnnotation = getterType.getIgnoreAnnotations() == null ? false : getterType.getIgnoreAnnotations(); if (ignoreGetterAnnotation) { annotationProcessingOptions.ignorePropertyLevelConstraintAnnotationsOnMember(method); } // valid if (getterType.getValid() != null) { addCascadedMember(beanClass, method); } // constraints for (ConstraintType constraint : getterType.getConstraint()) { MetaConstraint<?> metaConstraint = createMetaConstraint(constraint, beanClass, method, defaultPackage); addMetaConstraint(beanClass, metaConstraint); } } }
private void parseFieldLevelOverrides( List<FieldType> fields, Class<?> beanClass, String defaultPackage) { List<String> fieldNames = newArrayList(); for (FieldType fieldType : fields) { String fieldName = fieldType.getName(); if (fieldNames.contains(fieldName)) { throw log.getIsDefinedTwiceInMappingXmlForBeanException(fieldName, beanClass.getName()); } else { fieldNames.add(fieldName); } final boolean containsField = ReflectionHelper.containsDeclaredField(beanClass, fieldName); if (!containsField) { throw log.getBeanDoesNotContainTheFieldException(beanClass.getName(), fieldName); } final Field field = ReflectionHelper.getDeclaredField(beanClass, fieldName); // ignore annotations boolean ignoreFieldAnnotation = fieldType.getIgnoreAnnotations() == null ? false : fieldType.getIgnoreAnnotations(); if (ignoreFieldAnnotation) { annotationProcessingOptions.ignorePropertyLevelConstraintAnnotationsOnMember(field); } // valid if (fieldType.getValid() != null) { addCascadedMember(beanClass, field); } // constraints for (ConstraintType constraint : fieldType.getConstraint()) { MetaConstraint<?> metaConstraint = createMetaConstraint(constraint, beanClass, field, defaultPackage); addMetaConstraint(beanClass, metaConstraint); } } }
public ConstrainedParameter build( ConstraintHelper constraintHelper, ParameterNameProvider parameterNameProvider) { // TODO HV-919 Support specification of type parameter constraints via XML and API return new ConstrainedParameter( ConfigurationSource.API, ConstraintLocation.forParameter(executableContext.getExecutable(), parameterIndex), ReflectionHelper.typeOf(executableContext.getExecutable(), parameterIndex), parameterIndex, executableContext .getExecutable() .getParameterNames(parameterNameProvider) .get(parameterIndex), getConstraints(constraintHelper), Collections.<MetaConstraint<?>>emptySet(), groupConversions, isCascading, unwrapMode()); }
private <T> void determineConstrainedMethods( AnnotatedType<T> type, BeanDescriptor beanDescriptor, Set<AnnotatedCallable<? super T>> callables) { List<Method> overriddenAndImplementedMethods = InheritedMethodsHelper.getAllMethods(type.getJavaClass()); for (AnnotatedMethod<? super T> annotatedMethod : type.getMethods()) { Method method = annotatedMethod.getJavaMember(); boolean isGetter = ReflectionHelper.isGetterMethod(method); // obtain @ValidateOnExecution from the top-most method in the hierarchy Method methodForExecutableTypeRetrieval = replaceWithOverriddenOrInterfaceMethod(method, overriddenAndImplementedMethods); EnumSet<ExecutableType> classLevelExecutableTypes = executableTypesDefinedOnType(methodForExecutableTypeRetrieval.getDeclaringClass()); EnumSet<ExecutableType> memberLevelExecutableType = executableTypesDefinedOnMethod(methodForExecutableTypeRetrieval, isGetter); ExecutableType currentExecutableType = isGetter ? ExecutableType.GETTER_METHODS : ExecutableType.NON_GETTER_METHODS; // validation is enabled per default, so explicit configuration can just veto whether // validation occurs if (veto(classLevelExecutableTypes, memberLevelExecutableType, currentExecutableType)) { continue; } boolean needsValidation; if (isGetter) { needsValidation = isGetterConstrained(method, beanDescriptor); } else { needsValidation = isNonGetterConstrained(method, beanDescriptor); } if (needsValidation) { callables.add(annotatedMethod); } } }
private List<ConstrainedParameter> getParameters( ConstraintHelper constraintHelper, ParameterNameProvider parameterNameProvider) { List<ConstrainedParameter> constrainedParameters = newArrayList(); for (int i = 0; i < parameterContexts.length; i++) { ParameterConstraintMappingContextImpl parameter = parameterContexts[i]; if (parameter != null) { constrainedParameters.add(parameter.build(constraintHelper, parameterNameProvider)); } else { constrainedParameters.add( new ConstrainedParameter( ConfigurationSource.API, ConstraintLocation.forParameter(executable, i), ReflectionHelper.typeOf(executable, i), i, executable.getParameterNames(parameterNameProvider).get(i))); } } return constrainedParameters; }
private boolean isGetterConstrained(Method method, BeanDescriptor beanDescriptor) { String propertyName = ReflectionHelper.getPropertyName(method); PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty(propertyName); return propertyDescriptor != null && propertyDescriptor.findConstraints().declaredOn(ElementType.METHOD).hasConstraints(); }