Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.configurable; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.lang.reflect.*; |
| 5 | import java.lang.reflect.Proxy; |
| 6 | import java.net.*; |
| 7 | import java.util.*; |
| 8 | import java.util.regex.*; |
| 9 | |
| 10 | public class Configurable<T> { |
| 11 | |
| 12 | public static <T> T createConfigurable(Class<T> c, Map< ? , ? > properties) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 13 | Object o = Proxy.newProxyInstance(c.getClassLoader(), new Class< ? >[] { |
| 14 | c |
| 15 | }, new ConfigurableHandler(properties, c.getClassLoader())); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 16 | return c.cast(o); |
| 17 | } |
| 18 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 19 | public static <T> T createConfigurable(Class<T> c, Dictionary< ? , ? > properties) { |
| 20 | Map<Object,Object> alt = new HashMap<Object,Object>(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 21 | for (Enumeration< ? > e = properties.keys(); e.hasMoreElements();) { |
| 22 | Object key = e.nextElement(); |
| 23 | alt.put(key, properties.get(key)); |
| 24 | } |
| 25 | return createConfigurable(c, alt); |
| 26 | } |
| 27 | |
| 28 | static class ConfigurableHandler implements InvocationHandler { |
| 29 | final Map< ? , ? > properties; |
| 30 | final ClassLoader loader; |
| 31 | |
| 32 | ConfigurableHandler(Map< ? , ? > properties, ClassLoader loader) { |
| 33 | this.properties = properties; |
| 34 | this.loader = loader; |
| 35 | } |
| 36 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 37 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 38 | Config ad = method.getAnnotation(Config.class); |
| 39 | String id = Configurable.mangleMethodName(method.getName()); |
| 40 | |
| 41 | if (ad != null && !ad.id().equals(Config.NULL)) |
| 42 | id = ad.id(); |
| 43 | |
| 44 | Object o = properties.get(id); |
| 45 | if (o == null && args != null && args.length == 1) |
| 46 | o = args[0]; |
| 47 | |
| 48 | if (o == null) { |
| 49 | if (ad != null) { |
| 50 | if (ad.required()) |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 51 | throw new IllegalStateException("Attribute is required but not set " + method.getName()); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 52 | |
| 53 | o = ad.deflt(); |
| 54 | if (o.equals(Config.NULL)) |
| 55 | o = null; |
| 56 | } |
| 57 | } |
| 58 | if (o == null) { |
| 59 | Class< ? > rt = method.getReturnType(); |
| 60 | if (rt == boolean.class || rt == Boolean.class) |
| 61 | return false; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 62 | if (method.getReturnType().isPrimitive() || Number.class.isAssignableFrom(method.getReturnType())) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 63 | |
| 64 | o = "0"; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 65 | } else |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 66 | return null; |
| 67 | } |
| 68 | |
| 69 | if (args != null && args.length == 1) { |
| 70 | String s = (String) convert(String.class, o); |
| 71 | |
| 72 | // Allow a base to be specified for File and URL |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 73 | if (method.getReturnType() == File.class && args[0].getClass() == File.class) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 74 | return new File((File) args[0], s); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 75 | } else if (method.getReturnType() == URL.class && args[0].getClass() == File.class) { |
| 76 | return new URL(((File) args[0]).toURI().toURL(), s); |
| 77 | } else if (method.getReturnType() == URL.class && args[0].getClass() == URL.class) { |
| 78 | return new URL((URL) args[0], s); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 79 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 80 | } |
| 81 | return convert(method.getGenericReturnType(), o); |
| 82 | } |
| 83 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 84 | @SuppressWarnings({ |
| 85 | "unchecked", "rawtypes" |
| 86 | }) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 87 | public Object convert(Type type, Object o) throws Exception { |
| 88 | |
| 89 | // TODO type variables |
| 90 | // TODO wildcards |
| 91 | |
| 92 | if (type instanceof ParameterizedType) { |
| 93 | ParameterizedType pType = (ParameterizedType) type; |
| 94 | return convert(pType, o); |
| 95 | } |
| 96 | |
| 97 | if (type instanceof GenericArrayType) { |
| 98 | GenericArrayType gType = (GenericArrayType) type; |
| 99 | return convertArray(gType.getGenericComponentType(), o); |
| 100 | } |
| 101 | |
| 102 | Class< ? > resultType = (Class< ? >) type; |
| 103 | |
| 104 | if (resultType.isArray()) { |
| 105 | return convertArray(resultType.getComponentType(), o); |
| 106 | } |
| 107 | |
| 108 | Class< ? > actualType = o.getClass(); |
| 109 | if (actualType.isAssignableFrom(resultType)) |
| 110 | return o; |
| 111 | |
| 112 | if (resultType == boolean.class || resultType == Boolean.class) { |
| 113 | if (actualType == boolean.class || actualType == Boolean.class) |
| 114 | return o; |
| 115 | |
| 116 | if (Number.class.isAssignableFrom(actualType)) { |
| 117 | double b = ((Number) o).doubleValue(); |
| 118 | if (b == 0) |
| 119 | return false; |
| 120 | return true; |
| 121 | } |
| 122 | return true; |
| 123 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 124 | } else if (resultType == byte.class || resultType == Byte.class) { |
| 125 | if (Number.class.isAssignableFrom(actualType)) |
| 126 | return ((Number) o).byteValue(); |
| 127 | resultType = Byte.class; |
| 128 | } else if (resultType == char.class) { |
| 129 | resultType = Character.class; |
| 130 | } else if (resultType == short.class) { |
| 131 | if (Number.class.isAssignableFrom(actualType)) |
| 132 | return ((Number) o).shortValue(); |
| 133 | resultType = Short.class; |
| 134 | } else if (resultType == int.class) { |
| 135 | if (Number.class.isAssignableFrom(actualType)) |
| 136 | return ((Number) o).intValue(); |
| 137 | resultType = Integer.class; |
| 138 | } else if (resultType == long.class) { |
| 139 | if (Number.class.isAssignableFrom(actualType)) |
| 140 | return ((Number) o).longValue(); |
| 141 | resultType = Long.class; |
| 142 | } else if (resultType == float.class) { |
| 143 | if (Number.class.isAssignableFrom(actualType)) |
| 144 | return ((Number) o).floatValue(); |
| 145 | resultType = Float.class; |
| 146 | } else if (resultType == double.class) { |
| 147 | if (Number.class.isAssignableFrom(actualType)) |
| 148 | return ((Number) o).doubleValue(); |
| 149 | resultType = Double.class; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 150 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 151 | |
| 152 | if (resultType.isPrimitive()) |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 153 | throw new IllegalArgumentException("Unknown primitive: " + resultType); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 154 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 155 | if (Number.class.isAssignableFrom(resultType) && actualType == Boolean.class) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 156 | Boolean b = (Boolean) o; |
| 157 | o = b ? "1" : "0"; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 158 | } else if (actualType == String.class) { |
| 159 | String input = (String) o; |
| 160 | if (Enum.class.isAssignableFrom(resultType)) { |
| 161 | return Enum.valueOf((Class<Enum>) resultType, input); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 162 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 163 | if (resultType == Class.class && loader != null) { |
| 164 | return loader.loadClass(input); |
| 165 | } |
| 166 | if (resultType == Pattern.class) { |
| 167 | return Pattern.compile(input); |
| 168 | } |
| 169 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 170 | |
| 171 | try { |
| 172 | Constructor< ? > c = resultType.getConstructor(String.class); |
| 173 | return c.newInstance(o.toString()); |
| 174 | } |
| 175 | catch (Throwable t) { |
| 176 | // handled on next line |
| 177 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 178 | throw new IllegalArgumentException("No conversion to " + resultType + " from " + actualType + " value " + o); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 181 | private Object convert(ParameterizedType pType, Object o) throws InstantiationException, |
| 182 | IllegalAccessException, Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 183 | Class< ? > resultType = (Class< ? >) pType.getRawType(); |
| 184 | if (Collection.class.isAssignableFrom(resultType)) { |
| 185 | Collection< ? > input = toCollection(o); |
| 186 | if (resultType.isInterface()) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 187 | if (resultType == Collection.class || resultType == List.class) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 188 | resultType = ArrayList.class; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 189 | else if (resultType == Set.class || resultType == SortedSet.class) |
| 190 | resultType = TreeSet.class; |
| 191 | else if (resultType == Queue.class /* |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 192 | * || resultType == |
| 193 | * Deque.class |
| 194 | */) |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 195 | resultType = LinkedList.class; |
| 196 | else if (resultType == Queue.class /* |
| 197 | * || resultType == |
| 198 | * Deque.class |
| 199 | */) |
| 200 | resultType = LinkedList.class; |
| 201 | else |
| 202 | throw new IllegalArgumentException( |
| 203 | "Unknown interface for a collection, no concrete class found: " + resultType); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @SuppressWarnings("unchecked") |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 207 | Collection<Object> result = (Collection<Object>) resultType.newInstance(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 208 | Type componentType = pType.getActualTypeArguments()[0]; |
| 209 | |
| 210 | for (Object i : input) { |
| 211 | result.add(convert(componentType, i)); |
| 212 | } |
| 213 | return result; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 214 | } else if (pType.getRawType() == Class.class) { |
| 215 | return loader.loadClass(o.toString()); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 216 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 217 | if (Map.class.isAssignableFrom(resultType)) { |
| 218 | Map< ? , ? > input = toMap(o); |
| 219 | if (resultType.isInterface()) { |
| 220 | if (resultType == SortedMap.class) |
| 221 | resultType = TreeMap.class; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 222 | else if (resultType == Map.class) |
| 223 | resultType = LinkedHashMap.class; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 224 | else |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 225 | throw new IllegalArgumentException( |
| 226 | "Unknown interface for a collection, no concrete class found: " + resultType); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 227 | } |
| 228 | @SuppressWarnings("unchecked") |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 229 | Map<Object,Object> result = (Map<Object,Object>) resultType.newInstance(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 230 | Type keyType = pType.getActualTypeArguments()[0]; |
| 231 | Type valueType = pType.getActualTypeArguments()[1]; |
| 232 | |
| 233 | for (Map.Entry< ? , ? > entry : input.entrySet()) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 234 | result.put(convert(keyType, entry.getKey()), convert(valueType, entry.getValue())); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 235 | } |
| 236 | return result; |
| 237 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 238 | throw new IllegalArgumentException("cannot convert to " + pType |
| 239 | + " because it uses generics and is not a Collection or a map"); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | Object convertArray(Type componentType, Object o) throws Exception { |
| 243 | Collection< ? > input = toCollection(o); |
| 244 | Class< ? > componentClass = getRawClass(componentType); |
| 245 | Object array = Array.newInstance(componentClass, input.size()); |
| 246 | |
| 247 | int i = 0; |
| 248 | for (Object next : input) { |
| 249 | Array.set(array, i++, convert(componentType, next)); |
| 250 | } |
| 251 | return array; |
| 252 | } |
| 253 | |
| 254 | private Class< ? > getRawClass(Type type) { |
| 255 | if (type instanceof Class) |
| 256 | return (Class< ? >) type; |
| 257 | |
| 258 | if (type instanceof ParameterizedType) |
| 259 | return (Class< ? >) ((ParameterizedType) type).getRawType(); |
| 260 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 261 | throw new IllegalArgumentException("For the raw type, type must be ParamaterizedType or Class but is " |
| 262 | + type); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | private Collection< ? > toCollection(Object o) { |
| 266 | if (o instanceof Collection) |
| 267 | return (Collection< ? >) o; |
| 268 | |
| 269 | if (o.getClass().isArray()) { |
| 270 | if (o.getClass().getComponentType().isPrimitive()) { |
| 271 | int length = Array.getLength(o); |
| 272 | List<Object> result = new ArrayList<Object>(length); |
| 273 | for (int i = 0; i < length; i++) { |
| 274 | result.add(Array.get(o, i)); |
| 275 | } |
| 276 | return result; |
| 277 | } |
| 278 | return Arrays.asList((Object[]) o); |
| 279 | } |
| 280 | |
| 281 | if (o instanceof String) { |
| 282 | String s = (String) o; |
| 283 | if (s.indexOf('|') > 0) |
| 284 | return Arrays.asList(s.split("\\|")); |
| 285 | } |
| 286 | return Arrays.asList(o); |
| 287 | } |
| 288 | |
| 289 | private Map< ? , ? > toMap(Object o) { |
| 290 | if (o instanceof Map) |
| 291 | return (Map< ? , ? >) o; |
| 292 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 293 | throw new IllegalArgumentException("Cannot convert " + o + " to a map as requested"); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | } |
| 297 | |
| 298 | public static String mangleMethodName(String id) { |
| 299 | StringBuilder sb = new StringBuilder(id); |
| 300 | for (int i = 0; i < sb.length(); i++) { |
| 301 | char c = sb.charAt(i); |
| 302 | boolean twice = i < sb.length() - 1 && sb.charAt(i + 1) == c; |
| 303 | if (c == '$' || c == '_') { |
| 304 | if (twice) |
| 305 | sb.deleteCharAt(i + 1); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 306 | else if (c == '$') |
| 307 | sb.deleteCharAt(i--); // Remove dollars |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 308 | else |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 309 | sb.setCharAt(i, '.'); // Make _ into . |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | return sb.toString(); |
| 313 | } |
| 314 | } |