Search in sources :

Example 1 with Factory

use of org.springframework.cglib.proxy.Factory in project spring-framework by spring-projects.

the class ResolvableMethod method initProxy.

@SuppressWarnings("unchecked")
private static <T> T initProxy(Class<?> type, MethodInvocationInterceptor interceptor) {
    Assert.notNull(type, "'type' must not be null");
    if (type.isInterface()) {
        ProxyFactory factory = new ProxyFactory(EmptyTargetSource.INSTANCE);
        factory.addInterface(type);
        factory.addInterface(Supplier.class);
        factory.addAdvice(interceptor);
        return (T) factory.getProxy();
    } else {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(type);
        enhancer.setInterfaces(new Class<?>[] { Supplier.class });
        enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
        enhancer.setCallbackType(org.springframework.cglib.proxy.MethodInterceptor.class);
        Class<?> proxyClass = enhancer.createClass();
        Object proxy = null;
        if (objenesis.isWorthTrying()) {
            try {
                proxy = objenesis.newInstance(proxyClass, enhancer.getUseCache());
            } catch (ObjenesisException ex) {
                logger.debug("Objenesis failed, falling back to default constructor", ex);
            }
        }
        if (proxy == null) {
            try {
                proxy = ReflectionUtils.accessibleConstructor(proxyClass).newInstance();
            } catch (Throwable ex) {
                throw new IllegalStateException("Unable to instantiate proxy " + "via both Objenesis and default constructor fails as well", ex);
            }
        }
        ((Factory) proxy).setCallbacks(new Callback[] { interceptor });
        return (T) proxy;
    }
}
Also used : Enhancer(org.springframework.cglib.proxy.Enhancer) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Factory(org.springframework.cglib.proxy.Factory) ProxyFactory(org.springframework.aop.framework.ProxyFactory) LogFactory(org.apache.commons.logging.LogFactory) ObjenesisException(org.springframework.objenesis.ObjenesisException)

Example 2 with Factory

use of org.springframework.cglib.proxy.Factory in project spring-framework by spring-projects.

the class MvcUriComponentsBuilder method initProxy.

@SuppressWarnings("unchecked")
private static <T> T initProxy(Class<?> type, ControllerMethodInvocationInterceptor interceptor) {
    if (type.isInterface()) {
        ProxyFactory factory = new ProxyFactory(EmptyTargetSource.INSTANCE);
        factory.addInterface(type);
        factory.addInterface(MethodInvocationInfo.class);
        factory.addAdvice(interceptor);
        return (T) factory.getProxy();
    } else {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(type);
        enhancer.setInterfaces(new Class<?>[] { MethodInvocationInfo.class });
        enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
        enhancer.setCallbackType(org.springframework.cglib.proxy.MethodInterceptor.class);
        Class<?> proxyClass = enhancer.createClass();
        Object proxy = null;
        if (objenesis.isWorthTrying()) {
            try {
                proxy = objenesis.newInstance(proxyClass, enhancer.getUseCache());
            } catch (ObjenesisException ex) {
                logger.debug("Unable to instantiate controller proxy using Objenesis, " + "falling back to regular construction", ex);
            }
        }
        if (proxy == null) {
            try {
                proxy = ReflectionUtils.accessibleConstructor(proxyClass).newInstance();
            } catch (Throwable ex) {
                throw new IllegalStateException("Unable to instantiate controller proxy using Objenesis, " + "and regular controller instantiation via default constructor fails as well", ex);
            }
        }
        ((Factory) proxy).setCallbacks(new Callback[] { interceptor });
        return (T) proxy;
    }
}
Also used : Enhancer(org.springframework.cglib.proxy.Enhancer) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Factory(org.springframework.cglib.proxy.Factory) ProxyFactory(org.springframework.aop.framework.ProxyFactory) LogFactory(org.apache.commons.logging.LogFactory) ObjenesisException(org.springframework.objenesis.ObjenesisException)

Example 3 with Factory

use of org.springframework.cglib.proxy.Factory in project spring-data-mongodb by spring-projects.

the class DefaultDbRefResolver method createLazyLoadingProxy.

/**
 * Creates a proxy for the given {@link MongoPersistentProperty} using the given {@link DbRefResolverCallback} to
 * eventually resolve the value of the property.
 *
 * @param property must not be {@literal null}.
 * @param dbref can be {@literal null}.
 * @param callback must not be {@literal null}.
 * @return
 */
private Object createLazyLoadingProxy(MongoPersistentProperty property, @Nullable DBRef dbref, DbRefResolverCallback callback, DbRefProxyHandler handler) {
    Class<?> propertyType = property.getType();
    LazyLoadingInterceptor interceptor = new LazyLoadingInterceptor(property, dbref, exceptionTranslator, callback);
    if (!propertyType.isInterface()) {
        Factory factory = (Factory) objenesis.newInstance(getEnhancedTypeFor(propertyType));
        factory.setCallbacks(new Callback[] { interceptor });
        return handler.populateId(property, dbref, factory);
    }
    ProxyFactory proxyFactory = new ProxyFactory();
    for (Class<?> type : propertyType.getInterfaces()) {
        proxyFactory.addInterface(type);
    }
    proxyFactory.addInterface(LazyLoadingProxy.class);
    proxyFactory.addInterface(propertyType);
    proxyFactory.addAdvice(interceptor);
    return handler.populateId(property, dbref, proxyFactory.getProxy());
}
Also used : ProxyFactory(org.springframework.aop.framework.ProxyFactory) Factory(org.springframework.cglib.proxy.Factory) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) ProxyFactory(org.springframework.aop.framework.ProxyFactory)

Example 4 with Factory

use of org.springframework.cglib.proxy.Factory in project spring-data-mongodb by spring-projects.

the class DefaultDbRefResolver method bulkFetch.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.convert.DbRefResolver#bulkFetch(java.util.List)
	 */
@Override
public List<Document> bulkFetch(List<DBRef> refs) {
    Assert.notNull(mongoDbFactory, "Factory must not be null!");
    Assert.notNull(refs, "DBRef to fetch must not be null!");
    if (refs.isEmpty()) {
        return Collections.emptyList();
    }
    String collection = refs.iterator().next().getCollectionName();
    List<Object> ids = new ArrayList<>(refs.size());
    for (DBRef ref : refs) {
        if (!collection.equals(ref.getCollectionName())) {
            throw new InvalidDataAccessApiUsageException("DBRefs must all target the same collection for bulk fetch operation.");
        }
        ids.add(ref.getId());
    }
    List<Document> result = // 
    getCollection(refs.iterator().next()).find(// 
    new Document("_id", new Document("$in", ids))).into(new ArrayList<>());
    return // 
    ids.stream().flatMap(// 
    id -> documentWithId(id, result)).collect(Collectors.toList());
}
Also used : Document(org.bson.Document) Enhancer(org.springframework.cglib.proxy.Enhancer) DataAccessException(org.springframework.dao.DataAccessException) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) MongoCollection(com.mongodb.client.MongoCollection) ObjectInputStream(java.io.ObjectInputStream) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) LazyLoadingException(org.springframework.data.mongodb.LazyLoadingException) ArrayList(java.util.ArrayList) Filters(com.mongodb.client.model.Filters) PersistenceExceptionTranslator(org.springframework.dao.support.PersistenceExceptionTranslator) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Callback(org.springframework.cglib.proxy.Callback) MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) ObjectOutputStream(java.io.ObjectOutputStream) Nullable(org.springframework.lang.Nullable) Method(java.lang.reflect.Method) Collection(java.util.Collection) IOException(java.io.IOException) ObjenesisStd(org.springframework.objenesis.ObjenesisStd) Factory(org.springframework.cglib.proxy.Factory) Collectors(java.util.stream.Collectors) ClientSessionException(org.springframework.data.mongodb.ClientSessionException) Serializable(java.io.Serializable) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) List(java.util.List) Stream(java.util.stream.Stream) MethodProxy(org.springframework.cglib.proxy.MethodProxy) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) DBRef(com.mongodb.DBRef) ReflectionUtils(org.springframework.util.ReflectionUtils) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Collections(java.util.Collections) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ArrayList(java.util.ArrayList) DBRef(com.mongodb.DBRef) Document(org.bson.Document)

Aggregations

ProxyFactory (org.springframework.aop.framework.ProxyFactory)4 Factory (org.springframework.cglib.proxy.Factory)4 Enhancer (org.springframework.cglib.proxy.Enhancer)3 LogFactory (org.apache.commons.logging.LogFactory)2 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)2 ObjenesisException (org.springframework.objenesis.ObjenesisException)2 DBRef (com.mongodb.DBRef)1 MongoCollection (com.mongodb.client.MongoCollection)1 Filters (com.mongodb.client.model.Filters)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Serializable (java.io.Serializable)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1