KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > jaxb > WrapperHelper


1 package org.objectweb.celtix.jaxb;
2
3
4 import java.lang.reflect.Field JavaDoc;
5 import java.lang.reflect.InvocationTargetException JavaDoc;
6 import java.lang.reflect.Method JavaDoc;
7 import java.util.Collection JavaDoc;
8 import java.util.List JavaDoc;
9
10 import javax.xml.bind.annotation.XmlElement;
11
12
13 public final class WrapperHelper {
14
15     private WrapperHelper() {
16         //complete
17
}
18
19
20     public static void setWrappedPart(String JavaDoc partName, Object JavaDoc wrapperType, Object JavaDoc part)
21         throws IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc {
22         
23         if (part instanceof List JavaDoc) {
24             setWrappedListProperty(partName, wrapperType, part);
25         } else {
26             String JavaDoc fieldName = partName;
27             if (JAXBUtils.isJavaKeyword(partName)) {
28                 fieldName = JAXBUtils.nameToIdentifier(
29                                 partName,
30                                JAXBUtils.IdentifierType.VARIABLE);
31             }
32             
33             XmlElement el = null;
34             for (Field JavaDoc field : wrapperType.getClass().getDeclaredFields()) {
35                 if (field.getName().equals(fieldName)) {
36                     //JAXB Type get XmlElement Annotation
37
el = field.getAnnotation(XmlElement.class);
38                     assert el != null;
39                 }
40             }
41             
42             if (part == null) {
43                 if (!el.nillable()) {
44                     throw new IllegalArgumentException JavaDoc("null value for field not permitted.");
45                 }
46                 return;
47             }
48
49             String JavaDoc modifier =
50                 JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.SETTER);
51             
52             boolean setInvoked = false;
53             for (Method JavaDoc method : wrapperType.getClass().getMethods()) {
54                 if (method.getParameterTypes() != null
55                     && method.getParameterTypes().length == 1
56                     && modifier.equals(method.getName())) {
57                     
58                     Class JavaDoc<?> clazz = method.getParameterTypes()[0];
59                     if (method.getParameterTypes()[0].isPrimitive()) {
60                         clazz = el.type();
61                     }
62                     
63                     if (clazz.isAssignableFrom(part.getClass())) {
64                         method.invoke(wrapperType, part);
65                         setInvoked = true;
66                         break;
67                     }
68                 }
69             }
70             
71             if (!setInvoked) {
72                 throw new IllegalArgumentException JavaDoc("Could not find a modifier method on Wrapper Type");
73             }
74         }
75     }
76
77     private static void setWrappedListProperty(String JavaDoc partName, Object JavaDoc wrapperType, Object JavaDoc part)
78         throws IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc {
79
80         String JavaDoc accessorName = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.GETTER);
81         for (Method JavaDoc method : wrapperType.getClass().getMethods()) {
82             if (accessorName.equals(method.getName())
83                 && List JavaDoc.class.isAssignableFrom(method.getReturnType())) {
84                 
85                 Object JavaDoc ret = method.invoke(wrapperType);
86                 Method JavaDoc addAll = ret.getClass().getMethod("addAll", Collection JavaDoc.class);
87                 addAll.invoke(ret, part);
88                 break;
89             }
90         }
91     }
92     
93     public static Object JavaDoc getWrappedPart(String JavaDoc partName, Object JavaDoc wrapperType, Class JavaDoc<?> partClazz)
94         throws IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc {
95
96         String JavaDoc accessor = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.GETTER);
97         
98         if (partClazz.equals(boolean.class) || partClazz.equals(Boolean JavaDoc.class)) {
99             //JAXB Exception to get the Boolean property
100
accessor = accessor.replaceFirst("get", "is");
101         }
102         
103         for (Method JavaDoc method : wrapperType.getClass().getMethods()) {
104             if (method.getParameterTypes().length == 0
105                 && accessor.equals(method.getName())) {
106                 
107                 Class JavaDoc<?> clazz = method.getReturnType();
108
109                 if (clazz.isPrimitive() && !partClazz.isPrimitive()) {
110                     for (Field JavaDoc field : wrapperType.getClass().getDeclaredFields()) {
111                         if (JAXBUtils.isJavaKeyword(partName)) {
112                             partName = JAXBUtils.nameToIdentifier(
113                                             partName,
114                                            JAXBUtils.IdentifierType.VARIABLE);
115                         }
116                         if (field.getName().equals(partName)) {
117                             //JAXB Type get XmlElement Annotation
118
clazz = field.getAnnotation(XmlElement.class).type();
119                         }
120                     }
121                 }
122                 //TODO Support For Nillable Attribute
123
if (clazz.isAssignableFrom(partClazz)) {
124                     return method.invoke(wrapperType);
125                 }
126             }
127         }
128         return null;
129     }
130 }
131
Popular Tags