1 10 package org.mmbase.util.functions; 11 12 import java.util.Arrays ; 13 import java.lang.reflect.*; 14 15 import org.mmbase.util.logging.*; 16 17 27 class SetFunction extends AbstractFunction { 28 private static final Logger log = Logging.getLoggerInstance(SetFunction.class); 29 30 33 public static final int TYPE_CLASS = 1; 34 35 38 public static final int TYPE_INSTANCE = 2; 39 40 private Method functionMethod = null; 41 private Object functionInstance = null; 42 private int type = TYPE_CLASS; 43 44 public SetFunction(String name, Parameter[] def, ReturnType returnType, String className, String methodName) { 45 super(name, def, returnType); 46 initialize(className, methodName); 47 } 48 public SetFunction(String name, Parameter[] def ,String className, String methodName) { 49 super(name, def, null); 50 initialize(className, methodName); 51 } 52 53 55 public Object getFunctionValue(Parameters parameters) { 56 parameters.checkRequiredParameters(); 57 try { 58 return functionMethod.invoke(getInstance(), parameters.toArray()); 59 } catch (IllegalAccessException iae) { 60 log.error("Function call failed (method not available) : " + name +", method: " + functionMethod + 61 ", instance: " + getInstance() +", parameters: " + parameters); 62 return null; 63 } catch (InvocationTargetException ite) { 64 Throwable te = ite.getTargetException(); 65 if (te instanceof RuntimeException ) { 66 throw (RuntimeException ) te; 67 } else { 68 throw new RuntimeException (te); } 70 } catch (IllegalArgumentException iae) { 71 String mes = 72 "Function call failed (method not available) : " + name +", method: " + functionMethod + 73 ", instance: " + getInstance() +", parameters: " + parameters; 74 throw new RuntimeException (mes, iae); 75 76 } 77 } 78 79 public void setType(String t) { 80 if (t.equalsIgnoreCase("instance")) { 81 type = TYPE_INSTANCE; 82 } else { 83 type = TYPE_CLASS; 84 } 85 } 86 87 88 protected Object getInstance() { 89 if (functionInstance != null || type == TYPE_CLASS) return functionInstance; 90 try { 91 return functionMethod.getDeclaringClass().newInstance(); 92 } catch(Exception e) { 93 throw new RuntimeException ("Can't create an function instance : " + functionMethod.getDeclaringClass().getName(), e); 94 } 95 } 96 97 101 private void initialize(String className, String methodName) { 102 Class functionClass; 103 try { 104 functionClass = Class.forName(className); 105 } catch(Exception e) { 106 throw new RuntimeException ("Can't create an application function class : " + className + " " + e.getMessage(), e); 107 } 108 try { 109 functionMethod = functionClass.getMethod(methodName, createParameters().toClassArray()); 110 } catch(NoSuchMethodException e) { 111 throw new RuntimeException ("Function method not found : " + className + "." + methodName + "(" + Arrays.asList(getParameterDefinition()) +")", e); 112 } 113 if (Modifier.isStatic(functionMethod.getModifiers())) { 114 functionInstance = null; 115 } else { 116 if (type != TYPE_INSTANCE) { 117 try { 118 functionInstance = functionMethod.getDeclaringClass().newInstance(); 119 } catch(Exception e) { 120 throw new RuntimeException ("Can't create an function instance : " + functionMethod.getDeclaringClass().getName(), e); 121 } 122 } 123 } 124 if (returnType == null) returnType = new ReturnType(functionMethod.getReturnType(), functionMethod.getReturnType().getClass().getName()); 125 126 String returni = functionMethod.getReturnType().getName(); 127 String returnx = returnType.getDataType().getTypeAsClass().getName(); 128 if (returni.equals("boolean")) { returni = "java.lang.Boolean"; } 129 if (!returni.equals(returnx)) { 130 log.warn("Return value of function " + className + "." + methodName + "(" + returni + ") does not match method return type as specified in XML: (" + returnx + ")"); 131 } 132 } 133 } 134 | Popular Tags |