Example usage for org.apache.commons.beanutils ExtendedBeanUtils removeNonModifiableAttributes

List of usage examples for org.apache.commons.beanutils ExtendedBeanUtils removeNonModifiableAttributes

Introduction

In this page you can find the example usage for org.apache.commons.beanutils ExtendedBeanUtils removeNonModifiableAttributes.

Prototype

public static final <M extends Map<String, Pair<Method, Method>>> M removeNonModifiableAttributes(M attrsMap) 

Source Link

Usage

From source file:org.springframework.jdbc.repo.impl.RawPropertiesIdentifiedEntityRepoImpl.java

public RawPropertiesIdentifiedEntityRepoImpl(RawPropertiesRepo<E> rawRepo) {
    super((rawRepo == null) ? null : rawRepo.getEntityClass(), (rawRepo == null) ? null : rawRepo.idsBuilder());

    repo = Validate.notNull(rawRepo, "No raw repository", ArrayUtils.EMPTY_OBJECT_ARRAY);

    Class<E> eClass = rawRepo.getEntityClass();
    Map<String, Pair<Method, Method>> entityAttributes = ExtendedBeanUtils
            .removeNonModifiableAttributes(ExtendedBeanUtils.describeBean(eClass, false, true));
    props2entityTransformer = ExtendedBeanUtils
            .propertiesToBeanTransformer(ExtendedConstructorUtils.newInstanceFactory(eClass), entityAttributes);
    entity2propsTransfomer = ExtendedBeanUtils.beanToPropertiesTransformer(eClass, entityAttributes);
}

From source file:org.springframework.jdbc.repo.impl.RawPropertiesPolymorphicIdentifiedEntityRepoImpl.java

protected Pair<Transformer<Map<String, ?>, E>, Transformer<E, ? extends Map<String, ?>>> getEntityTransformers(
        final Class<?> eClass) {

    /*//ww w  . j a v a2  s.com
     * NOTE: there might be a "race condition" but we don't care since
     * result is the same either way
     */
    Pair<Transformer<Map<String, ?>, E>, Transformer<E, ? extends Map<String, ?>>> pair;
    synchronized (transformersMap) {
        if ((pair = transformersMap.get(eClass)) != null) {
            return pair;
        }
    }

    Map<String, Pair<Method, Method>> entityAttributes = ExtendedBeanUtils
            .removeNonModifiableAttributes(ExtendedBeanUtils.describeBean(eClass, false, true));
    @SuppressWarnings("unchecked")
    final Transformer<Map<String, ?>, E> pureEntityTransformer = ExtendedBeanUtils.propertiesToBeanTransformer(
            ExtendedConstructorUtils.newInstanceFactory((Class<E>) eClass), entityAttributes);
    Transformer<Map<String, ?>, E> props2entityTransformer = new Transformer<Map<String, ?>, E>() {
        @Override
        public E transform(Map<String, ?> beanProps) {
            Object value = beanProps.remove(CLASS_PROP_NAME);
            if (value == null) {
                throw new IllegalStateException(
                        "transform(" + eClass.getSimpleName() + ") missing reserved type property");
            }

            return pureEntityTransformer.transform(beanProps);
        }
    };

    @SuppressWarnings("unchecked")
    final Transformer<E, ? extends Map<String, ?>> purePropsTransformer = ExtendedBeanUtils
            .beanToPropertiesTransformer((Class<E>) eClass, entityAttributes);
    Transformer<E, Map<String, ?>> entity2propsTransfomer = new Transformer<E, Map<String, ?>>() {
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public Map<String, ?> transform(E input) {
            Map<String, ?> beanProps = purePropsTransformer.transform(input);
            if (ExtendedMapUtils.isEmpty(beanProps)) {
                return beanProps;
            }

            Object value = beanProps.get(CLASS_PROP_NAME);
            if (value != null) {
                throw new IllegalStateException(
                        "transform(" + input.getClass().getSimpleName() + ") reserved property used: " + value);
            }
            ((Map) beanProps).put(CLASS_PROP_NAME, eClass);
            return beanProps;
        }
    };

    pair = Pair.<Transformer<Map<String, ?>, E>, Transformer<E, ? extends Map<String, ?>>>of(
            props2entityTransformer, entity2propsTransfomer);
    synchronized (transformersMap) {
        transformersMap.put(eClass, pair);
    }

    logger.info("getEntityTransformers(" + eClass.getSimpleName() + ") created transformers");
    return pair;
}