1 16 17 package org.springframework.core; 18 19 import java.io.Externalizable ; 20 import java.io.Serializable ; 21 import java.lang.reflect.Proxy ; 22 import java.util.Collection ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.Set ; 26 27 import org.springframework.util.Assert; 28 import org.springframework.util.ClassUtils; 29 import org.springframework.util.StringUtils; 30 31 39 public abstract class Conventions { 40 41 44 private static final String PLURAL_SUFFIX = "List"; 45 46 47 51 private static final Set ignoredInterfaces = new HashSet (); 52 53 static { 54 ignoredInterfaces.add(Serializable .class); 55 ignoredInterfaces.add(Externalizable .class); 56 ignoredInterfaces.add(Cloneable .class); 57 ignoredInterfaces.add(Comparable .class); 58 } 59 60 61 71 public static String getVariableName(Object value) { 72 Assert.notNull(value, "Value must not be null"); 73 Class valueClass = null; 74 boolean pluralize = false; 75 76 if (value.getClass().isArray()) { 77 valueClass = value.getClass().getComponentType(); 78 pluralize = true; 79 } 80 else if (value instanceof Collection ) { 81 Collection collection = (Collection ) value; 82 if (collection.isEmpty()) { 83 throw new IllegalArgumentException ("Cannot generate variable name for an empty Collection"); 84 } 85 Object valueToCheck = peekAhead(collection); 86 valueClass = getClassForValue(valueToCheck); 87 pluralize = true; 88 } 89 else { 90 valueClass = getClassForValue(value); 91 } 92 93 String name = StringUtils.uncapitalize(getShortName(valueClass)); 94 return (pluralize ? pluralize(name) : name); 95 } 96 97 102 public static String attributeNameToPropertyName(String attributeName) { 103 Assert.notNull(attributeName, "'attributeName' must not be null"); 104 if (attributeName.indexOf("-") == -1) { 105 return attributeName; 106 } 107 char[] chars = attributeName.toCharArray(); 108 char[] result = new char[chars.length -1]; int currPos = 0; 110 boolean upperCaseNext = false; 111 for (int i = 0; i < chars.length; i++) { 112 char c = chars[i]; 113 if (c == '-') { 114 upperCaseNext = true; 115 } 116 else if (upperCaseNext) { 117 result[currPos++] = Character.toUpperCase(c); 118 upperCaseNext = false; 119 } 120 else { 121 result[currPos++] = c; 122 } 123 } 124 return new String (result, 0, currPos); 125 } 126 127 136 private static Class getClassForValue(Object value) { 137 if (Proxy.isProxyClass(value.getClass())) { 138 Class [] ifcs = value.getClass().getInterfaces(); 139 for (int i = 0; i < ifcs.length; i++) { 140 Class ifc = ifcs[i]; 141 if (!ignoredInterfaces.contains(ifc)) { 142 return ifc; 143 } 144 } 145 } 146 return value.getClass(); 147 } 148 149 155 private static String getShortName(Class valueClass) { 156 String shortName = ClassUtils.getShortName(valueClass); 157 int dotIndex = shortName.lastIndexOf('.'); 158 return (dotIndex != -1 ? shortName.substring(dotIndex + 1) : shortName); 159 } 160 161 164 private static String pluralize(String name) { 165 return name + PLURAL_SUFFIX; 166 } 167 168 173 private static Object peekAhead(Collection collection) { 174 Iterator it = collection.iterator(); 175 if (!it.hasNext()) { 176 throw new IllegalStateException ( 177 "Unable to peek ahead in non-empty collection - no element found"); 178 } 179 Object value = it.next(); 180 if (value == null) { 181 throw new IllegalStateException ( 182 "Unable to peek ahead in non-empty collection - only null element found"); 183 } 184 return value; 185 } 186 187 188 193 public static String getQualifiedAttributeName(Class enclosingClass, String attributeName) { 194 Assert.notNull(enclosingClass, "'enclosingClass' must not be null"); 195 Assert.notNull(attributeName, "'attributeName' must not be null"); 196 return enclosingClass.getName() + "." + attributeName; 197 } 198 199 } 200 | Popular Tags |