1 29 30 package com.caucho.jaxb.skeleton; 31 32 import com.caucho.jaxb.JAXBContextImpl; 33 import com.caucho.util.L10N; 34 35 import javax.xml.bind.JAXBException; 36 import javax.xml.bind.annotation.XmlAttribute; 37 import javax.xml.bind.annotation.XmlElement; 38 import javax.xml.bind.annotation.XmlType; 39 import javax.xml.namespace.QName ; 40 import java.beans.PropertyDescriptor ; 41 import java.lang.annotation.Annotation ; 42 import java.lang.reflect.Field ; 43 import java.lang.reflect.GenericArrayType ; 44 import java.lang.reflect.Method ; 45 import java.lang.reflect.ParameterizedType ; 46 import java.lang.reflect.Type ; 47 import java.util.Map ; 48 49 public class FieldAccessor extends Accessor { 50 private Field _field; 51 private Class _type; 52 private Type _genericType; 53 private String _name; 54 55 public FieldAccessor(Field f, JAXBContextImpl context) 56 throws JAXBException 57 { 58 super(context); 59 60 _field = f; 61 _type = _field.getType(); 62 _genericType = _field.getGenericType(); 63 _name = _field.getName(); 64 _property = _context.createProperty(_genericType); 65 66 if (_field.isAnnotationPresent(XmlElement.class)) { 67 XmlElement element = _field.getAnnotation(XmlElement.class); 68 69 if (! "##default".equals(element.name())) 70 _name = element.name(); 71 } 72 else if (_field.isAnnotationPresent(XmlAttribute.class)) { 73 XmlAttribute attribute = _field.getAnnotation(XmlAttribute.class); 74 75 if (! "##default".equals(attribute.name())) 76 _name = attribute.name(); 77 } 78 } 79 80 public Object get(Object o) 81 throws JAXBException 82 { 83 try { 84 return _field.get(o); 85 } 86 catch (Exception e) { 87 throw new JAXBException(e); 88 } 89 } 90 91 public void set(Object o, Object value) 92 throws JAXBException 93 { 94 try { 95 _field.set(o, value); 96 } 97 catch (Exception e) { 98 throw new JAXBException(e); 99 } 100 } 101 102 public <A extends Annotation > A getAnnotation(Class <A> c) 103 { 104 return _field.getAnnotation(c); 105 } 106 107 public Class getType() 108 { 109 return _type; 110 } 111 112 public Type getGenericType() 113 { 114 return _genericType; 115 } 116 117 public String getName() 118 { 119 return _name; 120 } 121 } 122 | Popular Tags |