All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.juffrou.reflect.DefaultBeanWrapperFactory Maven / Gradle / Ivy

Go to download

Performant java bean access through property names. If you are serious about bean handling, this is for you.

There is a newer version: 2.1.9
Show newest version
package net.sf.juffrou.reflect;

import java.lang.ref.WeakReference;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

import net.sf.juffrou.reflect.internal.DefaultBeanInstanceCreator;

/**
 * This class is responsible for creating and caching BeanWrapperContexts.
 * 

* A BeanWrapper uses the same BeanWrapperFactory to create the BeanWrapperContexts for its nested BeanWrappers. * BeanWrapperFactory can also be used as an umbrella to create BeanWrappers in a more performant way as it caches the * introspection information and re-uses it whenever possible. * * @author cemartins */ public class DefaultBeanWrapperFactory implements BeanWrapperFactory { static final Map> classContextMap = Collections.synchronizedMap(new WeakHashMap>()); // preferences info private final BeanInstanceBuilder beanInstanceCreator = new DefaultBeanInstanceCreator(); /** * Retrieves a BeanWrapperContext for java bean class. *

* If the BeanWrapperContext is not in cache then creates a new one. * * @param clazz * the bean class to inspect * @return a BeanWrapperContext with introspection information about the specified class. * @see {@link #getBeanWrapperContext(Class, Type...)} */ @Override public BeanWrapperContext getBeanWrapperContext(Class clazz) { return getBeanWrapperContext(clazz, null); } /** * Retrieves a BeanWrapperContext for a parameterized (generic) java bean class. *

* If the BeanWrapperContext is not in cache then creates a new one. * * @param clazz * the generic bean class to inspect. * @param types * the parameters that defined the generic bean class. * @return a BeanWrapperContext with introspection information about the specified class. */ @Override public BeanWrapperContext getBeanWrapperContext(Class clazz, Type... types) { WeakReference contextReference = classContextMap.get(clazz); BeanWrapperContext context = contextReference != null ? contextReference.get() : null; if (context == null) { context = new BeanWrapperContext(this, clazz, types); classContextMap.put(clazz, new WeakReference(context)); } return context; } /** * Construct a bean wrapper around a class. *

* Bean instances will be instances of that class and will be created only when necessary.
* Try to use a cached BeanWrapperContext to save introspection time. * * @param clazz * class to instantiate the wrapped bean */ @Override public JuffrouBeanWrapper getBeanWrapper(Class clazz) { return new JuffrouBeanWrapper(getBeanWrapperContext(clazz)); } /** * Construct a bean wrapper around an existing bean instance. *

* Will try to use a cached BeanWrapperContext to save introspection time. * * @param instance * the bean object to be wrapped */ @Override public JuffrouBeanWrapper getBeanWrapper(Object instance) { return new JuffrouBeanWrapper(getBeanWrapperContext(instance.getClass(), null), instance); } @Override public BeanInstanceBuilder getBeanInstanceBuilder() { return beanInstanceCreator; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy