BOO-999
[boo.git] / src / Boo.Lang.Compiler / TypeSystem / ExternalProperty.cs
blob9c1df0ccb450422c3ae6900e540e1f6576f56554
1 #region license
2 // Copyright (c) 2004, Rodrigo B. de Oliveira (rbo@acm.org)
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of Rodrigo B. de Oliveira nor the names of its
14 // contributors may be used to endorse or promote products derived from this
15 // software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #endregion
29 using System;
30 using System.Reflection;
32 namespace Boo.Lang.Compiler.TypeSystem
34 public class ExternalProperty : ExternalEntity<System.Reflection.PropertyInfo>, IProperty
36 private IParameter[] _parameters;
38 private bool? _isBooExtension;
39 private bool? _isClrExtension;
41 private System.Reflection.MethodInfo _accessor = null;
43 private CachedMethod _getter = null;
45 private CachedMethod _setter = null;
47 public ExternalProperty(TypeSystemServices typeSystemServices, System.Reflection.PropertyInfo property) : base(typeSystemServices, property)
51 public bool IsExtension
53 get
55 return IsBooExtension || IsClrExtension;
59 public bool IsBooExtension
61 get
63 if (null == _isBooExtension)
65 _isBooExtension = MetadataUtil.IsAttributeDefined(_memberInfo, Types.BooExtensionAttribute);
67 return _isBooExtension.Value;
71 public bool IsClrExtension
73 get
75 if (null == _isClrExtension)
77 _isClrExtension = MetadataUtil.HasClrExtensions()
78 && IsStatic
79 && MetadataUtil.IsAttributeDefined(_memberInfo, Types.ClrExtensionAttribute);
81 return _isClrExtension.Value;
85 public virtual IType DeclaringType
87 get
89 return _typeSystemServices.Map(_memberInfo.DeclaringType);
93 public bool IsStatic
95 get
97 return GetAccessor().IsStatic;
101 public bool IsPublic
105 return GetAccessor().IsPublic;
109 public bool IsProtected
113 System.Reflection.MethodInfo accessor = GetAccessor();
114 return accessor.IsFamily || accessor.IsFamilyOrAssembly;
118 public bool IsInternal
122 return GetAccessor().IsAssembly;
126 public bool IsPrivate
130 return GetAccessor().IsPrivate;
134 override public EntityType EntityType
138 return EntityType.Property;
142 public virtual IType Type
146 return _typeSystemServices.Map(_memberInfo.PropertyType);
150 public System.Reflection.PropertyInfo PropertyInfo
154 return _memberInfo;
158 public bool AcceptVarArgs
162 return false;
166 protected override Type MemberType
168 get { return _memberInfo.PropertyType; }
171 public virtual IParameter[] GetParameters()
173 if (null != _parameters) return _parameters;
175 return _parameters = _typeSystemServices.Map(_memberInfo.GetIndexParameters());
178 public virtual IMethod GetGetMethod()
180 if (null != _getter) return _getter.Value;
181 return (_getter = new CachedMethod(FindGetMethod())).Value;
184 private IMethod FindGetMethod()
186 System.Reflection.MethodInfo getter = _memberInfo.GetGetMethod(true);
187 if (null == getter)
189 PropertyInfo baseProperty = FindBaseProperty();
190 if (null == baseProperty) return null;
192 getter = baseProperty.GetGetMethod(true);
193 if (null == getter) return null;
195 return _typeSystemServices.Map(getter);
198 private PropertyInfo FindBaseProperty()
200 return _memberInfo.DeclaringType.BaseType.GetProperty(
201 _memberInfo.Name,
202 _memberInfo.PropertyType,
203 GetParameterTypes(_memberInfo.GetIndexParameters()));
206 private static Type[] GetParameterTypes(ParameterInfo[] parameters)
208 Type[] types = new Type[parameters.Length];
209 for (int i=0; i<parameters.Length; ++i)
211 types[i] = parameters[i].ParameterType;
213 return types;
216 public virtual IMethod GetSetMethod()
218 if (null != _setter) return _setter.Value;
219 return (_setter = new CachedMethod(FindSetMethod())).Value;
222 private IMethod FindSetMethod()
224 System.Reflection.MethodInfo setter = _memberInfo.GetSetMethod(true);
225 if (null == setter) return null;
226 return _typeSystemServices.Map(setter);
229 private System.Reflection.MethodInfo GetAccessor()
231 if (null != _accessor) return _accessor;
233 return _accessor = FindAccessor();
236 private System.Reflection.MethodInfo FindAccessor()
238 System.Reflection.MethodInfo getter = _memberInfo.GetGetMethod(true);
239 if (null != getter) return getter;
240 return _memberInfo.GetSetMethod(true);