KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > system > webservice > utils > HolderUtils


1 /*
2  * Copyright 2005 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  * $Header:$
17  */

18 package org.apache.beehive.controls.system.webservice.utils;
19
20 import java.lang.reflect.Field JavaDoc;
21
22 import javax.xml.rpc.holders.Holder JavaDoc;
23
24 public class HolderUtils {
25
26     public static void stuffHolderValue(Holder JavaDoc holder, Object JavaDoc value)
27             throws NoSuchFieldException JavaDoc, IllegalAccessException JavaDoc {
28
29         Field JavaDoc valueField = holder.getClass().getField("value");
30         Object JavaDoc curValueInHolder = valueField.get(holder);
31         Class JavaDoc classOfValueFieldInHolder;
32         if (curValueInHolder == null) {
33             classOfValueFieldInHolder = valueField.getType();
34         } else {
35             classOfValueFieldInHolder = curValueInHolder.getClass();
36         }
37
38         if (null == value) {
39             setFieldInObject(valueField, holder, null);
40             return;
41         }
42         if (classOfValueFieldInHolder.isAssignableFrom(value.getClass())) {
43             setFieldInObject(valueField, holder, value);
44
45         } else { // we need to convert the class to the holder type
46

47             try {
48                 Object JavaDoc convertedValue = org.apache.axis.utils.JavaUtils
49                         .convert(value, classOfValueFieldInHolder);
50                 setFieldInObject(valueField, holder, convertedValue);
51             } catch (IllegalArgumentException JavaDoc e) {
52
53                 e.printStackTrace();
54             } catch (IllegalAccessException JavaDoc e) {
55
56                 e.printStackTrace();
57             }
58         }
59
60     }
61
62     private static void setFieldInObject(Field JavaDoc valueField, Object JavaDoc destObject,
63             Object JavaDoc value) throws IllegalArgumentException JavaDoc,
64             IllegalAccessException JavaDoc {
65         if (valueField.getType().isPrimitive()) {
66             if (value == null)
67                 ; // Don't need to set anything
68
else
69                 valueField.set(destObject, value);
70         } else {
71             valueField.set(destObject, value);
72         }
73     }
74
75 }
76
Popular Tags