KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > metadata > unmarshalling > impl > AttributeBindingImpl


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

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 JavaDoc;
13 import java.lang.reflect.Field JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15
16 /**
17  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
18  * @version <tt>$Revision: 1.1.2.1 $</tt>
19  */

20 public class AttributeBindingImpl
21    implements AttributeBinding
22 {
23    private final QName JavaDoc attributeName;
24    private final Class JavaDoc javaType;
25    private final Field JavaDoc field;
26    private final Method JavaDoc getter;
27    private final Method JavaDoc setter;
28    private final Class JavaDoc fieldType;
29
30    public AttributeBindingImpl(QName JavaDoc attributeName, Class JavaDoc javaType, Class JavaDoc parentClass, String JavaDoc fieldName)
31    {
32       this.attributeName = attributeName;
33
34       Field JavaDoc field = null;
35       Method JavaDoc getter = null;
36       Method JavaDoc setter = null;
37
38       if(fieldName != null)
39       {
40          try
41          {
42             field = parentClass.getField(fieldName);
43          }
44          catch(NoSuchFieldException JavaDoc e)
45          {
46             String JavaDoc 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 JavaDoc[]{getter.getReturnType()});
51             }
52             catch(NoSuchMethodException JavaDoc 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 JavaDoc getAttributeName()
86    {
87       return attributeName;
88    }
89
90    public Class JavaDoc getJavaType()
91    {
92       return javaType;
93    }
94
95    public Field JavaDoc getField()
96    {
97       return field;
98    }
99
100    public Method JavaDoc getGetter()
101    {
102       return getter;
103    }
104
105    public Method JavaDoc getSetter()
106    {
107       return setter;
108    }
109
110    public Class JavaDoc getFieldType()
111    {
112       return fieldType;
113    }
114 }
115
Popular Tags