/** * @param <VL> * @param context * The current validation context. * @return the current {@link DynamicMetaBean} in context, or * <code>null</code> if the current meta bean is not dynamic. */ private static <VL extends ValidationListener> DynamicMetaBean getDynamicMetaBean(ValidationContext<VL> context) { return context.getMetaBean() instanceof DynamicMetaBean ? (DynamicMetaBean) context.getMetaBean() : null; }
/** * Initialize from {@link ValidationContext}. */ public void init() { this.rawType = validationContext.getMetaBean().getBeanClass(); this.type = this.rawType; }
/** * {@inheritDoc} */ @Override protected void complete() { super.complete(); if (validationContext.getMetaProperty() != null) { return; } if (validationContext.getMetaBean() instanceof DynamicMetaBean) { validationContext.setMetaBean(validationContext.getMetaBean().resolveMetaBean( ObjectUtils.defaultIfNull(validationContext.getBean(), rawType))); } } }
/** * Validate a single bean only, no related beans will be validated. */ public static <VL extends ValidationListener> void validateBean(ValidationContext<VL> context) { // execute all property level validations for (MetaProperty prop : context.getMetaBean().getProperties()) { context.setMetaProperty(prop); validateProperty(context); } // execute all bean level validations context.setMetaProperty(null); for (Validation validation : context.getMetaBean().getValidations()) { validation.validate(context); } }
/** * {@inheritDoc} internal validate a bean (=not a collection of beans) and * its related beans */ protected <VL extends ValidationListener> void validateBeanNet(ValidationContext<VL> context) { if (context.collectValidated()) { ValidationHelper.validateBean(context); for (MetaProperty prop : context.getMetaBean().getProperties()) { validateRelatedBean(context, prop); } } }
MetaBean metaBean = context.getMetaBean(); final DynamicMetaBean dyn = getDynamicMetaBean(context); context.setCurrentKey(null);
/** * {@inheritDoc} */ public void handleIndexOrKey(String token) { moveDownIfNecessary(); AccessStrategy access; if (IndexedAccess.getJavaElementType(type) != null) { try { Integer index = token == null ? null : Integer.valueOf(token); access = new IndexedAccess(type, index); validationContext.setCurrentIndex(index); } catch (NumberFormatException e) { throw new UnknownPropertyException(String.format("Cannot parse %s as an array/iterable index", token), e); } } else if (KeyedAccess.getJavaElementType(type) != null) { access = new KeyedAccess(type, token); validationContext.setCurrentKey(token); } else { throw new UnknownPropertyException(String.format("Cannot determine index/key type for %s", type)); } Object value = validationContext.getBean(); Object child = value == null ? null : access.get(value); setType(child == null ? access.getJavaType() : child.getClass()); validationContext.setBean(child, validationContext.getMetaBean().resolveMetaBean(child == null ? rawType : child)); }
/** * If we currently have a property, navigate the context such that the property becomes the bean, in preparation for * another property. * * @param validationContext */ public void moveDownIfNecessary() { MetaProperty mp = validationContext.getMetaProperty(); if (mp != null) { if (mp.getMetaBean() == null) { throw new UnknownPropertyException(String.format("Property %s.%s is not cascaded", mp .getParentMetaBean().getId(), mp.getName())); } validationContext.moveDown(mp, new NullSafePropertyAccess(validationContext.getMetaBean().getBeanClass(), mp.getName())); } }
final MetaBean mbean = context.getMetaBean(); final MetaBean mbean = context.getMetaBean(); for (AccessStrategy each : access) {
DynamicMetaBean dyn = getDynamicMetaBean(context); Object[] array = (Object[]) context.getBean(); MetaBean metaBean = context.getMetaBean(); context.setCurrentIndex(null);
int index = 0; Iterable<?> iterable = (Iterable<?>) context.getBean(); MetaBean metaBean = context.getMetaBean(); context.setCurrentIndex(null);
/** * {@inheritDoc} */ public void handleProperty(String token) { moveDownIfNecessary(); MetaBean metaBean = validationContext.getMetaBean(); if (metaBean instanceof DynamicMetaBean) { metaBean = metaBean.resolveMetaBean(ObjectUtils.defaultIfNull(validationContext.getBean(), rawType)); validationContext.setMetaBean(metaBean); } MetaProperty mp = metaBean.getProperty(token); if (mp == null) { // TODO this could indicate a property hosted on a superclass; should we shunt the context traversal down a path based on that type? PropertyAccess access = new PropertyAccess(rawType, token); if (access.isKnown()) { // add heretofore unknown, but valid, property on the fly: mp = Jsr303MetaBeanFactory.addMetaProperty(metaBean, access); } else { throw new UnknownPropertyException("unknown property '" + token + "' in " + metaBean.getId()); } } validationContext.setMetaProperty(mp); setType(mp.getType()); }