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

The following examples show how to use org.springframework.aop.support.AopUtils#isCglibProxy() . 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: ReflectionUtils.java    From Roothub with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 获取指定类的所有字段
 * <p>此方法会过滤掉被 static 修饰的字段和被 Transient 修饰的字段
 * 
 * @param clazz 指定要获取的 Class 对象
 * @return 返回字段的 List 集合
 */
public static List<Field> getFieldList(Class<?> clazz) {
	if (Objects.isNull(clazz) || Object.class.equals(clazz)) {
		return Collections.emptyList();
	}
	// 如果 class 是代理对象,则需要获取原来的 class
	if (AopUtils.isAopProxy(clazz) || AopUtils.isJdkDynamicProxy(clazz) || AopUtils.isCglibProxy(clazz)) {
		clazz = AopUtils.getTargetClass(clazz);
	}
	return Stream.of(clazz.getDeclaredFields()).filter(field -> {
		// 排除被 static 修饰的字段(Field 的 getModifiers() 方法返回 int 类型值表示该字段的修饰符)
		return !Modifier.isStatic(field.getModifiers());
	}).filter(field -> {
		// 排除被 Transient 修饰的字段
		return !Modifier.isTransient(field.getModifiers());
	}).collect(Collectors.toCollection(LinkedList::new));
}
 
Example 2
Source File: MethodHandlersBuilder.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
MethodHandlersBuilder(Object reactorService,
                      ServiceDescriptor serviceDefinition,
                      Class<CONTEXT> contextType,
                      Supplier<CONTEXT> contextResolver,
                      Class<REACT_SERVICE> reactorDetailedFallbackClass) {
    // CGLIB proxies do not retain generic type info. For these proxies we rely on a detailed fallback class definition to derive generic type info.
    Stream<Method> methodStream = AopUtils.isCglibProxy(reactorService) ? Stream.of(reactorDetailedFallbackClass.getMethods()) : Stream.of(reactorService.getClass().getMethods());
    this.reactorMethodMap = methodStream
            .filter(m -> !ReflectionExt.isObjectMethod(m))
            .collect(Collectors.toMap(Method::getName, Function.identity()));
    this.contextType = contextType;

    List<UnaryMethodHandler> unaryMethodHandlers = new ArrayList<>();
    List<ServerStreamingMethodHandler> serverStreamingMethodHandlers = new ArrayList<>();
    serviceDefinition.getMethods().forEach(methodDescriptor -> {
        GrpcToReactorMethodBinding binding = findReactorMethod(methodDescriptor);
        if (binding.isMono()) {
            unaryMethodHandlers.add(new UnaryMethodHandler<>(binding, contextResolver, reactorService));
        } else {
            serverStreamingMethodHandlers.add(new ServerStreamingMethodHandler<>(binding, contextResolver, reactorService));
        }
    });

    this.unaryMethodHandlers = unaryMethodHandlers;
    this.serverStreamingMethodHandlers = serverStreamingMethodHandlers;
}
 
Example 3
Source File: AopProxyUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Determine the ultimate target class of the given bean instance, traversing
 * not only a top-level proxy but any number of nested proxies as well &mdash;
 * as long as possible without side effects, that is, just for singleton targets.
 * @param candidate the instance to check (might be an AOP proxy)
 * @return the ultimate target class (or the plain class of the given
 * object as fallback; never {@code null})
 * @see org.springframework.aop.TargetClassAware#getTargetClass()
 * @see Advised#getTargetSource()
 */
public static Class<?> ultimateTargetClass(Object candidate) {
	Assert.notNull(candidate, "Candidate object must not be null");
	Object current = candidate;
	Class<?> result = null;
	while (current instanceof TargetClassAware) {
		result = ((TargetClassAware) current).getTargetClass();
		Object nested = null;
		if (current instanceof Advised) {
			TargetSource targetSource = ((Advised) current).getTargetSource();
			if (targetSource instanceof SingletonTargetSource) {
				nested = ((SingletonTargetSource) targetSource).getTarget();
			}
		}
		current = nested;
	}
	if (result == null) {
		result = (AopUtils.isCglibProxy(candidate) ? candidate.getClass().getSuperclass() : candidate.getClass());
	}
	return result;
}
 
Example 4
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 5
Source File: AopProxyUtils.java    From phone with Apache License 2.0 6 votes vote down vote up
private static void removeAdvisor(Object proxy, Class<? extends Advice> adviceClass) {
    if(!AopUtils.isAopProxy(proxy)) {
        return;
    }
    ProxyFactory proxyFactory = null;
    if(AopUtils.isJdkDynamicProxy(proxy)) {
        proxyFactory = findJdkDynamicProxyFactory(proxy);
    }
    if(AopUtils.isCglibProxy(proxy)) {
        proxyFactory = findCglibProxyFactory(proxy);
    }

    Advisor[] advisors = proxyFactory.getAdvisors();

    if(advisors == null || advisors.length == 0) {
        return;
    }

    for(Advisor advisor : advisors) {
        if(adviceClass.isAssignableFrom(advisor.getAdvice().getClass())) {
            proxyFactory.removeAdvisor(advisor);
            break;
        }
    }
}
 
Example 6
Source File: AopProxyUtils.java    From phone with Apache License 2.0 6 votes vote down vote up
private static boolean hasAdvice(Object proxy, Class<? extends Advice> adviceClass) {
    if(!AopUtils.isAopProxy(proxy)) {
        return false;
    }
    ProxyFactory proxyFactory = null;
    if(AopUtils.isJdkDynamicProxy(proxy)) {
        proxyFactory = findJdkDynamicProxyFactory(proxy);
    }
    if(AopUtils.isCglibProxy(proxy)) {
        proxyFactory = findCglibProxyFactory(proxy);
    }

    Advisor[] advisors = proxyFactory.getAdvisors();

    if(advisors == null || advisors.length == 0) {
        return false;
    }

    for(Advisor advisor : advisors) {
        if(adviceClass.isAssignableFrom(advisor.getAdvice().getClass())) {
            return true;
        }
    }
    return false;
}
 
Example 7
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 8
Source File: AopProxyUtils.java    From es with Apache License 2.0 6 votes vote down vote up
private static void removeAdvisor(Object proxy, Class<? extends Advice> adviceClass) {
    if(!AopUtils.isAopProxy(proxy)) {
        return;
    }
    ProxyFactory proxyFactory = null;
    if(AopUtils.isJdkDynamicProxy(proxy)) {
        proxyFactory = findJdkDynamicProxyFactory(proxy);
    }
    if(AopUtils.isCglibProxy(proxy)) {
        proxyFactory = findCglibProxyFactory(proxy);
    }

    Advisor[] advisors = proxyFactory.getAdvisors();

    if(advisors == null || advisors.length == 0) {
        return;
    }

    for(Advisor advisor : advisors) {
        if(adviceClass.isAssignableFrom(advisor.getAdvice().getClass())) {
            proxyFactory.removeAdvisor(advisor);
            break;
        }
    }
}
 
Example 9
Source File: AopProxyUtils.java    From es with Apache License 2.0 6 votes vote down vote up
private static boolean hasAdvice(Object proxy, Class<? extends Advice> adviceClass) {
    if(!AopUtils.isAopProxy(proxy)) {
        return false;
    }
    ProxyFactory proxyFactory = null;
    if(AopUtils.isJdkDynamicProxy(proxy)) {
        proxyFactory = findJdkDynamicProxyFactory(proxy);
    }
    if(AopUtils.isCglibProxy(proxy)) {
        proxyFactory = findCglibProxyFactory(proxy);
    }

    Advisor[] advisors = proxyFactory.getAdvisors();

    if(advisors == null || advisors.length == 0) {
        return false;
    }

    for(Advisor advisor : advisors) {
        if(adviceClass.isAssignableFrom(advisor.getAdvice().getClass())) {
            return true;
        }
    }
    return false;
}
 
Example 10
Source File: AopProxyUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Determine the ultimate target class of the given bean instance, traversing
 * not only a top-level proxy but any number of nested proxies as well &mdash;
 * as long as possible without side effects, that is, just for singleton targets.
 * @param candidate the instance to check (might be an AOP proxy)
 * @return the ultimate target class (or the plain class of the given
 * object as fallback; never {@code null})
 * @see org.springframework.aop.TargetClassAware#getTargetClass()
 * @see Advised#getTargetSource()
 */
public static Class<?> ultimateTargetClass(Object candidate) {
	Assert.notNull(candidate, "Candidate object must not be null");
	Object current = candidate;
	Class<?> result = null;
	while (current instanceof TargetClassAware) {
		result = ((TargetClassAware) current).getTargetClass();
		current = getSingletonTarget(current);
	}
	if (result == null) {
		result = (AopUtils.isCglibProxy(candidate) ? candidate.getClass().getSuperclass() : candidate.getClass());
	}
	return result;
}
 
Example 11
Source File: AopProxyUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Determine the ultimate target class of the given bean instance, traversing
 * not only a top-level proxy but any number of nested proxies as well &mdash;
 * as long as possible without side effects, that is, just for singleton targets.
 * @param candidate the instance to check (might be an AOP proxy)
 * @return the ultimate target class (or the plain class of the given
 * object as fallback; never {@code null})
 * @see org.springframework.aop.TargetClassAware#getTargetClass()
 * @see Advised#getTargetSource()
 */
public static Class<?> ultimateTargetClass(Object candidate) {
	Assert.notNull(candidate, "Candidate object must not be null");
	Object current = candidate;
	Class<?> result = null;
	while (current instanceof TargetClassAware) {
		result = ((TargetClassAware) current).getTargetClass();
		current = getSingletonTarget(current);
	}
	if (result == null) {
		result = (AopUtils.isCglibProxy(candidate) ? candidate.getClass().getSuperclass() : candidate.getClass());
	}
	return result;
}
 
Example 12
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 13
Source File: AopProxyUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determine the ultimate target class of the given bean instance, traversing
 * not only a top-level proxy but any number of nested proxies as well &mdash;
 * as long as possible without side effects, that is, just for singleton targets.
 * @param candidate the instance to check (might be an AOP proxy)
 * @return the ultimate target class (or the plain class of the given
 * object as fallback; never {@code null})
 * @see org.springframework.aop.TargetClassAware#getTargetClass()
 * @see Advised#getTargetSource()
 */
public static Class<?> ultimateTargetClass(Object candidate) {
	Assert.notNull(candidate, "Candidate object must not be null");
	Object current = candidate;
	Class<?> result = null;
	while (current instanceof TargetClassAware) {
		result = ((TargetClassAware) current).getTargetClass();
		current = getSingletonTarget(current);
	}
	if (result == null) {
		result = (AopUtils.isCglibProxy(candidate) ? candidate.getClass().getSuperclass() : candidate.getClass());
	}
	return result;
}
 
Example 14
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 15
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;
    }
}