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 GetterSetterAccessor extends Accessor { 50 private Method _get; 51 private Method _set; 52 private Class _type; 53 private Type _genericType; 54 private String _name; 55 56 private PropertyDescriptor _propertyDescriptor; 57 58 public GetterSetterAccessor(PropertyDescriptor propertyDescriptor, 59 JAXBContextImpl context) 60 throws JAXBException 61 { 62 super(context); 63 64 _propertyDescriptor = propertyDescriptor; 65 66 _get = _propertyDescriptor.getReadMethod(); 67 _set = _propertyDescriptor.getWriteMethod(); 68 _name = _propertyDescriptor.getName(); 69 _genericType = _get.getGenericReturnType(); 70 _property = _context.createProperty(_genericType); 71 72 if ("clazz".equals(_name)) 73 _name = "class"; 74 } 75 76 public Object get(Object o) 77 throws JAXBException 78 { 79 try { 80 if (_get == null) 81 return null; 82 83 return _get.invoke(o); 84 } 85 catch (Exception e) { 86 throw new JAXBException(e); 87 } 88 } 89 90 public void set(Object o, Object value) 91 throws JAXBException 92 { 93 try { 94 if (_set == null) 95 return; 96 97 _set.invoke(o, value); 98 } 99 catch (Exception e) { 100 throw new JAXBException(e); 101 } 102 } 103 104 public <A extends Annotation > A getAnnotation(Class <A> c) 105 { 106 A a = null; 107 108 if (_get != null) 109 a = _get.getAnnotation(c); 110 else if (_set != null) 111 a = _set.getAnnotation(c); 112 113 return a; 114 } 115 116 public Class getType() 117 { 118 return _propertyDescriptor.getPropertyType(); 119 } 120 121 public Type getGenericType() 122 { 123 return getType(); 124 } 125 126 public String getName() 127 { 128 return _name; 129 } 130 } 131 | Popular Tags |