KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > config > jaxb > JaxbBeanType


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.config.jaxb;
31
32 import com.caucho.config.*;
33 import com.caucho.config.j2ee.InjectIntrospector;
34 import com.caucho.loader.Environment;
35 import com.caucho.loader.EnvironmentListener;
36 import com.caucho.loader.WeakDestroyListener;
37 import com.caucho.util.L10N;
38 import com.caucho.xml.QName;
39 import com.caucho.xml.QNode;
40
41 import org.w3c.dom.Node JavaDoc;
42
43 import javax.annotation.PostConstruct;
44 import javax.annotation.PreDestroy;
45 import javax.xml.bind.annotation.*;
46 import javax.xml.bind.annotation.adapters.XmlAdapter;
47 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
48 import java.beans.Introspector JavaDoc;
49 import java.lang.annotation.Annotation JavaDoc;
50 import java.lang.reflect.Field JavaDoc;
51 import java.lang.reflect.Method JavaDoc;
52 import java.lang.reflect.Modifier JavaDoc;
53 import java.lang.reflect.ParameterizedType JavaDoc;
54 import java.lang.reflect.Type JavaDoc;
55 import java.util.ArrayList JavaDoc;
56 import java.util.Collection JavaDoc;
57 import java.util.HashMap JavaDoc;
58 import java.util.Map JavaDoc;
59
60 public class JaxbBeanType extends TypeStrategy {
61   protected static final L10N L = new L10N(BeanTypeStrategy.class);
62
63   private static final HashMap JavaDoc<Class JavaDoc,PrimType> _primTypes
64     = new HashMap JavaDoc<Class JavaDoc,PrimType>();
65
66   private ArrayList JavaDoc<BuilderProgram> _injectList
67     = new ArrayList JavaDoc<BuilderProgram>();
68
69   private ArrayList JavaDoc<Method JavaDoc> _postConstructList
70     = new ArrayList JavaDoc<Method JavaDoc>();
71   
72   private ArrayList JavaDoc<Method JavaDoc> _preDestroyList
73     = new ArrayList JavaDoc<Method JavaDoc>();
74
75   private HashMap JavaDoc<String JavaDoc,AttributeStrategy> _attributeMap
76     = new HashMap JavaDoc<String JavaDoc,AttributeStrategy>();
77
78   private AttributeStrategy _valueAttr;
79   
80   private final Class JavaDoc<?> _type;
81
82   public JaxbBeanType(Class JavaDoc<?> type)
83   {
84     _type = type;
85
86     introspect();
87   }
88
89   /**
90    * Returns the type class.
91    */

92   public Class JavaDoc getType()
93   {
94     return _type;
95   }
96
97   /**
98    * Returns the type name.
99    */

100   public String JavaDoc getTypeName()
101   {
102     return getType().getName();
103   }
104
105   /**
106    * Returns the type's configured value
107    *
108    * @param builder the context builder
109    * @param node the configuration node
110    * @param parent
111    */

112   public Object JavaDoc configure(NodeBuilder builder, Node node, Object JavaDoc parent)
113     throws Exception JavaDoc
114   {
115     Object JavaDoc bean = builder.evalELObject(node);
116
117     if (bean != null)
118       return bean;
119       
120     bean = _type.newInstance();
121     
122     configureBean(builder, bean, node);
123
124     return bean;
125   }
126
127   /**
128    * Configures the bean
129    *
130    * @param builder the context builder
131    * @param bean the bean to be configured
132    * @param top the configuration node
133    */

134   public void configureBean(NodeBuilder builder, Object JavaDoc bean, Node top)
135     throws Exception JavaDoc
136   {
137     for (int i = 0; i < _injectList.size(); i++) {
138       try {
139     _injectList.get(i).configureImpl(builder, bean);
140       } catch (Throwable JavaDoc e) {
141     throw builder.error(e, top);
142       }
143     }
144     
145     if (_valueAttr != null) {
146       _valueAttr.configure(builder, bean, ((QNode) top).getQName(), top);
147
148       builder.configureBeanAttributesImpl(this, bean, top);
149     }
150     else
151       builder.configureBeanImpl(this, bean, top);
152     
153     for (int i = 0; i < _postConstructList.size(); i++) {
154       try {
155     _postConstructList.get(i).invoke(bean);
156       } catch (Throwable JavaDoc e) {
157     throw builder.error(e, top);
158       }
159     }
160     
161     for (int i = 0; i < _preDestroyList.size(); i++) {
162       try {
163     Method JavaDoc method = _preDestroyList.get(i);
164     EnvironmentListener listener;
165     listener = new WeakDestroyListener(method, bean);
166     
167     Environment.addEnvironmentListener(listener);
168       } catch (Throwable JavaDoc e) {
169     throw builder.error(e, top);
170       }
171     }
172   }
173
174   /**
175    * Returns the appropriate strategy for the bean.
176    *
177    * @param attrName
178    * @return the strategy
179    * @throws ConfigException
180    */

181   @Override JavaDoc
182   public AttributeStrategy getAttributeStrategy(QName attrName)
183   {
184     AttributeStrategy strategy = _attributeMap.get(attrName.getName());
185
186     if (strategy != null)
187       return strategy;
188
189     strategy = _attributeMap.get(attrName.getLocalName());
190     
191     return strategy;
192   }
193
194   private void introspect()
195   {
196     XmlAccessorType accessorType
197       = _type.getPackage().getAnnotation(XmlAccessorType.class);
198
199     if (_type.isAnnotationPresent(XmlAccessorType.class))
200       accessorType = _type.getAnnotation(XmlAccessorType.class);
201
202     XmlAccessType accessType;
203
204     if (accessorType != null)
205       accessType = accessorType.value();
206     else
207       accessType = XmlAccessType.PUBLIC_MEMBER;
208
209
210     InjectIntrospector.configureClassResources(_injectList, _type);
211     
212     introspectMethods(accessType);
213     introspectFields(accessType);
214   }
215
216   private void introspectMethods(XmlAccessType accessType)
217   {
218     Method JavaDoc []methods = _type.getDeclaredMethods();
219
220     for (int i = 0; i < methods.length; i++) {
221       Method JavaDoc getter = methods[i];
222       String JavaDoc name = getter.getName();
223       String JavaDoc setterName;
224       String JavaDoc propName;
225
226       if (getter.isAnnotationPresent(PostConstruct.class)) {
227     if (getter.getParameterTypes().length != 0)
228       throw new ConfigException(L.l("@PostConstruct method must have zero arguments."));
229
230
231     _postConstructList.add(getter);
232       }
233       
234       if (getter.isAnnotationPresent(PreDestroy.class)) {
235     if (getter.getParameterTypes().length != 0)
236       throw new ConfigException(L.l("@PreDestroy method must have zero arguments."));
237
238
239     _preDestroyList.add(getter);
240       }
241
242       Class JavaDoc retType = getter.getReturnType();
243       if (void.class.equals(retType))
244     continue;
245
246       Class JavaDoc []paramTypes = getter.getParameterTypes();
247       if (paramTypes.length != 0)
248     continue;
249
250       if (name.startsWith("get")) {
251     propName = name.substring(3);
252     setterName = "set" + propName;
253
254     propName = Introspector.decapitalize(propName);
255       }
256       else if (name.startsWith("is")) {
257     propName = name.substring(2);
258     setterName = "set" + propName;
259     
260     if (! boolean.class.equals(retType))
261       continue;
262     
263     propName = Introspector.decapitalize(propName);
264       }
265       else
266     continue;
267       
268       Class JavaDoc<?> propType = retType;
269       Type JavaDoc genericType = getter.getGenericReturnType();
270
271       Method JavaDoc setter = findSetter(_type, setterName, retType);
272       if (setter != null) {
273     InjectIntrospector.configure(_injectList,
274                      setter,
275                      propName,
276                      retType);
277       }
278       else if (! Collection JavaDoc.class.isAssignableFrom(retType) &&
279            ! Map JavaDoc.class.isAssignableFrom(retType))
280     continue;
281
282       if (hasAnnotation(XmlTransient.class, getter, setter))
283     continue;
284
285       XmlJavaTypeAdapter xmlAdapt
286     = getAnnotation(XmlJavaTypeAdapter.class, getter, setter);
287
288       Adapter adapter = null;
289
290       if (xmlAdapt != null)
291     adapter = new Adapter(xmlAdapt.value());
292
293       XmlValue xmlValue
294     = getAnnotation(XmlValue.class, getter, setter);
295
296       XmlElements xmlElements
297     = getAnnotation(XmlElements.class, getter, setter);
298
299       XmlElement xmlElement
300     = getAnnotation(XmlElement.class, getter, setter);
301
302       XmlElementWrapper eltWrapper
303     = getAnnotation(XmlElementWrapper.class, getter, setter);
304       
305       XmlAttribute xmlAttribute
306     = getAnnotation(XmlAttribute.class, getter, setter);
307
308       name = Introspector.decapitalize(name.substring(3));
309
310       if (xmlElements != null) {
311       }
312       else if (xmlElement != null) {
313     if (! "##default".equals(xmlElement.name()))
314       name = xmlElement.name();
315       }
316       else if (xmlAttribute != null) {
317     if (! "##default".equals(xmlAttribute.name()))
318       name = xmlAttribute.name();
319       }
320       else if (eltWrapper != null
321            || xmlValue != null
322            || xmlAdapt != null) {
323       }
324       else if (accessType == XmlAccessType.PROPERTY) {
325       }
326       else if (accessType == XmlAccessType.PUBLIC_MEMBER
327            && Modifier.isPublic(getter.getModifiers())) {
328       }
329       else
330     continue;
331
332       Class JavaDoc<?> valueType = null;
333
334       if (Collection JavaDoc.class.isAssignableFrom(propType)) {
335     Class JavaDoc componentType = getCollectionComponent(genericType);
336
337     HashMap JavaDoc<String JavaDoc,AttributeStrategy> attrMap = _attributeMap;
338
339     if (eltWrapper != null) {
340       String JavaDoc wrapperName = name;
341
342       if (! "##default".equals(eltWrapper.name()))
343         wrapperName = eltWrapper.name();
344
345       CollectionWrapperProperty wrapperAttr
346         = new CollectionWrapperProperty();
347
348       _attributeMap.put(wrapperName, wrapperAttr);
349
350       attrMap = wrapperAttr.getAttributeMap();
351     }
352
353     if (xmlElements != null) {
354       for (XmlElement elt : xmlElements.value()) {
355         String JavaDoc eltName = name;
356         Class JavaDoc<?> eltType = componentType;
357       
358         if (! "##default".equals(elt.name()))
359           eltName = elt.name();
360
361         if (elt.type() != XmlElement.DEFAULT.class)
362           eltType = elt.type();
363         
364         TypeStrategy typeMarshal = createTypeMarshal(eltType,
365                              adapter);
366
367         AttributeStrategy attr;
368         attr = new CollectionProperty(typeMarshal, getter);
369
370         attrMap.put(eltName, attr);
371       }
372     }
373     else {
374       TypeStrategy typeMarshal = createTypeMarshal(componentType,
375                                adapter);
376
377       AttributeStrategy attr;
378       attr = new CollectionProperty(typeMarshal, getter);
379
380       attrMap.put(name, attr);
381     }
382       }
383       else if (xmlValue != null) {
384     _valueAttr = createPropertyAttribute(getter, setter,
385                          valueType, adapter);
386       }
387       else if (xmlElements != null) {
388     for (XmlElement elt : xmlElements.value()) {
389       String JavaDoc eltName = name;
390       Class JavaDoc<?> eltType = valueType;
391       
392       if (! "##default".equals(elt.name()))
393         eltName = elt.name();
394
395       if (elt.type() != XmlElement.DEFAULT.class)
396         eltType = elt.type();
397       
398       AttributeStrategy attr = createPropertyAttribute(getter,
399                                setter,
400                                eltType,
401                                adapter);
402
403       if (attr != null)
404         _attributeMap.put(eltName, attr);
405     }
406       }
407       else {
408     AttributeStrategy attr = createPropertyAttribute(getter,
409                              setter,
410                              valueType,
411                              adapter);
412
413     if (attr != null)
414       _attributeMap.put(name, attr);
415       }
416     }
417   }
418
419   private Method JavaDoc findSetter(Class JavaDoc type,
420                 String JavaDoc setName,
421                 Class JavaDoc retType)
422   {
423     if (type == null || Object JavaDoc.class.equals(type))
424       return null;
425     
426     Method JavaDoc []methods = type.getDeclaredMethods();
427
428     for (int i = 0; i < methods.length; i++) {
429       Method JavaDoc method = methods[i];
430
431       if (method.getParameterTypes().length != 1)
432     continue;
433       else if (! method.getParameterTypes()[0].equals(retType))
434     continue;
435       else if (! void.class.equals(method.getReturnType()))
436     continue;
437       else if (setName.equals(method.getName()))
438     return method;
439     }
440
441     return findSetter(type.getSuperclass(), setName, retType);
442   }
443
444   private void introspectFields(XmlAccessType accessType)
445   {
446     Field JavaDoc []fields = _type.getDeclaredFields();
447
448     for (int i = 0; i < fields.length; i++) {
449       Field JavaDoc field = fields[i];
450       String JavaDoc name = field.getName();
451       
452       InjectIntrospector.configure(_injectList,
453                    field,
454                    name,
455                    field.getType());
456
457       if (field.isAnnotationPresent(XmlTransient.class))
458     continue;
459       if (Modifier.isTransient(field.getModifiers()))
460     continue;
461
462       XmlJavaTypeAdapter xmlAdapt
463     = field.getAnnotation(XmlJavaTypeAdapter.class);
464
465       Adapter adapter = null;
466
467       if (xmlAdapt != null)
468     adapter = new Adapter(xmlAdapt.value());
469       
470       XmlValue xmlValue = field.getAnnotation(XmlValue.class);
471       XmlElements xmlElements = field.getAnnotation(XmlElements.class);
472       XmlElement xmlElement = field.getAnnotation(XmlElement.class);
473       XmlAttribute xmlAttribute = field.getAnnotation(XmlAttribute.class);
474       XmlElementWrapper eltWrapper
475     = field.getAnnotation(XmlElementWrapper.class);
476
477       if (xmlElements != null) {
478       }
479       else if (xmlElement != null) {
480     if (! "##default".equals(xmlElement.name()))
481       name = xmlElement.name();
482       }
483       else if (xmlAttribute != null) {
484     if (! "##default".equals(xmlAttribute.name()))
485       name = xmlAttribute.name();
486       }
487       else if (eltWrapper != null
488            || xmlValue != null
489            || xmlAdapt != null) {
490       }
491       else if (accessType == XmlAccessType.FIELD) {
492       }
493       else if (accessType == XmlAccessType.PUBLIC_MEMBER
494            && Modifier.isPublic(field.getModifiers())) {
495       }
496       else
497     continue;
498
499       Class JavaDoc<?> fieldType = field.getType();
500       Class JavaDoc<?> valueType = null;
501
502       if (Collection JavaDoc.class.isAssignableFrom(fieldType)) {
503     Class JavaDoc componentType
504       = getCollectionComponent(field.getGenericType());
505
506     HashMap JavaDoc<String JavaDoc,AttributeStrategy> attrMap = _attributeMap;
507
508     if (eltWrapper != null) {
509       String JavaDoc wrapperName = name;
510
511       if (! "##default".equals(eltWrapper.name()))
512         wrapperName = eltWrapper.name();
513
514       CollectionWrapperProperty wrapperAttr
515         = new CollectionWrapperProperty();
516
517       _attributeMap.put(wrapperName, wrapperAttr);
518
519       attrMap = wrapperAttr.getAttributeMap();
520     }
521
522     if (xmlElements != null) {
523       for (XmlElement elt : xmlElements.value()) {
524         String JavaDoc eltName = name;
525         Class JavaDoc<?> eltType;
526       
527         if (! "##default".equals(elt.name()))
528           eltName = elt.name();
529
530         if (elt.type() != XmlElement.DEFAULT.class)
531           eltType = elt.type();
532         else
533           eltType = componentType;
534         
535         TypeStrategy typeMarshal = createTypeMarshal(eltType,
536                              adapter);
537
538         AttributeStrategy attr;
539         attr = new CollectionField(typeMarshal, field);
540
541         attrMap.put(eltName, attr);
542       }
543     }
544     else {
545       TypeStrategy typeMarshal = createTypeMarshal(componentType,
546                                adapter);
547
548       AttributeStrategy attr;
549       attr = new CollectionField(typeMarshal, field);
550
551       attrMap.put(name, attr);
552     }
553       }
554       else if (xmlValue != null) {
555     _valueAttr = createFieldAttribute(field, valueType, adapter);
556       }
557       else if (xmlElements != null) {
558     for (XmlElement elt : xmlElements.value()) {
559       String JavaDoc eltName = name;
560       Class JavaDoc<?> eltType = valueType;
561       
562       if (! "##default".equals(elt.name()))
563         eltName = elt.name();
564
565       if (elt.type() != XmlElement.DEFAULT.class)
566         eltType = elt.type();
567       
568       AttributeStrategy attr = createFieldAttribute(field,
569                             eltType,
570                             adapter);
571
572       if (attr != null)
573         _attributeMap.put(eltName, attr);
574     }
575       }
576       else {
577     AttributeStrategy attr = createFieldAttribute(field,
578                               valueType,
579                               adapter);
580
581     if (attr != null)
582       _attributeMap.put(name, attr);
583       }
584     }
585   }
586
587   private AttributeStrategy createPropertyAttribute(Method JavaDoc getter,
588                             Method JavaDoc setter,
589                             Class JavaDoc valueType,
590                             Adapter adapter)
591   {
592     setter.setAccessible(true);
593
594     Class JavaDoc type = getter.getReturnType();
595     
596     if (adapter != null) {
597       if (! type.isAssignableFrom(adapter.getBoundClass())) {
598     throw new ConfigException(L.l("Can't assign XmlAdapter {0} to property {1}",
599                       adapter.getBoundClass(),
600                       type));
601       }
602       
603       TypeStrategy typeMarshal;
604
605       if (valueType != null)
606     typeMarshal = createTypeMarshal(valueType, null);
607       else
608     typeMarshal = createTypeMarshal(adapter.getValueClass(), null);
609
610       AttributeStrategy attr = new AdapterProperty(setter,
611                            typeMarshal,
612                            adapter.getAdapter());
613
614       return attr;
615     }
616     
617     PrimType primType = _primTypes.get(type);
618
619     if (primType != null) {
620       switch (primType) {
621       case BOOLEAN:
622       case BOOLEAN_OBJECT:
623     return new BooleanProperty(setter);
624     
625       case BYTE:
626       case BYTE_OBJECT:
627     return new ByteProperty(setter);
628     
629       case SHORT:
630       case SHORT_OBJECT:
631     return new ShortProperty(setter);
632     
633       case INTEGER:
634       case INTEGER_OBJECT:
635     return new IntProperty(setter);
636     
637       case LONG:
638       case LONG_OBJECT:
639     return new LongProperty(setter);
640     
641       case FLOAT:
642       case FLOAT_OBJECT:
643     return new FloatProperty(setter);
644     
645       case DOUBLE:
646       case DOUBLE_OBJECT:
647     return new DoubleProperty(setter);
648     
649       case STRING:
650     return new StringProperty(setter);
651
652       default:
653     throw new UnsupportedOperationException JavaDoc(primType.toString());
654       }
655     }
656
657     if (Collection JavaDoc.class.isAssignableFrom(type)) {
658       Type JavaDoc retType = getter.getGenericReturnType();
659       Class JavaDoc componentType = getCollectionComponent(retType);
660       
661       TypeStrategy typeMarshal = createTypeMarshal(componentType, null);
662
663       return new CollectionProperty(typeMarshal, getter);
664     }
665
666     return new BeanProperty(setter, type);
667   }
668
669   private static Class JavaDoc getCollectionComponent(Type JavaDoc type)
670   {
671     Class JavaDoc componentType = Object JavaDoc.class;
672
673     if (type instanceof ParameterizedType JavaDoc) {
674       ParameterizedType JavaDoc pType = (ParameterizedType JavaDoc) type;
675       Type JavaDoc []typeArgs = pType.getActualTypeArguments();
676
677       if (typeArgs.length > 0) {
678     if (typeArgs[0] instanceof Class JavaDoc)
679       componentType = (Class JavaDoc) typeArgs[0];
680     else if (typeArgs[0] instanceof ParameterizedType JavaDoc)
681       componentType = (Class JavaDoc) ((ParameterizedType JavaDoc) typeArgs[0]).getRawType();
682       }
683     }
684     
685     return componentType;
686   }
687
688   private TypeStrategy createTypeMarshal(Class JavaDoc valueType, Adapter adapter)
689   {
690     if (adapter != null) {
691       TypeStrategy valueStrategy = createTypeMarshal(valueType, null);
692
693       return new AdapterType(valueStrategy, adapter.getAdapter());
694     }
695     
696     PrimType primType = _primTypes.get(valueType);
697
698     if (primType != null) {
699       switch (primType) {
700       case BOOLEAN:
701       case BOOLEAN_OBJECT:
702     return BooleanType.TYPE;
703     
704       case BYTE:
705       case BYTE_OBJECT:
706     return ByteType.TYPE;
707     
708       case SHORT:
709       case SHORT_OBJECT:
710     return ShortType.TYPE;
711     
712       case INTEGER:
713       case INTEGER_OBJECT:
714     return IntegerType.TYPE;
715     
716       case LONG:
717       case LONG_OBJECT:
718     return LongType.TYPE;
719     
720       case FLOAT:
721       case FLOAT_OBJECT:
722     return FloatType.TYPE;
723     
724       case DOUBLE:
725       case DOUBLE_OBJECT:
726     return DoubleType.TYPE;
727     
728       case STRING:
729     return StringType.TYPE;
730       }
731     }
732
733     try {
734       return TypeStrategyFactory.getTypeStrategy(valueType);
735     } catch (Exception JavaDoc e) {
736       throw new ConfigException(e);
737     }
738   }
739
740   private static <X extends Annotation JavaDoc> X getAnnotation(Class JavaDoc<X> annType,
741                             Method JavaDoc getter,
742                             Method JavaDoc setter)
743   {
744     X ann = setter != null ? setter.getAnnotation(annType) : null;
745
746     if (ann != null)
747       return ann;
748
749     return getter.getAnnotation(annType);
750   }
751
752   private static boolean hasAnnotation(Class JavaDoc<? extends Annotation JavaDoc> annType,
753                        Method JavaDoc getter,
754                        Method JavaDoc setter)
755   {
756     return (getter != null && getter.isAnnotationPresent(annType)
757         || setter != null && setter.isAnnotationPresent(annType));
758   }
759
760   private AttributeStrategy createFieldAttribute(Field JavaDoc field,
761                          Class JavaDoc valueType,
762                          Adapter adapter)
763   {
764     field.setAccessible(true);
765     
766     if (adapter != null) {
767       if (! field.getType().isAssignableFrom(adapter.getBoundClass())) {
768     throw new ConfigException(L.l("Can't assign XmlAdapter {0} to field {1}",
769                       adapter.getBoundClass(),
770                       field.getType()));
771       }
772       
773       TypeStrategy typeMarshal;
774
775       if (valueType != null)
776     typeMarshal = createTypeMarshal(valueType, null);
777       else
778     typeMarshal = createTypeMarshal(adapter.getValueClass(), null);
779
780       AttributeStrategy attr = new AdapterField(field,
781                         typeMarshal,
782                         adapter.getAdapter());
783
784       return attr;
785     }
786
787     if (valueType == null)
788       valueType = field.getType();
789     
790     PrimType primType = _primTypes.get(valueType);
791
792     if (primType != null) {
793       switch (primType) {
794       case BOOLEAN:
795       case BOOLEAN_OBJECT:
796     return new BooleanField(field);
797     
798       case BYTE:
799       case BYTE_OBJECT:
800     return new ByteField(field);
801     
802       case SHORT:
803       case SHORT_OBJECT:
804     return new ShortField(field);
805     
806       case INTEGER:
807       case INTEGER_OBJECT:
808     return new IntField(field);
809     
810       case LONG:
811       case LONG_OBJECT:
812     return new LongField(field);
813     
814       case FLOAT:
815       case FLOAT_OBJECT:
816     return new FloatField(field);
817     
818       case DOUBLE:
819       case DOUBLE_OBJECT:
820     return new DoubleField(field);
821     
822       case STRING:
823     return new StringField(field);
824
825       default:
826     throw new UnsupportedOperationException JavaDoc(primType.toString());
827       }
828     }
829
830     return new BeanField(field, valueType);
831   }
832
833   public String JavaDoc toString()
834   {
835     return "JaxbBeanType[" + _type.getName() + "]";
836   }
837
838   static class Adapter {
839     private final Class JavaDoc<? extends XmlAdapter> _adapterClass;
840     private Class JavaDoc<?> _boundClass;
841     private Class JavaDoc<?> _valueClass;
842     
843     private XmlAdapter _adapter;
844
845     Adapter(Class JavaDoc<? extends XmlAdapter> adapterClass)
846     {
847       _adapterClass = adapterClass;
848
849       introspect(adapterClass.getGenericSuperclass());
850
851       try {
852     _adapter = _adapterClass.newInstance();
853       } catch (Exception JavaDoc e) {
854     throw new ConfigException(e);
855       }
856     }
857
858     Class JavaDoc<?> getValueClass()
859     {
860       return _valueClass;
861     }
862
863     Class JavaDoc<?> getBoundClass()
864     {
865       return _boundClass;
866     }
867
868     XmlAdapter getAdapter()
869     {
870       return _adapter;
871     }
872
873     private void introspect(Type JavaDoc type)
874     {
875       if (type == null)
876     throw new NullPointerException JavaDoc();
877
878       if (type instanceof ParameterizedType JavaDoc) {
879     ParameterizedType JavaDoc pType = (ParameterizedType JavaDoc) type;
880
881     if (XmlAdapter.class.equals(pType.getRawType())) {
882       Type JavaDoc []args = pType.getActualTypeArguments();
883       
884       _valueClass = (Class JavaDoc<?>) args[0];
885       _boundClass = (Class JavaDoc<?>) args[1];
886
887       return;
888     }
889     else
890       introspect(((Class JavaDoc) pType.getRawType()).getGenericSuperclass());
891       }
892       else if (type instanceof Class JavaDoc)
893     introspect(((Class JavaDoc) type).getGenericSuperclass());
894     }
895   }
896
897   enum PrimType {
898     BOOLEAN,
899     BOOLEAN_OBJECT,
900     
901     BYTE,
902     BYTE_OBJECT,
903     
904     SHORT,
905     SHORT_OBJECT,
906     
907     INTEGER,
908     INTEGER_OBJECT,
909     
910     LONG,
911     LONG_OBJECT,
912     
913     FLOAT,
914     FLOAT_OBJECT,
915     
916     DOUBLE,
917     DOUBLE_OBJECT,
918     
919     STRING,
920   };
921
922   static {
923     _primTypes.put(boolean.class, PrimType.BOOLEAN);
924     _primTypes.put(Boolean JavaDoc.class, PrimType.BOOLEAN_OBJECT);
925     
926     _primTypes.put(byte.class, PrimType.BYTE);
927     _primTypes.put(Byte JavaDoc.class, PrimType.BYTE_OBJECT);
928     
929     _primTypes.put(short.class, PrimType.SHORT);
930     _primTypes.put(Short JavaDoc.class, PrimType.SHORT_OBJECT);
931     
932     _primTypes.put(int.class, PrimType.INTEGER);
933     _primTypes.put(Integer JavaDoc.class, PrimType.INTEGER_OBJECT);
934     
935     _primTypes.put(long.class, PrimType.LONG);
936     _primTypes.put(Long JavaDoc.class, PrimType.LONG_OBJECT);
937     
938     _primTypes.put(float.class, PrimType.FLOAT);
939     _primTypes.put(Float JavaDoc.class, PrimType.FLOAT_OBJECT);
940     
941     _primTypes.put(double.class, PrimType.DOUBLE);
942     _primTypes.put(Double JavaDoc.class, PrimType.DOUBLE_OBJECT);
943     
944     _primTypes.put(String JavaDoc.class, PrimType.STRING);
945   }
946 }
947
Popular Tags