Utility methods for AOP support code.
Mainly for internal use within Spring's AOP support.
| Method from org.springframework.aop.support.AopUtils Detail: |
public static boolean canApply(Pointcut pc,
Class targetClass) {
return canApply(pc, targetClass, false);
}
|
public static boolean canApply(Advisor advisor,
Class targetClass) {
return canApply(advisor, targetClass, false);
}
Can the given advisor apply at all on the given class?
This is an important test as it can be used to optimize
out a advisor for a class. |
public static boolean canApply(Pointcut pc,
Class targetClass,
boolean hasIntroductions) {
if (!pc.getClassFilter().matches(targetClass)) {
return false;
}
MethodMatcher methodMatcher = pc.getMethodMatcher();
IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null;
if (methodMatcher instanceof IntroductionAwareMethodMatcher) {
introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher) methodMatcher;
}
Set classes = new HashSet(ClassUtils.getAllInterfacesForClassAsSet(targetClass));
classes.add(targetClass);
for (Iterator it = classes.iterator(); it.hasNext();) {
Class clazz = (Class) it.next();
Method[] methods = clazz.getMethods();
for (int j = 0; j < methods.length; j++) {
if ((introductionAwareMethodMatcher != null &&
introductionAwareMethodMatcher.matches(methods[j], targetClass, hasIntroductions)) ||
methodMatcher.matches(methods[j], targetClass)) {
return true;
}
}
}
return false;
}
|
public static boolean canApply(Advisor advisor,
Class targetClass,
boolean hasIntroductions) {
if (advisor instanceof IntroductionAdvisor) {
return ((IntroductionAdvisor) advisor).getClassFilter().matches(targetClass);
}
else if (advisor instanceof PointcutAdvisor) {
PointcutAdvisor pca = (PointcutAdvisor) advisor;
return canApply(pca.getPointcut(), targetClass, hasIntroductions);
}
else {
// It doesn't have a pointcut so we assume it applies.
return true;
}
}
Can the given advisor apply at all on the given class?
This is an important test as it can be used to optimize out a advisor for a class.
This version also takes into account introductions (for IntroductionAwareMethodMatchers). |
public static List findAdvisorsThatCanApply(List candidateAdvisors,
Class clazz) {
if (candidateAdvisors.isEmpty()) {
return candidateAdvisors;
}
List eligibleAdvisors = new LinkedList();
for (Iterator it = candidateAdvisors.iterator(); it.hasNext();) {
Advisor candidate = (Advisor) it.next();
if (candidate instanceof IntroductionAdvisor && canApply(candidate, clazz)) {
eligibleAdvisors.add(candidate);
}
}
boolean hasIntroductions = !eligibleAdvisors.isEmpty();
for (Iterator it = candidateAdvisors.iterator(); it.hasNext();) {
Advisor candidate = (Advisor) it.next();
if (candidate instanceof IntroductionAdvisor) {
// already processed
continue;
}
if (canApply(candidate, clazz, hasIntroductions)) {
eligibleAdvisors.add(candidate);
}
}
return eligibleAdvisors;
}
Determine the sublist of the candidateAdvisors list
that is applicable to the given class. |
public static Method getMostSpecificMethod(Method method,
Class targetClass) {
Method resolvedMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
// If we are dealing with method with generic parameters, find the original method.
if (JdkVersion.isAtLeastJava15()) {
resolvedMethod = BridgeMethodResolver.findBridgedMethod(resolvedMethod);
}
return resolvedMethod;
}
Given a method, which may come from an interface, and a target class used
in the current AOP invocation, find the corresponding target method if there
is one. E.g. the method may be IFoo.bar() and the target class
may be DefaultFoo. In this case, the method may be
DefaultFoo.bar(). This enables attributes on that method to be found.
NOTE: In contrast to org.springframework.util.ClassUtils#getMostSpecificMethod ,
this method resolves Java 5 bridge methods in order to retrieve attributes
from the original method definition. |
public static Class getTargetClass(Object candidate) {
Assert.notNull(candidate, "Candidate object must not be null");
if (candidate instanceof TargetClassAware) {
return ((TargetClassAware) candidate).getTargetClass();
}
if (isCglibProxyClass(candidate.getClass())) {
return candidate.getClass().getSuperclass();
}
return candidate.getClass();
}
|
public static Object invokeJoinpointUsingReflection(Object target,
Method method,
Object[] args) throws Throwable {
// Use reflection to invoke the method.
try {
ReflectionUtils.makeAccessible(method);
return method.invoke(target, args);
}
catch (InvocationTargetException ex) {
// Invoked method threw a checked exception.
// We must rethrow it. The client won't see the interceptor.
throw ex.getTargetException();
}
catch (IllegalArgumentException ex) {
throw new AopInvocationException("AOP configuration seems to be invalid: tried calling method [" +
method + "] on target [" + target + "]", ex);
}
catch (IllegalAccessException ex) {
throw new AopInvocationException("Could not access method [" + method + "]", ex);
}
}
Invoke the given target via reflection, as part of an AOP method invocation. |
public static boolean isAopProxy(Object object) {
return (object instanceof SpringProxy &&
(Proxy.isProxyClass(object.getClass()) || isCglibProxyClass(object.getClass())));
}
Check whether the given object is a JDK dynamic proxy or a CGLIB proxy. |
public static boolean isCglibProxy(Object object) {
return (object instanceof SpringProxy && isCglibProxyClass(object.getClass()));
}
Check whether the given object is a CGLIB proxy. |
public static boolean isCglibProxyClass(Class clazz) {
return (clazz != null && clazz.getName().indexOf(ClassUtils.CGLIB_CLASS_SEPARATOR) != -1);
}
Check whether the specified class is a CGLIB-generated class. |
public static boolean isEqualsMethod(Method method) {
return ReflectionUtils.isEqualsMethod(method);
}
Determine whether the given method is an "equals" method. |
public static boolean isFinalizeMethod(Method method) {
return (method != null && method.getName().equals("finalize") &&
method.getParameterTypes().length == 0);
}
Determine whether the given method is a "finalize" method. |
public static boolean isHashCodeMethod(Method method) {
return ReflectionUtils.isHashCodeMethod(method);
}
Determine whether the given method is a "hashCode" method. |
public static boolean isJdkDynamicProxy(Object object) {
return (object instanceof SpringProxy && Proxy.isProxyClass(object.getClass()));
}
Check whether the given object is a JDK dynamic proxy. |
public static boolean isToStringMethod(Method method) {
return ReflectionUtils.isToStringMethod(method);
}
Determine whether the given method is a "toString" method. |