Example usage for org.springframework.util ClassUtils isCacheSafe

List of usage examples for org.springframework.util ClassUtils isCacheSafe

Introduction

In this page you can find the example usage for org.springframework.util ClassUtils isCacheSafe.

Prototype

public static boolean isCacheSafe(Class<?> clazz, @Nullable ClassLoader classLoader) 

Source Link

Document

Check whether the given class is cache-safe in the given context, i.e.

Usage

From source file:net.yasion.common.core.bean.wrapper.CachedIntrospectionResults.java

/**
 * Create CachedIntrospectionResults for the given bean class.
 * //from  w w  w .  ja  va2s  . com
 * @param beanClass
 *            the bean class to analyze
 * @return the corresponding CachedIntrospectionResults
 * @throws BeansException
 *             in case of introspection failure
 */
public static CachedIntrospectionResults forClass(Class<?> beanClass) throws BeansException {
    CachedIntrospectionResults results = strongClassCache.get(beanClass);
    if (results != null) {
        return results;
    }
    results = softClassCache.get(beanClass);
    if (results != null) {
        return results;
    }

    results = new CachedIntrospectionResults(beanClass);
    ConcurrentMap<Class<?>, CachedIntrospectionResults> classCacheToUse;

    if (ClassUtils.isCacheSafe(beanClass, CachedIntrospectionResults.class.getClassLoader())
            || isClassLoaderAccepted(beanClass.getClassLoader())) {
        classCacheToUse = strongClassCache;
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug(
                    "Not strongly caching class [" + beanClass.getName() + "] because it is not cache-safe");
        }
        classCacheToUse = softClassCache;
    }

    CachedIntrospectionResults existing = classCacheToUse.putIfAbsent(beanClass, results);
    return (existing != null ? existing : results);
}

From source file:org.grails.beans.support.CachedIntrospectionResults.java

/**
 * Create CachedIntrospectionResults for the given bean class.
 * @param beanClass the bean class to analyze
 * @return the corresponding CachedIntrospectionResults
 * @throws org.springframework.beans.BeansException in case of introspection failure
 *//*w  ww  .j  ava 2s  . co  m*/
@SuppressWarnings("unchecked")
public static CachedIntrospectionResults forClass(Class<?> beanClass) throws BeansException {
    CachedIntrospectionResults results = strongClassCache.get(beanClass);
    if (results != null) {
        return results;
    }
    results = softClassCache.get(beanClass);
    if (results != null) {
        return results;
    }

    results = new CachedIntrospectionResults(beanClass);
    ConcurrentMap<Class<?>, CachedIntrospectionResults> classCacheToUse;

    if (ClassUtils.isCacheSafe(beanClass, CachedIntrospectionResults.class.getClassLoader())
            || isClassLoaderAccepted(beanClass.getClassLoader())) {
        classCacheToUse = strongClassCache;
    } else {
        classCacheToUse = softClassCache;
    }

    CachedIntrospectionResults existing = classCacheToUse.putIfAbsent(beanClass, results);
    return (existing != null ? existing : results);
}

From source file:org.hopen.framework.rewrite.CachedIntrospectionResults.java

/**
 * Create CachedIntrospectionResults for the given bean class.
 * <P>We don't want to use synchronization here. Object references are atomic,
 * so we can live with doing the occasional unnecessary lookup at startup only.
 * @param beanClass the bean class to analyze
 * @return the corresponding CachedIntrospectionResults
 * @throws BeansException in case of introspection failure
 *///from  w w  w .  j a v  a  2 s. c o m
static CachedIntrospectionResults forClass(Class beanClass) throws BeansException {
    CachedIntrospectionResults results;
    Object value = classCache.get(beanClass);
    if (value instanceof Reference) {
        Reference ref = (Reference) value;
        results = (CachedIntrospectionResults) ref.get();
    } else {
        results = (CachedIntrospectionResults) value;
    }
    if (results == null) {
        // On JDK 1.5 and higher, it is almost always safe to cache the bean class...
        // The sole exception is a custom BeanInfo class being provided in a non-safe ClassLoader.
        boolean fullyCacheable = ClassUtils.isCacheSafe(beanClass,
                CachedIntrospectionResults.class.getClassLoader())
                || isClassLoaderAccepted(beanClass.getClassLoader());
        if (fullyCacheable
                || !ClassUtils.isPresent(beanClass.getName() + "BeanInfo", beanClass.getClassLoader())) {
            results = new CachedIntrospectionResults(beanClass, fullyCacheable);
            classCache.put(beanClass, results);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("Not strongly caching class [" + beanClass.getName()
                        + "] because it is not cache-safe");
            }
            results = new CachedIntrospectionResults(beanClass, true);
            classCache.put(beanClass, new WeakReference<CachedIntrospectionResults>(results));
        }
    }
    return results;
}

From source file:org.springframework.beans.CachedIntrospectionResults.java

/**
 * Create CachedIntrospectionResults for the given bean class.
 * @param beanClass the bean class to analyze
 * @return the corresponding CachedIntrospectionResults
 * @throws BeansException in case of introspection failure
 *//*from  www.  ja  v  a2 s .c  o  m*/
@SuppressWarnings("unchecked")
static CachedIntrospectionResults forClass(Class<?> beanClass) throws BeansException {
    CachedIntrospectionResults results = strongClassCache.get(beanClass);
    if (results != null) {
        return results;
    }
    results = softClassCache.get(beanClass);
    if (results != null) {
        return results;
    }

    results = new CachedIntrospectionResults(beanClass);
    ConcurrentMap<Class<?>, CachedIntrospectionResults> classCacheToUse;

    if (ClassUtils.isCacheSafe(beanClass, CachedIntrospectionResults.class.getClassLoader())
            || isClassLoaderAccepted(beanClass.getClassLoader())) {
        classCacheToUse = strongClassCache;
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug(
                    "Not strongly caching class [" + beanClass.getName() + "] because it is not cache-safe");
        }
        classCacheToUse = softClassCache;
    }

    CachedIntrospectionResults existing = classCacheToUse.putIfAbsent(beanClass, results);
    return (existing != null ? existing : results);
}

From source file:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.java

@Override
@SuppressWarnings("unchecked")
public <T> T createBean(Class<T> beanClass) throws BeansException {
    // Use prototype bean definition, to avoid registering bean as dependent bean.
    RootBeanDefinition bd = new RootBeanDefinition(beanClass);
    bd.setScope(SCOPE_PROTOTYPE);/*from ww w .  j a v a  2  s  .  c  o  m*/
    bd.allowCaching = ClassUtils.isCacheSafe(beanClass, getBeanClassLoader());
    return (T) createBean(beanClass.getName(), bd, null);
}

From source file:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.java

@Override
public void autowireBean(Object existingBean) {
    // Use non-singleton bean definition, to avoid registering bean as dependent bean.
    RootBeanDefinition bd = new RootBeanDefinition(ClassUtils.getUserClass(existingBean));
    bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bd.allowCaching = ClassUtils.isCacheSafe(bd.getBeanClass(), getBeanClassLoader());
    BeanWrapper bw = new BeanWrapperImpl(existingBean);
    initBeanWrapper(bw);//from w ww. j  av a  2 s  . c o m
    populateBean(bd.getBeanClass().getName(), bd, bw);
}

From source file:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.java

@Override
public Object configureBean(Object existingBean, String beanName) throws BeansException {
    markBeanAsCreated(beanName);//from  w  w w  .  ja va2s  . c om
    BeanDefinition mbd = getMergedBeanDefinition(beanName);
    RootBeanDefinition bd = null;
    if (mbd instanceof RootBeanDefinition) {
        RootBeanDefinition rbd = (RootBeanDefinition) mbd;
        bd = (rbd.isPrototype() ? rbd : rbd.cloneBeanDefinition());
    }
    if (bd == null) {
        bd = new RootBeanDefinition(mbd);
    }
    if (!bd.isPrototype()) {
        bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
        bd.allowCaching = ClassUtils.isCacheSafe(ClassUtils.getUserClass(existingBean), getBeanClassLoader());
    }
    BeanWrapper bw = new BeanWrapperImpl(existingBean);
    initBeanWrapper(bw);
    populateBean(beanName, bd, bw);
    return initializeBean(beanName, existingBean, bd);
}

From source file:org.tinygroup.beanwrapper.CachedIntrospectionResults.java

/**
 * Create CachedIntrospectionResults for the given bean class.
 * <P>We don't want to use synchronization here. Object references are atomic,
 * so we can live with doing the occasional unnecessary lookup at startup only.
 * @param beanClass the bean class to analyze
 * @return the corresponding CachedIntrospectionResults
 * @throws BeansException in case of introspection failure
 *//*from ww w.j  a  va 2  s .  com*/
static CachedIntrospectionResults forClass(Class beanClass) throws BeansException {
    CachedIntrospectionResults results = null;
    Object value = classCache.get(beanClass);
    if (value instanceof Reference) {
        Reference ref = (Reference) value;
        results = (CachedIntrospectionResults) ref.get();
    } else {
        results = (CachedIntrospectionResults) value;
    }
    if (results == null) {
        // can throw BeansException
        results = new CachedIntrospectionResults(beanClass);
        if (ClassUtils.isCacheSafe(beanClass, CachedIntrospectionResults.class.getClassLoader())
                || isClassLoaderAccepted(beanClass.getClassLoader())) {
            classCache.put(beanClass, results);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("Not strongly caching class [" + beanClass.getName()
                        + "] because it is not cache-safe");
            }
            classCache.put(beanClass, new WeakReference(results));
        }
    }
    return results;
}