KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > FloatingMethodFunction


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solution. 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 /*
10  * Created on Apr 12, 2005
11  *
12  * Author Ben Yu
13  * ZBS
14  */

15 package jfun.yan;
16
17 import jfun.util.SerializableMethod;
18 import jfun.yan.function.Function;
19
20 /**
21  * For an instance method with the receiver object not fixed.
22  * <p>
23  * Zephyr Business Solution
24  *
25  * @author Ben Yu
26  *
27  */

28 final class FloatingMethodFunction implements Function {
29   private final Class JavaDoc type;
30   private final SerializableMethod mtd;
31   public boolean isConcrete(){
32     return false;
33   }
34   FloatingMethodFunction(final Class JavaDoc type,
35       final java.lang.reflect.Method JavaDoc mtd) {
36     this.type = type;
37     this.mtd = new SerializableMethod(mtd);
38   }
39   public Class JavaDoc getReturnType() {
40     return mtd.getMethod().getReturnType();
41   }
42   public Class JavaDoc[] getParameterTypes() {
43     final Class JavaDoc[] ptypes = mtd.getMethod().getParameterTypes();
44     final Class JavaDoc[] r = new Class JavaDoc[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 JavaDoc call(Object JavaDoc[] args) throws Throwable JavaDoc{
52     final Object JavaDoc[] nargs = new Object JavaDoc[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 JavaDoc e){
60       throw Utils.wrapInvocationException(e);
61     }
62   }
63
64   public boolean equals(Object JavaDoc 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 JavaDoc 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 JavaDoc getName(){
81     return mtd.getMethod().getName();
82   }
83 }
84
Popular Tags