View Javadoc

1   /*
2    * Copyright (c) 2010 Carman Consulting, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.metastopheles;
18  
19  import java.beans.BeanDescriptor;
20  import java.io.Serializable;
21  import java.lang.reflect.AnnotatedElement;
22  import java.lang.reflect.Method;
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.TreeSet;
27  
28  /**
29   * @author James Carman
30   * @since 1.0
31   */
32  public class BeanMetaData extends MetaDataObject
33  {
34  //**********************************************************************************************************************
35  // Fields
36  //**********************************************************************************************************************
37  
38      private static final long serialVersionUID = 1L;
39      private final BeanDescriptor beanDescriptor;
40      private final Map<String, PropertyMetaData> propertyMetaDataMap = new HashMap<String,PropertyMetaData>();
41      private final Map<Method,MethodMetaData> methodMethodMetaDataMap = new HashMap<Method,MethodMetaData>();
42      private final String factoryId;
43  
44  //**********************************************************************************************************************
45  // Constructors
46  //**********************************************************************************************************************
47  
48      BeanMetaData(String factoryId, BeanDescriptor beanDescriptor)
49      {
50          this.factoryId = factoryId;
51          this.beanDescriptor = beanDescriptor;
52      }
53  
54  //**********************************************************************************************************************
55  // Getter/Setter Methods
56  //**********************************************************************************************************************
57  
58      public BeanDescriptor getBeanDescriptor()
59      {
60          return beanDescriptor;
61      }
62  
63  //**********************************************************************************************************************
64  // Other Methods
65  //**********************************************************************************************************************
66  
67      @Override
68      protected AnnotatedElement getDefaultAnnotationSource()
69      {
70          return beanDescriptor.getBeanClass();
71      }
72  
73      void addMethodMetaData(MethodMetaData methodMetaData)
74      {
75          methodMethodMetaDataMap.put(methodMetaData.getMethodDescriptor().getMethod(), methodMetaData);
76      }
77  
78      void addPropertyMetaData(PropertyMetaData propertyMetaData)
79      {
80          propertyMetaDataMap.put(propertyMetaData.getPropertyDescriptor().getName(), propertyMetaData);
81      }
82  
83      protected Object writeReplace()
84      {
85          return new SerializedForm(factoryId, beanDescriptor.getBeanClass());
86      }
87  
88      public MethodMetaData getMethodMetaData(String methodName, Class<?>... parameterTypes)
89      {
90          try
91          {
92              return methodMethodMetaDataMap.get(getBeanDescriptor().getBeanClass().getMethod(methodName, parameterTypes));
93          }
94          catch (NoSuchMethodException e)
95          {
96              throw new IllegalArgumentException("Method " + methodName + " not found on class " + getBeanDescriptor().getBeanClass().getName() + ".", e);
97          }
98      }
99  
100     /**
101      * Returns the @{link PropertyMetaData} object for the property named <code>propertyName</code>.
102      * @param propertyName the property name
103      * @return the @{link PropertyMetaData} object for the property named <code>propertyName</code>
104      */
105     public PropertyMetaData getPropertyMetaData(String propertyName)
106     {
107         return propertyMetaDataMap.get(propertyName);
108     }
109 
110     /**
111      * Returns a (modifiable) set containing all of the property names for this bean type.
112      * @return a (modifiable) set containing all of the property names for this bean type
113      */
114     public Set<String> getPropertyNames()
115     {
116         return new TreeSet<String>(propertyMetaDataMap.keySet());
117     }
118 
119 //**********************************************************************************************************************
120 // Inner Classes
121 //**********************************************************************************************************************
122 
123     private static class SerializedForm implements Serializable
124     {
125         private final String factoryId;
126         private final Class beanClass;
127 
128         private SerializedForm(String factoryId, Class beanClass)
129         {
130             this.factoryId = factoryId;
131             this.beanClass = beanClass;
132         }
133 
134         protected Object readResolve()
135         {
136             return BeanMetaDataFactory.get(factoryId).getBeanMetaData(beanClass);
137         }
138     }
139 }