~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/PropertyDefinition.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// PropertyDefinition.cs
 
3
//
 
4
// Author:
 
5
//   Jb Evain (jbevain@gmail.com)
 
6
//
 
7
// Copyright (c) 2008 - 2010 Jb Evain
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
//
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
//
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System.Text;
 
30
 
 
31
using Mono.Collections.Generic;
 
32
 
 
33
namespace Mono.Cecil {
 
34
 
 
35
        public sealed class PropertyDefinition : PropertyReference, IMemberDefinition, IConstantProvider {
 
36
 
 
37
                bool? has_this;
 
38
                ushort attributes;
 
39
 
 
40
                Collection<CustomAttribute> custom_attributes;
 
41
 
 
42
                internal MethodDefinition get_method;
 
43
                internal MethodDefinition set_method;
 
44
                internal Collection<MethodDefinition> other_methods;
 
45
 
 
46
                object constant = Mixin.NotResolved;
 
47
 
 
48
                public PropertyAttributes Attributes {
 
49
                        get { return (PropertyAttributes) attributes; }
 
50
                        set { attributes = (ushort) value; }
 
51
                }
 
52
 
 
53
                public bool HasThis {
 
54
                        get {
 
55
                                if (has_this.HasValue)
 
56
                                        return has_this.Value;
 
57
 
 
58
                                if (GetMethod != null)
 
59
                                        return get_method.HasThis;
 
60
 
 
61
                                if (SetMethod != null)
 
62
                                        return set_method.HasThis;
 
63
 
 
64
                                return false;
 
65
                        }
 
66
                        set { has_this = value; }
 
67
                }
 
68
 
 
69
                public bool HasCustomAttributes {
 
70
                        get {
 
71
                                if (custom_attributes != null)
 
72
                                        return custom_attributes.Count > 0;
 
73
 
 
74
                                return this.GetHasCustomAttributes (Module);
 
75
                        }
 
76
                }
 
77
 
 
78
                public Collection<CustomAttribute> CustomAttributes {
 
79
                        get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); }
 
80
                }
 
81
 
 
82
                public MethodDefinition GetMethod {
 
83
                        get {
 
84
                                if (get_method != null)
 
85
                                        return get_method;
 
86
 
 
87
                                InitializeMethods ();
 
88
                                return get_method;
 
89
                        }
 
90
                        set { get_method = value; }
 
91
                }
 
92
 
 
93
                public MethodDefinition SetMethod {
 
94
                        get {
 
95
                                if (set_method != null)
 
96
                                        return set_method;
 
97
 
 
98
                                InitializeMethods ();
 
99
                                return set_method;
 
100
                        }
 
101
                        set { set_method = value; }
 
102
                }
 
103
 
 
104
                public bool HasOtherMethods {
 
105
                        get {
 
106
                                if (other_methods != null)
 
107
                                        return other_methods.Count > 0;
 
108
 
 
109
                                InitializeMethods ();
 
110
                                return !other_methods.IsNullOrEmpty ();
 
111
                        }
 
112
                }
 
113
 
 
114
                public Collection<MethodDefinition> OtherMethods {
 
115
                        get {
 
116
                                if (other_methods != null)
 
117
                                        return other_methods;
 
118
 
 
119
                                InitializeMethods ();
 
120
 
 
121
                                if (other_methods != null)
 
122
                                        return other_methods;
 
123
 
 
124
                                return other_methods = new Collection<MethodDefinition> ();
 
125
                        }
 
126
                }
 
127
 
 
128
                public bool HasParameters {
 
129
                        get {
 
130
                                if (get_method != null)
 
131
                                        return get_method.HasParameters;
 
132
 
 
133
                                if (set_method != null)
 
134
                                        return set_method.HasParameters && set_method.Parameters.Count > 1;
 
135
 
 
136
                                return false;
 
137
                        }
 
138
                }
 
139
 
 
140
                public override Collection<ParameterDefinition> Parameters {
 
141
                        get {
 
142
                                InitializeMethods ();
 
143
 
 
144
                                if (get_method != null)
 
145
                                        return MirrorParameters (get_method, 0);
 
146
 
 
147
                                if (set_method != null)
 
148
                                        return MirrorParameters (set_method, 1);
 
149
 
 
150
                                return new Collection<ParameterDefinition> ();
 
151
                        }
 
152
                }
 
153
 
 
154
                static Collection<ParameterDefinition> MirrorParameters (MethodDefinition method, int bound)
 
155
                {
 
156
                        var parameters = new Collection<ParameterDefinition> ();
 
157
                        if (!method.HasParameters)
 
158
                                return parameters;
 
159
 
 
160
                        var original_parameters = method.Parameters;
 
161
                        var end = original_parameters.Count - bound;
 
162
 
 
163
                        for (int i = 0; i < end; i++)
 
164
                                parameters.Add (original_parameters [i]);
 
165
 
 
166
                        return parameters;
 
167
                }
 
168
 
 
169
                public bool HasConstant {
 
170
                        get {
 
171
                                ResolveConstant ();
 
172
 
 
173
                                return constant != Mixin.NoValue;
 
174
                        }
 
175
                        set { if (!value) constant = Mixin.NoValue; }
 
176
                }
 
177
 
 
178
                public object Constant {
 
179
                        get { return HasConstant ? constant : null;     }
 
180
                        set { constant = value; }
 
181
                }
 
182
 
 
183
                void ResolveConstant ()
 
184
                {
 
185
                        if (constant != Mixin.NotResolved)
 
186
                                return;
 
187
 
 
188
                        this.ResolveConstant (ref constant, Module);
 
189
                }
 
190
 
 
191
                #region PropertyAttributes
 
192
 
 
193
                public bool IsSpecialName {
 
194
                        get { return attributes.GetAttributes ((ushort) PropertyAttributes.SpecialName); }
 
195
                        set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.SpecialName, value); }
 
196
                }
 
197
 
 
198
                public bool IsRuntimeSpecialName {
 
199
                        get { return attributes.GetAttributes ((ushort) PropertyAttributes.RTSpecialName); }
 
200
                        set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.RTSpecialName, value); }
 
201
                }
 
202
 
 
203
                public bool HasDefault {
 
204
                        get { return attributes.GetAttributes ((ushort) PropertyAttributes.HasDefault); }
 
205
                        set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.HasDefault, value); }
 
206
                }
 
207
 
 
208
                #endregion
 
209
 
 
210
                public new TypeDefinition DeclaringType {
 
211
                        get { return (TypeDefinition) base.DeclaringType; }
 
212
                        set { base.DeclaringType = value; }
 
213
                }
 
214
 
 
215
                public override bool IsDefinition {
 
216
                        get { return true; }
 
217
                }
 
218
 
 
219
                public override string FullName {
 
220
                        get {
 
221
                                var builder = new StringBuilder ();
 
222
                                builder.Append (PropertyType.ToString ());
 
223
                                builder.Append (' ');
 
224
                                builder.Append (MemberFullName ());
 
225
                                builder.Append ('(');
 
226
                                if (HasParameters) {
 
227
                                        var parameters = Parameters;
 
228
                                        for (int i = 0; i < parameters.Count; i++) {
 
229
                                                if (i > 0)
 
230
                                                        builder.Append (',');
 
231
                                                builder.Append (parameters [i].ParameterType.FullName);
 
232
                                        }
 
233
                                }
 
234
                                builder.Append (')');
 
235
                                return builder.ToString ();
 
236
                        }
 
237
                }
 
238
 
 
239
                public PropertyDefinition (string name, PropertyAttributes attributes, TypeReference propertyType)
 
240
                        : base (name, propertyType)
 
241
                {
 
242
                        this.attributes = (ushort) attributes;
 
243
                        this.token = new MetadataToken (TokenType.Property);
 
244
                }
 
245
 
 
246
                void InitializeMethods ()
 
247
                {
 
248
                        if (get_method != null || set_method != null)
 
249
                                return;
 
250
 
 
251
                        var module = this.Module;
 
252
                        if (!module.HasImage ())
 
253
                                return;
 
254
 
 
255
                        module.Read (this, (property, reader) => reader.ReadMethods (property));
 
256
                }
 
257
        }
 
258
}