Java Code Examples for org.springframework.aop.TargetSource#getTargetClass()

The following examples show how to use org.springframework.aop.TargetSource#getTargetClass() . 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: ProxyFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a proxy for the specified {@code TargetSource} that extends
 * the target class of the {@code TargetSource}.
 * @param targetSource the TargetSource that the proxy should invoke
 * @return the proxy object
 */
public static Object getProxy(TargetSource targetSource) {
	if (targetSource.getTargetClass() == null) {
		throw new IllegalArgumentException("Cannot create class proxy for TargetSource with null target class");
	}
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTargetSource(targetSource);
	proxyFactory.setProxyTargetClass(true);
	return proxyFactory.getProxy();
}
 
Example 2
Source File: ProxyFactoryBean.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a new prototype instance of this class's created proxy object,
 * backed by an independent AdvisedSupport configuration.
 * @return a totally independent proxy, whose advice we may manipulate in isolation
 */
private synchronized Object newPrototypeInstance() {
	// In the case of a prototype, we need to give the proxy
	// an independent instance of the configuration.
	// In this case, no proxy will have an instance of this object's configuration,
	// but will have an independent copy.
	if (logger.isTraceEnabled()) {
		logger.trace("Creating copy of prototype ProxyFactoryBean config: " + this);
	}

	ProxyCreatorSupport copy = new ProxyCreatorSupport(getAopProxyFactory());
	// The copy needs a fresh advisor chain, and a fresh TargetSource.
	TargetSource targetSource = freshTargetSource();
	copy.copyConfigurationFrom(this, targetSource, freshAdvisorChain());
	if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) {
		// Rely on AOP infrastructure to tell us what interfaces to proxy.
		Class<?> targetClass = targetSource.getTargetClass();
		if (targetClass != null) {
			copy.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader));
		}
	}
	copy.setFrozen(this.freezeProxy);

	if (logger.isTraceEnabled()) {
		logger.trace("Using ProxyCreatorSupport copy: " + copy);
	}
	return getProxy(copy.createAopProxy());
}
 
Example 3
Source File: ScriptFactoryPostProcessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a refreshable proxy for the given AOP TargetSource.
 * @param ts the refreshable TargetSource
 * @param interfaces the proxy interfaces (may be {@code null} to
 * indicate proxying of all interfaces implemented by the target class)
 * @return the generated proxy
 * @see RefreshableScriptTargetSource
 */
protected Object createRefreshableProxy(TargetSource ts, @Nullable Class<?>[] interfaces, boolean proxyTargetClass) {
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTargetSource(ts);
	ClassLoader classLoader = this.beanClassLoader;

	if (interfaces != null) {
		proxyFactory.setInterfaces(interfaces);
	}
	else {
		Class<?> targetClass = ts.getTargetClass();
		if (targetClass != null) {
			proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.beanClassLoader));
		}
	}

	if (proxyTargetClass) {
		classLoader = null;  // force use of Class.getClassLoader()
		proxyFactory.setProxyTargetClass(true);
	}

	DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts);
	introduction.suppressInterface(TargetSource.class);
	proxyFactory.addAdvice(introduction);

	return proxyFactory.getProxy(classLoader);
}
 
Example 4
Source File: ProxyFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create a proxy for the specified {@code TargetSource} that extends
 * the target class of the {@code TargetSource}.
 * @param targetSource the TargetSource that the proxy should invoke
 * @return the proxy object
 */
public static Object getProxy(TargetSource targetSource) {
	if (targetSource.getTargetClass() == null) {
		throw new IllegalArgumentException("Cannot create class proxy for TargetSource with null target class");
	}
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTargetSource(targetSource);
	proxyFactory.setProxyTargetClass(true);
	return proxyFactory.getProxy();
}
 
Example 5
Source File: ProxyFactoryBean.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create a new prototype instance of this class's created proxy object,
 * backed by an independent AdvisedSupport configuration.
 * @return a totally independent proxy, whose advice we may manipulate in isolation
 */
private synchronized Object newPrototypeInstance() {
	// In the case of a prototype, we need to give the proxy
	// an independent instance of the configuration.
	// In this case, no proxy will have an instance of this object's configuration,
	// but will have an independent copy.
	if (logger.isTraceEnabled()) {
		logger.trace("Creating copy of prototype ProxyFactoryBean config: " + this);
	}

	ProxyCreatorSupport copy = new ProxyCreatorSupport(getAopProxyFactory());
	// The copy needs a fresh advisor chain, and a fresh TargetSource.
	TargetSource targetSource = freshTargetSource();
	copy.copyConfigurationFrom(this, targetSource, freshAdvisorChain());
	if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) {
		// Rely on AOP infrastructure to tell us what interfaces to proxy.
		Class<?> targetClass = targetSource.getTargetClass();
		if (targetClass != null) {
			copy.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader));
		}
	}
	copy.setFrozen(this.freezeProxy);

	if (logger.isTraceEnabled()) {
		logger.trace("Using ProxyCreatorSupport copy: " + copy);
	}
	return getProxy(copy.createAopProxy());
}
 
Example 6
Source File: ScriptFactoryPostProcessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create a refreshable proxy for the given AOP TargetSource.
 * @param ts the refreshable TargetSource
 * @param interfaces the proxy interfaces (may be {@code null} to
 * indicate proxying of all interfaces implemented by the target class)
 * @return the generated proxy
 * @see RefreshableScriptTargetSource
 */
protected Object createRefreshableProxy(TargetSource ts, @Nullable Class<?>[] interfaces, boolean proxyTargetClass) {
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTargetSource(ts);
	ClassLoader classLoader = this.beanClassLoader;

	if (interfaces != null) {
		proxyFactory.setInterfaces(interfaces);
	}
	else {
		Class<?> targetClass = ts.getTargetClass();
		if (targetClass != null) {
			proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.beanClassLoader));
		}
	}

	if (proxyTargetClass) {
		classLoader = null;  // force use of Class.getClassLoader()
		proxyFactory.setProxyTargetClass(true);
	}

	DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts);
	introduction.suppressInterface(TargetSource.class);
	proxyFactory.addAdvice(introduction);

	return proxyFactory.getProxy(classLoader);
}
 
Example 7
Source File: ProxyFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a proxy for the specified {@code TargetSource} that extends
 * the target class of the {@code TargetSource}.
 * @param targetSource the TargetSource that the proxy should invoke
 * @return the proxy object
 */
public static Object getProxy(TargetSource targetSource) {
	if (targetSource.getTargetClass() == null) {
		throw new IllegalArgumentException("Cannot create class proxy for TargetSource with null target class");
	}
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTargetSource(targetSource);
	proxyFactory.setProxyTargetClass(true);
	return proxyFactory.getProxy();
}
 
Example 8
Source File: ProxyFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Create a proxy for the specified {@code TargetSource} that extends
 * the target class of the {@code TargetSource}.
 * @param targetSource the TargetSource that the proxy should invoke
 * @return the proxy object
 */
public static Object getProxy(TargetSource targetSource) {
	if (targetSource.getTargetClass() == null) {
		throw new IllegalArgumentException("Cannot create class proxy for TargetSource with null target class");
	}
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTargetSource(targetSource);
	proxyFactory.setProxyTargetClass(true);
	return proxyFactory.getProxy();
}
 
Example 9
Source File: PrivateMethodUtil.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * spring注入对象的私有方法调用
 * 
 * @param owner
 *            注入的对象
 * @param methodName
 *            私有方法名
 * @param parameterTypes
 *            私有方法参数类型
 * @param parameters
 *            私有方法参数
 * @return 私有方法返回值
 */
@SuppressWarnings({ "rawtypes" })
public static Object invokeMethod(final Object owner,
        final String methodName, final Class[] parameterTypes,
        final Object[] parameters) throws Exception {

    // get class
    final Class ownerclass = owner.getClass();

    // get property
    try {

        @SuppressWarnings("unchecked")
        final Method getTargetClass = ownerclass
                .getMethod("getTargetSource");
        final TargetSource target = (TargetSource) getTargetClass.invoke(
                owner, new Object[] {});
        final Class targetClass = target.getTargetClass();
        @SuppressWarnings("unchecked")
        final Method method = targetClass.getDeclaredMethod(methodName,
                parameterTypes);

        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        final Object targetInstance = target.getTarget();
        return method.invoke(targetInstance, parameters);

    } catch (NoSuchMethodException e) {

        return invokeMethod(owner, 0, methodName, parameterTypes,
                parameters);
        // e.printStackTrace();
    }
}
 
Example 10
Source File: PrivateMethodUtil.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * spring注入对象的私有方法调用
 * 
 * @param owner
 *            注入的对象
 * @param methodName
 *            私有方法名
 * @param parameterTypes
 *            私有方法参数类型
 * @param parameters
 *            私有方法参数
 * @return 私有方法返回值
 */
@SuppressWarnings({ "rawtypes" })
public static Object invokeMethod(final Object owner,
        final String methodName, final Class[] parameterTypes,
        final Object[] parameters) throws Exception {

    // get class
    final Class ownerclass = owner.getClass();

    // get property
    try {

        @SuppressWarnings("unchecked")
        final Method getTargetClass = ownerclass
                .getMethod("getTargetSource");
        final TargetSource target = (TargetSource) getTargetClass.invoke(
                owner, new Object[] {});
        final Class targetClass = target.getTargetClass();
        @SuppressWarnings("unchecked")
        final Method method = targetClass.getDeclaredMethod(methodName,
                parameterTypes);

        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        final Object targetInstance = target.getTarget();
        return method.invoke(targetInstance, parameters);

    } catch (NoSuchMethodException e) {

        return invokeMethod(owner, 0, methodName, parameterTypes,
                parameters);
        // e.printStackTrace();
    }
}