/** * Create a {@link MetaProperty} from the specified {@link PropertyDescriptor}. * * @param pd * @param existing * @return MetaProperty */ protected MetaProperty buildMetaProperty(PropertyDescriptor pd, MetaProperty existing) { MetaProperty meta = new MetaProperty(); meta.setName(pd.getName()); meta.setType(determineGenericPropertyType(pd)); if (pd.isHidden()) { meta.putFeature(HIDDEN, Boolean.TRUE); } if (pd.isPreferred()) { meta.putFeature(PREFERRED, Boolean.TRUE); } if (pd.isConstrained()) { meta.putFeature(READONLY, Boolean.TRUE); } Enumeration<String> enumeration = pd.attributeNames(); while (enumeration.hasMoreElements()) { String key = enumeration.nextElement(); Object value = pd.getValue(key); meta.putFeature(key, value); } return meta; }
/** * Create a new PropertyDescriptorImpl instance. * * @param property */ PropertyDescriptorImpl(MetaProperty property) { super(property.getParentMetaBean(), property.getTypeClass(), property.getValidations()); setCascaded(property.getMetaBean() != null || property.getFeature(Features.Property.REF_CASCADE) != null); setPropertyPath(property.getName()); }
protected void computeRelationships(MetaBean beanInfo, Map<String, MetaBean> cached) { for (MetaProperty prop : beanInfo.getProperties()) { String beanRef = (String) prop.getFeature(REF_BEAN_ID); if (beanRef != null) { prop.setMetaBean(cached.get(beanRef)); } } }
private PropertyDescriptor getPropertyDescriptor(MetaProperty prop) { PropertyDescriptorImpl edesc = prop.getFeature(Jsr303Features.Property.PropertyDescriptor); if (edesc == null) { edesc = new PropertyDescriptorImpl(prop); prop.putFeature(Jsr303Features.Property.PropertyDescriptor, edesc); } return edesc; }
/** * Add a {@link MetaProperty} to a {@link MetaBean}. * @param parentMetaBean * @param access * @return the created {@link MetaProperty} */ public static MetaProperty addMetaProperty(MetaBean parentMetaBean, AccessStrategy access) { final MetaProperty result = new MetaProperty(); final String name = access.getPropertyName(); result.setName(name); result.setType(access.getJavaType()); parentMetaBean.putProperty(name, result); return result; }
/** * {@inheritDoc} * * @return the property descriptors having at least a constraint defined */ public Set<PropertyDescriptor> getConstrainedProperties() { Set<PropertyDescriptor> validatedProperties = new HashSet<PropertyDescriptor>(); for (MetaProperty prop : metaBean.getProperties()) { if (prop.getValidations().length > 0 || (prop.getMetaBean() != null || prop.getFeature(Features.Property.REF_CASCADE) != null)) { validatedProperties.add(getPropertyDescriptor(prop)); } } return Collections.unmodifiableSet(validatedProperties); }
/** * Learn whether this property is considered mandatory. * @return <code>true</code> if the <code>MANDATORY</code> feature is set to <code>true</code>. * @see {@link Features.Property#MANDATORY} */ public boolean isMandatory() { return getFeature(MANDATORY, Boolean.FALSE).booleanValue(); }
AccessStrategy[] access = prop.getFeature(Features.Property.REF_CASCADE); if (access == null && prop.getMetaBean() != null) { // single property access strategy context.moveDown(prop, PropertyAccess.getInstance(bean.getClass(), prop.getName())); ValidationHelper.validateContext(context, new BeanValidatorCallback(context), treatMapsLikeBeans);
/** * 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())); } }
protected MetaProperty enrichElement(MetaBean meta, XMLMetaElement xmlProp, XMLResult result) throws Exception { MetaProperty prop = meta.getProperty(xmlProp.getName()); if (prop == null) { prop = new MetaProperty(); prop.setName(xmlProp.getName()); meta.putProperty(xmlProp.getName(), prop); } xmlProp.mergeInto(prop); enrichValidations(prop, xmlProp, result, true); return prop; }
public void mergeInto(MetaProperty prop) throws ClassNotFoundException { mergeFeaturesInto(prop); if (getType() != null && getType().length() > 0) { prop.setType(Reflection.toClass(getType())); // enhancement: or use getGenericType() ? } if (getHidden() != null) { prop.putFeature(HIDDEN, getHidden().booleanValue()); } if (getMandatory() != null) { prop.putFeature(MANDATORY, getMandatory().equals("true")); } if (getMaxLength() != null) { prop.putFeature(MAX_LENGTH, getMaxLength()); } if (getMinLength() != null) { prop.putFeature(MIN_LENGTH, getMinLength()); } if (getReadonly() != null) { prop.putFeature(READONLY, getReadonly()); } if (getDenied() != null) { prop.putFeature(DENIED, getDenied()); } } }
/** * Returns true if the bean involves validation: * <ul> * <li>a constraint is hosted on the bean itself</li> * <li>a constraint is hosted on one of the bean properties, OR</li> * <li>a bean property is marked for cascade (<code>@Valid</code>)</li> * </ul> * * @return true if the bean involves validation */ public boolean isBeanConstrained() { if (hasAnyConstraints()) return true; for (MetaProperty mprop : metaBean.getProperties()) { if (mprop.getMetaBean() != null || mprop.getFeature(Features.Property.REF_CASCADE) != null) return true; } return false; }
/** * Return the property level constraints for a given propertyName or {@code null} if * either the property does not exist or has no constraint. The returned * object (and associated objects including ConstraintDescriptors) are * immutable. * * @param propertyName * property evaluated */ public PropertyDescriptor getConstraintsForProperty(String propertyName) { if (propertyName == null || propertyName.trim().length() == 0) { throw new IllegalArgumentException("propertyName cannot be null or empty"); } MetaProperty prop = metaBean.getProperty(propertyName); if (prop == null) return null; // If no constraints and not cascaded, return null if (prop.getValidations().length == 0 && prop.getFeature(Features.Property.REF_CASCADE) == null) { return null; } return getPropertyDescriptor(prop); }
/** * Convenience method to access metaProperty.name * * @return null or the name of the current property */ @Override public String getPropertyName() { return metaProperty == null ? null : metaProperty.getName(); }
/** * Set this property as being mandatory (or not). * @param mandatory * @see {@link Features.Property#MANDATORY} */ public void setMandatory(boolean mandatory) { putFeature(MANDATORY, Boolean.valueOf(mandatory)); }
boolean doCascade = cascade && (prop == null || prop.getMetaBean() != null); context.moveUp(bean, prop.getParentMetaBean()); context.setMetaProperty(prop); if (fixed) { context.moveUp(bean, prop.getParentMetaBean()); context.setMetaProperty(prop); if (fixed) {
/** * Validate a single property only. Performs all validations * for this property. */ public static <VL extends ValidationListener> void validateProperty(ValidationContext<VL> context) { for (Validation validation : context.getMetaProperty().getValidations()) { validation.validate(context); } } }
/** * Resolve the type of this property to a class. * @return Class, <code>null</code> if cannot be determined */ public Class<?> getTypeClass() { Type targetType = type instanceof DynaType ? ((DynaType) type).getRawType() : type; if (targetType == null) { return null; } Type assigningType = getParentMetaBean() == null ? null : getParentMetaBean().getBeanClass(); return TypeUtils.getRawType(targetType, assigningType); }
@Override protected void computeRelatedMetaBean(MetaProperty prop, String beanRef) { if (beanRef != null) { prop.setMetaBean(findForId(beanRef)); } else { super.computeRelatedMetaBean(prop, beanRef); } } }
/** * {@inheritDoc} */ @Override public void moveDown(MetaProperty prop, AccessStrategy access) { setBean(getPropertyValue(access), prop.getMetaBean()); }