/*****************************************************************************
* Copyright (C) Codehaus.org. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
*****************************************************************************/
/*
* Created on Feb 28, 2005
*
* Author Ben Yu
* ZBS
*/
package jfun.yan;
import jfun.util.Misc;
import jfun.util.SerializableMethod;
import jfun.yan.function.Function;
/**
* Codehaus.org.
*
* @author Ben Yu
*
*/
final class MethodFunction implements Function{
private final Object obj;
private final jfun.util.SerializableMethod mtd;
public Object call(Object[] args)
throws Throwable{
try{
return mtd.getMethod().invoke(obj, args);
}
catch(java.lang.reflect.InvocationTargetException e){
throw Utils.wrapInvocationException(e);
}
}
public Class[] getParameterTypes() {
return mtd.getMethod().getParameterTypes();
}
public Class getReturnType() {
return mtd.getMethod().getReturnType();
}
public boolean isConcrete(){
return false;
}
MethodFunction(final Object obj, final java.lang.reflect.Method mtd) {
this.obj = obj;
this.mtd = new SerializableMethod(mtd);
}
public boolean equals(Object other) {
if(other instanceof MethodFunction){
final MethodFunction m2 = (MethodFunction)other;
return obj==m2.obj && mtd.equals(m2.mtd);
}
else return false;
}
public String getName() {
return mtd.getMethod().getName();
}
public int hashCode() {
return Misc.hashcode(obj)*31+mtd.hashCode();
}
public String toString() {
return mtd.toString();
}
}