1 10 package org.mmbase.util.functions; 11 12 import java.lang.reflect.*; 13 import java.lang.reflect.Field ; 14 import java.util.*; 15 16 import org.mmbase.util.logging.*; 17 18 19 31 public class Functions { 32 33 private static final Logger log = Logging.getLoggerInstance(Functions.class); 34 35 38 public static Parameters buildParameters(Parameter[] def, List args) { 39 Parameters a; 40 if (args instanceof Parameters) { 41 a = (Parameters) args; 42 } else { 43 a = new Parameters(def, args); 44 } 45 return a; 46 } 47 48 52 public static List define(Parameter[] def, List list) { 53 if (def == null) return list; 54 for (int i = 0; i < def.length; i++) { 55 if (def[i] instanceof Parameter.Wrapper) { 56 define(((Parameter.Wrapper) def[i]).arguments, list); 57 } else { 58 list.add(def[i]); 59 } 60 } 61 return list; 62 } 63 64 67 public static Method getMethodFromClass(Class claz, String name) { 68 Method method = null; 69 Method[] methods = claz.getMethods(); 70 for (int j = 0; j < methods.length; j++) { 71 if (methods[j].getName().equals(name)) { 72 if (method != null) { 73 throw new IllegalArgumentException ("There is more than one method with name '" + name + "' in " + claz); 74 } 75 method = methods[j]; 76 } 77 } 78 if (method == null) { 79 throw new IllegalArgumentException ("There is no method with name '" + name + "' in " + claz); 80 } 81 return method; 82 } 83 84 116 public static Map getParameterDefinitonsByReflection(Class clazz, Map map) { 117 118 log.debug("Searching " + clazz); 119 Field [] fields = clazz.getDeclaredFields(); 120 for (int i = 0 ; i < fields.length; i++) { 121 Field field = fields[i]; 122 int mod = field.getModifiers(); 123 if (Modifier.isStatic(mod) && Modifier.isPublic(mod) && Modifier.isFinal(mod) && 125 field.getType().equals(Parameter[].class)) { 126 String name = field.getName().toLowerCase(); 128 int underscore = name.indexOf("_"); 129 if (underscore > 0) { 130 name = name.substring(0, underscore); 131 } 132 if (! map.containsKey(name)) { try { 134 Parameter[] params = (Parameter[])field.get(null); 135 if (log.isDebugEnabled()) { 136 log.debug("Found a function definition '" + name + "' in " + clazz + " with parameters " + Arrays.asList(params)); 137 } 138 map.put(name, params); 139 } catch (IllegalAccessException iae) { 140 log.error("Found inaccessible parameter[] constant: " + field.getName()); 142 } 143 } 144 } 145 } 146 Class sup = clazz.getSuperclass(); 147 if (sup != null) { 148 getParameterDefinitonsByReflection(sup, map); 149 } 150 return map; 151 } 152 153 } 154 | Popular Tags |