KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > introspection > ClassInfo


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.xb.binding.introspection;
23
24 import java.beans.BeanInfo JavaDoc;
25 import java.beans.IntrospectionException JavaDoc;
26 import java.beans.PropertyDescriptor JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.Map JavaDoc;
29 import org.jboss.xb.binding.JBossXBRuntimeException;
30 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
31
32 /**
33  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
34  * @version <tt>$Revision: $</tt>
35  */

36 public class ClassInfo
37 {
38    private static final Object JavaDoc FIELD_INFO_NA = new Object JavaDoc();
39    private final Class JavaDoc type;
40    private Map JavaDoc fields = new ConcurrentHashMap();
41    private boolean introspected;
42
43    public ClassInfo(Class JavaDoc cls)
44    {
45       this.type = cls;
46    }
47
48    public Class JavaDoc getType()
49    {
50       return type;
51    }
52
53    /**
54     * @param name the name of the field
55     * @param required if true never returns null (if the FieldInfo is not available, an exception will be thrown).
56     * If false and FieldInfo is not available then null will be returned.
57     * @return an instance of FieldInfo or null
58     */

59    public FieldInfo getFieldInfo(String JavaDoc name, boolean required)
60    {
61       Object JavaDoc o = fields.get(name);
62       if(o == null)
63       {
64          FieldInfo fieldInfo = FieldInfo.getFieldInfo(this, name);
65          if(fieldInfo == null)
66          {
67             fields.put(name, FIELD_INFO_NA);
68          }
69          else
70          {
71             return fieldInfo;
72          }
73       }
74       else if(o != FIELD_INFO_NA)
75       {
76          return (FieldInfo)o;
77       }
78
79       if(required)
80       {
81          throw new JBossXBRuntimeException(
82             "Failed to find read method or field for property '" + name + "' in " + type
83          );
84       }
85
86       return null;
87    }
88
89    void addFieldInfo(FieldInfo fieldInfo)
90    {
91       fields.put(fieldInfo.getName(), fieldInfo);
92    }
93
94    FieldInfo introspect(String JavaDoc name)
95    {
96       if(introspected)
97       {
98          return null;
99       }
100
101       try
102       {
103          BeanInfo JavaDoc info = java.beans.Introspector.getBeanInfo(type);
104          PropertyDescriptor JavaDoc[] props = info.getPropertyDescriptors();
105          if(props != null)
106          {
107             for(int i = 0; i < props.length; ++i)
108             {
109                PropertyDescriptor JavaDoc prop = props[i];
110                Method JavaDoc readMethod = prop.getReadMethod();
111                // todo: there are issues with null readMethod, e.g. scale in BigDecimal...
112
if(readMethod != null)
113                {
114                   Method JavaDoc writeMethod = prop.getWriteMethod();
115                   FieldInfo fieldInfo = new FieldInfo(type, prop.getName(), readMethod, writeMethod);
116                   addFieldInfo(fieldInfo);
117                }
118             }
119          }
120       }
121       catch(IntrospectionException JavaDoc e)
122       {
123       }
124
125       introspected = true;
126       return getFieldInfo(name, false);
127    }
128 }
129
Popular Tags