1 16 17 package org.springframework.aop.framework; 18 19 import java.util.Arrays ; 20 21 import org.springframework.aop.SpringProxy; 22 import org.springframework.aop.support.AopUtils; 23 import org.springframework.util.Assert; 24 25 36 public abstract class AopProxyUtils { 37 38 47 public static Class getTargetClass(Object candidate) { 48 Assert.notNull(candidate, "Candidate object must not be null"); 49 if (AopUtils.isCglibProxy(candidate)) { 50 return candidate.getClass().getSuperclass(); 51 } 52 if (candidate instanceof Advised) { 53 return ((Advised) candidate).getTargetSource().getTargetClass(); 54 } 55 return candidate.getClass(); 56 } 57 58 67 public static Class [] completeProxiedInterfaces(AdvisedSupport advised) { 68 Class [] specifiedInterfaces = advised.getProxiedInterfaces(); 69 if (specifiedInterfaces.length == 0) { 70 Class targetClass = advised.getTargetClass(); 72 if (targetClass != null && targetClass.isInterface()) { 73 specifiedInterfaces = new Class [] {targetClass}; 74 } 75 } 76 boolean addSpringProxy = !advised.isInterfaceProxied(SpringProxy.class); 77 boolean addAdvised = !advised.isOpaque() && !advised.isInterfaceProxied(Advised.class); 78 int nonUserIfcCount = 0; 79 if (addSpringProxy) { 80 nonUserIfcCount++; 81 } 82 if (addAdvised) { 83 nonUserIfcCount++; 84 } 85 Class [] proxiedInterfaces = new Class [specifiedInterfaces.length + nonUserIfcCount]; 86 System.arraycopy(specifiedInterfaces, 0, proxiedInterfaces, 0, specifiedInterfaces.length); 87 if (addSpringProxy) { 88 proxiedInterfaces[specifiedInterfaces.length] = SpringProxy.class; 89 } 90 if (addAdvised) { 91 proxiedInterfaces[proxiedInterfaces.length - 1] = Advised.class; 92 } 93 return proxiedInterfaces; 94 } 95 96 104 public static Class [] proxiedUserInterfaces(Object proxy) { 105 Class [] proxyInterfaces = proxy.getClass().getInterfaces(); 106 int nonUserIfcCount = 0; 107 if (proxy instanceof SpringProxy) { 108 nonUserIfcCount++; 109 } 110 if (proxy instanceof Advised) { 111 nonUserIfcCount++; 112 } 113 Class [] userInterfaces = new Class [proxyInterfaces.length - nonUserIfcCount]; 114 System.arraycopy(proxyInterfaces, 0, userInterfaces, 0, userInterfaces.length); 115 Assert.notEmpty(userInterfaces, "JDK proxy must implement one or more interfaces"); 116 return userInterfaces; 117 } 118 119 124 public static boolean equalsInProxy(AdvisedSupport a, AdvisedSupport b) { 125 return (a == b || 126 (equalsProxiedInterfaces(a, b) && equalsAdvisors(a, b) && a.getTargetSource().equals(b.getTargetSource()))); 127 } 128 129 132 public static boolean equalsProxiedInterfaces(AdvisedSupport a, AdvisedSupport b) { 133 return Arrays.equals(a.getProxiedInterfaces(), b.getProxiedInterfaces()); 134 } 135 136 139 public static boolean equalsAdvisors(AdvisedSupport a, AdvisedSupport b) { 140 return Arrays.equals(a.getAdvisors(), b.getAdvisors()); 141 } 142 143 } 144 | Popular Tags |