Java Code Examples for org.springframework.aop.support.AopUtils#isJdkDynamicProxy()

The following examples show how to use org.springframework.aop.support.AopUtils#isJdkDynamicProxy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaskBatchExecutionListenerFactoryBean.java    From spring-cloud-task with Apache License 2.0 6 votes vote down vote up
private MapTaskBatchDao getMapTaskBatchDao() throws Exception {
	Field taskExecutionDaoField = ReflectionUtils.findField(SimpleTaskExplorer.class,
			"taskExecutionDao");
	taskExecutionDaoField.setAccessible(true);

	MapTaskExecutionDao taskExecutionDao;

	if (AopUtils.isJdkDynamicProxy(this.taskExplorer)) {
		SimpleTaskExplorer dereferencedTaskRepository = (SimpleTaskExplorer) ((Advised) this.taskExplorer)
				.getTargetSource().getTarget();

		taskExecutionDao = (MapTaskExecutionDao) ReflectionUtils
				.getField(taskExecutionDaoField, dereferencedTaskRepository);
	}
	else {
		taskExecutionDao = (MapTaskExecutionDao) ReflectionUtils
				.getField(taskExecutionDaoField, this.taskExplorer);
	}

	return new MapTaskBatchDao(taskExecutionDao.getBatchJobAssociations());
}
 
Example 2
Source File: AopProxyUtils.java    From phone with Apache License 2.0 6 votes vote down vote up
/**
 * 是否代理了多次
 * see http://jinnianshilongnian.iteye.com/blog/1894465
 * @param proxy
 * @return
 */
public static boolean isMultipleProxy(Object proxy) {
    try {
        ProxyFactory proxyFactory = null;
        if(AopUtils.isJdkDynamicProxy(proxy)) {
            proxyFactory = findJdkDynamicProxyFactory(proxy);
        }
        if(AopUtils.isCglibProxy(proxy)) {
            proxyFactory = findCglibProxyFactory(proxy);
        }
        TargetSource targetSource = (TargetSource) ReflectionUtils.getField(ProxyFactory_targetSource_FIELD, proxyFactory);
        return AopUtils.isAopProxy(targetSource.getTarget());
    } catch (Exception e) {
        throw new IllegalArgumentException("proxy args maybe not proxy with cglib or jdk dynamic proxy. this method not support", e);
    }
}
 
Example 3
Source File: ScopedEventBus.java    From vaadin4spring with Apache License 2.0 6 votes vote down vote up
/**
 * @param scope          the scope of the events that this event bus handles.
 * @param parentEventBus the parent event bus to use, may be {@code null};
 */
public ScopedEventBus(EventScope scope, EventBus parentEventBus) {
    eventScope = scope;
    this.parentEventBus = parentEventBus;
    if (parentEventBus != null) {
        if (AopUtils.isJdkDynamicProxy(parentEventBus)) {
            logger.debug("Parent event bus [{}] is proxied, trying to get the real EventBus instance",
                    parentEventBus);
            try {
                this.parentEventBus = (EventBus) ((Advised) parentEventBus).getTargetSource().getTarget();
            } catch (Exception e) {
                logger.error("Could not get target EventBus from proxy", e);
                throw new RuntimeException("Could not get parent event bus", e);
            }
        }
        logger.debug("Using parent event bus [{}]", this.parentEventBus);
        this.parentEventBus.subscribe(parentListener);
    }
}
 
Example 4
Source File: AopProxyUtils.java    From es with Apache License 2.0 6 votes vote down vote up
/**
 * 是否代理了多次
 * see http://jinnianshilongnian.iteye.com/blog/1894465
 * @param proxy
 * @return
 */
public static boolean isMultipleProxy(Object proxy) {
    try {
        ProxyFactory proxyFactory = null;
        if(AopUtils.isJdkDynamicProxy(proxy)) {
            proxyFactory = findJdkDynamicProxyFactory(proxy);
        }
        if(AopUtils.isCglibProxy(proxy)) {
            proxyFactory = findCglibProxyFactory(proxy);
        }
        TargetSource targetSource = (TargetSource) ReflectionUtils.getField(ProxyFactory_targetSource_FIELD, proxyFactory);
        return AopUtils.isAopProxy(targetSource.getTarget());
    } catch (Exception e) {
        throw new IllegalArgumentException("proxy args maybe not proxy with cglib or jdk dynamic proxy. this method not support", e);
    }
}
 
Example 5
Source File: SpringRegistry.java    From disconf with Apache License 2.0 5 votes vote down vote up
protected <T> T getTargetObject(Object proxy, Class<T> targetClass) throws Exception {
    if (AopUtils.isJdkDynamicProxy(proxy)) {
        return (T) ((Advised) proxy).getTargetSource().getTarget();
    } else if (AopUtils.isCglibProxy(proxy)) {
        return (T) ((Advised) proxy).getTargetSource().getTarget();
    } else {
        return (T) proxy;
    }
}
 
Example 6
Source File: MetadataMBeanInfoAssembler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Throws an IllegalArgumentException if it encounters a JDK dynamic proxy.
 * Metadata can only be read from target classes and CGLIB proxies!
 */
@Override
protected void checkManagedBean(Object managedBean) throws IllegalArgumentException {
	if (AopUtils.isJdkDynamicProxy(managedBean)) {
		throw new IllegalArgumentException(
				"MetadataMBeanInfoAssembler does not support JDK dynamic proxies - " +
				"export the target beans directly or use CGLIB proxies instead");
	}
}
 
Example 7
Source File: AopTargetUtil.java    From tcc-transaction with Apache License 2.0 5 votes vote down vote up
/**
 * 获取 目标对象
 * @param proxy 代理对象
 * @return
 * @throws Exception
 */
public static Object getTarget(Object proxy) throws Exception {
    if(!AopUtils.isAopProxy(proxy)) {
        return proxy;//不是代理对象
    }
    if(AopUtils.isJdkDynamicProxy(proxy)) {
        return getJdkDynamicProxyTargetObject(proxy);
    } else { //cglib
        return getCglibProxyTargetObject(proxy);
    }
}
 
Example 8
Source File: SimpleAopUtil.java    From jkes with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
public static <T> T getTargetObject(Object proxy) throws Exception {
    if (AopUtils.isJdkDynamicProxy(proxy)) {
        return (T) getTargetObject(((Advised)proxy).getTargetSource().getTarget());
    }
    return (T) proxy; // expected to be cglib proxy then, which is simply a specialized class
}
 
Example 9
Source File: EurekaRegistration.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked" })
protected <T> T getTargetObject(Object proxy, Class<T> targetClass) throws Exception {
	if (AopUtils.isJdkDynamicProxy(proxy)) {
		return (T) ((Advised) proxy).getTargetSource().getTarget();
	}
	else {
		return (T) proxy; // expected to be cglib proxy then, which is simply a
		// specialized class
	}
}
 
Example 10
Source File: AopHelper.java    From bird-java with MIT License 5 votes vote down vote up
/**
 * 获取 目标对象
 *
 * @param proxy 代理对象
 * @return 目标对象
 */
public static Object getTarget(Object proxy) {
    if (!AopUtils.isAopProxy(proxy)) {
        return proxy;
    }
    try {
        Object target = AopUtils.isJdkDynamicProxy(proxy)
                ? getJdkDynamicProxyTargetObject(proxy)
                : getCglibProxyTargetObject(proxy);

        return getTarget(target);
    } catch (Exception ex) {
        return proxy;
    }
}
 
Example 11
Source File: SpringRegistry.java    From disconf with Apache License 2.0 5 votes vote down vote up
protected <T> T getTargetObject(Object proxy, Class<T> targetClass) throws Exception {
    if (AopUtils.isJdkDynamicProxy(proxy)) {
        return (T) ((Advised) proxy).getTargetSource().getTarget();
    } else if (AopUtils.isCglibProxy(proxy)) {
        return (T) ((Advised) proxy).getTargetSource().getTarget();
    } else {
        return (T) proxy;
    }
}
 
Example 12
Source File: AopTargetUtil.java    From galaxy with Apache License 2.0 5 votes vote down vote up
/**
 * 获取 目标对象
 * @param proxy 代理对象
 * @return
 * @throws Exception
 */
public static Object getTarget(Object proxy) throws Exception {
    if(!AopUtils.isAopProxy(proxy)) {
        return proxy;//不是代理对象
    }
    if(AopUtils.isJdkDynamicProxy(proxy)) {
        return getJdkDynamicProxyTargetObject(proxy);
    } else { //cglib
        return getCglibProxyTargetObject(proxy);
    }
}
 
Example 13
Source File: AopTargetUtil.java    From eagle with Apache License 2.0 5 votes vote down vote up
/**
 * 获取目标对象.
 *
 * @param proxy 代理对象
 * @return 目标对象
 */
public static Object getTarget(final Object proxy) {
    if (!AopUtils.isAopProxy(proxy)) {
        return proxy;
    }
    if (AopUtils.isJdkDynamicProxy(proxy)) {
        return getProxyTargetObject(proxy, "h");
    } else {
        return getProxyTargetObject(proxy, "CGLIB$CALLBACK_0");
    }
}
 
Example 14
Source File: AopTargetUtils.java    From timer with Apache License 2.0 5 votes vote down vote up
/**
 * 获取 目标对象
 *
 * @param proxy 代理对象
 * @return
 * @throws Exception
 */
public static Object getTarget(Object proxy) throws Exception {

    if (!AopUtils.isAopProxy(proxy)) {
        return proxy;//不是代理对象
    }

    if (AopUtils.isJdkDynamicProxy(proxy)) {
        return getJdkDynamicProxyTargetObject(proxy);
    } else { //cglib
        return getCglibProxyTargetObject(proxy);
    }


}
 
Example 15
Source File: GuavaEventBusBeanFactoryPostProcessor.java    From tutorials with MIT License 5 votes vote down vote up
private Object getTargetObject(Object proxy) throws BeansException {
    if (AopUtils.isJdkDynamicProxy(proxy)) {
        try {
            return ((Advised)proxy).getTargetSource().getTarget();
        } catch (Exception e) {
            throw new FatalBeanException("Error getting target of JDK proxy", e);
        }
    }
    return proxy;
}
 
Example 16
Source File: TableInfoBuilder.java    From Roothub with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 获取 Model 所有的字段
 * @param modelClass
 * @return
 */
public static List<Field> getAllFields(Class<?> modelClass) {
    // 如果 modelClass 是代理对象,则需要获取原来的 modelClass
    if (AopUtils.isAopProxy(modelClass)
            || AopUtils.isJdkDynamicProxy(modelClass)
            || AopUtils.isCglibProxy(modelClass)) {
        modelClass = AopUtils.getTargetClass(modelClass);
    }
    List<Field> fieldList = getFieldList(modelClass);
    return fieldList;
}
 
Example 17
Source File: MetadataMBeanInfoAssembler.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Throws an IllegalArgumentException if it encounters a JDK dynamic proxy.
 * Metadata can only be read from target classes and CGLIB proxies!
 */
@Override
protected void checkManagedBean(Object managedBean) throws IllegalArgumentException {
	if (AopUtils.isJdkDynamicProxy(managedBean)) {
		throw new IllegalArgumentException(
				"MetadataMBeanInfoAssembler does not support JDK dynamic proxies - " +
				"export the target beans directly or use CGLIB proxies instead");
	}
}
 
Example 18
Source File: DtsTransactionScaner.java    From dts with Apache License 2.0 5 votes vote down vote up
private AdvisedSupport getAdvisedSupport(Object proxy) throws Exception {
    Field h;
    if (AopUtils.isJdkDynamicProxy(proxy)) {
        h = proxy.getClass().getSuperclass().getDeclaredField("h");
    } else {
        h = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0");
    }
    h.setAccessible(true);
    Object dynamicAdvisedInterceptor = h.get(proxy);
    Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised");
    advised.setAccessible(true);
    return (AdvisedSupport)advised.get(dynamicAdvisedInterceptor);
}
 
Example 19
Source File: MetadataMBeanInfoAssembler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Throws an IllegalArgumentException if it encounters a JDK dynamic proxy.
 * Metadata can only be read from target classes and CGLIB proxies!
 */
@Override
protected void checkManagedBean(Object managedBean) throws IllegalArgumentException {
	if (AopUtils.isJdkDynamicProxy(managedBean)) {
		throw new IllegalArgumentException(
				"MetadataMBeanInfoAssembler does not support JDK dynamic proxies - " +
				"export the target beans directly or use CGLIB proxies instead");
	}
}
 
Example 20
Source File: AbstractReflectiveMBeanInfoAssembler.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Return the class to be used for the JMX descriptor field "class".
 * Only applied when the "exposeClassDescriptor" property is "true".
 * <p>The default implementation returns the first implemented interface
 * for a JDK proxy, and the target class else.
 * @param managedBean the bean instance (might be an AOP proxy)
 * @return the class to expose in the descriptor field "class"
 * @see #setExposeClassDescriptor
 * @see #getClassToExpose(Class)
 * @see org.springframework.aop.framework.AopProxyUtils#proxiedUserInterfaces(Object)
 */
protected Class<?> getClassForDescriptor(Object managedBean) {
	if (AopUtils.isJdkDynamicProxy(managedBean)) {
		return AopProxyUtils.proxiedUserInterfaces(managedBean)[0];
	}
	return getClassToExpose(managedBean);
}