1 7 package org.jboss.xb.binding.metadata.unmarshalling.impl; 8 9 import org.jboss.xb.binding.metadata.unmarshalling.AttributeBinding; 10 import org.jboss.xb.binding.JBossXBRuntimeException; 11 12 import javax.xml.namespace.QName ; 13 import java.lang.reflect.Field ; 14 import java.lang.reflect.Method ; 15 16 20 public class AttributeBindingImpl 21 implements AttributeBinding 22 { 23 private final QName attributeName; 24 private final Class javaType; 25 private final Field field; 26 private final Method getter; 27 private final Method setter; 28 private final Class fieldType; 29 30 public AttributeBindingImpl(QName attributeName, Class javaType, Class parentClass, String fieldName) 31 { 32 this.attributeName = attributeName; 33 34 Field field = null; 35 Method getter = null; 36 Method setter = null; 37 38 if(fieldName != null) 39 { 40 try 41 { 42 field = parentClass.getField(fieldName); 43 } 44 catch(NoSuchFieldException e) 45 { 46 String baseMethodName = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); 47 try 48 { 49 getter = parentClass.getMethod("get" + baseMethodName, null); 50 setter = parentClass.getMethod("set" + baseMethodName, new Class []{getter.getReturnType()}); 51 } 52 catch(NoSuchMethodException e1) 53 { 54 throw new JBossXBRuntimeException("Failed to bind attribute " + 55 attributeName + 56 ": neither field nor getter/setter were found for field " + 57 fieldName + 58 " in " + 59 parentClass 60 ); 61 } 62 } 63 } 64 65 this.field = field; 66 this.getter = getter; 67 this.setter = setter; 68 69 fieldType = field == null ? (getter == null ? null : getter.getReturnType()) : field.getType(); 70 if(fieldType == null) 71 { 72 throw new JBossXBRuntimeException("Failed to bind attribute " + 73 attributeName + 74 " to field " + 75 fieldName + 76 " in " + 77 parentClass + 78 ": failed to resolve field's type." 79 ); 80 } 81 82 this.javaType = javaType == null ? fieldType : javaType; 83 } 84 85 public QName getAttributeName() 86 { 87 return attributeName; 88 } 89 90 public Class getJavaType() 91 { 92 return javaType; 93 } 94 95 public Field getField() 96 { 97 return field; 98 } 99 100 public Method getGetter() 101 { 102 return getter; 103 } 104 105 public Method getSetter() 106 { 107 return setter; 108 } 109 110 public Class getFieldType() 111 { 112 return fieldType; 113 } 114 } 115 | Popular Tags |