1 package com.tirsen.nanning.attribute; 2 3 import java.lang.reflect.Field ; 4 import java.lang.reflect.Method ; 5 import java.util.HashMap ; 6 import java.util.Map ; 7 8 public class ClassAttributes { 9 10 private Class attributeClass; 11 private Map classAttributes = new HashMap (); 12 private Map fieldAttributes = new HashMap (); 13 private Map methodAttributes = new HashMap (); 14 15 public ClassAttributes(Class aClass) { 16 this.attributeClass = aClass; 17 } 18 19 public Class getAttributeClass() { 20 return attributeClass; 21 } 22 23 void setMethodAttribute(Method method, String attributeName, String attributeValue) { 24 getMap(methodAttributes, method).put(attributeName, attributeValue); 25 } 26 27 void setFieldAttribute(Field field, String attributeName, String attributeValue) { 28 getMap(fieldAttributes, field).put(attributeName, attributeValue); 29 } 30 31 void setClassAttribute(String attributeName, String attributeValue) { 32 classAttributes.put(attributeName, attributeValue); 33 } 34 35 public String getAttribute(String attribute) { 36 return (String ) classAttributes.get(attribute); 37 } 38 39 public boolean hasAttribute(String attribute) { 40 return classAttributes.containsKey(attribute); 41 } 42 43 public String getAttribute(Field field, String attribute) { 44 return (String ) getMap(fieldAttributes, field).get(attribute); 45 } 46 47 private Map getMap(Map map, Object key) { 48 assert map != null : "properties not loaded"; 49 Map result = (Map ) map.get(key); 50 if (result == null) { 51 result = new HashMap (); 52 map.put(key, result); 53 } 54 return result; 55 } 56 57 public boolean hasAttribute(Field field, String attribute) { 58 return getMap(fieldAttributes, field).containsKey(attribute); 59 } 60 61 public String getAttribute(Method method, String attribute) { 62 return (String ) getMap(methodAttributes, method).get(attribute); 63 } 64 65 public boolean hasAttribute(Method method, String attribute) { 66 return getMap(methodAttributes, method).containsKey(attribute); 67 } 68 } 69 | Popular Tags |