1 8 9 15 package jfun.yan; 16 17 import jfun.util.SerializableMethod; 18 import jfun.yan.function.Function; 19 20 28 final class FloatingMethodFunction implements Function { 29 private final Class type; 30 private final SerializableMethod mtd; 31 public boolean isConcrete(){ 32 return false; 33 } 34 FloatingMethodFunction(final Class type, 35 final java.lang.reflect.Method mtd) { 36 this.type = type; 37 this.mtd = new SerializableMethod(mtd); 38 } 39 public Class getReturnType() { 40 return mtd.getMethod().getReturnType(); 41 } 42 public Class [] getParameterTypes() { 43 final Class [] ptypes = mtd.getMethod().getParameterTypes(); 44 final Class [] r = new Class [ptypes.length+1]; 45 r[0] = type; 46 for(int i=1; i<r.length;i++){ 47 r[i] = ptypes[i-1]; 48 } 49 return r; 50 } 51 public Object call(Object [] args) throws Throwable { 52 final Object [] nargs = new Object [args.length-1]; 53 for(int i=0; i<nargs.length; i++){ 54 nargs[i] = args[i+1]; 55 } 56 try{ 57 return mtd.getMethod().invoke(args[0], nargs); 58 } 59 catch(java.lang.reflect.InvocationTargetException e){ 60 throw Utils.wrapInvocationException(e); 61 } 62 } 63 64 public boolean equals(Object obj) { 65 if(obj instanceof FloatingMethodFunction){ 66 final FloatingMethodFunction other = (FloatingMethodFunction)obj; 67 return type.equals(other.type) && mtd.equals(other.mtd); 68 } 69 else return false; 70 } 71 public int hashCode() { 72 return type.hashCode()*31+mtd.hashCode(); 73 } 74 public String toString() { 75 return jfun.util.Misc.getTypeName(type) 76 + "::" + mtd.getMethod().getName() 77 + jfun.util.StringUtils.listString("(",",",")", 78 mtd.getMethod().getParameterTypes()); 79 } 80 public String getName(){ 81 return mtd.getMethod().getName(); 82 } 83 } 84 | Popular Tags |