~ubuntu-branches/ubuntu/oneiric/cecil/oneiric

« back to all changes in this revision

Viewing changes to Mono.Cecil/PropertyDefinition.cs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane, Iain Lane, Jo Shields
  • Date: 2011-08-07 22:38:20 UTC
  • mfrom: (1.1.7 upstream) (6.1.6 experimental)
  • Revision ID: james.westby@ubuntu.com-20110807223820-nfdm4q0pk2smjm11
Tags: 0.9.5+dfsg-1
[ Iain Lane ]
* [411dc78] Update to use my d.o email address
* [74bedaf] Disable clilibs; this is an unstable library
  apps grow unnecessary depends otherwise
* [5288c1f] Mangle debian version in watch file to take care of repacking.
  Also update watch file to look at new github location for tarballs
* [8f7110f] Relax version restriction on cli-common-dev; anything from 0.8
  will do

[ Jo Shields ]
* [e846eb8] Imported Upstream version 0.9.5+dfsg
* [3017d96] Bump build dependencies, as we're building for Mono 2.10 now.
* [27c2cff] Set to DebSrc 3.0, so we can apply patches via Quilt.
* [d0447b3] Update build to use XBuild, not manual compiler invocation.
* [08d2b92] Patch to avoid building tests (which rely on NUnit 2.4)
* [fa5a033] Update install file to include all new assemblies and locations.
* [43bd1e2] Since upstream no longer ships a pcfile, add our own.
* [942ead4] Don't try to ship a Changelog when none exists.
* [ba8232d] Erase obj/ folders in clean rule.
* [090af34] Exclude modulerefs on Windowsy libraries.

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 - 2011 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
                                InitializeMethods ();
 
131
 
 
132
                                if (get_method != null)
 
133
                                        return get_method.HasParameters;
 
134
 
 
135
                                if (set_method != null)
 
136
                                        return set_method.HasParameters && set_method.Parameters.Count > 1;
 
137
 
 
138
                                return false;
 
139
                        }
 
140
                }
 
141
 
 
142
                public override Collection<ParameterDefinition> Parameters {
 
143
                        get {
 
144
                                InitializeMethods ();
 
145
 
 
146
                                if (get_method != null)
 
147
                                        return MirrorParameters (get_method, 0);
 
148
 
 
149
                                if (set_method != null)
 
150
                                        return MirrorParameters (set_method, 1);
 
151
 
 
152
                                return new Collection<ParameterDefinition> ();
 
153
                        }
 
154
                }
 
155
 
 
156
                static Collection<ParameterDefinition> MirrorParameters (MethodDefinition method, int bound)
 
157
                {
 
158
                        var parameters = new Collection<ParameterDefinition> ();
 
159
                        if (!method.HasParameters)
 
160
                                return parameters;
 
161
 
 
162
                        var original_parameters = method.Parameters;
 
163
                        var end = original_parameters.Count - bound;
 
164
 
 
165
                        for (int i = 0; i < end; i++)
 
166
                                parameters.Add (original_parameters [i]);
 
167
 
 
168
                        return parameters;
 
169
                }
 
170
 
 
171
                public bool HasConstant {
 
172
                        get {
 
173
                                ResolveConstant ();
 
174
 
 
175
                                return constant != Mixin.NoValue;
 
176
                        }
 
177
                        set { if (!value) constant = Mixin.NoValue; }
 
178
                }
 
179
 
 
180
                public object Constant {
 
181
                        get { return HasConstant ? constant : null;     }
 
182
                        set { constant = value; }
 
183
                }
 
184
 
 
185
                void ResolveConstant ()
 
186
                {
 
187
                        if (constant != Mixin.NotResolved)
 
188
                                return;
 
189
 
 
190
                        this.ResolveConstant (ref constant, Module);
 
191
                }
 
192
 
 
193
                #region PropertyAttributes
 
194
 
 
195
                public bool IsSpecialName {
 
196
                        get { return attributes.GetAttributes ((ushort) PropertyAttributes.SpecialName); }
 
197
                        set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.SpecialName, value); }
 
198
                }
 
199
 
 
200
                public bool IsRuntimeSpecialName {
 
201
                        get { return attributes.GetAttributes ((ushort) PropertyAttributes.RTSpecialName); }
 
202
                        set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.RTSpecialName, value); }
 
203
                }
 
204
 
 
205
                public bool HasDefault {
 
206
                        get { return attributes.GetAttributes ((ushort) PropertyAttributes.HasDefault); }
 
207
                        set { attributes = attributes.SetAttributes ((ushort) PropertyAttributes.HasDefault, value); }
 
208
                }
 
209
 
 
210
                #endregion
 
211
 
 
212
                public new TypeDefinition DeclaringType {
 
213
                        get { return (TypeDefinition) base.DeclaringType; }
 
214
                        set { base.DeclaringType = value; }
 
215
                }
 
216
 
 
217
                public override bool IsDefinition {
 
218
                        get { return true; }
 
219
                }
 
220
 
 
221
                public override string FullName {
 
222
                        get {
 
223
                                var builder = new StringBuilder ();
 
224
                                builder.Append (PropertyType.ToString ());
 
225
                                builder.Append (' ');
 
226
                                builder.Append (MemberFullName ());
 
227
                                builder.Append ('(');
 
228
                                if (HasParameters) {
 
229
                                        var parameters = Parameters;
 
230
                                        for (int i = 0; i < parameters.Count; i++) {
 
231
                                                if (i > 0)
 
232
                                                        builder.Append (',');
 
233
                                                builder.Append (parameters [i].ParameterType.FullName);
 
234
                                        }
 
235
                                }
 
236
                                builder.Append (')');
 
237
                                return builder.ToString ();
 
238
                        }
 
239
                }
 
240
 
 
241
                public PropertyDefinition (string name, PropertyAttributes attributes, TypeReference propertyType)
 
242
                        : base (name, propertyType)
 
243
                {
 
244
                        this.attributes = (ushort) attributes;
 
245
                        this.token = new MetadataToken (TokenType.Property);
 
246
                }
 
247
 
 
248
                void InitializeMethods ()
 
249
                {
 
250
                        if (get_method != null || set_method != null)
 
251
                                return;
 
252
 
 
253
                        var module = this.Module;
 
254
                        if (!module.HasImage ())
 
255
                                return;
 
256
 
 
257
                        module.Read (this, (property, reader) => reader.ReadMethods (property));
 
258
                }
 
259
 
 
260
                public override PropertyDefinition Resolve ()
 
261
                {
 
262
                        return this;
 
263
                }
 
264
        }
 
265
}