1 22 23 package org.jboss.test.visitor; 24 25 import java.io.InputStream ; 26 import java.io.IOException ; 27 import java.lang.reflect.Method ; 28 import java.lang.reflect.Field ; 29 import java.net.URL ; 30 import java.util.HashMap ; 31 import java.util.Properties ; 32 33 import org.jboss.logging.Logger; 34 35 41 public class PropertiesVisitorImpl implements TypeVisitor 42 { 43 private static Logger log = Logger.getLogger(PropertiesVisitorImpl.class); 44 45 private HashMap <Class , Properties > typeProperties = new HashMap <Class , Properties >(); 46 47 private HashMap <String , Properties > methodProperties = new HashMap <String , Properties >(); 48 49 private HashMap <String , Properties > fieldProperties = new HashMap <String , Properties >(); 50 51 54 public HashMap <Class , Properties > getTypeProperties() 55 { 56 return typeProperties; 57 } 58 59 62 public HashMap <String , Properties > getMethodProperties() 63 { 64 return methodProperties; 65 } 66 67 70 public HashMap <String , Properties > getFieldProperties() 71 { 72 return fieldProperties; 73 } 74 75 77 81 public void visitClass(Class type) 82 { 83 String className = type.getName(); 84 int dot = className.lastIndexOf('.'); 85 if( dot >= 0 ) 86 className = className.substring(dot+1); 87 String name = className + ".properties"; 88 loadProperties(type, type, name, typeProperties); 89 } 90 91 95 public void visitInterfaces(Class [] ifaces) 96 { 97 for(int n = 0; n < ifaces.length; n ++) 98 { 99 Class i = ifaces[n]; 100 visitClass(i); 101 } 102 } 103 104 public void visitMethods(Method [] methods) 105 { 106 for(int n = 0; n < methods.length; n ++) 107 { 108 Method m = methods[n]; 109 String name = m.getName(); 110 Class type = m.getDeclaringClass(); 111 loadProperties(type, name, name, methodProperties); 112 } 113 } 114 115 public void visitFields(Field [] fields) 116 { 117 for(int n = 0; n < fields.length; n ++) 118 { 119 Field f = fields[n]; 120 String name = f.getName(); 121 Class type = f.getDeclaringClass(); 122 loadProperties(type, name, name, fieldProperties); 123 } 124 } 125 127 136 protected <T> void loadProperties(Class type, T key, String name, HashMap <T, Properties > map) 137 { 138 URL res = type.getResource(name); 139 if( res != null ) 140 { 141 Properties props = map.get(key); 142 if( props == null ) 143 { 144 props = new Properties (); 145 map.put(key, props); 146 } 147 try 148 { 149 InputStream is = res.openStream(); 150 props.load(is); 151 is.close(); 152 } 153 catch (IOException e) 154 { 155 log.warn("Failed to load properties for name: "+name, e); 156 } 157 catch (NullPointerException e) 159 { 160 log.warn("Failed to load properties for name: "+name, e); 161 } 162 } 163 } 164 } 165 | Popular Tags |