1 7 package org.jboss.cache.aop; 8 9 import java.lang.reflect.Field ; 10 import java.lang.reflect.Method ; 11 import java.lang.reflect.Modifier ; 12 import java.util.*; 13 14 19 20 public class CachedType 21 { 22 protected static Set immediates = 24 new HashSet(Arrays.asList(new Object []{ 25 String .class, 26 Boolean .class, 27 Double .class, 28 Float .class, 29 Integer .class, 30 Long .class, 31 Short .class, 32 Character .class, 33 Boolean.TYPE, 34 Double.TYPE, 35 Float.TYPE, 36 Integer.TYPE, 37 Long.TYPE, 38 Short.TYPE, 39 Character.TYPE, 40 Class .class})); 41 42 protected Class type; 43 protected boolean immutable; 44 protected boolean immediate; 45 46 protected List attributes = new ArrayList(); 48 protected Map attributeMap = new HashMap(); 50 protected List fields = new ArrayList(); 52 protected Map fieldMap = new HashMap(); 54 public CachedType() 55 { 56 } 57 58 public CachedType(Class type) 59 { 60 this.type = type; 61 analyze(); 62 } 63 64 public Class getType() 65 { 66 return type; 67 } 68 69 public boolean isImmediate() 71 { 72 return immediate; 73 } 74 75 public static boolean isImmediate(Class clazz) 76 { 77 return immediates.contains(clazz); 78 } 79 80 public boolean isImmutable() 81 { 82 return immutable; 83 } 84 85 public List getFields() 86 { 87 return fields; 88 } 89 90 public Field getField(String name) 91 { 92 return (Field ) fieldMap.get(name); 93 } 94 95 127 128 public String toString() 129 { 130 StringBuffer sb = new StringBuffer (); 131 sb.append(type.getName()).append(" {\n"); 132 for (Iterator i = attributes.iterator(); i.hasNext();) { 133 CachedAttribute attr = (CachedAttribute) i.next(); 134 sb 135 .append("\t") 136 .append(attr.getType().getName()) 137 .append(" ") 138 .append(attr.getName()) 139 .append(" [") 140 .append(attr.getGet() == null 141 ? "<no get>" 142 : attr.getGet().getName()) 143 .append(", ") 144 .append(attr.getSet() == null 145 ? "<no set>" 146 : attr.getSet().getName()) 147 .append("]\n"); 148 } 149 sb.append("}, immutable =" + immutable); 150 return sb.toString(); 151 } 152 153 154 155 private void analyze() 156 { 157 158 179 analyzeFields(type); 180 181 immediate = isImmediate(type); 182 183 } 184 185 void analyzeFields(Class clazz) 186 { 187 if (clazz == null) 188 return; 189 190 analyzeFields(clazz.getSuperclass()); 191 192 Field [] classFields = clazz.getDeclaredFields(); 193 for (int i = 0; i < classFields.length; i++) { 194 Field f = classFields[i]; 195 if(isNonReplicatable(f)) continue; 196 197 f.setAccessible(true); 198 fields.add(f); 199 fieldMap.put(f.getName(), f); 200 } 201 } 202 203 public static boolean isNonReplicatable(Field f) { 204 int mods = f.getModifiers(); 205 209 if (Modifier.isStatic(mods) 210 || Modifier.isTransient(mods) 211 || Modifier.isFinal(mods)) { 212 return true; 213 } 214 return false; 215 } 216 217 220 protected String attributeName(String methodName) 221 { 222 return methodName.substring(3, 4).toLowerCase() 223 + methodName.substring(4); 224 } 225 226 protected CachedAttribute getAttribute(Method method, 227 Map map, 228 boolean create) 229 { 230 String name = attributeName(method.getName()); 231 232 CachedAttribute attribute = (CachedAttribute) map.get(name); 233 if (create && attribute == null) { 234 attribute = new CachedAttribute(name); 235 map.put(name, attribute); 236 } 237 return attribute; 238 } 239 240 protected boolean isGet(Method method) 241 { 242 return method.getName().startsWith("get") 243 && method.getParameterTypes().length == 0 244 && method.getReturnType() != Void.TYPE; 245 } 246 247 protected boolean isSet(Method method) 248 { 249 return method.getName().startsWith("set") 250 && method.getParameterTypes().length == 1 251 && method.getReturnType() == Void.TYPE; 252 } 253 254 } | Popular Tags |