KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > util > ReflectionUtil


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.util;
15
16
17 import java.lang.reflect.Constructor JavaDoc;
18 import java.lang.reflect.Field JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.lang.reflect.Modifier JavaDoc;
21 import jfun.util.Misc;
22 import jfun.util.typing.TypeUtils;
23
24
25
26 /**
27  * <p>
28  * The utility class to help reflection.
29  * </p>
30  * <p>
31  * throughout this class, public members are defined as members
32  * defined with "public" keyword and declared in a public type.
33  * public members declared by a non-public class is considered non-public
34  * because access to it from outside is prohibited by the java access control
35  * anyway.
36  * </p>
37  * <p>
38  * public members defined in public classes are always prefered even
39  * when we allow private/protected members and types to be visible.
40  * So if a non-public subtype and a public super type both have a field
41  * with the same name, the field in the public super type is always used.
42  * </p>
43  * Codehaus.org.
44  *
45  * @author Ben Yu
46  *
47  */

48 public final class ReflectionUtil {
49   /**
50    * Convert a primitive type to the corresponding wrapper type
51    * if any.
52    * @param c the type that could be primitive.
53    * @return the wrapper type.
54    */

55   public static Class JavaDoc toObjectType(Class JavaDoc c){
56     return TypeUtils.toObjectType(c);
57   }
58   /**
59    * To tell if an object of class t2 can be set to
60    * a parameter of type t1 using reflection.
61    * <p>
62    * for example, Integer can be set to int.
63    * </p>
64    * @param t1 the parameter type.
65    * @param t2 the object type.
66    * @return true if assignable.
67    */

68   public static boolean isAssignableFrom(Class JavaDoc t1, Class JavaDoc t2){
69     if(void.class.equals(t2)){
70       return !t1.isPrimitive();
71     }
72     return toObjectType(t1).isAssignableFrom(toObjectType(t2));
73   }
74   /**
75    * To determine if two types are sub-type relationship.
76    * @param a the first type.
77    * @param b the second type.
78    * @return true if a is assignable from b or b is assignable from a.
79    */

80   public static boolean isCompatible(Class JavaDoc a, Class JavaDoc b){
81     return isAssignableFrom(a,b) || isAssignableFrom(b, a);
82   }
83   /**
84    * To tell if an object can be assigned to a parameter type
85    * using reflection.
86    * <p>
87    * for example, an object of Integer is an instance of int.
88    * </p>
89    * @param t the parameter type.
90    * @param obj the object.
91    * @return true if the object can be viewed as an instance of the type.
92    */

93   public static boolean isInstance(Class JavaDoc t, Object JavaDoc obj){
94     if(void.class.equals(t)){
95       //wildcard type
96
return true;
97     }
98     if(obj == null){
99       return !t.isPrimitive();
100     }
101     else return toObjectType(t).isInstance(obj);
102   }
103
104
105   private static final Introspector introspector = new Introspector();
106   /**
107    * To find the only public constructor with the expected number of
108    * formal parameters in the class.
109    * @param c the type to find constructor.
110    * @param param_count the number of formal parameters.
111    * @return the Constructor object.
112    * @throws AmbiguityException if more than one are found.
113    * @throws IllegalArgumentException if no public constructor is found
114    * or if the class is not public.
115    */

116   public static <T> Constructor JavaDoc<T> getConstructor(Class JavaDoc<T> c, int param_count)
117   throws AmbiguityException{
118     return getConstructor(c, param_count, false);
119   }
120   /**
121    * To find the only public constructor with the expected number of
122    * formal parameters in the class.
123    * @param c the type to find constructor.
124    * @param param_count the number of formal parameters.
125    * @param suppress_security whether access to the private/protected/package private members is allowed.
126    * @return the Constructor object.
127    * @throws AmbiguityException if more than one are found.
128    * @throws IllegalArgumentException if no constructor is found.
129    */

130   public static <T> Constructor JavaDoc<T> getConstructor(Class JavaDoc<T> c,
131       int param_count, boolean suppress_security){
132     final Constructor JavaDoc<T> ctor = introspector.getClassDescriptor(c)
133     .getConstructor(param_count, suppress_security);
134     if(ctor==null)
135       throw new IllegalArgumentException JavaDoc("constructor not found for type "
136           + Misc.getTypeName(c));
137     return ctor;
138   }
139   /**
140    * To find the only public constructor in a class.
141    * @param c the type to find constructor.
142    * @return the Constructor object.
143    * @throws AmbiguityException if more than one are found.
144    * @throws IllegalArgumentException if no public constructor is found
145    * or if the class is not public.
146    */

147   public static <T> Constructor JavaDoc<T> getConstructor(Class JavaDoc<T> c)
148   throws AmbiguityException{
149     return getConstructor(c, false);
150   }
151   /**
152    * To find the only constructor in a class.
153    * @param c the type to find constructor.
154    * @param suppress_security whether access to the private/protected/package private members is allowed.
155    * @return the Constructor object.
156    * @throws AmbiguityException if more than one are found.
157    * @throws IllegalArgumentException if no constructor is found.
158    */

159   public static <T> Constructor JavaDoc<T> getConstructor(Class JavaDoc<T> c, boolean suppress_security){
160     final Constructor JavaDoc<T> ctor = introspector.getClassDescriptor(c)
161     .getConstructor(suppress_security);
162     if(ctor==null)
163       throw new IllegalArgumentException JavaDoc("constructor not found for type "
164           + Misc.getTypeName(c));
165     return ctor;
166     //checkPublic(c);
167
/*
168     final Constructor[] ctors = c.getConstructors();
169     if(ctors.length==0){
170       throw new IllegalArgumentException("type "
171           + Misc.getTypeName(c)
172           + " has no public constructor");
173     }
174     else if(ctors.length>1){
175       throw new IllegalArgumentException("type "
176           +Misc.getTypeName(c) +
177           " has more than one public constructors");
178     }
179     final Constructor r = ctors[0];
180     ClassDescriptor.forceAccess(r);
181     return r;*/

182   }
183   /**
184    * To find a public constructor in a class.
185    * @param c the type to find constructor.
186    * @param param_types the parameter types.
187    * @return the Constructor object.
188    * @throws IllegalArgumentException if constructor is not found
189    * or if the class is not public.
190    */

191   public static <T> Constructor JavaDoc<T> getConstructor(Class JavaDoc<T> c, Class JavaDoc[] param_types){
192     return getConstructor(c, param_types, false);
193   }
194   /**
195    * To find a constructor in a class.
196    * @param c the type to find constructor.
197    * @param param_types the parameter types.
198    * @param suppress_security whether access to the private/protected/package private members is allowed.
199    * @return the Constructor object.
200    * @throws IllegalArgumentException if constructor is not found.
201    */

202   public static <T> Constructor JavaDoc<T> getConstructor(Class JavaDoc<T> c, Class JavaDoc[] param_types,
203       boolean suppress_security){
204     //checkPublic(c);
205
/*
206     try{
207       final Constructor r = c.getConstructor(checkTypes(param_types));
208       ClassDescriptor.forceAccess(r);
209       return r;
210     }
211     catch(NoSuchMethodException e){
212       throw new IllegalArgumentException("constructor not found for: "
213           + Misc.getTypeName(c) + jfun.util.StringUtils.listString("(",",",")",
214               param_types)
215       );
216     }*/

217     final Constructor JavaDoc<T> ctor = introspector.getClassDescriptor(c)
218     .getConstructor(param_types, suppress_security);
219     if(ctor == null){
220       throw new IllegalArgumentException JavaDoc("constructor not found for: "
221           + Misc.getTypeName(c) + jfun.util.StringUtils.listString("(",",",")",
222               param_types)
223        );
224     }
225     return ctor;
226   }
227   /**
228    * To find a public static method in a class.
229    * There should be only one public static method with this name,
230    * otherwise considered ambiguity.
231    * @param c the type to search the method.
232    * @param name the method name.
233    * @return the Method object.
234    * @throws AmbiguityException when more than one methods are found.
235    * @throws IllegalArgumentException if the method is not found or
236    * if the class is not public.
237    */

238   public static Method JavaDoc getStaticMethod(Class JavaDoc c, String JavaDoc name)
239   throws AmbiguityException{
240     return getStaticMethod(c, name, false);
241   }
242   /**
243    * To find a static method in a class. There should be only one visible
244    * static method with this name, otherwise considered ambiguity.
245    * @param c the type to search the method.
246    * @param name the method name.
247    * @param suppress_security whether access to the private/protected/package private members is allowed.
248    * @return the Method object.
249    * @throws AmbiguityException if more than one methods are found.
250    * @throws IllegalArgumentException if the method is not found.
251    */

252   public static Method JavaDoc getStaticMethod(Class JavaDoc c, final String JavaDoc name,
253       boolean suppress_security)
254   throws AmbiguityException{
255     return filterMethod(c, name, new MethodPredicate(){
256         public boolean isMethod(Method JavaDoc mtd){
257           return java.lang.reflect.Modifier.isStatic(mtd.getModifiers())
258             //&& name.equals(mtd.getName())
259
;
260         }
261         public String JavaDoc toString(){
262           return "static method named "+name;
263         }
264       }, suppress_security);
265   }
266   
267   /**
268    * To find a public static method in a class.
269    * There should be only one public static method with this name
270    * and the expected number of formal parameters,
271    * otherwise considered ambiguity.
272    * @param c the type to search the method.
273    * @param name the method name.
274    * @param param_count the number of formal parameters.
275    * @return the Method object.
276    * @throws AmbiguityException when more than one methods are found.
277    * @throws IllegalArgumentException if the method is not found or
278    * if the class is not public.
279    */

280   public static Method JavaDoc getStaticMethod(Class JavaDoc c, String JavaDoc name, int param_count)
281   throws AmbiguityException{
282     return getStaticMethod(c, name, param_count, false);
283   }
284   /**
285    * To find a static method in a class. There should be only one visible
286    * static method with this name and the expected number of
287    * formal parameters, otherwise considered ambiguity.
288    * @param c the type to search the method.
289    * @param name the method name.
290    * @param param_count the number of formal parameters.
291    * @param suppress_security whether access to the private/protected/package private members is allowed.
292    * @return the Method object.
293    * @throws AmbiguityException if more than one methods are found.
294    * @throws IllegalArgumentException if the method is not found.
295    */

296   public static Method JavaDoc getStaticMethod(Class JavaDoc c, final String JavaDoc name,
297       final int param_count, boolean suppress_security)
298   throws AmbiguityException{
299     return filterMethod(c, name, new MethodPredicate(){
300         public boolean isMethod(Method JavaDoc mtd){
301           return java.lang.reflect.Modifier.isStatic(mtd.getModifiers())
302             && mtd.getParameterTypes().length==param_count;
303             //&& name.equals(mtd.getName())
304
}
305         public String JavaDoc toString(){
306           return "static " + toMethodString(name, param_count);
307         }
308       }, suppress_security);
309   }
310
311   private static String JavaDoc getQualified(Class JavaDoc c, String JavaDoc name){
312     return Misc.getTypeName(c)+"."+name;
313   }
314   /**
315    * To find a public field in a class.
316    * @param c the type to search the field.
317    * @param name the field name.
318    * @return the Field object.
319    * @throws IllegalArgumentException if the field is not found.
320    */

321   public static Field JavaDoc getField(Class JavaDoc c, final String JavaDoc name){
322     return getField(c, name, false);
323   }
324   /**
325    * To find a field in a class.
326    * @param c the type to search the field.
327    * @param name the field name.
328    * @param suppress_security whether access to the private/protected/package private members is allowed.
329    * @return the Field object.
330    * @throws IllegalArgumentException if the field is not found.
331    */

332   public static Field JavaDoc getField(Class JavaDoc c, final String JavaDoc name,
333       boolean suppress_security){
334     final Field JavaDoc fld = introspector.getClassDescriptor(c)
335     .getField(name,suppress_security);
336     if(fld==null)
337       throw new IllegalArgumentException JavaDoc(getQualified(c,name)+" not found.");
338     return fld;
339     /*
340     try{
341       final Field fld = c.getField(name);
342       ClassDescriptor.forceAccess(fld);
343       return fld;
344     }
345     catch(NoSuchFieldException e){
346       throw new IllegalArgumentException(getQualified(c,name)+" not found.");
347     }*/

348   }
349   /**
350    * To find a public static field in a class.
351    * @param c the type to search the field.
352    * @param name the field name.
353    * @return the Field object.
354    * @throws IllegalArgumentException if the field is not found.
355    */

356   public static Field JavaDoc getStaticField(Class JavaDoc c, final String JavaDoc name){
357     return getStaticField(c, name, false);
358   }
359   /**
360    * To find a static field in a class.
361    * @param c the type to search the field.
362    * @param name the field name.
363    * @param suppress_security whether access to the private/protected/package private members is allowed.
364    * @return the Field object.
365    * @throws IllegalArgumentException if the field is not found.
366    */

367   public static Field JavaDoc getStaticField(Class JavaDoc c, final String JavaDoc name,
368       boolean suppress_security){
369     final Field JavaDoc fld = getField(c, name, suppress_security);
370     if(!Modifier.isStatic(fld.getModifiers())){
371       throw new IllegalArgumentException JavaDoc(getQualified(c,name)+" is not a static field");
372     }
373     return fld;
374     /*
375     final Field fld = getField(c, name);
376     if(!Modifier.isStatic(fld.getModifiers())){
377       throw new IllegalArgumentException(getQualified(c,name)+" is not a static field");
378     }
379     ClassDescriptor.forceAccess(fld);
380     return fld;*/

381   }
382   /**
383    * To find a public instance field in a class.
384    * @param c the type to search the field.
385    * @param name the field name.
386    * @return the Field object.
387    * @throws IllegalArgumentException if the field is not found.
388    */

389   public static Field JavaDoc getInstanceField(Class JavaDoc c, final String JavaDoc name){
390     return getInstanceField(c, name, false);
391   }
392   /**
393    * To find an instance field in a class.
394    * @param c the type to search the field.
395    * @param name the field name.
396    * @param suppress_security whether access to the private/protected/package private members is allowed.
397    * @return the Field object.
398    * @throws IllegalArgumentException if the field is not found.
399    */

400   public static Field JavaDoc getInstanceField(Class JavaDoc c, final String JavaDoc name,
401       boolean suppress_security){
402     final Field JavaDoc fld = getField(c, name, suppress_security);
403     if(Modifier.isStatic(fld.getModifiers())){
404       throw new IllegalArgumentException JavaDoc(getQualified(c,name)+" is a static field");
405     }
406     //ClassDescriptor.forceAccess(fld);
407
return fld;
408   }
409   /**
410    * To find a public instance method in a class.
411    * There should be only one public
412    * instance method with this name, otherwise considered ambiguity.
413    * @param c the type to search the method.
414    * @param name the method name.
415    * @return the Method object.
416    * @throws AmbiguityException if more than one methods are found.
417    * @throws IllegalArgumentException if the method is not found.
418    */

419   public static Method JavaDoc getInstanceMethod(Class JavaDoc c, final String JavaDoc name)
420   throws AmbiguityException{
421     return getInstanceMethod(c, name, false);
422   }
423   /**
424    * To find an instance method in a class.
425    * There should be only visible instance method with this name,
426    * otherwise considered ambiguity.
427    * @param c the type to search the method.
428    * @param name the method name.
429    * @param suppress_security whether access to the private/protected/package private members is allowed.
430    * @return the Method object.
431    * @throws AmbiguityException when more than one methods are found.
432    * @throws IllegalArgumentException if the method is not found.
433    */

434   public static Method JavaDoc getInstanceMethod(Class JavaDoc c, final String JavaDoc name,
435       boolean suppress_security){
436     return filterMethod(c, name, new MethodPredicate(){
437         public boolean isMethod(Method JavaDoc mtd){
438           return !java.lang.reflect.Modifier.isStatic(mtd.getModifiers())
439             //&& name.equals(mtd.getName())
440
;
441         }
442         public String JavaDoc toString(){
443           return "instance method named "+name;
444         }
445       }, suppress_security);
446   }
447   /**
448    * To find a public instance method in a class.
449    * There should be only one public
450    * instance method with this name and the expected
451    * number of formal parameters, otherwise considered ambiguity.
452    * @param c the type to search the method.
453    * @param name the method name.
454    * @param param_count the number of formal parameters.
455    * @return the Method object.
456    * @throws AmbiguityException if more than one methods are found.
457    * @throws IllegalArgumentException if the method is not found.
458    */

459   public static Method JavaDoc getInstanceMethod(Class JavaDoc c,
460       int param_count, final String JavaDoc name)
461   throws AmbiguityException{
462     return getInstanceMethod(c, name, param_count, false);
463   }
464   /**
465    * To find an instance method in a class.
466    * There should be only visible instance method with this name
467    * and the expected number of formal parameters,
468    * otherwise considered ambiguity.
469    * @param c the type to search the method.
470    * @param name the method name.
471    * @param param_count the number of formal parameters.
472    * @param suppress_security whether access to the private/protected/package private members is allowed.
473    * @return the Method object.
474    * @throws AmbiguityException when more than one methods are found.
475    * @throws IllegalArgumentException if the method is not found.
476    */

477   public static Method JavaDoc getInstanceMethod(Class JavaDoc c, final String JavaDoc name,
478       final int param_count, boolean suppress_security){
479     return filterMethod(c, name, new MethodPredicate(){
480         public boolean isMethod(Method JavaDoc mtd){
481           return !java.lang.reflect.Modifier.isStatic(mtd.getModifiers())
482             && mtd.getParameterTypes().length==param_count;
483         }
484         public String JavaDoc toString(){
485           return "instance method "+ toMethodString(name, param_count);
486         }
487       }, suppress_security);
488   }
489
490   /**
491    * To find a public method in a class.
492    * There should be only one public method with this name,
493    * otherwise considered ambiguity.
494    * @param c the type to search the method.
495    * @param name the method name.
496    * @return the Method object.
497    * @throws AmbiguityException if more than one are found.
498    * @throws IllegalArgumentException if the method is not found.
499    */

500   public static Method JavaDoc getMethod(Class JavaDoc c, final String JavaDoc name){
501     return getMethod(c, name, false);
502   }
503   /**
504    * To find a method in a class. There should be only one visible
505    * method with this name, otherwise considered ambiguity.
506    * @param c the type to search the method.
507    * @param name the method name.
508    * @param suppress_security whether access to the private/protected/package private members is allowed.
509    * @return the Method object.
510    * @throws IllegalArgumentException if the method is not found or more than one are found.
511    */

512   public static Method JavaDoc getMethod(Class JavaDoc c, final String JavaDoc name,
513       boolean suppress_security){
514     final Method JavaDoc mtd = introspector.getClassDescriptor(c)
515     .getMethod(name, suppress_security);
516     if(mtd == null){
517       throw new IllegalArgumentException JavaDoc(
518           "method " + Misc.getTypeName(c)+ '.' + name
519           + " not found");
520     }
521     return mtd;
522       
523     /*
524     return getPublicMethod(c, new MethodPredicate(){
525           public boolean isMethod(Method m){
526             return name.equals(m.getName());
527           }
528           public String toString(String classname){
529             return "public method " + classname + '.' + name;
530           }
531         });
532     */

533   }
534   /**
535    * To find a public method in a class.
536    * There should be only one public method with this name
537    * and the expected number of formal parameters,
538    * otherwise considered ambiguity.
539    * @param c the type to search the method.
540    * @param name the method name.
541    * @param param_count the number of formal parameters.
542    * @return the Method object.
543    * @throws AmbiguityException if more than one are found.
544    * @throws IllegalArgumentException if the method is not found.
545    */

546   public static Method JavaDoc getMethod(Class JavaDoc c, final String JavaDoc name, int param_count){
547     return getMethod(c, name, param_count, false);
548   }
549   /**
550    * To find a method in a class. There should be only one visible
551    * method with this name
552    * and the expected number of formal parameters,
553    * otherwise considered ambiguity.
554    * @param c the type to search the method.
555    * @param name the method name.
556    * @param param_count the number of formal parameters.
557    * @param suppress_security whether access to the private/protected/package private members is allowed.
558    * @return the Method object.
559    * @throws IllegalArgumentException if the method is not found or more than one are found.
560    */

561   public static Method JavaDoc getMethod(Class JavaDoc c, final String JavaDoc name,
562       final int param_count, boolean suppress_security){
563     final Method JavaDoc mtd = introspector.getClassDescriptor(c)
564     .filterMethod(name, getParamCountPredicate(name, param_count), suppress_security);
565     if(mtd == null){
566       throw new IllegalArgumentException JavaDoc(
567           "method " + Misc.getTypeName(c)+ '.' + name
568           + " not found");
569     }
570     return mtd;
571   }
572
573   /**
574    * To find a public method in a class with a certain signature.
575    * @param c the type to search the method.
576    * @param name the method name.
577    * @param param_types the parameter types.
578    * @return the Method object.
579    * @throws IllegalArgumentException if the method is not found.
580    */

581   public static Method JavaDoc getMethod(Class JavaDoc c, String JavaDoc name, Class JavaDoc[] param_types){
582     return getMethod(c, name, param_types, false);
583   }
584   /**
585    * To find a method in a class with a certain signature.
586    * @param c the type to search the method.
587    * @param name the method name.
588    * @param param_types the parameter types.
589    * @param suppress_security whether access to the private/protected/package private members is allowed.
590    * @return the Method object.
591    * @throws IllegalArgumentException if the method is not found.
592    */

593   public static Method JavaDoc getMethod(Class JavaDoc c, String JavaDoc name, Class JavaDoc[] param_types,
594       boolean suppress_security){
595     //checkPublic(c);
596
final Method JavaDoc mtd = introspector.getClassDescriptor(c)
597       .getMethod(name, checkTypes(param_types), suppress_security);
598     if(mtd==null){
599       throw new IllegalArgumentException JavaDoc("method "
600           + Misc.getTypeName(c)+'.'+name+
601           Utils.toString(param_types)
602           +" not found.");
603     }
604     return mtd;
605   }
606   /*
607   private static Method getMethod(Class c, List mtds, final MethodPredicate mp){
608     Method rmtd = null;
609     final int sz = mtds.size();
610     for(int i=0; i<sz; i++){
611       final Method mtd = (Method)mtds.get(i);
612       if(mp.isMethod(mtd)){
613         if(rmtd!=null){
614           throw new AmbiguityException(
615               "ambiguous method: " + Misc.getTypeName(c)
616               + "." + rmtd.getName());
617         }
618         rmtd = mtd;
619       }
620     }
621     if(rmtd == null){
622       throw new IllegalArgumentException(
623           mp.toString(Misc.getTypeName(c))
624           + " not found");
625     }
626     return rmtd;
627   }*/

628
629   private static Method JavaDoc filterMethod(Class JavaDoc c, String JavaDoc name,
630       final MethodPredicate pred,
631       boolean suppress_security)
632   throws AmbiguityException, IllegalArgumentException JavaDoc{
633     final Method JavaDoc ret = introspector.getClassDescriptor(c)
634     .filterMethod(name, pred, suppress_security);
635     if(ret==null){
636       throw new IllegalArgumentException JavaDoc(
637           pred.toString()+" cannot be found in "
638           + Misc.getTypeName(c)
639       );
640     }
641     return ret;
642
643     //checkPublic(c);
644
/*
645     Method rmtd = null;
646     final Method[] mtds = c.getMethods();
647     for(int i=0; i<mtds.length; i++){
648       final Method mtd = mtds[i];
649       if(mp.isMethod(mtd)){
650         if(rmtd!=null){
651           throw new IllegalArgumentException(
652               "ambiguous method: " + Misc.getTypeName(c)
653               + "." + rmtd.getName());
654         }
655         rmtd = mtd;
656       }
657     }
658     if(rmtd == null){
659       throw new IllegalArgumentException(
660           mp.toString(Misc.getTypeName(c))
661           + " not found");
662     }
663     if(rmtd!=null)
664       ClassDescriptor.forceAccess(rmtd);
665     return rmtd;*/

666   }
667   private static MethodPredicate getParamCountPredicate(
668       final String JavaDoc name, final int param_count){
669     return new MethodPredicate(){
670       public boolean isMethod(Method JavaDoc mtd) {
671         return mtd.getParameterTypes().length==param_count;
672       }
673       public String JavaDoc toString(){
674         return toMethodString(name, param_count);
675       }
676     };
677   }
678   /**
679    * Create a string representation of a method with
680    * the given number of formal parameters.
681    * @param name the method name.
682    * @param param_count the number of formal parameters.
683    * @return the string representation.
684    */

685   public static String JavaDoc toMethodString(String JavaDoc name, int param_count){
686     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
687     buf.append(name);
688     buf.append('(');
689     for(int i=0; i<param_count; i++){
690       if(i>0) buf.append(',');
691       buf.append('_');
692     }
693     buf.append(')');
694     return buf.toString();
695   }
696   private static Class JavaDoc[] checkTypes(Class JavaDoc[] ptypes){
697     return ptypes==null?no_params:ptypes;
698   }
699   static final Class JavaDoc[] no_params = new Class JavaDoc[0];
700
701 }
702
Popular Tags