KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xml > utils > synthetic > reflection > Field


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: Field.java,v 1.6 2004/02/17 04:24:21 minchau Exp $
18  */

19 package org.apache.xml.utils.synthetic.reflection;
20
21 import org.apache.xml.utils.synthetic.Class;
22 import org.apache.xml.utils.synthetic.SynthesisException;
23
24 /**
25  * A Field provides information about, and dynamic access
26  * to, a single field of a class or an interface. The reflected
27  * field may be a class (static) field or an instance field.
28  * <p>
29  * A Field permits widening conversions to occur during a
30  * get or set access operation, but throws an
31  * IllegalArgumentException if a narrowing conversion
32  * would occur.
33  *
34  * @xsl.usage internal
35  */

36 public class Field extends Object JavaDoc implements Member
37 {
38
39   /** Field name, initializer */
40   public String JavaDoc name, initializer = null;
41
42   /** Field modifiers */
43   int modifiers;
44
45   /** Field realfield */
46   java.lang.reflect.Field JavaDoc realfield = null;
47
48   /** Field declaringClass, type */
49   Class JavaDoc declaringClass, type;
50
51   /**
52    * Proxy constructor
53    *
54    * @param realfield
55    * @param declaringClass
56    */

57   public Field(java.lang.reflect.Field JavaDoc realfield,
58                org.apache.xml.utils.synthetic.Class declaringClass)
59   {
60
61     this(realfield.getName(), declaringClass);
62
63     this.realfield = realfield;
64     this.type =
65       org.apache.xml.utils.synthetic.Class.forClass(realfield.getType());
66   }
67
68   /**
69    * Synthesis constructor
70    *
71    * @param name
72    * @param declaringClass
73    */

74   public Field(String JavaDoc name,
75                org.apache.xml.utils.synthetic.Class declaringClass)
76   {
77     this.name = name;
78     this.declaringClass = declaringClass;
79   }
80
81   /**
82    * Compares this Field against the specified object.
83    * Returns true if the objects are the same. Two
84    * Fields are the same if they were declared by the
85    * same class and have the same name and type.
86    *
87    * @param obj
88    *
89    */

90   public boolean equals(Object JavaDoc obj)
91   {
92
93     if (realfield != null)
94       return realfield.equals(obj);
95     else if (obj instanceof Field)
96     {
97       Field objf = (Field) obj;
98
99       return (declaringClass.equals(objf.declaringClass)
100               && name.equals(objf.name) && type.equals(objf.type));
101     }
102     else
103       return false;
104   }
105
106   /**
107    * Returns the value of the field represented by this
108    * Field, on the specified object. The value is
109    * automatically wrapped in an object if it has a
110    * primitive type.
111    * <p>
112    * The underlying field's value is obtained as follows:
113    * <p>
114    * If the underlying field is a static field, the object
115    * argument is ignored; it may be null.
116    * <p>
117    * Otherwise, the underlying field is an instance
118    * field. If the specified object argument is null, the
119    * method throws a NullPointerException. If the
120    * specified object is not an instance of the class or
121    * interface declaring the underlying field, the
122    * method throws an IllegalArgumentException.
123    * <p>
124    * If this Field object enforces Java language access
125    * control, and the underlying field is inaccessible,
126    * the method throws an IllegalAccessException.
127    * <p>
128    * Otherwise, the value is retrieved from the
129    * underlying instance or static field. If the field has a
130    * primitive type, the value is wrapped in an object
131    * before being returned, otherwise it is returned as
132    * is.
133    *
134    *
135    * @param obj
136    *
137    * @throws IllegalAccessException
138    * if the underlying constructor is inaccessible.
139    * @throws IllegalArgumentException
140    * if the specified object is not an instance of
141    * the class or interface declaring the
142    * underlying field.
143    * @throws NullPointerException
144    * if the specified object is null.
145    */

146   public Object JavaDoc get(Object JavaDoc obj)
147           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
148   {
149
150     if (realfield != null)
151       return realfield.get(obj);
152
153     throw new java.lang.IllegalStateException JavaDoc();
154   }
155
156   /**
157    * Get the value of a field as a boolean on specified
158    * object.
159    *
160    *
161    * @param obj
162    *
163    * @throws IllegalAccessException
164    * if the underlying constructor is inaccessible.
165    * @throws IllegalArgumentException
166    * if the field value cannot be converted to the
167    * return type by a widening conversion.
168    */

169   public boolean getBoolean(Object JavaDoc obj)
170           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
171   {
172
173     if (realfield != null)
174       return realfield.getBoolean(obj);
175
176     throw new java.lang.IllegalStateException JavaDoc();
177   }
178
179   /**
180    * Get the value of a field as a byte on specified
181    * object.
182    *
183    *
184    * @param obj
185    *
186    * @throws IllegalAccessException
187    * if the underlying constructor is inaccessible.
188    * @throws IllegalArgumentException
189    * if the field value cannot be converted to the
190    * return type by a widening conversion.
191    */

192   public byte getByte(Object JavaDoc obj)
193           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
194   {
195
196     if (realfield != null)
197       return realfield.getByte(obj);
198
199     throw new java.lang.IllegalStateException JavaDoc();
200   }
201
202   /**
203    * Get the value of a field as a char on specified
204    * object.
205    *
206    *
207    * @param obj
208    *
209    * @throws IllegalAccessException
210    * if the underlying constructor is inaccessible.
211    * @throws IllegalArgumentException
212    * if the field value cannot be converted to the
213    * return type by a widening conversion.
214    */

215   public char getChar(Object JavaDoc obj)
216           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
217   {
218
219     if (realfield != null)
220       return realfield.getChar(obj);
221
222     throw new java.lang.IllegalStateException JavaDoc();
223   }
224
225   /**
226    * Returns the Class object representing the class or
227    * interface that declares the field represented by this
228    * Field object.
229    *
230    */

231   public org.apache.xml.utils.synthetic.Class getDeclaringClass()
232   {
233
234     if (realfield != null)
235       return org.apache.xml.utils.synthetic.Class.forClass(
236         realfield.getDeclaringClass());
237
238     throw new java.lang.IllegalStateException JavaDoc();
239   }
240
241   /**
242    * Get the value of a field as a double on specified
243    * object.
244    *
245    *
246    * @param obj
247    *
248    * @throws IllegalAccessException
249    * if the underlying constructor is inaccessible.
250    * @throws IllegalArgumentException
251    * if the field value cannot be converted to the
252    * return type by a widening conversion.
253    */

254   public double getDouble(Object JavaDoc obj)
255           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
256   {
257
258     if (realfield != null)
259       return realfield.getDouble(obj);
260
261     throw new java.lang.IllegalStateException JavaDoc();
262   }
263
264   /**
265    * Get the value of a field as a float on specified
266    * object.
267    *
268    *
269    * @param obj
270    *
271    * @throws IllegalAccessException
272    * if the underlying constructor is inaccessible.
273    * @throws IllegalArgumentException
274    * if the field value cannot be converted to the
275    * return type by a widening conversion.
276    */

277   public float getFloat(Object JavaDoc obj)
278           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
279   {
280
281     if (realfield != null)
282       return realfield.getFloat(obj);
283
284     throw new java.lang.IllegalStateException JavaDoc();
285   }
286
287   /**
288    * Get the value of a field as a int on specified object.
289    *
290    *
291    * @param obj
292    *
293    * @throws IllegalAccessException
294    * if the underlying constructor is inaccessible.
295    * @throws IllegalArgumentException
296    * if the field value cannot be converted to the
297    * return type by a widening conversion.
298    */

299   public int getInt(Object JavaDoc obj)
300           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
301   {
302
303     if (realfield != null)
304       return realfield.getInt(obj);
305
306     throw new java.lang.IllegalStateException JavaDoc();
307   }
308
309   /**
310    * Get the value of a field as a long on specified
311    * object.
312    *
313    *
314    * @param obj
315    *
316    * @throws IllegalAccessException
317    * if the underlying constructor is inaccessible.
318    * @throws IllegalArgumentException
319    * if the field value cannot be converted to the
320    * return type by a widening conversion.
321    */

322   public long getLong(Object JavaDoc obj)
323           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
324   {
325
326     if (realfield != null)
327       return realfield.getLong(obj);
328
329     throw new java.lang.IllegalStateException JavaDoc();
330   }
331
332   /**
333    * Returns the Java language modifiers for the field
334    * represented by this Field object, as an integer. The
335    * Modifier class should be used to decode the
336    * modifiers.
337    *
338    */

339   public int getModifiers()
340   {
341
342     if (realfield != null)
343       modifiers = realfield.getModifiers();
344
345     return modifiers;
346   }
347
348   /**
349    * Method getInitializer
350    *
351    *
352    * (getInitializer) @return
353    */

354   public String JavaDoc getInitializer()
355   {
356     return initializer;
357   }
358
359   /**
360    * Method setInitializer
361    *
362    *
363    * @param i
364    *
365    * @throws SynthesisException
366    */

367   public void setInitializer(String JavaDoc i) throws SynthesisException
368   {
369
370     if (realfield != null)
371       throw new SynthesisException(SynthesisException.REIFIED);
372
373     initializer = i;
374   }
375
376   /**
377    * Insert the method's description here.
378    * Creation date: (12-25-99 2:02:26 PM)
379    * @return java.lang.String
380    */

381   public java.lang.String JavaDoc getName()
382   {
383     return name;
384   }
385
386   /**
387    * Get the value of a field as a short on specified
388    * object.
389    *
390    *
391    * @param obj
392    *
393    * @throws IllegalAccessException
394    * if the underlying constructor is inaccessible.
395    * @throws IllegalArgumentException
396    * if the field value cannot be converted to the
397    * return type by a widening conversion.
398    */

399   public short getShort(Object JavaDoc obj)
400           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
401   {
402
403     if (realfield != null)
404       return realfield.getShort(obj);
405
406     throw new java.lang.IllegalStateException JavaDoc();
407   }
408
409   /**
410    * Returns a Class object that identifies the declared
411    * type for the field represented by this Field object.
412    *
413    */

414   public Class JavaDoc getType()
415   {
416
417     if (realfield != null)
418       type = Class.forClass(realfield.getType());
419
420     return type;
421   }
422
423   /**
424    * Method setType
425    *
426    *
427    * @param type
428    *
429    * @throws SynthesisException
430    */

431   public void setType(org.apache.xml.utils.synthetic.Class type)
432           throws SynthesisException
433   {
434
435     if (realfield != null)
436       throw new SynthesisException(SynthesisException.REIFIED);
437
438     this.type = type;
439   }
440
441   /**
442    * Returns a hashcode for this Field. This is
443    * computed as the exclusive-or of the hashcodes for
444    * the underlying field's declaring class name and its
445    * name.
446    *
447    */

448   public int hashCode()
449   {
450
451     if (realfield != null)
452       return realfield.hashCode();
453     else
454       return declaringClass.getName().hashCode() ^ name.hashCode();
455   }
456
457   /**
458    * Sets the field represented by this Field object on
459    * the specified object argument to the specified new
460    * value. The new value is automatically unwrapped
461    * if the underlying field has a primitive type.
462    *
463    * The operation proceeds as follows:
464    *
465    * If the underlying field is static, the object
466    * argument is ignored; it may be null.
467    *
468    * Otherwise the underlying field is an instance field.
469    * If the specified object argument is null, the
470    * method throws a NullPointerException. If the
471    * specified object argument is not an instance of the
472    * class or interface declaring the underlying field,
473    * the method throws an IllegalArgumentException.
474    *
475    * If this Field object enforces Java language access
476    * control, and the underlying field is inaccessible,
477    * the method throws an IllegalAccessException.
478    *
479    * If the underlying field is final, the method throws
480    * an IllegalAccessException.
481    *
482    * If the underlying field is of a primitive type, an
483    * unwrapping conversion is attempted to convert the
484    * new value to a value of a primitive type. If this
485    * attempt fails, the method throws an
486    * IllegalArgumentException.
487    *
488    * If, after possible unwrapping, the new value
489    * cannot be converted to the type of the underlying
490    * field by an identity or widening conversion, the
491    * method throws an IllegalArgumentException.
492    *
493    * The field is set to the possibly unwrapped and
494    * widened new value.
495    *
496    *
497    * @param obj
498    * @param value
499    * @throws IllegalAccessException
500    * if the underlying constructor is inaccessible.
501    * @throws IllegalArgumentException
502    * if the specified object is not an instance of
503    * the class or interface declaring the
504    * underlying field, or if an unwrapping
505    * conversion fails.
506    * @throws NullPointerException
507    * if the specified object is null.
508    */

509   public void set(Object JavaDoc obj, Object JavaDoc value)
510           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
511   {
512
513     if (realfield != null)
514       realfield.set(obj, value);
515
516     throw new java.lang.IllegalStateException JavaDoc();
517   }
518
519   /**
520    * Set the value of a field as a boolean on specified
521    * object.
522    *
523    *
524    * @param obj
525    * @param z
526    * @throws IllegalAccessException
527    * if the underlying constructor is inaccessible.
528    * @throws IllegalArgumentException
529    * if the specified object is not an instance of
530    * the class or interface declaring the
531    * underlying field, or if an unwrapping
532    * conversion fails.
533    */

534   public void setBoolean(Object JavaDoc obj, boolean z)
535           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
536   {
537
538     if (realfield != null)
539       realfield.setBoolean(obj, z);
540
541     throw new java.lang.IllegalStateException JavaDoc();
542   }
543
544   /**
545    * Set the value of a field as a byte on specified
546    * object.
547    *
548    *
549    * @param obj
550    * @param b
551    * @throws IllegalAccessException
552    * if the underlying constructor is inaccessible.
553    * @throws IllegalArgumentException
554    * if the specified object is not an instance of
555    * the class or interface declaring the
556    * underlying field, or if an unwrapping
557    * conversion fails.
558    */

559   public void setByte(Object JavaDoc obj, byte b)
560           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
561   {
562
563     if (realfield != null)
564       realfield.setByte(obj, b);
565
566     throw new java.lang.IllegalStateException JavaDoc();
567   }
568
569   /**
570    * Set the value of a field as a char on specified
571    * object.
572    *
573    *
574    * @param obj
575    * @param c
576    * @throws IllegalAccessException
577    * if the underlying constructor is inaccessible.
578    * @throws IllegalArgumentException
579    * if the specified object is not an instance of
580    * the class or interface declaring the
581    * underlying field, or if an unwrapping
582    * conversion fails.
583    */

584   public void setChar(Object JavaDoc obj, char c)
585           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
586   {
587
588     if (realfield != null)
589       realfield.setChar(obj, c);
590
591     throw new java.lang.IllegalStateException JavaDoc();
592   }
593
594   /**
595    * Returns the Class object representing the class that
596    * declares the constructor represented by this
597    * Constructor object.
598    *
599    * @param declaringClass
600    */

601   public void setDeclaringClass(
602           org.apache.xml.utils.synthetic.Class declaringClass)
603   {
604     this.declaringClass = declaringClass;
605   }
606
607   /**
608    * Set the value of a field as a double on specified
609    * object.
610    *
611    *
612    * @param obj
613    * @param d
614    * @throws IllegalAccessException
615    * if the underlying constructor is inaccessible.
616    * @throws IllegalArgumentException
617    * if the specified object is not an instance of
618    * the class or interface declaring the
619    * underlying field, or if an unwrapping
620    * conversion fails.
621    */

622   public void setDouble(Object JavaDoc obj, double d)
623           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
624   {
625
626     if (realfield != null)
627       realfield.setDouble(obj, d);
628
629     throw new java.lang.IllegalStateException JavaDoc();
630   }
631
632   /**
633    * Set the value of a field as a float on specified
634    * object.
635    *
636    *
637    * @param obj
638    * @param f
639    * @throws IllegalAccessException
640    * if the underlying constructor is inaccessible.
641    * @throws IllegalArgumentException
642    * if the specified object is not an instance of
643    * the class or interface declaring the
644    * underlying field, or if an unwrapping
645    * conversion fails.
646    */

647   public void setFloat(Object JavaDoc obj, float f)
648           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
649   {
650
651     if (realfield != null)
652       realfield.setFloat(obj, f);
653
654     throw new java.lang.IllegalStateException JavaDoc();
655   }
656
657   /**
658    * Set the value of a field as an int on specified
659    * object.
660    *
661    *
662    * @param obj
663    * @param i
664    * @throws IllegalAccessException
665    * if the underlying constructor is inaccessible.
666    * @throws IllegalArgumentException
667    * if the specified object is not an instance of
668    * the class or interface declaring the
669    * underlying field, or if an unwrapping
670    * conversion fails.
671    */

672   public void setInt(Object JavaDoc obj, int i)
673           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
674   {
675
676     if (realfield != null)
677       realfield.setInt(obj, i);
678
679     throw new java.lang.IllegalStateException JavaDoc();
680   }
681
682   /**
683    * Set the value of a field as a long on specified
684    * object.
685    *
686    *
687    * @param obj
688    * @param l
689    * @throws IllegalAccessException
690    * if the underlying constructor is inaccessible.
691    * @throws IllegalArgumentException
692    * if the specified object is not an instance of
693    * the class or interface declaring the
694    * underlying field, or if an unwrapping
695    * conversion fails.
696    */

697   public void setLong(Object JavaDoc obj, long l)
698           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
699   {
700
701     if (realfield != null)
702       realfield.setLong(obj, l);
703
704     throw new java.lang.IllegalStateException JavaDoc();
705   }
706
707   /**
708    * Insert the method's description here.
709    * Creation date: (12-25-99 1:28:28 PM)
710    * @return int
711    * @param modifiers int
712    *
713    * @throws SynthesisException
714    */

715   public void setModifiers(int modifiers) throws SynthesisException
716   {
717
718     if (realfield != null)
719       throw new SynthesisException(SynthesisException.REIFIED);
720
721     this.modifiers = modifiers;
722   }
723
724   /**
725    * Set the value of a field as a short on specified
726    * object.
727    *
728    *
729    * @param obj
730    * @param s
731    * @throws IllegalAccessException
732    * if the underlying constructor is inaccessible.
733    * @throws IllegalArgumentException
734    * if the specified object is not an instance of
735    * the class or interface declaring the
736    * underlying field, or if an unwrapping
737    * conversion fails.
738    */

739   public void setShort(Object JavaDoc obj, short s)
740           throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc
741   {
742
743     if (realfield != null)
744       realfield.setShort(obj, s);
745
746     throw new java.lang.IllegalStateException JavaDoc();
747   }
748
749   /**
750    * Return a string describing this Field. The format is
751    * the access modifiers for the field, if any, followed
752    * by the field type, followed by a space, followed by
753    * the fully-qualified name of the class declaring the
754    * field, followed by a period, followed by the name
755    * of the field. For example:
756    * <code>
757    * public static final int java.lang.Thread.MIN_PRIORITY
758    * private int java.io.FileDescriptor.fd
759    * </code>
760    *
761    * The modifiers are placed in canonical order as
762    * specified by "The Java Language Specification".
763    * This is public, protected or private first,
764    * and then other modifiers in the following order:
765    * static, final, transient, volatile.
766    *
767    */

768   public String JavaDoc toString()
769   {
770
771     if (realfield != null)
772       return realfield.toString();
773
774     throw new java.lang.IllegalStateException JavaDoc();
775   }
776
777   /**
778    * Output the Field as Java sourcecode
779    *
780    */

781   public String JavaDoc toSource()
782   {
783
784     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(
785       java.lang.reflect.Modifier.toString(getModifiers())).append(' ').append(
786       getType().getJavaName()).append(' ').append(getName());
787     String JavaDoc i = getInitializer();
788
789     if (i != null && i.length() > 0)
790       sb.append('=').append(i);
791
792     sb.append(';');
793
794     return sb.toString();
795   }
796 }
797
Popular Tags