/** * Cache the specified MetaBean. * @param beanInfo */ public void cache(MetaBean beanInfo) { cacheById.put(beanInfo.getId(), beanInfo); if (beanInfo.getBeanClass() != null && beanInfo.getId().equals(beanInfo.getBeanClass().getName())) { cacheByClass.putIfAbsent(beanInfo.getBeanClass(), beanInfo); } }
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)); } } }
/** * {@inheritDoc} */ @Override public void buildMetaBean(MetaBean meta) throws Exception { if (meta.getBeanClass() == null) { return; // handle only, when local class exists } BeanInfo info = Introspector.getBeanInfo(meta.getBeanClass()); if (meta.getName() == null && info.getBeanDescriptor() != null) { meta.setName(info.getBeanDescriptor().getName()); // (display?)name = simple class name! } for (PropertyDescriptor pd : info.getPropertyDescriptors()) { if (!(pd instanceof IndexedPropertyDescriptor || pd.getName().equals("class"))) { MetaProperty metaProp = buildMetaProperty(pd, meta.getProperty(pd.getName())); meta.putProperty(pd.getName(), metaProp); } } }
/** * Create a new BeanDescriptorImpl instance. * * @param factoryContext * @param metaBean */ protected BeanDescriptorImpl(ApacheFactoryContext factoryContext, MetaBean metaBean) { super(metaBean, metaBean.getBeanClass(), metaBean.getValidations()); this.factoryContext = factoryContext; }
/** * Build a MetaBean for the specified class. * * @param clazz * @return MetaBean * @throws Exception */ public MetaBean buildForClass(Class<?> clazz) throws Exception { MetaBean meta = new MetaBean(); if (clazz != null) { // local class here? meta.setBeanClass(clazz); meta.setId(clazz.getName()); // default id = full class name! } for (MetaBeanFactory factory : factories) { factory.buildMetaBean(meta); } return meta; }
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; }
/** * {@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()); }
/** * Create a new ConstructorDescriptorImpl instance. * @param metaBean * @param validations */ protected ConstructorDescriptorImpl(MetaBean metaBean, Validation[] validations) { super(metaBean, metaBean.getBeanClass(), validations); }
ClassHelper.fillFullClassHierarchyAsList(classHierarchy, context.getMetaBean().getBeanClass()); Class<?> initialOwner = context.getCurrentOwner(); context.getMetaBean().getFeature("{GroupSequence:" + owner.getCanonicalName() + "}"); for (Group each : ownerDefaultGroups) { context.setCurrentGroup(each); for (MetaProperty prop : context.getMetaBean().getProperties()) { validateCascadedBean(context, prop);
/** * 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); } }
@Override public void buildMetaBean(final MetaBean metaBean) throws Exception { if (metaBean.getId() == null) return; visitXMLBeanMeta(metaBean.getId(), new Visitor() { @Override public void visit(XMLMetaBean xmlMeta, XMLMetaBeanInfos xmlInfos) throws Exception { enrichMetaBean(metaBean, new XMLResult(xmlMeta, xmlInfos)); } @Override public MetaBean getMetaBean() { return metaBean; } }); }
public void enrichMetaBean(MetaBean meta, XMLResult result) throws Exception { if (result.xmlMeta.getId() != null) { meta.setId(result.xmlMeta.getId()); } if (result.xmlMeta.getName() != null) { meta.setName(result.xmlMeta.getName()); } /* if (meta.getBeanClass() == null && result.xmlMeta.getImpl() != null) { meta.setBeanClass(findLocalClass(result.xmlMeta.getImpl())); }*/ result.xmlMeta.mergeFeaturesInto(meta); enrichValidations(meta, result.xmlMeta, result, false); if (result.xmlMeta.getProperties() != null) { for (XMLMetaProperty xmlProp : result.xmlMeta.getProperties()) { enrichElement(meta, xmlProp, result); } } if (result.xmlMeta.getBeanRefs() != null) { for (XMLMetaBeanReference xmlRef : result.xmlMeta.getBeanRefs()) { enrichElement(meta, xmlRef, result); } } }
/** * 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); }
/** * 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; }
initialMetaBean.setBeanClass(beanType); GroupValidationContext<T> context = createContext(initialMetaBean, object, beanType, groups); ValidationContextTraversal contextTraversal = createValidationContextTraversal(context); contextTraversal.moveDownIfNecessary(); if (context.getMetaBean() instanceof DynamicMetaBean) { context.setMetaBean(context.getMetaBean().resolveMetaBean( ObjectUtils.defaultIfNull(context.getBean(), contextTraversal.getRawType()))); contextTraversal.moveDownIfNecessary(); if (context.getMetaBean() instanceof DynamicMetaBean) { context.setMetaBean(context.getMetaBean().resolveMetaBean( ObjectUtils.defaultIfNull(context.getBean(), contextTraversal.getRawType())));
metaProperty = null; } else { // property level metaProperty = metabean.getProperty(access.getPropertyName()); create = metaProperty == null; if (create) { meta.getAccessStrategy(), new AppendValidationToMeta(metaProperty == null ? metabean : metaProperty)) && create) { metabean.putProperty(access.getPropertyName(), null); MetaProperty metaProperty = metabean.getProperty(access.getPropertyName()); boolean create = metaProperty == null; if (create) { metabean.putProperty(access.getPropertyName(), null);
/** * Initialize from {@link ValidationContext}. */ public void init() { this.rawType = validationContext.getMetaBean().getBeanClass(); this.type = this.rawType; }
Map<String, MetaBean> allBuilt = builder.buildAll(); for (MetaBean meta : allBuilt.values()) { MetaBean cached = cache.findForId(meta.getId()); if (cached == null) { cache.cache(meta);
MetaProperty metaProperty = metabean.getProperty(field.getName()); metabean.putProperty(metaProperty.getName(), null); if (!factoryContext.getFactory().getAnnotationIgnores().isIgnoreAnnotations(method)) { AccessStrategy access = new MethodAccess(propName, method); MetaProperty metaProperty = metabean.getProperty(propName); boolean create = metaProperty == null; metabean.putProperty(propName, null);
/** * Remove a single MetaBean from the cache. * @param beanInfo */ public void removeFromCache(MetaBean beanInfo) { cacheById.remove(beanInfo.getId()); if (beanInfo.getBeanClass() != null && beanInfo.getId().equals(beanInfo.getBeanClass().getName())) { cacheByClass.remove(beanInfo.getBeanClass()); } }