IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
AssemblyClassLoader.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2015 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.Reflection;
26
28using IKVM.Runtime;
30
32{
33
35 {
36
37#if FIRST_PASS == false
38
39 static ClassLoaderAccessor classLoaderAccessor;
40
41 static ClassLoaderAccessor ClassLoaderAccessor => JVM.Internal.BaseAccessors.Get(ref classLoaderAccessor);
42
43#endif
44
45 public static void setWrapper(global::java.lang.ClassLoader _this, Assembly assembly)
46 {
47#if FIRST_PASS
48 throw new NotImplementedException();
49#else
51#endif
52 }
53
54 public static global::java.lang.Class loadClass(global::java.lang.ClassLoader _this, string name, bool resolve)
55 {
56#if FIRST_PASS
57 throw new NotImplementedException();
58#else
59 try
60 {
61 if (ClassLoaderAccessor.InvokeCheckName(_this, name) == false)
62 throw new ClassNotFoundException(name);
63
64 var wrapper = (RuntimeAssemblyClassLoader)JVM.Context.ClassLoaderFactory.GetClassLoaderWrapper(_this);
65 var tw = wrapper.LoadClass(name);
66 if (tw == null)
67 {
68 JVM.Context.Diagnostics.GenericClassLoadingInfo($"Failed to load class \"{name}\" from {_this}");
69 global::java.lang.Throwable.suppressFillInStackTrace = true;
70 throw new global::java.lang.ClassNotFoundException(name);
71 }
72
73 JVM.Context.Diagnostics.GenericClassLoadingInfo($"Loaded class \"{name}\" from {_this}");
74 return tw.ClassObject;
75 }
77 {
78 JVM.Context.Diagnostics.GenericClassLoadingInfo($"Failed to load class \"{name}\" from {_this}: {x.Message}");
79 throw new global::java.lang.ClassNotFoundException(x.Message);
80 }
81 catch (ClassLoadingException x)
82 {
83 JVM.Context.Diagnostics.GenericClassLoadingInfo($"Failed to load class \"{name}\" from {_this}: {x.Message}");
84 throw x.InnerException;
85 }
87 {
88 JVM.Context.Diagnostics.GenericClassLoadingInfo($"Failed to load class \"{name}\" from {_this}: {x.Message}");
89 throw x.ToJava();
90 }
91#endif
92 }
93
94 public static global::java.net.URL getResource(global::java.lang.ClassLoader _this, string name)
95 {
96#if FIRST_PASS
97 throw new NotImplementedException();
98#else
99 var wrapper = (RuntimeAssemblyClassLoader)JVM.Context.ClassLoaderFactory.GetClassLoaderWrapper(_this);
100 foreach (global::java.net.URL url in wrapper.GetResources(name))
101 return url;
102
103 return null;
104#endif
105 }
106
107 public static global::java.util.Enumeration getResources(global::java.lang.ClassLoader _this, string name)
108 {
109#if FIRST_PASS
110 throw new NotImplementedException();
111#else
112 return new global::ikvm.runtime.EnumerationWrapper(((RuntimeAssemblyClassLoader)JVM.Context.ClassLoaderFactory.GetClassLoaderWrapper(_this)).GetResources(name));
113#endif
114 }
115
116 public static global::java.net.URL findResource(global::java.lang.ClassLoader _this, string name)
117 {
118#if FIRST_PASS
119 throw new NotImplementedException();
120#else
121 var wrapper = (RuntimeAssemblyClassLoader)JVM.Context.ClassLoaderFactory.GetClassLoaderWrapper(_this);
122 foreach (global::java.net.URL url in wrapper.FindResources(name))
123 {
124 return url;
125 }
126
127 return null;
128#endif
129 }
130
131 public static global::java.util.Enumeration findResources(global::java.lang.ClassLoader _this, string name)
132 {
133#if FIRST_PASS
134 throw new NotImplementedException();
135#else
136 return new global::ikvm.runtime.EnumerationWrapper(((RuntimeAssemblyClassLoader)JVM.Context.ClassLoaderFactory.GetClassLoaderWrapper(_this)).FindResources(name));
137#endif
138 }
139
140#if !FIRST_PASS
141
142 static global::java.net.URL GetCodeBase(Assembly assembly)
143 {
144 try
145 {
146 if (assembly.Location != null)
147 return new global::java.io.File(assembly.Location).toURI().toURL();
148 }
149 catch (NotSupportedException)
150 {
151
152 }
153 catch (global::java.net.MalformedURLException)
154 {
155
156 }
157
158 return null;
159 }
160
168 static string GetAttributeValue(global::java.util.jar.Attributes.Name name, global::java.util.jar.Attributes first, global::java.util.jar.Attributes second)
169 {
170 return first?.getValue(name) ?? second?.getValue(name);
171 }
172
173#endif
174
175#if !FIRST_PASS
176
185 static global::java.util.jar.Manifest GetManifestForAssemblyJar(Assembly assembly, string resourceName)
186 {
187 if (assembly is null)
188 throw new ArgumentNullException(nameof(assembly));
189 if (string.IsNullOrEmpty(resourceName))
190 throw new ArgumentException($"'{nameof(resourceName)}' cannot be null or empty.", nameof(resourceName));
191
192 using var resStream = assembly.GetManifestResourceStream(resourceName);
193 if (resStream == null)
194 throw new InternalException($"Could not find assembly resource {resourceName}.");
195
196 using var wrpStream = new global::ikvm.io.InputStreamWrapper(resStream);
197 using var jarStream = new global::java.util.jar.JarInputStream(wrpStream);
198 var manifest = jarStream.getManifest();
199
200 return manifest;
201 }
202
203#endif
204
209 public static void lazyDefinePackages(global::java.lang.ClassLoader _this)
210 {
211#if FIRST_PASS
212 throw new NotImplementedException();
213#else
214 var wrapper = (RuntimeAssemblyClassLoader)JVM.Context.ClassLoaderFactory.GetClassLoaderWrapper(_this);
215 var sealBase = GetCodeBase(wrapper.MainAssembly);
216
217 foreach (var packages in wrapper.GetPackageInfo())
218 {
219 // if the package exists in a JAR within the main assembly, load its manifest
220 var manifest = packages.Key != null ? GetManifestForAssemblyJar(wrapper.MainAssembly, packages.Key) : null;
221
222 // if we loaded a manifest, get the main attributes
223 var mainAttributes = manifest?.getMainAttributes();
224
225 // for each package, check if defined, else define
226 foreach (var name in packages.Value)
227 {
228 if (_this.getPackage(name) != null)
229 continue;
230
231 // manifest might contain an overriding entry for this specific package
232 var packageAttributes = manifest?.getAttributes(name.Replace('.', '/') + '/');
233
234 // define package with package information from package attributes or main attributes
235 _this.definePackage(
236 name,
237 GetAttributeValue(global::java.util.jar.Attributes.Name.SPECIFICATION_TITLE, packageAttributes, mainAttributes),
238 GetAttributeValue(global::java.util.jar.Attributes.Name.SPECIFICATION_VERSION, packageAttributes, mainAttributes),
239 GetAttributeValue(global::java.util.jar.Attributes.Name.SPECIFICATION_VENDOR, packageAttributes, mainAttributes),
240 GetAttributeValue(global::java.util.jar.Attributes.Name.IMPLEMENTATION_TITLE, packageAttributes, mainAttributes),
241 GetAttributeValue(global::java.util.jar.Attributes.Name.IMPLEMENTATION_VERSION, packageAttributes, mainAttributes),
242 GetAttributeValue(global::java.util.jar.Attributes.Name.IMPLEMENTATION_VENDOR, packageAttributes, mainAttributes),
243 string.Equals(GetAttributeValue(global::java.util.jar.Attributes.Name.SEALED, packageAttributes, mainAttributes), "true", StringComparison.OrdinalIgnoreCase) ? sealBase : null);
244 }
245 }
246#endif
247 }
248
249 public static string toString(global::java.lang.ClassLoader _this)
250 {
251#if FIRST_PASS
252 throw new NotImplementedException();
253#else
254 return ((RuntimeAssemblyClassLoader)JVM.Context.ClassLoaderFactory.GetClassLoaderWrapper(_this)).MainAssembly.FullName;
255#endif
256 }
257
258 public static global::java.lang.ClassLoader getAssemblyClassLoader(Assembly asm)
259 {
260#if FIRST_PASS
261 throw new NotImplementedException();
262#else
263 // note that we don't do a security check here, because if you have the Assembly object,
264 // you can already get at all the types in it.
265 return JVM.Context.AssemblyClassLoaderFactory.FromAssembly(asm).GetJavaClassLoader();
266#endif
267 }
268
269 }
270
271}
IKVM.Reflection.Assembly Assembly
static global::java.util.Enumeration getResources(global::java.lang.ClassLoader _this, string name)
static void setWrapper(global::java.lang.ClassLoader _this, Assembly assembly)
static global::java.util.Enumeration findResources(global::java.lang.ClassLoader _this, string name)
static global::java.net.URL findResource(global::java.lang.ClassLoader _this, string name)
static global::java.lang.Class loadClass(global::java.lang.ClassLoader _this, string name, bool resolve)
static string toString(global::java.lang.ClassLoader _this)
static global::java.lang.ClassLoader getAssemblyClassLoader(Assembly asm)
static global::java.net.URL getResource(global::java.lang.ClassLoader _this, string name)
static void lazyDefinePackages(global::java.lang.ClassLoader _this)
Defines all of the packages in the class loader that are not currently defined, using the assembly as...
This is not a Java exception, but instead it wraps a Java exception that was thrown by a class loader...
Represents an internal error that occurred within IKVM.
Main state of the running JVM.
.NET exception that corresponds to a Java exception.
RuntimeAssemblyClassLoader FromAssembly(Assembly assembly)
Obtains the RuntimeAssemblyClassLoader for the given Assembly. This method should not be used with dy...
void SetWrapperForClassLoader(java.lang.ClassLoader javaClassLoader, RuntimeClassLoader wrapper)
IDiagnosticHandler Diagnostics
Gets the IDiagnosticHandler where events should be sent.
RuntimeAssemblyClassLoaderFactory AssemblyClassLoaderFactory
Gets the RuntimeAssemblyClassLoaderFactory associated with this instance of the runtime.
RuntimeClassLoaderFactory ClassLoaderFactory
Gets the RuntimeClassLoaderFactory associated with this instance of the runtime.
void GenericClassLoadingInfo(string arg0)
The 'GenericClassLoadingInfo' diagnostic.