KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > utils > FieldPropertyDescriptor


1 /*
2  * Copyright 2001-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 package org.apache.axis.utils;
18
19 import java.beans.PropertyDescriptor JavaDoc;
20 import java.lang.reflect.Array JavaDoc;
21 import java.lang.reflect.Field JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23
24
25 /**
26  *
27  * @author Glen Daniels (gdaniels@apache.org)
28  */

29 public class FieldPropertyDescriptor extends BeanPropertyDescriptor {
30     private Field JavaDoc field = null;
31
32     /**
33       * Construct a BPD with a field
34       * Both must be set
35       * @param _name is the name of the property
36       * @param _field is the name of the public instance field
37       */

38      public FieldPropertyDescriptor(String JavaDoc _name,
39                                    Field JavaDoc _field) {
40          field = _field;
41          try {
42              myPD = new PropertyDescriptor JavaDoc(_name, null, null);
43          } catch (Exception JavaDoc e) {
44              // ???
45
}
46          if (_field == null || _name == null) {
47              throw new IllegalArgumentException JavaDoc(
48                      Messages.getMessage(_field == null ?
49                                           "badField00" : "badProp03"));
50          }
51      }
52
53     public String JavaDoc getName() {
54         return field.getName();
55     }
56
57     /**
58      * Query if property is readable
59      * @return true if readable
60      */

61     public boolean isReadable() {
62         return true;
63     }
64
65     /**
66      * Query if property is writeable
67      * @return true if writeable
68      */

69     public boolean isWriteable() {
70         return true;
71     }
72
73     /**
74      * Query if property is indexed.
75      * Indexed properties require valid setters/getters
76      * @return true if indexed methods exist
77      */

78     public boolean isIndexed() {
79         return (field.getType().getComponentType() != null);
80     }
81
82     /**
83      * Get the property value
84      * @param obj is the object
85      * @return the entire propery value
86      */

87     public Object JavaDoc get(Object JavaDoc obj)
88             throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc {
89         return field.get(obj);
90     }
91
92     /**
93      * Set the property value
94      * @param obj is the object
95      * @param newValue is the new value
96      */

97     public void set(Object JavaDoc obj, Object JavaDoc newValue)
98             throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc {
99         field.set(obj, newValue);
100     }
101
102     /**
103      * Get an indexed property
104      * @param obj is the object
105      * @param i the index
106      * @return the object at the indicated index
107      */

108     public Object JavaDoc get(Object JavaDoc obj, int i)
109             throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc {
110         if (!isIndexed()) {
111             throw new IllegalAccessException JavaDoc("Not an indexed property");
112         }
113
114         Object JavaDoc array = field.get(obj);
115         return Array.get(array, i);
116     }
117
118     /**
119      * Set an indexed property value
120      * @param obj is the object
121      * @param i the index
122      * @param newValue is the new value
123      */

124     public void set(Object JavaDoc obj, int i, Object JavaDoc newValue)
125             throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc {
126         if (!isIndexed()) {
127             throw new IllegalAccessException JavaDoc("Not an indexed field!");
128         }
129         Class JavaDoc componentType = field.getType().getComponentType();
130         growArrayToSize(obj, componentType, i);
131         Array.set(get(obj), i, newValue);
132     }
133
134     /**
135      * Get the type of a property
136      * @return the type of the property
137      */

138     public Class JavaDoc getType() {
139         if (isIndexed()) {
140             return field.getType().getComponentType();
141         } else {
142             return field.getType();
143         }
144     }
145
146     public Class JavaDoc getActualType() {
147         return field.getType();
148     }
149
150     public Field JavaDoc getField() {
151         return field;
152     }
153 }
154
Popular Tags