KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > MethodFunction


1 /*****************************************************************************
2  * Copyright (C) Codehaus.org. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*
9  * Created on Feb 28, 2005
10  *
11  * Author Ben Yu
12  * ZBS
13  */

14 package jfun.yan;
15
16 import jfun.util.Misc;
17 import jfun.util.SerializableMethod;
18 import jfun.yan.function.Function;
19
20 /**
21  * Codehaus.org.
22  *
23  * @author Ben Yu
24  *
25  */

26 final class MethodFunction implements Function{
27   private final Object JavaDoc obj;
28   private final jfun.util.SerializableMethod mtd;
29   
30   public Object JavaDoc call(Object JavaDoc[] args)
31   throws Throwable JavaDoc{
32     try{
33       return mtd.getMethod().invoke(obj, args);
34     }
35     catch(java.lang.reflect.InvocationTargetException JavaDoc e){
36       throw Utils.wrapInvocationException(e);
37     }
38   }
39   public Class JavaDoc[] getParameterTypes() {
40     return mtd.getMethod().getParameterTypes();
41   }
42   public Class JavaDoc getReturnType() {
43     return mtd.getMethod().getReturnType();
44   }
45   public boolean isConcrete(){
46     return false;
47   }
48   MethodFunction(final Object JavaDoc obj, final java.lang.reflect.Method JavaDoc mtd) {
49     this.obj = obj;
50     this.mtd = new SerializableMethod(mtd);
51   }
52   
53   public boolean equals(Object JavaDoc other) {
54     if(other instanceof MethodFunction){
55       final MethodFunction m2 = (MethodFunction)other;
56       return obj==m2.obj && mtd.equals(m2.mtd);
57     }
58     else return false;
59   }
60   public String JavaDoc getName() {
61     return mtd.getMethod().getName();
62   }
63   public int hashCode() {
64     return Misc.hashcode(obj)*31+mtd.hashCode();
65   }
66   public String JavaDoc toString() {
67     return mtd.toString();
68   }
69 }
70
Popular Tags