IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
RuntimeAssemblyClassLoaderFactory.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2013 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;
27
28#if IMPORTER || EXPORTER
29using IKVM.Reflection;
30
31using Type = IKVM.Reflection.Type;
32#else
33using System.Reflection;
34#endif
35
36#if IMPORTER
38#endif
39
40namespace IKVM.Runtime
41{
46 {
47
48 readonly RuntimeContext context;
49
54 readonly ConditionalWeakTable<Assembly, RuntimeAssemblyClassLoader> assemblyClassLoaders = new();
55
56#if !IMPORTER && !EXPORTER && !FIRST_PASS
57 internal Dictionary<string, string> customClassLoaderRedirects;
58#endif
59
66 {
67 this.context = context ?? throw new ArgumentNullException(nameof(context));
68 }
69
76 public RuntimeAssemblyClassLoader FromAssembly(Assembly assembly)
77 {
78 if (assemblyClassLoaders.TryGetValue(assembly, out RuntimeAssemblyClassLoader loader))
79 return loader;
80
81 loader = Create(assembly);
82
83 lock (assemblyClassLoaders)
84 {
85 if (assemblyClassLoaders.TryGetValue(assembly, out var existing))
86 loader = existing;
87 else
88 assemblyClassLoaders.Add(assembly, loader);
89 }
90
91 return loader;
92 }
93
99 RuntimeAssemblyClassLoader Create(Assembly assembly)
100 {
101 // If the assembly is a part of a multi-assembly shared class loader,
102 // it will export the __<MainAssembly> type from the main assembly in the group.
103 var forwarder = assembly.GetType("__<MainAssembly>");
104 if (forwarder != null)
105 {
106 var mainAssembly = forwarder.Assembly;
107 if (mainAssembly != assembly)
108 return FromAssembly(mainAssembly);
109 }
110
111 var baseAssembly = context.Resolver.ResolveBaseAssembly().AsReflection();
112#if IMPORTER
113 if (baseAssembly != null && assembly.IsDefined(context.Resolver.ResolveRuntimeType("IKVM.Attributes.RemappedClassAttribute").AsReflection(), false))
114 context.ClassLoaderFactory.LoadRemappedTypes();
115#endif
116
117 if (baseAssembly == assembly)
118 {
119 // This cast is necessary for ikvmc and a no-op for the runtime.
120 // Note that the cast cannot fail, because ikvmc will only return a non AssemblyClassLoader
121 // from GetBootstrapClassLoader() when compiling the core assembly and in that case JVM.CoreAssembly
122 // will be null.
123 return (RuntimeAssemblyClassLoader)context.ClassLoaderFactory.GetBootstrapClassLoader();
124 }
125
126 return new RuntimeAssemblyClassLoader(context, assembly);
127 }
128
129 }
130
131}
IKVM.Reflection.Type Type
IKVM.Reflection.Assembly Assembly
Maintains instances of RuntimeAssemblyClassLoader.
RuntimeAssemblyClassLoaderFactory(RuntimeContext context)
Initializes a new instance.
RuntimeAssemblyClassLoader FromAssembly(Assembly assembly)
Obtains the RuntimeAssemblyClassLoader for the given Assembly. This method should not be used with dy...
Maintains services relevant to an instane of the IKVM runtime.