IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Assembly.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2009-2012 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25using System.Collections.Generic;
26using System.Text;
27
28namespace IKVM.Reflection
29{
30
31 internal abstract class Assembly : ICustomAttributeProvider
32 {
33
34 readonly Universe universe;
35 protected string fullName; // AssemblyBuilder needs access to this field to clear it when the name changes
36 protected List<ModuleResolveEventHandler> resolvers;
37
42 internal Assembly(Universe universe)
43 {
44 this.universe = universe ?? throw new ArgumentNullException(nameof(universe));
45 }
46
50 public Universe Universe => universe;
51
52 public sealed override string ToString()
53 {
54 return FullName;
55 }
56
57 public event ModuleResolveEventHandler ModuleResolve
58 {
59 add
60 {
61 resolvers ??= new List<ModuleResolveEventHandler>();
62 resolvers.Add(value);
63 }
64 remove
65 {
66 resolvers.Remove(value);
67 }
68 }
69
70 public abstract Type[] GetTypes();
71
72 public abstract AssemblyName GetName();
73
74 public abstract string ImageRuntimeVersion { get; }
75
76 public abstract Module ManifestModule { get; }
77 public abstract MethodInfo EntryPoint { get; }
78 public abstract string Location { get; }
79 public abstract AssemblyName[] GetReferencedAssemblies();
80 public abstract Module[] GetModules(bool getResourceModules);
81 public abstract Module[] GetLoadedModules(bool getResourceModules);
82 public abstract Module GetModule(string name);
83 public abstract string[] GetManifestResourceNames();
84 public abstract ManifestResourceInfo GetManifestResourceInfo(string resourceName);
85 public abstract System.IO.Stream GetManifestResourceStream(string name);
86
87 public virtual System.IO.Stream GetManifestResourceStream(Type type, string name)
88 {
89 var sb = new StringBuilder();
90 if (type == null)
91 {
92 throw new ArgumentNullException(nameof(type));
93 }
94 else
95 {
96 string? nameSpace = type.Namespace;
97 if (nameSpace != null)
98 {
99 sb.Append(nameSpace);
100 if (name != null)
101 {
102 sb.Append(System.Type.Delimiter);
103 }
104 }
105 }
106
107 if (name != null)
108 {
109 sb.Append(name);
110 }
111
112 return GetManifestResourceStream(sb.ToString());
113 }
114
115 internal abstract Type FindType(TypeName name);
116 internal abstract Type FindTypeIgnoreCase(TypeName lowerCaseName);
117
118 // The differences between ResolveType and FindType are:
119 // - ResolveType is only used when a type is assumed to exist (because another module's metadata claims it)
120 // - ResolveType can return a MissingType
121 internal Type ResolveType(Module requester, TypeName typeName)
122 {
123 return FindType(typeName) ?? universe.GetMissingTypeOrThrow(requester, this.ManifestModule, null, typeName);
124 }
125
126 public string FullName
127 {
128 get { return fullName ??= GetName().FullName; }
129 }
130
131 public Module[] GetModules()
132 {
133 return GetModules(true);
134 }
135
136 public IEnumerable<Module> Modules
137 {
138 get { return GetLoadedModules(); }
139 }
140
141 public Module[] GetLoadedModules()
142 {
143 return GetLoadedModules(true);
144 }
145
146 public AssemblyName GetName(bool copiedName)
147 {
148 return GetName();
149 }
150
151 public bool ReflectionOnly
152 {
153 get { return true; }
154 }
155
156 public Type[] GetExportedTypes()
157 {
158 var list = new List<Type>();
159 foreach (var type in GetTypes())
160 if (type.IsVisible)
161 list.Add(type);
162
163 return list.ToArray();
164 }
165
166 public IEnumerable<Type> ExportedTypes
167 {
168 get { return GetExportedTypes(); }
169 }
170
171 public IEnumerable<TypeInfo> DefinedTypes
172 {
173 get
174 {
175 var types = GetTypes();
176 var typeInfos = new TypeInfo[types.Length];
177 for (int i = 0; i < types.Length; i++)
178 typeInfos[i] = types[i].GetTypeInfo();
179
180 return typeInfos;
181 }
182 }
183
184 public Type GetType(string name)
185 {
186 return GetType(name, false);
187 }
188
189 public Type GetType(string name, bool throwOnError)
190 {
191 return GetType(name, throwOnError, false);
192 }
193
194 public Type GetType(string name, bool throwOnError, bool ignoreCase)
195 {
196 var parser = TypeNameParser.Parse(name, throwOnError);
197 if (parser.Error)
198 return null;
199
200 if (parser.AssemblyName != null)
201 {
202 if (throwOnError)
203 throw new ArgumentException("Type names passed to Assembly.GetType() must not specify an assembly.");
204 else
205 return null;
206 }
207
208 var typeName = TypeName.Split(TypeNameParser.Unescape(parser.FirstNamePart));
209 var type = ignoreCase ? FindTypeIgnoreCase(typeName.ToLowerInvariant()) : FindType(typeName);
210 if (type == null && __IsMissing)
211 throw new MissingAssemblyException((MissingAssembly)this);
212
213 return parser.Expand(type, this.ManifestModule, throwOnError, name, false, ignoreCase);
214 }
215
216 public virtual Module LoadModule(string moduleName, byte[] rawModule)
217 {
218 throw new NotSupportedException();
219 }
220
221 public Module LoadModule(string moduleName, byte[] rawModule, byte[] rawSymbolStore)
222 {
223 return LoadModule(moduleName, rawModule);
224 }
225
226 public bool IsDefined(Type attributeType, bool inherit)
227 {
228 return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit).Count != 0;
229 }
230
231 public IList<CustomAttributeData> __GetCustomAttributes(Type attributeType, bool inherit)
232 {
233 return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit);
234 }
235
236 public IList<CustomAttributeData> GetCustomAttributesData()
237 {
238 return CustomAttributeData.GetCustomAttributes(this);
239 }
240
241 public IEnumerable<CustomAttributeData> CustomAttributes
242 {
243 get { return GetCustomAttributesData(); }
244 }
245
246 public static string CreateQualifiedName(string assemblyName, string typeName)
247 {
248 return typeName + ", " + assemblyName;
249 }
250
251 public static Assembly GetAssembly(Type type)
252 {
253 return type.Assembly;
254 }
255
256 public string CodeBase
257 {
258 get
259 {
260 var path = this.Location.Replace(System.IO.Path.DirectorySeparatorChar, '/');
261 if (path.StartsWith("/") == false)
262 path = "/" + path;
263
264 return "file://" + path;
265 }
266 }
267
268 public virtual bool IsDynamic => false;
269
270 public virtual bool __IsMissing => false;
271
272 public AssemblyNameFlags __AssemblyFlags => GetAssemblyFlags();
273
274 protected virtual AssemblyNameFlags GetAssemblyFlags()
275 {
276 return GetName().Flags;
277 }
278
279 internal abstract IList<CustomAttributeData> GetCustomAttributesData(Type attributeType);
280
281 }
282
283}
IKVM.Reflection.Module Module
IKVM.Reflection.Type Type
IKVM.Reflection.Assembly Assembly
IKVM.Reflection.AssemblyName AssemblyName
IKVM.Reflection.MethodInfo MethodInfo