Clover.NET coverage report - Coverage

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

File Stats: LOC: 237   Methods: 8
NCLOC: 140 Classes: 2
 
Source File Conditionals Statements Methods TOTAL
Utilities\TypesResolver\TypeResolver.cs 62.5 % 78.8 % 87.5 % 75.4 %
coverage coverage
1  
2   #region Apache Notice
3   /*****************************************************************************
4   * $Header: $
5   * $Revision: $
6   * $Date: $
7   *
8   * Copyright 2004 the original author or authors.
9   *
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   *
14   * http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   *
22   ********************************************************************************/
23   #endregion
24  
25   #region Remarks
26   // Code from Spring.NET
27   #endregion
28  
29   #region Imports
30  
31   using System;
32   using System.Reflection;
33  
34   using IBatisNet.Common.Exceptions;
35   #endregion
36  
37   namespace IBatisNet.Common.Utilities.TypesResolver
38   {
39   /// <summary>
40   /// Resolves a <see cref="System.Type"/> by name.
41   /// </summary>
42   /// <remarks>
43   /// <p>
44   /// The rationale behind the creation of this class is to centralise the
45   /// resolution of type names to <see cref="System.Type"/> instances beyond that
46   /// offered by the plain vanilla <see cref="System.Type.GetType"/> method call.
47   /// </p>
48   /// </remarks>
49   /// <version>$Id: TypeResolver.cs,v 1.5 2004/09/28 07:51:47 springboy Exp $</version>
50   public class TypeResolver
51   {
52   #region Constructor (s) / Destructor
53   /// <summary>
54   /// Creates a new instance of the TypeResolver class.
55   /// </summary>
56 1361 public TypeResolver () {}
57   #endregion
58  
59   #region Methods
60   /// <summary>
61   /// Resolves the supplied type name into a <see cref="System.Type"/>
62   /// instance.
63   /// </summary>
64   /// <param name="typeName">
65   /// The (possibly partially assembly qualified) name of a <see cref="System.Type"/>.
66   /// </param>
67   /// <returns>
68   /// A resolved <see cref="System.Type"/> instance.
69   /// </returns>
70   /// <exception cref="System.TypeLoadException">
71   /// If the type could not be resolved.
72   /// </exception>
73 37 public virtual Type Resolve (string typeName)
74   {
75  
76   #region Sanity Check
77 37 if (typeName == null || typeName.Trim().Length==0)
78   {
79 0 throw new ConfigurationException (
80   "Could not load type with a null or zero length parameter.");
81   }
82   #endregion
83  
84 37 Type type = null;
85 37 string canonicalTypeName = TypeAliasResolver.Resolve (typeName);
86 37 TypeAssemblyInfo typeInfo = new TypeAssemblyInfo (canonicalTypeName);
87 37 if (typeInfo.IsAssemblyQualified)
88   {
89   // assembly qualified... load the assembly, then the Type
90 16 Assembly assembly = Assembly.LoadWithPartialName (typeInfo.AssemblyName);
91 16 if (assembly != null)
92   {
93 16 type = assembly.GetType (typeInfo.TypeName, true, true);
94   }
95   }
96   else
97   {
98   // bare type name... loop thru all loaded assemblies
99 21 Assembly [] assemblies = AppDomain.CurrentDomain.GetAssemblies ();
100 21 foreach (Assembly assembly in assemblies)
101   {
102 21 type = assembly.GetType (typeInfo.TypeName, false, false);
103 21 if (type != null)
104   {
105 21 break;
106   }
107   }
108   }
109 37 if (type == null)
110   {
111 0 throw new TypeLoadException (
112   "Could not load type : " + typeName);
113   }
114 37 return type;
115   }
116   #endregion
117  
118   #region Inner Class : TypeAssemblyInfo
119   /// <summary>
120   /// Holds data about a <see cref="System.Type"/> and it's
121   /// attendant <see cref="System.Reflection.Assembly"/>.
122   /// </summary>
123   internal class TypeAssemblyInfo
124   {
125   #region Constants
126   /// <summary>
127   /// The string that separates <see cref="System.Type"/> names
128   /// from their attendant <see cref="System.Reflection.Assembly"/>
129   /// names in an assembly qualified type name.
130   /// </summary>
131   public const string TypeAssemblySeparator = ",";
132   #endregion
133  
134   #region Fields
135   private string unresolvedAssemblyName;
136   private string unresolvedTypeName;
137   #endregion
138  
139   #region Properties
140   /// <summary>
141   /// The (unresolved) type name portion of the original type name.
142   /// </summary>
143   public string TypeName
144   {
145 37 get
146   {
147 37 return unresolvedTypeName;
148   }
149   }
150  
151   /// <summary>
152   /// The (unresolved, possibly partial) name of the attandant assembly.
153   /// </summary>
154   public string AssemblyName
155   {
156 69 get
157   {
158 69 return unresolvedAssemblyName;
159   }
160   }
161  
162   /// <summary>
163   /// Is the type name being resolved assembly qualified?
164   /// </summary>
165   public bool IsAssemblyQualified
166   {
167 37 get
168   {
169 37 if (AssemblyName == null || AssemblyName.Trim().Length==0)
170   {
171 21 return false;
172   }
173   else
174   {
175 16 return true;
176   }
177   }
178   }
179  
180   /// <summary>
181   /// The (possibly assembly qualified) <see cref="System.Type"/> name.
182   /// </summary>
183 0 public string OriginalTypeName
184   {
185   get
186   {
187   System.Text.StringBuilder buffer
188   = new System.Text.StringBuilder (TypeName);
189   if (IsAssemblyQualified)
190   {
191   buffer.Append (TypeAssemblySeparator);
192   buffer.Append (AssemblyName);
193   }
194   return buffer.ToString ();
195   }
196   }
197   #endregion
198  
199   #region Constructor (s) / Destructor
200   /// <summary>
201   /// Creates a new instance of the TypeAssemblyInfo class.
202   /// </summary>
203   /// <param name="unresolvedTypeName">
204   /// The unresolved name of a <see cref="System.Type"/>.
205   /// </param>
206 37 public TypeAssemblyInfo (string unresolvedTypeName)
207   {
208 37 SplitTypeAndAssemblyNames (unresolvedTypeName);
209   }
210   #endregion
211  
212  
213   #region Methods
214 37 private void SplitTypeAndAssemblyNames (string originalTypeName)
215   {
216 37 int typeAssemblyIndex
217   = originalTypeName.IndexOf (
218   TypeAssemblyInfo.TypeAssemblySeparator);
219 37 if (typeAssemblyIndex < 0)
220   {
221 21 unresolvedTypeName = originalTypeName;
222   }
223   else
224   {
225 16 unresolvedTypeName = originalTypeName.Substring (
226   0, typeAssemblyIndex).Trim ();
227 16 unresolvedAssemblyName = originalTypeName.Substring (
228   typeAssemblyIndex + 1).Trim ();
229   }
230   }
231   #endregion
232  
233   }
234   #endregion
235   }
236   }
237