|
From: Michael D. <mik...@us...> - 2004-08-22 06:26:19
|
Update of /cvsroot/nhibernate/nhibernate/src/NHibernate/Util In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32288/NHibernate/Util Modified Files: ReflectHelper.cs Log Message: Moved Getter and Setter code out of ReflectHelper and into the Property namespace. Index: ReflectHelper.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Util/ReflectHelper.cs,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** ReflectHelper.cs 1 Aug 2004 14:58:46 -0000 1.12 --- ReflectHelper.cs 22 Aug 2004 06:26:06 -0000 1.13 *************** *** 3,67 **** using NHibernate.Type; ! namespace NHibernate.Util { ! public sealed class ReflectHelper { private static System.Type[] NoClasses = new System.Type[0]; private static System.Type[] Object = new System.Type[] { typeof(object) }; private static MethodInfo ObjectEquals; ! public sealed class Setter { ! private System.Type clazz; ! private PropertyInfo property; ! private string propertyName; ! ! public Setter(System.Type clazz, PropertyInfo property, string propertyName) { ! this.clazz = clazz; ! this.property = property; ! this.propertyName = propertyName; ! } ! ! public void Set(object target, object value) { ! try { ! property.SetValue(target, value, new object[0]); ! } catch (Exception e) { ! throw new PropertyAccessException(e, "Exception occurred", true, clazz, propertyName); ! } ! } ! public PropertyInfo Property { ! get { return property; } ! } ! } ! ! public sealed class Getter { ! private System.Type clazz; ! private PropertyInfo property; ! private string propertyName; ! ! public Getter(System.Type clazz, PropertyInfo property, string propertyName) { ! this.clazz = clazz; ! this.property = property; ! this.propertyName = propertyName; ! } ! ! public object Get(object target) { ! try { ! return property.GetValue(target, new object[0]); ! } catch (Exception e) { ! throw new PropertyAccessException(e, "Exception occurred", false, clazz, propertyName); ! } ! } ! public System.Type ReturnType { ! get { return property.PropertyType; } ! } ! public PropertyInfo Property { ! get { return property; } ! } ! } ! ! static ReflectHelper() { MethodInfo eq; ! try { eq = typeof(object).GetMethod("Equals", BindingFlags.Instance | BindingFlags.Public ); ! } catch (Exception e) { throw new AssertionFailure("Could not find Object.Equals()", e); } --- 3,27 ---- using NHibernate.Type; ! namespace NHibernate.Util ! { ! public sealed class ReflectHelper ! { ! //TODO: this dependency is kind of bad - H2.0.3 comment ! private static readonly Property.BasicPropertyAccessor basicPropertyAccessor = new Property.BasicPropertyAccessor(); ! private static System.Type[] NoClasses = new System.Type[0]; private static System.Type[] Object = new System.Type[] { typeof(object) }; private static MethodInfo ObjectEquals; ! static ReflectHelper() ! { MethodInfo eq; ! try ! { eq = typeof(object).GetMethod("Equals", BindingFlags.Instance | BindingFlags.Public ); ! } ! catch (Exception e) ! { throw new AssertionFailure("Could not find Object.Equals()", e); } *************** *** 69,77 **** } ! public static bool OverridesEquals(System.Type clazz) { MethodInfo equals; ! try { equals = clazz.GetMethod("Equals"); ! } catch (Exception) { return false; } --- 29,41 ---- } ! public static bool OverridesEquals(System.Type clazz) ! { MethodInfo equals; ! try ! { equals = clazz.GetMethod("Equals"); ! } ! catch (Exception) ! { return false; } *************** *** 79,136 **** } ! public static Setter GetSetter(System.Type type, string propertyName) { ! Setter result = GetSetterOrNull(type, propertyName); ! if (result==null) throw new PropertyNotFoundException( "Could not find a setter for property " + propertyName + " in class " + type.FullName ); ! return result; ! } ! ! private static Setter GetSetterOrNull(System.Type type, string propertyName) { ! if (type == typeof(object) || type == null) return null; ! ! //PropertyInfo property = type.GetProperty(propertyName); ! PropertyInfo property = type.GetProperty(propertyName, BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.DeclaredOnly); ! ! if (property != null) { ! return new Setter(type, property, propertyName); ! } else { ! Setter setter = GetSetterOrNull( type.BaseType, propertyName ); ! if (setter == null) { ! System.Type[] interfaces = type.GetInterfaces(); ! for ( int i=0; setter==null && i<interfaces.Length; i++) { ! setter = GetSetterOrNull(interfaces[i], propertyName); ! } ! } ! return setter; } } ! public static Getter GetGetter(System.Type theClass, string propertyName) { ! Getter result = GetGetterOrNull(theClass, propertyName); ! if (result == null) throw new PropertyNotFoundException( "Could not find a setter for property " + propertyName + " in class " + theClass.FullName ); ! return result; ! } ! ! private static Getter GetGetterOrNull(System.Type type, string propertyName) { ! if (type==typeof(object) || type==null) return null; ! ! //PropertyInfo property = type.GetProperty(propertyName); ! PropertyInfo property = type.GetProperty(propertyName, BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.DeclaredOnly); ! ! if (property != null) { ! return new Getter(type, property, propertyName); ! } else { ! Getter getter = GetGetterOrNull( type.BaseType, propertyName ); ! if (getter == null) { ! System.Type[] interfaces = type.GetInterfaces(); ! for (int i=0; getter==null && i<interfaces.Length; i++) { ! getter = GetGetterOrNull( interfaces[i], propertyName ); ! } ! } ! return getter; ! } - } ! public static IType ReflectedPropertyType(System.Type theClass, string name) { return TypeFactory.HueristicType( GetGetter(theClass, name).ReturnType.Name ); } --- 43,67 ---- } ! //TODO: most calls to this will be replaced by the Mapping.Property.GetGetter() but ! // there will still be a call from hql into this. ! public static Property.IGetter GetGetter(System.Type theClass, string propertyName) ! { ! try ! { ! return basicPropertyAccessor.GetGetter(theClass, propertyName); ! } ! catch (PropertyNotFoundException pnfe) ! { ! //TODO: figure out how we are going to know the field accessor to use here... ! throw pnfe; } } ! //TODO: add a method in here ReflectedPropertyClass and replace most calls to GetGetter ! // with calls to it ! public static IType ReflectedPropertyType(System.Type theClass, string name) ! { return TypeFactory.HueristicType( GetGetter(theClass, name).ReturnType.Name ); } *************** *** 141,170 **** /// <param name="name">The name of the class. Can be a name with the assembly included or just the name of the class.</param> /// <returns>The Type for the Class.</returns> ! public static System.Type ClassForName(string name) { return System.Type.GetType(name, true); } ! public static object GetConstantValue(string name) { System.Type clazz; ! try { clazz = ClassForName( StringHelper.Qualifier(name) ); ! } catch(Exception) { return null; } ! try { return clazz.GetProperty( StringHelper.Unqualify(name) ).GetValue(null, new object[0]); //?? ! } catch (Exception) { return null; } } ! public static ConstructorInfo GetDefaultConstructor(System.Type type) { if (IsAbstractClass(type)) return null; ! try { ConstructorInfo contructor = type.GetConstructor(BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic, null, CallingConventions.HasThis, NoClasses, null); return contructor; ! } catch (Exception) { throw new PropertyNotFoundException( "Object class " + type.FullName + " must declare a default (no-arg) constructor" --- 72,113 ---- /// <param name="name">The name of the class. Can be a name with the assembly included or just the name of the class.</param> /// <returns>The Type for the Class.</returns> ! public static System.Type ClassForName(string name) ! { return System.Type.GetType(name, true); } ! public static object GetConstantValue(string name) ! { System.Type clazz; ! try ! { clazz = ClassForName( StringHelper.Qualifier(name) ); ! } ! catch(Exception) ! { return null; } ! try ! { return clazz.GetProperty( StringHelper.Unqualify(name) ).GetValue(null, new object[0]); //?? ! } ! catch (Exception) ! { return null; } } ! public static ConstructorInfo GetDefaultConstructor(System.Type type) ! { if (IsAbstractClass(type)) return null; ! try ! { ConstructorInfo contructor = type.GetConstructor(BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic, null, CallingConventions.HasThis, NoClasses, null); return contructor; ! } ! catch (Exception) ! { throw new PropertyNotFoundException( "Object class " + type.FullName + " must declare a default (no-arg) constructor" *************** *** 173,177 **** } ! public static bool IsAbstractClass(System.Type type) { return (type.IsAbstract || type.IsInterface); } --- 116,121 ---- } ! public static bool IsAbstractClass(System.Type type) ! { return (type.IsAbstract || type.IsInterface); } |