KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > Functions


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 java.lang.reflect.Constructor JavaDoc;
17 import java.lang.reflect.Field JavaDoc;
18 import java.lang.reflect.Method JavaDoc;
19 import java.lang.reflect.Modifier JavaDoc;
20
21 import jfun.util.beans.Bean;
22 import jfun.util.beans.BeanType;
23 import jfun.yan.function.Function;
24 import jfun.yan.util.ReflectionUtil;
25
26
27 /**
28  * This is the facade class to create different Function objects.
29  * <p>
30  * throughout this class, when searching reflection objects,
31  * 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  * <P>
44  * Codehaus.org.
45  *
46  * @author Ben Yu
47  *
48  */

49 public final class Functions {
50   /**
51    * Adapts a {@link java.lang.reflect.Constructor} object
52    * to a Function object.
53    * @param ctor the constructor object.
54    * @return the Function object.
55    */

56   public static <T> Function<T> ctor(Constructor JavaDoc<T> ctor){
57     return new ConstructorFunction<T>(ctor);
58   }
59   /**
60    * Adapts a {@link java.lang.reflect.Method} object and the receiver object
61    * to a Function object.
62    * @param obj the receiver object to call the method against.
63    * If the method is static, the object can be null.
64    * @param mtd the method object.
65    * @return the Function object.
66    */

67   public static Function method(Object JavaDoc obj, Method JavaDoc mtd){
68     return new MethodFunction(canonicalize(obj, mtd), mtd);
69   }
70   private static final Object JavaDoc canonicalize(Object JavaDoc obj, Method JavaDoc mtd){
71     //if method is static, we do not save the object,
72
//lest the object is not serializable and prevent us from serializing
73
//the function.
74
if(java.lang.reflect.Modifier.isStatic(mtd.getModifiers())){
75       return null;
76     }
77     else return obj;
78   }
79   private static final Object JavaDoc canonicalize(Object JavaDoc obj, Field JavaDoc fld){
80     //if field is static, we do not save the object,
81
//lest the object is not serializable and prevent us from serializing
82
//the function.
83
if(java.lang.reflect.Modifier.isStatic(fld.getModifiers())){
84       return null;
85     }
86     else return obj;
87   }
88   /**
89    * Create a Function object that uses the public constructor of a class.
90    * This class should have one and only one public constructor.
91    * <br>
92    * Array type is given a special constructor so that the constructor of
93    * array type T has one parameter of type int
94    * and calls new T[size] when invoked.
95    * @param c the class.
96    * @return the Function object.
97    * @throws IllegalArgumentException
98    * if the class has no public constructor
99    * or has more than one public constructor.
100    */

101   public static <T> Function<T> ctor(Class JavaDoc<T> c)
102   throws IllegalArgumentException JavaDoc{
103     return ctor(c, false);
104   }
105   /**
106    * Create a Function object that uses the constructor of a class.
107    * This class should have one and only one visible constructor.
108    * <br>
109    * Array type is given a special constructor so that the constructor of
110    * array type T has one parameter of type int
111    * and calls new T[size] when invoked.
112    * @param c the class.
113    * @param suppress_security whether access to non-public members is allowed.
114    * @return the Function object.
115    * @throws IllegalArgumentException
116    * if the class has no public constructor
117    * or has more than one public constructor.
118    */

119   public static <T> Function<T> ctor(Class JavaDoc<T> c, boolean suppress_security)
120   throws IllegalArgumentException JavaDoc{
121     if(c.isArray()){
122       return new ArrayConstructorFunction<T>(c);
123     }
124     else{
125       final Constructor JavaDoc<T> ctor = ReflectionUtil.getConstructor(c, suppress_security);
126       return ctor(ctor);
127     }
128   }
129   /**
130    * Create a Function object that uses one public constructor of a class.
131    * The constructor to invoke is determined by the parameter types specified.
132    * <br>
133    * Array type is given a special constructor so that the constructor of
134    * array type T has one parameter of type int
135    * and calls new T[size] when invoked.
136    * @param c the class.
137    * @param param_types the parameter types of the constructor to use.
138    * null indicates the default constructor.
139    * @return the Function object.
140    * @throws IllegalArgumentException
141    * if the public constructor cannot be found.
142    */

143   public static <T> Function<T> ctor(Class JavaDoc<T> c, Class JavaDoc[] param_types){
144     return ctor(c, param_types, false);
145   }
146   /**
147    * Create a Function object that uses one constructor of a class.
148    * The constructor to invoke is determined by the parameter types specified.
149    * <br>
150    * Array type is given a special constructor so that the constructor of
151    * array type T has one parameter of type int
152    * and calls new T[size] when invoked.
153    * @param c the class.
154    * @param param_types the parameter types of the constructor to use.
155    * @param suppress_security whether access to non-public member is allowed.
156    * null indicates the default constructor.
157    * @return the Function object.
158    * @throws IllegalArgumentException
159    * if the public constructor cannot be found.
160    */

161   public static <T> Function<T> ctor(Class JavaDoc<T> c, Class JavaDoc[] param_types, boolean suppress_security)
162   throws IllegalArgumentException JavaDoc{
163     if(c.isArray() && param_types!=null &&
164         param_types.length==1 && int.class.equals(param_types[0])){
165       return new ArrayConstructorFunction<T>(c);
166     }
167     else{
168       final Constructor JavaDoc<T> ctor = ReflectionUtil.getConstructor(c, param_types,
169           suppress_security);
170       return ctor(ctor);
171     }
172   }
173   /**
174    * Create a Function object that invokes a public static method.
175    * This class should have one and only one public static method of this name.
176    * @param c the class where the static method belongs.
177    * @param name the static method name.
178    * @return the Component object.
179    * @throws IllegalArgumentException
180    * thrown if any of the following conditions is true: <br>
181    * 1. this method is not public. <br>
182    * 2. this method is not static. <br>
183    * 3. more than one public static method with the specified name. <br>
184    * 4. no public static method with the specified name. <br>
185    * 5. the class is not public.<br>
186    */

187   public static Function static_method(Class JavaDoc c, String JavaDoc name){
188     return static_method(c, name, false);
189   }
190   /**
191    * Create a Function object that invokes a static method.
192    * This class should have one and only one visible static method of this name.
193    * @param c the class where the static method belongs.
194    * @param name the static method name.
195    * @param suppress_security whether to look at non-public methods.
196    * @return the Component object.
197    * @throws IllegalArgumentException
198    * thrown if any of the following conditions is true: <br>
199    * 1. this method is not visible. <br>
200    * 2. this method is not static. <br>
201    * 3. more than one visible static method with the specified name. <br>
202    * 4. no visible static method with the specified name. <br>
203    * 5. the class is not visible.<br>
204    */

205   public static Function static_method(Class JavaDoc c, final String JavaDoc name, boolean suppress_security){
206     final Method JavaDoc mtd = ReflectionUtil.getStaticMethod(c, name, suppress_security);
207     return method(null, mtd);
208   }
209   /**
210    * Create a Function object that invokes a public static method.
211    * The static method to invoke is determined by the method name
212    * and the parameter types.
213    * @param c the class where the static method belongs.
214    * @param name the static method name.
215    * @param param_types the parameter types.
216    * null indicates a parameter-less method.
217    * @return the Function object.
218    * @throws IllegalArgumentException
219    * thrown if any of the following conditions is true: <br>
220    * 1. this method is not public. <br>
221    * 2. this method is not static. <br>
222    * 3. no public static method with the specified name and the parameter types. <br>
223    * 4. the class is not public. <br>
224    */

225   public static Function static_method(Class JavaDoc c, String JavaDoc name,
226       Class JavaDoc[] param_types){
227     return static_method(c, name, param_types, false);
228   }
229   /**
230    * Create a Function object that invokes a static method.
231    * The static method to invoke is determined by the method name
232    * and the parameter types.
233    * @param c the class where the static method belongs.
234    * @param name the static method name.
235    * @param param_types the parameter types.
236    * null indicates a parameter-less method.
237    * @param suppress_security whether to look at non-public methods.
238    * @return the Function object.
239    * @throws IllegalArgumentException
240    * thrown if any of the following conditions is true: <br>
241    * 1. this method is not visible. <br>
242    * 2. this method is not static. <br>
243    * 3. no visible static method with the specified name and the parameter types. <br>
244    * 4. the class is not visible.<br>
245    */

246   public static Function static_method(Class JavaDoc c, String JavaDoc name,
247       Class JavaDoc[] param_types, boolean suppress_security)
248   throws IllegalArgumentException JavaDoc{
249     final Method JavaDoc mtd = ReflectionUtil.getMethod(c, name, param_types, suppress_security);
250     return static_method(mtd);
251   }
252   /**
253    * Create a Function object that invokes a static method.
254    * The static method to invoke is determined by the method name
255    * and the parameter types.
256    * @param mtd the method.
257    * @return the Function object.
258    * @throws IllegalArgumentException
259    * thrown if this method is not static. <br>
260    */

261   public static Function static_method(Method JavaDoc mtd){
262     if(!Modifier.isStatic(mtd.getModifiers())){
263       throw new IllegalArgumentException JavaDoc(""+mtd
264           + " is not static");
265     }
266     return method(null, mtd);
267   }
268   /**
269    * Create a Function object that invokes a public instance method.
270    * This class should have one and only one public instance method of this name.
271    * <br>
272    * The first parameter of this Function will be the object to
273    * invoke the method against.
274    * @param c the class where the instance method belongs.
275    * @param name the instance method name.
276    * @return the Component object.
277    * @throws IllegalArgumentException
278    * thrown if any of the following conditions is true: <br>
279    * 1. this method is not public. <br>
280    * 2. this method is static. <br>
281    * 3. more than one public instance method with the specified name. <br>
282    * 4. no public instance method with the specified name. <br>
283    * 5. the class is not public. <br>
284    */

285   public static Function instance_method(Class JavaDoc c, String JavaDoc name){
286     return instance_method(c, name, false);
287   }
288   /**
289    * Create a Function object that invokes an instance method.
290    * This class should have one and only one visible instance method of this name.
291    * <br>
292    * The first parameter of this Function will be the object to
293    * invoke the method against.
294    * @param c the class where the instance method belongs.
295    * @param name the instance method name.
296    * @param suppress_security whether to look at non-public methods.
297    * @return the Component object.
298    * @throws IllegalArgumentException
299    * thrown if any of the following conditions is true: <br>
300    * 1. this method is not visible. <br>
301    * 2. this method is static. <br>
302    * 3. more than one visible instance method with the specified name. <br>
303    * 4. no visible instance method with the specified name. <br>
304    * 5. the class is not visible. <br>
305    */

306   public static Function instance_method(Class JavaDoc c, String JavaDoc name,
307       boolean suppress_security)
308   throws IllegalArgumentException JavaDoc{
309     final Method JavaDoc mtd = ReflectionUtil.getInstanceMethod(c, name,
310         suppress_security);
311     return instance_method(c, mtd);
312   }
313   /**
314    * Create a Function object that invokes a public instance method.
315    * The instance method to invoke is determined by the method name
316    * and the parameter types.
317    * <br>
318    * The first parameter of this Function will be the object to
319    * invoke the method against.
320    * @param c the class where the instance method belongs.
321    * @param name the method name.
322    * @param param_types the parameter types.
323    * null indicates a parameter-less method.
324    * @return the Function object.
325    * @throws IllegalArgumentException
326    * thrown if any of the following conditions is true: <br>
327    * 1. this method is not public. <br>
328    * 2. this method is static. <br>
329    * 3. no public instance method with the specified name and the parameter types. <br>
330    */

331   public static Function instance_method(Class JavaDoc c, String JavaDoc name,
332       Class JavaDoc[] param_types){
333     return instance_method(c, name, param_types, false);
334   }
335   /**
336    * Create a Function object that invokes an instance method.
337    * The instance method to invoke is determined by the method name
338    * and the parameter types.
339    * <br>
340    * The first parameter of this Function will be the object to
341    * invoke the method against.
342    * @param c the class where the instance method belongs.
343    * @param name the method name.
344    * @param param_types the parameter types.
345    * @param suppress_security whether to look at non-public ones.
346    * null indicates a parameter-less method.
347    * @return the Function object.
348    * @throws IllegalArgumentException
349    * thrown if any of the following conditions is true: <br>
350    * 1. this method is not visible. <br>
351    * 2. this method is static. <br>
352    * 3. no visible instance method with the specified name and the parameter types. <br>
353    * 4. the class is not visible.<br>
354    */

355   public static Function instance_method(Class JavaDoc c, String JavaDoc name,
356       Class JavaDoc[] param_types, boolean suppress_security)
357   throws IllegalArgumentException JavaDoc{
358     final Method JavaDoc mtd = ReflectionUtil.getMethod(c, name, param_types,
359         suppress_security);
360     return instance_method(c, mtd);
361   }
362   /**
363    * Create a Function object that invokes an instance method.
364    * <br>
365    * The first parameter of this Function will be the object to
366    * invoke the method against.
367    * @param c the class where the instance method belongs.
368    * @param mtd the method.
369    * @return the Function object.
370    * @throws IllegalArgumentException
371    * thrown if this method is static. <br>
372    */

373   public static Function instance_method(Class JavaDoc c, Method JavaDoc mtd){
374     if(Modifier.isStatic(mtd.getModifiers())){
375       throw new IllegalArgumentException JavaDoc(""+mtd
376           +" is static");
377     }
378     return new FloatingMethodFunction(c, mtd);
379   }
380
381
382   /**
383    * Create a Function object that invokes a public method against a given object.
384    * The class of the object
385    * should be public and have one and only one public method of this name.
386    * @param obj the object to run the method against. It cannot be null.
387    * @param name the method name.
388    * @return the Function object.
389    * @throws IllegalArgumentException
390    * thrown if any of the following conditions is true: <br>
391    * 1. this method is not public. <br>
392    * 2. more than one public method with the specified name. <br>
393    * 3. no public method with the specified name. <br>
394    */

395   public static Function method(Object JavaDoc obj, String JavaDoc name){
396     return method(obj, name, false);
397   }
398   /**
399    * Create a Function object that invokes a method against a given object.
400    * The class of the object
401    * should have one and only one visible method of this name.
402    * @param obj the object to run the method against. It cannot be null.
403    * @param name the method name.
404    * @param suppress_security whether to look at non-public ones.
405    * @return the Function object.
406    * @throws IllegalArgumentException
407    * thrown if any of the following conditions is true: <br>
408    * 1. this method is not visible. <br>
409    * 2. more than one public method with the specified name. <br>
410    * 3. no visible method with the specified name. <br>
411    */

412   public static Function method(Object JavaDoc obj, final String JavaDoc name,
413       boolean suppress_security){
414     return method(obj.getClass(), obj, name, suppress_security);
415   }
416   /**
417    * Create a Function object that invokes a public method against a given object.
418    * The class of the object
419    * should have one and only one public method of this name.
420    * @param type the Class object to look up method.
421    * This parameter can be used when we only want to look up in
422    * a super type rather than obj.getClass().
423    * @param obj the object to run the method against.
424    * @param name the method name.
425    * @return the Function object.
426    * @throws IllegalArgumentException
427    * thrown if any of the following conditions is true: <br>
428    * 1. this method is not public. <br>
429    * 2. more than one public method with the specified name. <br>
430    * 3. no public method with the specified name. <br>
431    */

432   public static Function method(Class JavaDoc type, Object JavaDoc obj, String JavaDoc name){
433     return method(type, obj, name, false);
434   }
435   /**
436    * Create a Function object that invokes a method against a given object.
437    * The class of the object
438    * should have one and only one visible method of this name.
439    * @param type the Class object to look up method.
440    * This parameter can be used when we only want to look up in
441    * a super type rather than obj.getClass().
442    * @param obj the object to run the method against.
443    * @param name the method name.
444    * @param suppress_security whether to look at non-public ones.
445    * @return the Function object.
446    * @throws IllegalArgumentException
447    * thrown if any of the following conditions is true: <br>
448    * 1. this method is not public. <br>
449    * 2. more than one public method with the specified name. <br>
450    * 3. no public method with the specified name. <br>
451    */

452   public static Function method(Class JavaDoc type, Object JavaDoc obj, final String JavaDoc name,
453       boolean suppress_security){
454     final Method JavaDoc mtd = ReflectionUtil.getMethod(type, name,
455         suppress_security);
456     return method(obj, mtd);
457   }
458   /**
459    * Create a Function object that invokes a public method against a given object.
460    * The method to invoke is determined by the method name
461    * and the parameter types.
462    * @param obj the object to run the method against. It cannot be null.
463    * @param name the method name.
464    * @param param_types the parameter types.
465    * null indicates a parameter-less method.
466    * @return the Function object.
467    * @throws IllegalArgumentException
468    * thrown if any of the following conditions is true: <br>
469    * 1. this method is not public. <br>
470    * 2. no public method with the specified name and the parameter types. <br>
471    */

472   public static Function method(Object JavaDoc obj, String JavaDoc name, Class JavaDoc[] param_types){
473     return method(obj, name, param_types, false);
474   }
475   /**
476    * Create a Function object that invokes a method against a given object.
477    * The method to invoke is determined by the method name
478    * and the parameter types.
479    * @param obj the object to run the method against. It cannot be null.
480    * @param name the method name.
481    * @param param_types the parameter types.
482    * @param suppress_security whether to look at non-public ones.
483    * null indicates a parameter-less method.
484    * @return the Function object.
485    * @throws IllegalArgumentException
486    * thrown if any of the following conditions is true: <br>
487    * 1. this method is not visible. <br>
488    * 2. no visible method with the specified name and the parameter types. <br>
489    */

490   public static Function method(Object JavaDoc obj,
491       String JavaDoc name, Class JavaDoc[] param_types, boolean suppress_security){
492     return method(obj.getClass(), obj, name, param_types,
493         suppress_security);
494   }
495   /**
496    * Create a Function object that invokes a public method against a given object.
497    * The method to invoke is determined by the method name
498    * and the parameter types.
499    * @param type the Class object to look up method.
500    * This parameter can be used when we only want to look up in
501    * a super type rather than obj.getClass().
502    * @param obj the object to run the method against. It cannot be null.
503    * @param name the method name.
504    * @param param_types the parameter types.
505    * null indicates a parameter-less method.
506    * @return the Function object.
507    * @throws IllegalArgumentException
508    * thrown if any of the following conditions is true: <br>
509    * 1. this method is not public. <br>
510    * 2. no public method with the specified name and the parameter types. <br>
511    */

512   public static Function method(Class JavaDoc type, Object JavaDoc obj,
513       String JavaDoc name, Class JavaDoc[] param_types){
514     return method(type, obj, name, param_types, false);
515   }
516   /**
517    * Create a Function object that invokes a method against a given object.
518    * The method to invoke is determined by the method name
519    * and the parameter types.
520    * @param type the Class object to look up method.
521    * This parameter can be used when we only want to look up in
522    * a super type rather than obj.getClass().
523    * @param obj the object to run the method against. It cannot be null.
524    * @param name the method name.
525    * @param param_types the parameter types.
526    * null indicates a parameter-less method.
527    * @param suppress_security whether to look at non-public ones.
528    * @return the Function object.
529    * @throws IllegalArgumentException
530    * thrown if any of the following conditions is true: <br>
531    * 1. this method is not visible. <br>
532    * 2. no visible method with the specified name and the parameter types. <br>
533    */

534   public static Function method(Class JavaDoc type, Object JavaDoc obj,
535       String JavaDoc name, Class JavaDoc[] param_types, boolean suppress_security){
536     final Method JavaDoc mtd = ReflectionUtil.getMethod(
537         type, name, param_types, suppress_security);
538     return method(obj, mtd);
539   }
540   
541   /**
542    * Creates a Function object that invokes a property getter.
543    * @param bean the bean object.
544    * @param name the property name.
545    * @return the Function object.
546    */

547   public static Function getter(Bean bean, String JavaDoc name){
548     if(bean.getBeanType().getReader(name) == null)
549       throw new IllegalArgumentException JavaDoc("property getter for "+
550         bean.getBeanType().getType().getName()+"."+name+" not found.");
551     return new PropertyReader(bean, name);
552   }
553   /**
554    * Creates a Function object that invokes an indexed property getter.
555    * @param bean the bean object.
556    * @param name the property name.
557    * @param ind the index.
558    * @return the Function object.
559    */

560   public static Function indexed_getter(Bean bean, String JavaDoc name, int ind){
561     if(bean.getBeanType().getIndexedReader(name) == null)
562       throw new IllegalArgumentException JavaDoc("indexed property getter for "
563         +bean.getBeanType().getType().getName()+"."+name+" not found.");
564     return new IndexedPropertyReader(bean, name, ind);
565   }
566   /**
567    * Creates a Function object that invokes a property setter.
568    * @param bean the bean object.
569    * @param name the property name.
570    * @return the Function object.
571    */

572   public static Function setter(Bean bean, String JavaDoc name){
573     final BeanType btype = bean.getBeanType();
574     if(btype.getWriter(name) == null
575         && btype.getIndexedWriter(name)==null){
576       throw new IllegalArgumentException JavaDoc("property setter for "+
577         bean.getBeanType().getType().getName()+"."+name+" not found.");
578     }
579     else
580       return new PropertyWriter(bean, name);
581   }
582   /**
583    * Creates a Function object that invokes an indexed property setter.
584    * @param bean the bean object.
585    * @param name the property name.
586    * @param ind the index.
587    * @return the Function object.
588    */

589   public static Function indexed_setter(Bean bean, String JavaDoc name, int ind){
590     if(bean.getBeanType().getIndexedWriter(name) == null)
591       throw new IllegalArgumentException JavaDoc("indexed property setter for "+
592           bean.getBeanType().getType().getName()+"."+name+" not found.");
593     return new IndexedPropertyWriter(bean, name, ind);
594   }
595   
596   
597   /**
598    * Create a Function object that reads a public static field.
599    * @param c the class where the static field belongs.
600    * @param name the static field name.
601    * @return the Component object.
602    * @throws IllegalArgumentException
603    * thrown if any of the following conditions is true: <br>
604    * 1. field is not found. <br>
605    * 2. field is not static. <br>
606    * 3. field is not public. <br>
607    */

608   public static Function static_field(Class JavaDoc c, String JavaDoc name){
609     return static_field(c, name, false);
610   }
611   /**
612    * Create a Function object that reads a static field.
613    * @param c the class where the static field belongs.
614    * @param name the static field name.
615    * @return the Component object.
616    * @throws IllegalArgumentException
617    * thrown if any of the following conditions is true: <br>
618    * 1. field is not found. <br>
619    * 2. field is not static.
620    */

621   public static Function static_field(Class JavaDoc c, final String JavaDoc name,
622       boolean suppress_security){
623     final Field JavaDoc fld = ReflectionUtil.getStaticField(c, name,
624         suppress_security);
625     return field(null, fld);
626   }
627
628   /**
629    * Create a Function object that reads a static field.
630    * The static field to read is determined by the field name
631    * and the parameter types.
632    * @param fld the field.
633    * @return the Function object.
634    * @throws IllegalArgumentException
635    * thrown if this field is not static. <br>
636    */

637   public static Function static_field(Field JavaDoc fld){
638     if(!Modifier.isStatic(fld.getModifiers())){
639       throw new IllegalArgumentException JavaDoc(""+fld
640           + " is not static");
641     }
642     return field(null, fld);
643   }
644   /**
645    * Create a Function object that reads a public instance field.
646    * <br>
647    * The first parameter of this Function will be the object to
648    * read the field from.
649    * @param c the class where the instance field belongs.
650    * @param name the instance field name.
651    * @return the Component object.
652    * @throws IllegalArgumentException
653    * thrown if any of the following conditions is true: <br>
654    * 1. field is not found. <br>
655    * 2. this field is static. <br>
656    * 3. the field is not public. <br>
657    */

658   public static Function instance_field(Class JavaDoc c, String JavaDoc name){
659     return instance_field(c, name, false);
660   }
661   /**
662    * Create a Function object that reads an instance field.
663    * <br>
664    * The first parameter of this Function will be the object to
665    * read the field from.
666    * @param c the class where the instance field belongs.
667    * @param name the instance field name.
668    * @param suppress_security whether to look at non-public ones.
669    * @return the Component object.
670    * @throws IllegalArgumentException
671    * thrown if any of the following conditions is true: <br>
672    * 1. field is not found. <br>
673    * 2. this field is static. <br>
674    */

675   public static Function instance_field(Class JavaDoc c, String JavaDoc name,
676       boolean suppress_security)
677   throws IllegalArgumentException JavaDoc{
678     final Field JavaDoc fld = ReflectionUtil.getInstanceField(c, name,
679         suppress_security);
680     return instance_field(c, fld);
681   }
682
683   /**
684    * Create a Function object that reads an instance field.
685    * <br>
686    * The first parameter of this Function will be the object to
687    * read the field from.
688    * @param c the class where the instance field belongs.
689    * @param fld the field.
690    * @return the Function object.
691    * @throws IllegalArgumentException
692    * thrown if this field is static. <br>
693    */

694   public static Function instance_field(Class JavaDoc c, Field JavaDoc fld){
695     if(Modifier.isStatic(fld.getModifiers())){
696       throw new IllegalArgumentException JavaDoc(""+fld
697           +" is static");
698     }
699     return new FloatingFieldFunction(c, fld);
700   }
701
702
703   /**
704    * Create a Function object that reads a public field from a given object.
705    * @param obj the object to read the field from. It cannot be null.
706    * @param name the field name.
707    * @return the Function object.
708    * @throws IllegalArgumentException
709    * thrown if any of the following conditions is true: <br>
710    * 1. field is not found.<br>
711    * 2. field is not public.<br>
712    */

713   public static Function field(Object JavaDoc obj, String JavaDoc name){
714     return field(obj, name, false);
715   }
716   /**
717    * Create a Function object that reads a field from a given object.
718    * @param obj the object to read the field from. It cannot be null.
719    * @param name the field name.
720    * @param suppress_security whether to look at non-public ones.
721    * @return the Function object.
722    * @throws IllegalArgumentException
723    * thrown if any of the following conditions is true: <br>
724    * 1. field is not found.
725    */

726   public static Function field(Object JavaDoc obj, final String JavaDoc name,
727       boolean suppress_security){
728     return field(obj.getClass(), obj, name, suppress_security);
729   }
730   /**
731    * Create a Function object that reads a public field from a given object.
732    * @param type the Class object to look up field.
733    * This parameter can be used when we only want to look up in
734    * a super type rather than obj.getClass().
735    * @param obj the object to read field from.
736    * @param name the field name.
737    * @return the Function object.
738    * @throws IllegalArgumentException
739    * thrown if any of the following conditions is true: <br>
740    * 1. field is not found. <br>
741    * 2. field is not public. <br>
742    */

743   public static Function field(Class JavaDoc type, Object JavaDoc obj, String JavaDoc name){
744     return field(type, obj, name, false);
745   }
746   /**
747    * Create a Function object that reads a field from a given object.
748    * @param type the Class object to look up field.
749    * This parameter can be used when we only want to look up in
750    * a super type rather than obj.getClass().
751    * @param obj the object to read field from.
752    * @param name the field name.
753    * @param suppress_security whether to look at non-public ones.
754    * @return the Function object.
755    * @throws IllegalArgumentException
756    * thrown if any of the following conditions is true: <br>
757    * 1. field is not found. <br>
758    */

759   public static Function field(Class JavaDoc type, Object JavaDoc obj, final String JavaDoc name,
760       boolean suppress_security){
761     final Field JavaDoc fld = ReflectionUtil.getField(type, name, suppress_security);
762     return field(obj, fld);
763   }
764   /**
765    * Adapts a {@link java.lang.reflect.Field} object and the receiver object
766    * to a Function object.
767    * @param obj the receiver object to read the field from.
768    * If the field is static, the object can be null.
769    * @param fld the field object.
770    * @return the Function object.
771    */

772   public static Function field(Object JavaDoc obj, Field JavaDoc fld){
773     return new FieldFunction(canonicalize(obj, fld), fld);
774   }
775   
776 }
777
Popular Tags