IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
AppDomainAssemblyClassLoader.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.Collections;
26using System.Collections.Generic;
27using System.Reflection;
28
29using IKVM.Runtime;
30
32{
33
35 {
36
37#if FIRST_PASS == false
38
45 static IEnumerable<T> AsEnumerable<T>(this global::java.util.Enumeration enumeration)
46 {
47 while (enumeration.hasMoreElements())
48 yield return (T)enumeration.nextElement();
49 }
50
57 static global::java.lang.Class LoadClassFromAssembly(Assembly assembly, string className)
58 {
59 return assembly.IsDynamic == false ? (JVM.Context.AssemblyClassLoaderFactory.FromAssembly(assembly).DoLoad(className)?.ClassObject) : null;
60 }
61
67 static IEnumerable<global::java.net.URL> FindResources(string name)
68 {
69 var done = new HashSet<RuntimeAssemblyClassLoader>();
70
71 foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
72 {
73 if (asm.IsDynamic)
74 continue;
75
76 var acl = JVM.Context.AssemblyClassLoaderFactory.FromAssembly(asm);
77 if (done.Add(acl))
78 foreach (var url in acl.FindResources(name))
79 yield return url;
80 }
81 }
82
88 static IEnumerable<global::java.net.URL> ConcatResources(IEnumerable<global::java.net.URL> parent, string name)
89 {
90 var hs = new HashSet<global::java.net.URL>();
91
92 foreach (var url in parent)
93 if (url != null && hs.Add(url))
94 yield return url;
95
96 foreach (var url in FindResources(name))
97 if (url != null && hs.Add(url))
98 yield return url;
99 }
100
101#endif
102
109 public static object findClass(object self, string name)
110 {
111#if FIRST_PASS
112 throw new NotImplementedException();
113#else
114 foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
115 if (LoadClassFromAssembly(assembly, name) is { } c)
116 return c;
117
118 throw new global::java.lang.ClassNotFoundException(name);
119#endif
120 }
121
128 public static object findResource(object self, string name)
129 {
130#if FIRST_PASS
131 throw new NotImplementedException();
132#else
133 foreach (var url in FindResources(name))
134 return url;
135
136 return null;
137#endif
138 }
139
146 public static object concatResources(object self, object existing, string name)
147 {
148#if FIRST_PASS
149 throw new NotImplementedException();
150#else
151 return new global::ikvm.runtime.EnumerationWrapper(ConcatResources(AsEnumerable<global::java.net.URL>((global::java.util.Enumeration)existing), name));
152#endif
153 }
154
155 }
156
157}
IKVM.Reflection.Assembly Assembly
static object findResource(object self, string name)
Finds the first matching resource.
static object concatResources(object self, object existing, string name)
Concats resources from this loader with those given by existing .
static object findClass(object self, string name)
Finds the class with the given name within the AppDomain.
Main state of the running JVM.
RuntimeAssemblyClassLoader FromAssembly(Assembly assembly)
Obtains the RuntimeAssemblyClassLoader for the given Assembly. This method should not be used with dy...
RuntimeAssemblyClassLoaderFactory AssemblyClassLoaderFactory
Gets the RuntimeAssemblyClassLoaderFactory associated with this instance of the runtime.