1 16 17 package org.springframework.beans.factory.support; 18 19 import java.beans.PropertyDescriptor ; 20 import java.lang.reflect.Constructor ; 21 import java.lang.reflect.Method ; 22 import java.lang.reflect.Modifier ; 23 import java.util.Arrays ; 24 import java.util.Comparator ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 28 import org.springframework.util.ClassUtils; 29 30 38 abstract class AutowireUtils { 39 40 47 public static void sortConstructors(Constructor [] constructors) { 48 Arrays.sort(constructors, new Comparator () { 49 public int compare(Object o1, Object o2) { 50 Constructor c1 = (Constructor ) o1; 51 Constructor c2 = (Constructor ) o2; 52 boolean p1 = Modifier.isPublic(c1.getModifiers()); 53 boolean p2 = Modifier.isPublic(c2.getModifiers()); 54 if (p1 != p2) { 55 return (p1 ? -1 : 1); 56 } 57 int c1pl = c1.getParameterTypes().length; 58 int c2pl = c2.getParameterTypes().length; 59 return (new Integer (c1pl)).compareTo(new Integer (c2pl)) * -1; 60 } 61 }); 62 } 63 64 79 public static int getTypeDifferenceWeight(Class [] paramTypes, Object [] args) { 80 int result = 0; 81 for (int i = 0; i < paramTypes.length; i++) { 82 if (!ClassUtils.isAssignableValue(paramTypes[i], args[i])) { 83 return Integer.MAX_VALUE; 84 } 85 if (args[i] != null) { 86 Class superClass = args[i].getClass().getSuperclass(); 87 while (superClass != null) { 88 if (ClassUtils.isAssignable(paramTypes[i], superClass)) { 89 result++; 90 superClass = superClass.getSuperclass(); 91 } 92 else { 93 superClass = null; 94 } 95 } 96 } 97 } 98 return result; 99 } 100 101 107 public static boolean isExcludedFromDependencyCheck(PropertyDescriptor pd) { 108 Method wm = pd.getWriteMethod(); 109 if (wm == null) { 110 return false; 111 } 112 if (wm.getDeclaringClass().getName().indexOf("$$") == -1) { 113 return false; 115 } 116 Class superclass = wm.getDeclaringClass().getSuperclass(); 119 return !ClassUtils.hasMethod(superclass, wm.getName(), wm.getParameterTypes()); 120 } 121 122 129 public static boolean isSetterDefinedInInterface(PropertyDescriptor pd, Set interfaces) { 130 Method setter = pd.getWriteMethod(); 131 if (setter != null) { 132 Class targetClass = setter.getDeclaringClass(); 133 for (Iterator it = interfaces.iterator(); it.hasNext();) { 134 Class ifc = (Class ) it.next(); 135 if (ifc.isAssignableFrom(targetClass) && 136 ClassUtils.hasMethod(ifc, setter.getName(), setter.getParameterTypes())) { 137 return true; 138 } 139 } 140 } 141 return false; 142 } 143 144 } 145 | Popular Tags |