@Inject public InjectingConstraintValidatorFactory(BeanManager beanManager) { Contracts.assertNotNull( beanManager, "The BeanManager cannot be null" ); this.beanManager = beanManager; }
/** * Used to register the method validation interceptor binding annotation. * * @param beforeBeanDiscoveryEvent event fired before the bean discovery process starts * @param beanManager the bean manager. */ public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery beforeBeanDiscoveryEvent, final BeanManager beanManager) { Contracts.assertNotNull( beforeBeanDiscoveryEvent, "The BeforeBeanDiscovery event cannot be null" ); Contracts.assertNotNull( beanManager, "The BeanManager cannot be null" ); // Register the interceptor explicitly. This way, no beans.xml is needed AnnotatedType<ValidationInterceptor> annotatedType = beanManager.createAnnotatedType( ValidationInterceptor.class ); beforeBeanDiscoveryEvent.addAnnotatedType( annotatedType, ValidationInterceptor.class.getName() ); }
/** * Watches the {@code ProcessBean} event in order to determine whether beans for {@code ValidatorFactory} and * {@code Validator} already have been registered by some other component. * * @param processBeanEvent event fired for each enabled bean. */ public void processBean(@Observes ProcessBean<?> processBeanEvent) { Contracts.assertNotNull( processBeanEvent, "The ProcessBean event cannot be null" ); Bean<?> bean = processBeanEvent.getBean(); if ( bean.getTypes().contains( ValidatorFactory.class ) || bean instanceof ValidatorFactoryBean ) { if ( bean.getQualifiers().contains( defaultQualifier ) ) { defaultValidatorFactoryBean = bean; } if ( bean.getQualifiers().contains( hibernateValidatorQualifier ) ) { hibernateValidatorFactoryBean = bean; } } else if ( bean.getTypes().contains( Validator.class ) || bean instanceof ValidatorBean ) { if ( bean.getQualifiers().contains( defaultQualifier ) ) { defaultValidatorBean = bean; } if ( bean.getQualifiers().contains( hibernateValidatorQualifier ) ) { hibernateValidatorBean = bean; } } }
/** * Get a list of all methods which the given class declares, implements, * overrides or inherits. Methods are added by adding first all methods of * the class itself and its implemented interfaces, then the super class and * its interfaces, etc. * * @param clazz the class for which to retrieve the methods * * @return set of all methods of the given class */ public static List<Method> getAllMethods(Class<?> clazz) { Contracts.assertNotNull( clazz ); List<Method> methods = newArrayList(); for ( Class<?> hierarchyClass : ClassHierarchyHelper.getHierarchy( clazz ) ) { Collections.addAll( methods, run( GetMethods.action( hierarchyClass ) ) ); } return methods; }
/** * Used to register the method validation interceptor bindings. * * @param processAnnotatedTypeEvent event fired for each annotated type * @param <T> the annotated type */ public <T> void processAnnotatedType(@Observes @WithAnnotations({ Constraint.class, Valid.class, ValidateOnExecution.class }) ProcessAnnotatedType<T> processAnnotatedTypeEvent) { Contracts.assertNotNull( processAnnotatedTypeEvent, "The ProcessAnnotatedType event cannot be null" ); // validation globally disabled if ( !isExecutableValidationEnabled ) { return; } AnnotatedType<T> type = processAnnotatedTypeEvent.getAnnotatedType(); Set<AnnotatedCallable<? super T>> constrainedCallables = determineConstrainedCallables( type ); if ( !constrainedCallables.isEmpty() ) { ValidationEnabledAnnotatedType<T> wrappedType = new ValidationEnabledAnnotatedType<>( type, constrainedCallables ); processAnnotatedTypeEvent.setAnnotatedType( wrappedType ); } }
Contracts.assertNotNull( afterBeanDiscoveryEvent, "The AfterBeanDiscovery event cannot be null" ); Contracts.assertNotNull( beanManager, "The BeanManager cannot be null" );
ValidatorBundleLocator(final String bundleName, final ClassLoader classLoader, final boolean aggregate) { Contracts.assertNotNull(bundleName, "bundleName"); this.bundleName = bundleName; this.classLoader = classLoader; this.aggregate = aggregate && RESOURCE_BUNDLE_CONTROL_INSTANTIABLE; }
@Override public final PropertyDescriptor getConstraintsForProperty(String propertyName) { assertNotNull( propertyName, "The property name cannot be null" ); return constrainedProperties.get( propertyName ); }
@Override public HibernateConstraintValidatorContext addMessageParameter(String name, Object value) { Contracts.assertNotNull( name, "null is not a valid value for a parameter name" ); if ( messageParameters == null ) { messageParameters = new HashMap<>(); } this.messageParameters.put( name, value ); return this; }
private static Type getArrayType(Type componentType) { Contracts.assertNotNull( componentType, "componentType" ); if ( componentType instanceof Class<?> ) { return Array.newInstance( (Class<?>) componentType, 0 ).getClass(); } return genericArrayType( componentType ); }
@Override public HibernateValidatorConfiguration scriptEvaluatorFactory(ScriptEvaluatorFactory scriptEvaluatorFactory) { Contracts.assertNotNull( scriptEvaluatorFactory, MESSAGES.parameterMustNotBeNull( "scriptEvaluatorFactory" ) ); this.scriptEvaluatorFactory = scriptEvaluatorFactory; return this; }
@Override public <T> Set<ConstraintViolation<T>> validateParameters(T object, Method method, Object[] parameterValues, Class<?>... groups) { Contracts.assertNotNull( object, MESSAGES.validatedObjectMustNotBeNull() ); Contracts.assertNotNull( method, MESSAGES.validatedMethodMustNotBeNull() ); Contracts.assertNotNull( parameterValues, MESSAGES.validatedParameterArrayMustNotBeNull() ); return validateParameters( object, (Executable) method, parameterValues, groups ); }
@Override public <T> Set<ConstraintViolation<T>> validateReturnValue(T object, Method method, Object returnValue, Class<?>... groups) { Contracts.assertNotNull( object, MESSAGES.validatedObjectMustNotBeNull() ); Contracts.assertNotNull( method, MESSAGES.validatedMethodMustNotBeNull() ); return validateReturnValue( object, (Executable) method, returnValue, groups ); }
@Override public <T> Set<ConstraintViolation<T>> validateConstructorReturnValue(Constructor<? extends T> constructor, T createdObject, Class<?>... groups) { Contracts.assertNotNull( constructor, MESSAGES.validatedConstructorMustNotBeNull() ); Contracts.assertNotNull( createdObject, MESSAGES.validatedConstructorCreatedInstanceMustNotBeNull() ); return validateReturnValue( null, constructor, createdObject, groups ); }
private void sanityCheckGroups(Class<?>[] groups) { Contracts.assertNotNull( groups, MESSAGES.groupMustNotBeNull() ); for ( Class<?> clazz : groups ) { if ( clazz == null ) { throw new IllegalArgumentException( MESSAGES.groupMustNotBeNull() ); } } }
private static Type[] getResolvedInterfaces(Type type) { Contracts.assertNotNull( type, "type" ); Class<?> rawType = getErasedReferenceType( type ); Type[] interfaces = rawType.getGenericInterfaces(); Type[] resolvedInterfaces = new Type[interfaces.length]; for ( int i = 0; i < interfaces.length; i++ ) { resolvedInterfaces[i] = resolveTypeVariables( interfaces[i], type ); } return resolvedInterfaces; }
@Override public MethodDescriptor getConstraintsForMethod(String methodName, Class<?>... parameterTypes) { Contracts.assertNotNull( methodName, MESSAGES.methodNameMustNotBeNull() ); return constrainedMethods.get( ExecutableHelper.getSignature( methodName, parameterTypes ) ); }
private static Type getResolvedSuperclass(Type type) { Contracts.assertNotNull( type, "type" ); Class<?> rawType = getErasedReferenceType( type ); Type supertype = rawType.getGenericSuperclass(); if ( supertype == null ) { return null; } return resolveTypeVariables( supertype, type ); }
@Override public final <C> TypeConstraintMappingContext<C> type(Class<C> type) { Contracts.assertNotNull( type, MESSAGES.beanTypeMustNotBeNull() ); if ( configuredTypes.contains( type ) ) { throw LOG.getBeanClassHasAlreadyBeConfiguredViaProgrammaticApiException( type ); } TypeConstraintMappingContextImpl<C> typeContext = new TypeConstraintMappingContextImpl<>( this, type ); typeContexts.add( typeContext ); configuredTypes.add( type ); return typeContext; }
public ProgrammaticMetaDataProvider(ConstraintHelper constraintHelper, TypeResolutionHelper typeResolutionHelper, ValueExtractorManager valueExtractorManager, Set<DefaultConstraintMapping> constraintMappings) { Contracts.assertNotNull( constraintMappings ); configuredBeans = CollectionHelper.toImmutableMap( createBeanConfigurations( constraintMappings, constraintHelper, typeResolutionHelper, valueExtractorManager ) ); assertUniquenessOfConfiguredTypes( constraintMappings ); annotationProcessingOptions = mergeAnnotationProcessingOptions( constraintMappings ); }