Clover.NET coverage report - Coverage

Coverage timestamp: Friday, May 20, 2005 9:17:00 PM

File Stats: LOC: 316   Methods: 14
NCLOC: 180 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Utilities\Objects\ReflectionInfo.cs 50.0 % 79.5 % 71.4 % 72.5 %
coverage coverage
1  
2   #region Apache Notice
3   /*****************************************************************************
4   * $Header: $
5   * $Revision: $
6   * $Date: $
7   *
8   * iBATIS.NET Data Mapper
9   * Copyright (C) 2004 - Gilles Bayon
10   *
11   *
12   * Licensed under the Apache License, Version 2.0 (the "License");
13   * you may not use this file except in compliance with the License.
14   * You may obtain a copy of the License at
15   *
16   * http://www.apache.org/licenses/LICENSE-2.0
17   *
18   * Unless required by applicable law or agreed to in writing, software
19   * distributed under the License is distributed on an "AS IS" BASIS,
20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   * See the License for the specific language governing permissions and
22   * limitations under the License.
23   *
24   ********************************************************************************/
25   #endregion
26  
27   using System;
28   using System.Collections;
29   using System.Collections.Specialized;
30   using System.Reflection;
31  
32   using IBatisNet.Common.Exceptions;
33  
34   namespace IBatisNet.Common.Utilities.Objects
35   {
36   /// <summary>
37   /// This class represents a cached set of class definition information that
38   /// allows for easy mapping between property names and get/set methods.
39   /// </summary>
40   public class ReflectionInfo
41   {
42   /// <summary>
43   ///
44   /// </summary>
45   public static BindingFlags BINDING_FLAGS_GET
46   = BindingFlags.Public
47   | BindingFlags.GetProperty
48   | BindingFlags.Instance
49   | BindingFlags.GetField
50   ;
51  
52   /// <summary>
53   ///
54   /// </summary>
55   public static BindingFlags BINDING_FLAGS_SET
56   = BindingFlags.Public
57   | BindingFlags.SetProperty
58   | BindingFlags.Instance
59   | BindingFlags.SetField
60   ;
61  
62   private static readonly string[] _emptyStringArray = new string[0];
63   private static ArrayList _simleTypeMap = new ArrayList();
64   private static Hashtable _reflectionInfoMap = Hashtable.Synchronized(new Hashtable());
65  
66   private string _className = string.Empty;
67   private string[] _readablePropertyNames = _emptyStringArray;
68   private string[] _writeablePropertyNames = _emptyStringArray;
69   // (propertyName, property)
70   private Hashtable _setProperties = new Hashtable();
71   // (propertyName, property)
72   private Hashtable _getProperties = new Hashtable();
73   // (propertyName, property type)
74   private Hashtable _setTypes = new Hashtable();
75   // (propertyName, property type)
76   private Hashtable _getTypes = new Hashtable();
77  
78   /// <summary>
79   ///
80   /// </summary>
81 0 public string ClassName
82   {
83   get
84   {
85   return _className;
86   }
87   }
88  
89   /// <summary>
90   ///
91   /// </summary>
92 1 static ReflectionInfo()
93   {
94 1 _simleTypeMap.Add(typeof(string));
95 1 _simleTypeMap.Add(typeof(byte));
96 1 _simleTypeMap.Add(typeof(char));
97 1 _simleTypeMap.Add(typeof(bool));
98 1 _simleTypeMap.Add(typeof(Guid));
99 1 _simleTypeMap.Add(typeof(Int16));
100 1 _simleTypeMap.Add(typeof(Int32));
101 1 _simleTypeMap.Add(typeof(Int64));
102 1 _simleTypeMap.Add(typeof(Single));
103 1 _simleTypeMap.Add(typeof(Double));
104 1 _simleTypeMap.Add(typeof(Decimal));
105 1 _simleTypeMap.Add(typeof(DateTime));
106 1 _simleTypeMap.Add(typeof(TimeSpan));
107 1 _simleTypeMap.Add(typeof(Hashtable));
108 1 _simleTypeMap.Add(typeof(SortedList));
109 1 _simleTypeMap.Add(typeof(ListDictionary));
110 1 _simleTypeMap.Add(typeof(HybridDictionary));
111  
112  
113   // _simleTypeMap.Add(Class.class);
114   // _simleTypeMap.Add(Collection.class);
115   // _simleTypeMap.Add(HashMap.class);
116   // _simleTypeMap.Add(TreeMap.class);
117 1 _simleTypeMap.Add(typeof(ArrayList));
118   // _simleTypeMap.Add(HashSet.class);
119   // _simleTypeMap.Add(TreeSet.class);
120   // _simleTypeMap.Add(Vector.class);
121 1 _simleTypeMap.Add(typeof(IEnumerator));
122   }
123  
124   /// <summary>
125   ///
126   /// </summary>
127   /// <param name="type"></param>
128 16 private ReflectionInfo(Type type)
129   {
130 16 _className = type.Name;
131 16 AddPropertiess(type);
132  
133 16 string[] getArray = new string[_getProperties.Count];
134 16 _getProperties.Keys.CopyTo(getArray,0);
135 16 _readablePropertyNames = getArray;
136  
137 16 string[] setArray = new string[_setProperties.Count];
138 16 _setProperties.Keys.CopyTo(setArray,0);
139 16 _writeablePropertyNames = setArray;
140   }
141  
142   /// <summary>
143   ///
144   /// </summary>
145   /// <param name="type"></param>
146 16 private void AddPropertiess(Type type)
147   {
148 16 PropertyInfo[] properties = type.GetProperties(BINDING_FLAGS_SET);
149 100 for (int i = 0; i < properties.Length; i++)
150   {
151 84 string name = properties[i].Name;
152 84 _setProperties.Add(name, properties[i]);
153 84 _setTypes.Add(name, properties[i].PropertyType);
154   }
155  
156 16 properties = type.GetProperties(BINDING_FLAGS_GET);
157 100 for (int i = 0; i < properties.Length; i++)
158   {
159 84 string name = properties[i].Name;
160 84 _getProperties.Add(name, properties[i]);
161 84 _getTypes.Add(name, properties[i].PropertyType);
162   }
163   }
164  
165   /// <summary>
166   ///
167   /// </summary>
168   /// <param name="propertyName"></param>
169   /// <returns></returns>
170 26229 public PropertyInfo GetSetter(string propertyName)
171   {
172 26229 PropertyInfo propertyInfo = (PropertyInfo) _setProperties[propertyName];
173 26229 if (propertyInfo == null)
174   {
175 0 throw new ProbeException("There is no Set property named '" + propertyName + "' in class '" + _className + "'");
176   }
177 26229 return propertyInfo;
178   }
179  
180   /// <summary>
181   ///
182   /// </summary>
183   /// <param name="propertyName"></param>
184   /// <returns></returns>
185 514 public PropertyInfo GetGetter(string propertyName)
186   {
187 514 PropertyInfo propertyInfo = (PropertyInfo) _getProperties[propertyName];
188 514 if (propertyInfo == null)
189   {
190 1 throw new ProbeException("There is no Get property named '" + propertyName + "' in class '" + _className + "'");
191   }
192 513 return propertyInfo;
193   }
194  
195   /// <summary>
196   ///
197   /// </summary>
198   /// <param name="propertyName"></param>
199   /// <returns></returns>
200 268 public Type GetSetterType(string propertyName)
201   {
202 268 Type type = (Type) _setTypes[propertyName];
203 268 if (type == null)
204   {
205 0 throw new ProbeException("There is no Set property named '" + propertyName + "' in class '" + _className + "'");
206   }
207 268 return type;
208   }
209  
210   /// <summary>
211   ///
212   /// </summary>
213   /// <param name="propertyName"></param>
214   /// <returns></returns>
215 29070 public Type GetGetterType(string propertyName)
216   {
217 29070 Type type = (Type) _getTypes[propertyName];
218 29070 if (type == null)
219   {
220 0 throw new ProbeException("There is no Get property named '" + propertyName + "' in class '" + _className + "'");
221   }
222 29070 return type;
223   }
224  
225   /// <summary>
226   ///
227   /// </summary>
228   /// <returns></returns>
229 0 public string[] GetReadablePropertyNames()
230   {
231   return _readablePropertyNames;
232   }
233  
234   /// <summary>
235   ///
236   /// </summary>
237   /// <returns></returns>
238 69 public string[] GetWriteablePropertyNames()
239   {
240 69 return _writeablePropertyNames;
241   }
242  
243   /// <summary>
244   ///
245   /// </summary>
246   /// <param name="propertyName"></param>
247   /// <returns></returns>
248 0 public bool HasWritableProperty(string propertyName)
249   {
250   return _setProperties.ContainsKey(propertyName);
251   }
252  
253   /// <summary>
254   ///
255   /// </summary>
256   /// <param name="propertyName"></param>
257   /// <returns></returns>
258 2 public bool HasReadableProperty(string propertyName)
259   {
260 2 return _getProperties.ContainsKey(propertyName);
261   }
262  
263   /// <summary>
264   ///
265   /// </summary>
266   /// <param name="type"></param>
267   /// <returns></returns>
268 0 public static bool IsKnownType(Type type)
269   {
270   if (_simleTypeMap.Contains(type))
271   {
272   return true;
273   }
274   else if (typeof(IList).IsAssignableFrom(type))
275   {
276   return true;
277   }
278   else if (typeof(IDictionary).IsAssignableFrom(type))
279   {
280   return true;
281   }
282   else if (typeof(IEnumerator).IsAssignableFrom(type))
283   {
284   return true;
285   }
286   else
287   {
288   return false;
289   }
290   }
291  
292  
293   /// <summary>
294   /// Gets an instance of ReflectionInfo for the specified type.
295   /// </summary>summary>
296   /// <param name="type">The type for which to lookup the method cache.</param>
297   /// <returns>The properties cache for the type</returns>
298 55509 public static ReflectionInfo GetInstance(Type type)
299   {
300 55509 lock (type)
301   {
302 55509 ReflectionInfo cache = (ReflectionInfo) _reflectionInfoMap[type];
303 55509 if (cache == null)
304   {
305 16 cache = new ReflectionInfo(type);
306 16 _reflectionInfoMap.Add(type, cache);
307   }
308 55509 return cache;
309   }
310   }
311  
312  
313   }
314  
315   }
316