bb757d38ce80fb403a0623ee2ce99f09f61e26c0
[mono.git] / mcs / class / Mono.Cecil / Mono.Cecil / AssemblyDefinition.cs
1 //
2 // AssemblyDefinition.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;
30 using System.IO;
31
32 using Mono.Collections.Generic;
33
34 namespace Mono.Cecil {
35
36         public sealed class AssemblyDefinition : ICustomAttributeProvider, ISecurityDeclarationProvider {
37
38                 AssemblyNameDefinition name;
39
40                 internal ModuleDefinition main_module;
41                 Collection<ModuleDefinition> modules;
42                 Collection<CustomAttribute> custom_attributes;
43                 Collection<SecurityDeclaration> security_declarations;
44
45                 public AssemblyNameDefinition Name {
46                         get { return name; }
47                         set { name = value; }
48                 }
49
50                 public string FullName {
51                         get { return name != null ? name.FullName : string.Empty; }
52                 }
53
54                 public MetadataToken MetadataToken {
55                         get { return new MetadataToken (TokenType.Assembly, 1); }
56                         set { }
57                 }
58
59                 public Collection<ModuleDefinition> Modules {
60                         get {
61                                 if (modules != null)
62                                         return modules;
63
64                                 if (main_module.HasImage)
65                                         return modules = main_module.Read (this, (_, reader) => reader.ReadModules ());
66
67                                 return modules = new Collection<ModuleDefinition> { main_module };
68                         }
69                 }
70
71                 public ModuleDefinition MainModule {
72                         get { return main_module; }
73                 }
74
75                 public MethodDefinition EntryPoint {
76                         get { return main_module.EntryPoint; }
77                         set { main_module.EntryPoint = value; }
78                 }
79
80                 public bool HasCustomAttributes {
81                         get {
82                                 if (custom_attributes != null)
83                                         return custom_attributes.Count > 0;
84
85                                 return this.GetHasCustomAttributes (main_module);
86                         }
87                 }
88
89                 public Collection<CustomAttribute> CustomAttributes {
90                         get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (main_module)); }
91                 }
92
93                 public bool HasSecurityDeclarations {
94                         get {
95                                 if (security_declarations != null)
96                                         return security_declarations.Count > 0;
97
98                                 return this.GetHasSecurityDeclarations (main_module);
99                         }
100                 }
101
102                 public Collection<SecurityDeclaration> SecurityDeclarations {
103                         get { return security_declarations ?? (security_declarations = this.GetSecurityDeclarations (main_module)); }
104                 }
105
106                 internal AssemblyDefinition ()
107                 {
108                 }
109
110 #if !READ_ONLY
111                 public static AssemblyDefinition CreateAssembly (AssemblyNameDefinition assemblyName, string moduleName, ModuleKind kind)
112                 {
113                         return CreateAssembly (assemblyName, moduleName, new ModuleParameters { Kind = kind });
114                 }
115
116                 public static AssemblyDefinition CreateAssembly (AssemblyNameDefinition assemblyName, string moduleName, ModuleParameters parameters)
117                 {
118                         if (assemblyName == null)
119                                 throw new ArgumentNullException ("assemblyName");
120                         if (moduleName == null)
121                                 throw new ArgumentNullException ("moduleName");
122                         Mixin.CheckParameters (parameters);
123                         if (parameters.Kind == ModuleKind.NetModule)
124                                 throw new ArgumentException ("kind");
125
126                         var assembly = ModuleDefinition.CreateModule (moduleName, parameters).Assembly;
127                         assembly.Name = assemblyName;
128
129                         return assembly;
130                 }
131 #endif
132
133                 public static AssemblyDefinition ReadAssembly (string fileName)
134                 {
135                         return ReadAssembly (ModuleDefinition.ReadModule (fileName));
136                 }
137
138                 public static AssemblyDefinition ReadAssembly (string fileName, ReaderParameters parameters)
139                 {
140                         return ReadAssembly (ModuleDefinition.ReadModule (fileName, parameters));
141                 }
142
143                 public static AssemblyDefinition ReadAssembly (Stream stream)
144                 {
145                         return ReadAssembly (ModuleDefinition.ReadModule (stream));
146                 }
147
148                 public static AssemblyDefinition ReadAssembly (Stream stream, ReaderParameters parameters)
149                 {
150                         return ReadAssembly (ModuleDefinition.ReadModule (stream, parameters));
151                 }
152
153                 static AssemblyDefinition ReadAssembly (ModuleDefinition module)
154                 {
155                         var assembly = module.Assembly;
156                         if (assembly == null)
157                                 throw new ArgumentException ();
158
159                         return assembly;
160                 }
161
162 #if !READ_ONLY
163                 public void Write (string fileName)
164                 {
165                         Write (fileName, new WriterParameters ());
166                 }
167
168                 public void Write (Stream stream)
169                 {
170                         Write (stream, new WriterParameters ());
171                 }
172
173                 public void Write (string fileName, WriterParameters parameters)
174                 {
175                         main_module.Write (fileName, parameters);
176                 }
177
178                 public void Write (Stream stream, WriterParameters parameters)
179                 {
180                         main_module.Write (stream, parameters);
181                 }
182 #endif
183
184                 public override string ToString ()
185                 {
186                         return this.FullName;
187                 }
188         }
189 }