/** * Create a new ConstructorDescriptorImpl instance. * @param metaBean * @param validations */ protected ConstructorDescriptorImpl(MetaBean metaBean, Validation[] validations) { super(metaBean, metaBean.getBeanClass(), validations); }
/** * 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); } }
/** * 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()); } }
/** * Initialize from {@link ValidationContext}. */ public void init() { this.rawType = validationContext.getMetaBean().getBeanClass(); this.type = this.rawType; }
/** * Create a new BeanDescriptorImpl instance. * * @param factoryContext * @param metaBean */ protected BeanDescriptorImpl(ApacheFactoryContext factoryContext, MetaBean metaBean) { super(metaBean, metaBean.getBeanClass(), metaBean.getValidations()); this.factoryContext = factoryContext; }
/** * {@inheritDoc} */ public ElementDescriptor.ConstraintFinder lookingAt(Scope scope) { if (scope.equals(Scope.LOCAL_ELEMENT)) { findInScopes.remove(Scope.HIERARCHY); for (Iterator<ConstraintValidation<?>> it = constraintDescriptors.iterator(); it.hasNext();) { ConstraintValidation<?> cv = it.next(); if (cv.getOwner() != metaBean.getBeanClass()) { it.remove(); } } } return this; }
private boolean isInScope(ConstraintValidation<?> descriptor) { if (findInScopes.size() == Scope.values().length) return true; // all scopes if (metaBean != null) { Class<?> owner = descriptor.getOwner(); for (Scope scope : findInScopes) { switch (scope) { case LOCAL_ELEMENT: if (owner.equals(metaBean.getBeanClass())) return true; break; case HIERARCHY: if (!owner.equals(metaBean.getBeanClass())) return true; break; } } } return false; }
/** * {@inheritDoc} Add the validation features to the metabean that come from JSR303 annotations in the beanClass. */ public void buildMetaBean(MetaBean metabean) { try { final Class<?> beanClass = metabean.getBeanClass(); processGroupSequence(beanClass, metabean); // process class, superclasses and interfaces List<Class<?>> classSequence = new ArrayList<Class<?>>(); ClassHelper.fillFullClassHierarchyAsList(classSequence, beanClass); // start with superclasses and go down the hierarchy so that // the child classes are processed last to have the chance to // overwrite some declarations // of their superclasses and that they see what they inherit at the // time of processing for (int i = classSequence.size() - 1; i >= 0; i--) { Class<?> eachClass = classSequence.get(i); processClass(eachClass, metabean); processGroupSequence(eachClass, metabean, "{GroupSequence:" + eachClass.getCanonicalName() + "}"); } } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } catch (InvocationTargetException e) { throw new IllegalArgumentException(e.getTargetException()); } }
/** * {@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); } } }
/** * 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); }
private void buildConstructorConstraints(MethodBeanDescriptorImpl beanDesc) throws InvocationTargetException, IllegalAccessException { beanDesc.setConstructorConstraints(new HashMap<Constructor<?>, ConstructorDescriptor>()); for (Constructor<?> cons : beanDesc.getMetaBean().getBeanClass().getDeclaredConstructors()) { if (!factoryContext.getFactory().getAnnotationIgnores().isIgnoreAnnotations(cons)) { ConstructorDescriptorImpl consDesc = new ConstructorDescriptorImpl(beanDesc.getMetaBean(), new Validation[0]); beanDesc.putConstructorDescriptor(cons, consDesc); Annotation[][] paramsAnnos = cons.getParameterAnnotations(); int idx = 0; for (Annotation[] paramAnnos : paramsAnnos) { ParameterAccess access = new ParameterAccess(cons.getParameterTypes()[idx], idx); processAnnotations(consDesc, paramAnnos, access, idx); idx++; } } } }
private void buildMethodConstraints(MethodBeanDescriptorImpl beanDesc) throws InvocationTargetException, IllegalAccessException { beanDesc.setMethodConstraints(new HashMap<Method, MethodDescriptor>()); for (Method method : beanDesc.getMetaBean().getBeanClass().getDeclaredMethods()) { if (!factoryContext.getFactory().getAnnotationIgnores().isIgnoreAnnotations(method)) { MethodDescriptorImpl methodDesc = new MethodDescriptorImpl(beanDesc.getMetaBean(), new Validation[0]); beanDesc.putMethodDescriptor(method, methodDesc); // return value validations AppendValidationToList validations = new AppendValidationToList(); ReturnAccess returnAccess = new ReturnAccess(method.getReturnType()); for (Annotation anno : method.getAnnotations()) { if (anno instanceof Valid || anno instanceof Validate) { methodDesc.setCascaded(true); } else { processAnnotation(anno, methodDesc, returnAccess, validations); } } methodDesc.addValidations(validations.getValidations()); // parameter validations Annotation[][] paramsAnnos = method.getParameterAnnotations(); int idx = 0; for (Annotation[] paramAnnos : paramsAnnos) { ParameterAccess access = new ParameterAccess(method.getParameterTypes()[idx], idx); processAnnotations(methodDesc, paramAnnos, access, idx); idx++; } } } }
/** * 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())); } }
private boolean isReachable(GroupValidationContext<?> context) { PathImpl path = context.getPropertyPath(); NodeImpl node = path.getLeafNode(); PathImpl beanPath = path.getPathWithoutLeafNode(); if (beanPath == null) { beanPath = PathImpl.create(null); } try { if (!context.getTraversableResolver().isReachable(context.getBean(), node, context.getRootMetaBean().getBeanClass(), beanPath, access.getElementType())) return false; } catch (RuntimeException e) { throw new ValidationException("Error in TraversableResolver.isReachable() for " + context.getBean(), e); } return true; }
context.getRootMetaBean().getBeanClass(), beanPath, access.getElementType())) return false; } catch (RuntimeException e) { context.getRootMetaBean().getBeanClass(), beanPath, access.getElementType())) return false; } catch (RuntimeException e) {
&& parentMetaBean.getBeanClass() != builder.getConstraintValidation().getOwner() && builder.getConstraintValidation().getGroups().size() == 1 && builder.getConstraintValidation().getGroups().contains(Default.class)) {
ClassHelper.fillFullClassHierarchyAsList(classHierarchy, context.getMetaBean().getBeanClass()); Class<?> initialOwner = context.getCurrentOwner();