1 16 package org.apache.commons.jxpath; 17 18 import java.lang.reflect.Constructor ; 19 import java.lang.reflect.Method ; 20 import java.util.Collections ; 21 import java.util.Set ; 22 23 import org.apache.commons.jxpath.functions.ConstructorFunction; 24 import org.apache.commons.jxpath.functions.MethodFunction; 25 import org.apache.commons.jxpath.util.MethodLookupUtils; 26 27 53 public class ClassFunctions implements Functions { 54 private Class functionClass; 55 private String namespace; 56 private static final Object [] EMPTY_ARRAY = new Object [0]; 57 58 public ClassFunctions(Class functionClass, String namespace) { 59 this.functionClass = functionClass; 60 this.namespace = namespace; 61 } 62 63 68 public Set getUsedNamespaces() { 69 return Collections.singleton(namespace); 70 } 71 72 83 public Function getFunction( 84 String namespace, 85 String name, 86 Object [] parameters) 87 { 88 if (!namespace.equals(this.namespace)) { 89 return null; 90 } 91 92 if (parameters == null) { 93 parameters = EMPTY_ARRAY; 94 } 95 96 if (name.equals("new")) { 97 Constructor constructor = 98 MethodLookupUtils.lookupConstructor(functionClass, parameters); 99 if (constructor != null) { 100 return new ConstructorFunction(constructor); 101 } 102 } 103 else { 104 Method method = MethodLookupUtils. 105 lookupStaticMethod(functionClass, name, parameters); 106 if (method != null) { 107 return new MethodFunction(method); 108 } 109 110 method = MethodLookupUtils. 111 lookupMethod(functionClass, name, parameters); 112 if (method != null) { 113 return new MethodFunction(method); 114 } 115 } 116 117 return null; 118 } 119 } | Popular Tags |