/** * {@inheritDoc} */ @Override public Object get(Object instance) { if (instance instanceof Map<?, ?>) { Map<?, ?> map = (Map<?, ?>) instance; Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(containerType, Map.class); Type keyType = TypeUtils.unrollVariables(typeArguments, MAP_TYPEVARS[0]); if (key == null || keyType == null || TypeUtils.isInstance(key, keyType)) { return map.get(key); } if (key instanceof String) { String name = (String) key; Class<?> rawKeyType = TypeUtils.getRawType(keyType, containerType); if (rawKeyType.isEnum()) { @SuppressWarnings({ "unchecked", "rawtypes" }) final Object result = map.get(Enum.valueOf((Class<? extends Enum>) rawKeyType, name)); return result; } for (Map.Entry<?, ?> e : map.entrySet()) { if (name.equals(e.getKey())) { return e.getValue(); } } } } return null; }
ValidateParameters(ApacheFactoryContext validatorContext, T object, E executable, Object[] parameterValues, Class<?>[] groups, Meta<E> meta) { super(validatorContext, groups, meta); this.object = object; this.parameterValues = Validate.notNull(parameterValues, IllegalArgumentException::new, "null parameter values").clone(); final Type[] genericParameterTypes = executable.getGenericParameterTypes(); Exceptions.raiseUnless(parameterValues.length == genericParameterTypes.length, IllegalArgumentException::new, PARAMETERS_DO_NOT_MATCH); IntStream.range(0, genericParameterTypes.length) .forEach(n -> Exceptions.raiseUnless(TypeUtils.isInstance(parameterValues[n], genericParameterTypes[n]), IllegalArgumentException::new, PARAMETERS_DO_NOT_MATCH)); }
ValidateReturnValue(ApacheFactoryContext validatorContext, Meta<E> meta, Object returnValue, Class<?>[] groups) { super(validatorContext, groups, meta); final Type type = Validate.notNull(meta, IllegalArgumentException::new, "meta").getType(); if (!TypeUtils.isInstance(returnValue, type)) { Exceptions.raise(IllegalArgumentException::new, "%s is not an instance of %s", returnValue, type); } this.returnValue = returnValue; }
ValidateReturnValue(ApacheFactoryContext validatorContext, Meta<E> meta, Object returnValue, Class<?>[] groups) { super(validatorContext, groups, meta); final Type type = Validate.notNull(meta, IllegalArgumentException::new, "meta").getType(); if (!TypeUtils.isInstance(returnValue, type)) { Exceptions.raise(IllegalArgumentException::new, "%s is not an instance of %s", returnValue, type); } this.returnValue = returnValue; }
ValidateParameters(ApacheFactoryContext validatorContext, T object, E executable, Object[] parameterValues, Class<?>[] groups, Meta<E> meta) { super(validatorContext, groups, meta); this.object = object; this.parameterValues = Validate.notNull(parameterValues, IllegalArgumentException::new, "null parameter values").clone(); final Type[] genericParameterTypes = executable.getGenericParameterTypes(); Exceptions.raiseUnless(parameterValues.length == genericParameterTypes.length, IllegalArgumentException::new, PARAMETERS_DO_NOT_MATCH); IntStream.range(0, genericParameterTypes.length) .forEach(n -> Exceptions.raiseUnless(TypeUtils.isInstance(parameterValues[n], genericParameterTypes[n]), IllegalArgumentException::new, PARAMETERS_DO_NOT_MATCH)); }
/** * Get the value of <code>this.attributeName</code> from <code>map</code>. * * @param <V> * @param map * @return V if you say so */ public <V> V get(Map<? super String, ? super V> map) { @SuppressWarnings("unchecked") final V result = (V) map.get(getAttributeName()); if (!TypeUtils.isInstance(result, getType())) { Exceptions.raise(IllegalStateException::new, "Invalid '%s' value: %s", getAttributeName(), result); } return result; }
/** * Get the value of <code>this.attributeName</code> from <code>map</code>. * * @param <V> * @param map * @return V if you say so */ public <V> V get(Map<? super String, ? super V> map) { @SuppressWarnings("unchecked") final V result = (V) map.get(getAttributeName()); if (!TypeUtils.isInstance(result, getType())) { Exceptions.raise(IllegalStateException::new, "Invalid '%s' value: %s", getAttributeName(), result); } return result; }
ValidateProperty(ApacheFactoryContext validatorContext, Class<T> rootBeanClass, String property, Object value, Class<?>[] groups) { this(new ForPropertyValue<>(validatorContext, rootBeanClass, value), validatorContext, rootBeanClass, property, groups); if (descriptor == null) { // should only occur when the root class is raw final Class<?> t; if (value == null) { t = Object.class; } else { t = value.getClass(); } descriptor = (ElementD<?, ?>) validatorContext.getDescriptorManager().getBeanDescriptor(t); } else if (hasWork()) { final Class<?> propertyType = descriptor.getElementClass(); if (!TypeUtils.isInstance(value, propertyType)) { Exceptions.raise(IllegalArgumentException::new, "%s is not an instance of %s", value, propertyType); } } }
ValidateProperty(ApacheFactoryContext validatorContext, Class<T> rootBeanClass, String property, Object value, Class<?>[] groups) { this(new ForPropertyValue<>(validatorContext, rootBeanClass, value), validatorContext, rootBeanClass, property, groups); if (descriptor == null) { // should only occur when the root class is raw final Class<?> t; if (value == null) { t = Object.class; } else { t = value.getClass(); } descriptor = (ElementD<?, ?>) validatorContext.getDescriptorManager().getBeanDescriptor(t); } else if (hasWork()) { final Class<?> propertyType = descriptor.getElementClass(); if (!TypeUtils.isInstance(value, propertyType)) { Exceptions.raise(IllegalArgumentException::new, "%s is not an instance of %s", value, propertyType); } } }