1 10 package org.mmbase.util.functions; 11 12 13 import java.lang.reflect.*; 14 15 27 public class MethodFunction extends AbstractFunction { 28 29 public static Function getFunction(Method method, String name) { 30 return new MethodFunction(method, name); } 32 33 private Method method = null; 34 public MethodFunction(Method method, String name) { 35 super(name, null, null); 36 this.method = method; 37 if (! Modifier.isStatic(method.getModifiers())) { 38 throw new IllegalArgumentException ("The method " + method + " is not static"); } 40 41 Class [] parameters = method.getParameterTypes(); 42 Parameter[] def = new Parameter[parameters.length]; 43 for (int i = 0; i < parameters.length; i++) { 44 def[i] = new Parameter("parameter" + (i + 1), parameters[i]); } 46 47 setParameterDefinition(def); 48 49 ReturnType returnType = new ReturnType(method.getReturnType(), ""); 50 setReturnType(returnType); 51 52 } 53 54 public Object getFunctionValue(Parameters parameters) { 55 try { 56 return method.invoke(null, parameters.toArray()); 57 } catch (Exception e) { 58 throw new RuntimeException (e); 59 } 60 } 61 62 } 63 | Popular Tags |