KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > xml > nut > MethodSuite


1 package jfun.yan.xml.nut;
2
3 import java.lang.reflect.Method JavaDoc;
4 import java.util.Arrays JavaDoc;
5 import java.util.List JavaDoc;
6
7
8 import jfun.util.Misc;
9 import jfun.util.typing.TypedSuite;
10
11
12 /**
13  * Contains a suite of 1-parametered methods.
14  * Upon lookup, the method with the most restrictive parameter type is selected.
15  * <p>
16  * @author Ben Yu
17  * Dec 29, 2005 12:48:52 AM
18  */

19 final class MethodSuite extends TypedSuite implements java.io.Serializable JavaDoc {
20   protected Object JavaDoc ambiguity(Object JavaDoc arg, Object JavaDoc candidate1, Object JavaDoc candidate2) {
21     final Method1 result = (Method1)candidate1;
22     final Method1 dup = (Method1)candidate2;
23     throw new IllegalArgumentException JavaDoc("Ambiguous method: "
24         + result.getMethod().toString() + " and "
25         + dup.getMethod().toString());
26   }
27
28   protected Class JavaDoc getType(Object JavaDoc candidate) {
29     return ((Method1)candidate).getParameterType();
30   }
31
32   protected Object JavaDoc not_found(Object JavaDoc arg) {
33     throw new IllegalArgumentException JavaDoc("Method " + name + " with parameter type "
34         + (arg==null?null:Misc.getTypeName(arg.getClass()))
35         +" cannot be found.");
36   }
37   public Method JavaDoc getMethod(Object JavaDoc arg){
38     final Method1 result = (Method1)super.getCandidate(arg);
39     return result.getMethod();
40   }
41   private final Method1[] methods;
42   /*
43   private static final int UNKNOWN = Integer.MIN_VALUE;
44   private static int compareClass(Class a, Class b){
45     if(a.equals(b)){
46       return 0;
47     }
48     else if(ReflectionUtil.toObjectType(a).equals(b)){
49       return -1;
50     }
51     else if(ReflectionUtil.toObjectType(b).equals(a)){
52       return 1;
53     }
54     else{
55       a = ReflectionUtil.toObjectType(a);
56       b = ReflectionUtil.toObjectType(b);
57       if(a.isAssignableFrom(b)){
58       return 1;
59       }
60       else if(b.isAssignableFrom(a)){
61         return -1;
62       }
63       else{
64         return UNKNOWN;
65       }
66     }
67   }
68   
69   
70   private final WeakHashMap cache = new WeakHashMap();
71   private Method nullmethod = null;
72
73   synchronized Method getMethod(Object arg){
74     if(arg==null){
75       if(nullmethod==null){
76         nullmethod = findNullMethod();
77       }
78       return nullmethod;
79     }
80     final Class argtype = arg.getClass();
81     Method result = (Method)cache.get(argtype);
82     if(result==null){
83       result = findMethod(arg);
84       cache.put(argtype, result);
85     }
86     return result;
87   }
88   private Method findNullMethod(){
89     return findMethod(null);
90   }
91   private Method findMethod(Object arg){
92     Method1 result = null;
93     Method1 dup = null;
94     for(int i=0; i<methods.length; i++){
95       final Method1 mtd = methods[i];
96       final Class paramtype = mtd.getParameterType();
97       if(ReflectionUtil.isInstance(paramtype, arg)){
98         if(result==null){
99           result = mtd;
100         }
101         else{
102           final int cmp = compareClass(paramtype, result.getParameterType());
103           switch(cmp){
104           case 0:
105             dup = mtd;
106             break;
107           case -1:
108             result = mtd;
109             if(dup!=null){
110               final int dupcmp = compareClass(paramtype, dup.getParameterType());
111               switch(dupcmp){
112               case 0:
113                 dup = null;
114                 break;
115               case -1:
116                 dup = null;
117                 break;
118               case 1:
119                 throw new IllegalStateException("invalid class subtype relationship");
120               default:
121               }
122             }
123             break;
124           case 1:
125             break;
126           default:
127             dup = mtd;
128           }//switch
129         }//else
130       }//if
131     }//for
132     if(result==null){
133       throw new IllegalArgumentException("Method with parameter type "
134           + (arg==null?null:Misc.getTypeName(arg.getClass()))
135           +" cannot be found.");
136     }
137     else if(dup!=null){
138       throw new IllegalArgumentException("Ambiguous method: "
139           + result.getMethod().toString() + " and "
140           + dup.getMethod().toString());
141     }
142     return result.getMethod();
143
144   }*/

145   protected List JavaDoc getCandidates(){
146     return Arrays.asList(methods);
147   }
148   MethodSuite(final String JavaDoc name, Method1[] methods) {
149     this.methods = methods;
150     this.name = name;
151   }
152   private final String JavaDoc name;
153 }
154
Popular Tags