@Inject public InjectingConstraintValidatorFactory(BeanManager beanManager) { Contracts.assertNotNull( beanManager, "The BeanManager cannot be null" ); this.beanManager = beanManager; }
public ValidationEnabledAnnotatedType(AnnotatedType<T> type, Set<AnnotatedCallable<? super T>> constrainedCallables) { this.wrappedType = type; this.wrappedMethods = CollectionHelper.newHashSet(); this.wrappedConstructors = CollectionHelper.newHashSet(); buildWrappedCallable( constrainedCallables ); }
/** * 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; }
public ValidatorFactoryBean(BeanManager beanManager, ValidationProviderHelper validationProviderHelper) { this.beanManager = beanManager; this.destructibleResources = newHashSet( 5 ); this.validationProviderHelper = validationProviderHelper; this.types = Collections.unmodifiableSet( CollectionHelper.<Type>newHashSet( ClassHierarchyHelper.getHierarchy( validationProviderHelper.getValidatorFactoryBeanClass() ) ) ); }
public ValidationExtension() { Configuration<?> config = Validation.byDefaultProvider().configure(); // we use the default ParameterNameProvider here as we cannot use the injected one // as it hasn't been turned into a managed bean yet and might not be able to // return a parameter name. At this stage, we don't care about the parameter names. config.parameterNameProvider( config.getDefaultParameterNameProvider() ); BootstrapConfiguration bootstrap = config.getBootstrapConfiguration(); globalExecutableTypes = bootstrap.getDefaultValidatedExecutableTypes(); isExecutableValidationEnabled = bootstrap.isExecutableValidationEnabled(); validatorFactory = config.buildValidatorFactory(); validator = validatorFactory.getValidator(); getterPropertySelectionStrategyHelper = GetterPropertySelectionStrategyHelper.forValidationFactory( validatorFactory ); executableHelper = new ExecutableHelper( new TypeResolutionHelper() ); }
/** * {@inheritDoc} * * @throws NullPointerException if the specified key is null */ @Override public boolean remove(Object key, Object value) { int hash = hashOf(key); if (value == null) return false; return segmentFor(hash).remove(key, hash, value, false) != null; }
private Method replaceWithOverriddenOrInterfaceMethod(Method method, List<Method> allMethodsOfType) { LinkedList<Method> list = new LinkedList<>( allMethodsOfType ); Iterator<Method> iterator = list.descendingIterator(); while ( iterator.hasNext() ) { Method overriddenOrInterfaceMethod = iterator.next(); if ( executableHelper.overrides( method, overriddenOrInterfaceMethod ) ) { if ( method.getAnnotation( ValidateOnExecution.class ) != null ) { throw log.getValidateOnExecutionOnOverriddenOrInterfaceMethodException( method ); } return overriddenOrInterfaceMethod; } } return method; } }
public ElementDescriptorImpl(Type type, Set<ConstraintDescriptorImpl<?>> constraintDescriptors, boolean defaultGroupSequenceRedefined, List<Class<?>> defaultGroupSequence) { this.type = (Class<?>) TypeHelper.getErasedType( type ); this.constraintDescriptors = CollectionHelper.toImmutableSet( constraintDescriptors ); this.defaultGroupSequenceRedefined = defaultGroupSequenceRedefined; this.defaultGroupSequence = CollectionHelper.toImmutableList( defaultGroupSequence ); }
final Object newKeyReference(K key, ReferenceType keyType, ReferenceQueue<Object> refQueue) { if (keyType == ReferenceType.WEAK) return new WeakKeyReference<K>(key, hash, refQueue); if (keyType == ReferenceType.SOFT) return new SoftKeyReference<K>(key, hash, refQueue); return key; }
final Object newValueReference(V value, ReferenceType valueType, ReferenceQueue<Object> refQueue) { if (valueType == ReferenceType.WEAK) return new WeakValueReference<V>(value, keyRef, hash, refQueue); if (valueType == ReferenceType.SOFT) return new SoftValueReference<V>(value, keyRef, hash, refQueue); return value; }
/** * Builds an {@link Iterator} for a given array. It is (un)necessarily ugly because we have to deal with array of primitives. * * @param object a given array * @return an {@code Iterator} iterating over the array */ @SuppressWarnings({ "unchecked", "rawtypes" }) // Reflection is used to ensure the correct types are used public static Iterator<?> iteratorFromArray(Object object) { return new ArrayIterator( accessorFromArray( object ), object ); }
/** * Builds an {@link Iterable} for a given array. It is (un)necessarily ugly because we have to deal with array of primitives. * * @param object a given array * @return an {@code Iterable} providing iterators over the array */ @SuppressWarnings({ "unchecked", "rawtypes" }) // Reflection is used to ensure the correct types are used public static Iterable<?> iterableFromArray(Object object) { return new ArrayIterable( accessorFromArray( object ), object ); }
public ValidatorBean(BeanManager beanManager, Bean<?> validatorFactoryBean, ValidationProviderHelper validationProviderHelper) { this.beanManager = beanManager; this.validatorFactoryBean = validatorFactoryBean; this.validationProviderHelper = validationProviderHelper; this.types = Collections.unmodifiableSet( CollectionHelper.<Type>newHashSet( ClassHierarchyHelper.getHierarchy( validationProviderHelper.getValidatorBeanClass() ) ) ); }
/** * 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() ); }
/** * Returns the qualifiers to be used for registering a validator or validator factory. */ @SuppressWarnings("serial") private static Set<Annotation> determineRequiredQualifiers(boolean isDefaultProvider, boolean isHibernateValidator) { HashSet<Annotation> qualifiers = newHashSet( 3 ); if ( isDefaultProvider ) { qualifiers.add( new AnnotationLiteral<Default>() { } ); } if ( isHibernateValidator ) { qualifiers.add( new AnnotationLiteral<HibernateValidator>() { } ); } qualifiers.add( new AnnotationLiteral<Any>() { } ); return qualifiers; }
/** * 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; } } }
/** * 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" );