IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
IKVM.Runtime.DynamicClassLoaderFactory Class Reference

Maintains instances of DynamicClassLoader. More...

Public Member Functions

 DynamicClassLoaderFactory (RuntimeContext context)
 Initializes a new instance.
 
DynamicClassLoader GetOrCreate (RuntimeClassLoader loader)
 Gets the DynamicClassLoader instance which should be used for dynamic classes emitted by the given class loader.
 
bool ReserveName (string name)
 Reserves the given type name.
 

Static Public Member Functions

static string TypeNameMangleImpl (ConcurrentDictionary< string, RuntimeJavaType > dict, string name, RuntimeJavaType javaType)
 Associated the given Java type with the given type name, ensuring the actual stored name is unique.
 

Detailed Description

Maintains instances of DynamicClassLoader.

Definition at line 45 of file DynamicClassLoaderFactory.cs.

Constructor & Destructor Documentation

◆ DynamicClassLoaderFactory()

IKVM.Runtime.DynamicClassLoaderFactory.DynamicClassLoaderFactory ( RuntimeContext context)

Initializes a new instance.

Parameters
context
Exceptions
ArgumentNullException

Definition at line 57 of file DynamicClassLoaderFactory.cs.

58 {
59 this.context = context ?? throw new ArgumentNullException(nameof(context));
60
61#if IMPORTER == false
62 // we attach to the AppDomain.TypeResolve event to be notified when the CLR cannot find a type, and we must provide it
63 AppDomain.CurrentDomain.TypeResolve += new ResolveEventHandler(OnTypeResolve);
64
65 // exclude '<Module>' as a valid type name, as it overlaps with the pseudo-type for global fields and methods
66 dynamicTypes.TryAdd("<Module>", null);
67#endif
68 }

Member Function Documentation

◆ GetOrCreate()

DynamicClassLoader IKVM.Runtime.DynamicClassLoaderFactory.GetOrCreate ( RuntimeClassLoader loader)

Gets the DynamicClassLoader instance which should be used for dynamic classes emitted by the given class loader.

Parameters
loader
Returns

Definition at line 115 of file DynamicClassLoaderFactory.cs.

116 {
117#if IMPORTER
118 // importer uses only one class loader, and we can just return a single dynamic class loader
119 return new DynamicClassLoader(context, loader.Diagnostics, ((ImportClassLoader)loader).CreateModuleBuilder(), false);
120#else
121 // each assembly class loader gets its own dynamic class loader
122 if (loader is RuntimeAssemblyClassLoader acl)
123 {
124 var name = acl.MainAssembly.GetName().Name + context.Options.DynamicAssemblySuffixAndPublicKey;
125 foreach (var attr in acl.MainAssembly.GetCustomAttributes<InternalsVisibleToAttribute>())
126 {
127 if (attr.AssemblyName == name)
128 {
129 var n = new AssemblyName(name);
130#if NETFRAMEWORK
131 n.KeyPair = DynamicClassLoader.ForgedKeyPair.Instance;
132#endif
133 return new DynamicClassLoader(context, loader.Diagnostics, DynamicClassLoader.CreateModuleBuilder(context, n), true);
134 }
135 }
136 }
137
138 return instance ??= new DynamicClassLoader(context, loader.Diagnostics, DynamicClassLoader.CreateModuleBuilder(context), false);
139#endif
140 }
IKVM.Reflection.AssemblyName AssemblyName
RuntimeContextOptions Options
Gets the RuntimeContextOptions associated with this instance of the runtime.

◆ ReserveName()

bool IKVM.Runtime.DynamicClassLoaderFactory.ReserveName ( string name)

Reserves the given type name.

Parameters
name
Returns

Definition at line 147 of file DynamicClassLoaderFactory.cs.

148 {
149 return dynamicTypes.TryAdd(name, null);
150 }

◆ TypeNameMangleImpl()

static string IKVM.Runtime.DynamicClassLoaderFactory.TypeNameMangleImpl ( ConcurrentDictionary< string, RuntimeJavaType > dict,
string name,
RuntimeJavaType javaType )
static

Associated the given Java type with the given type name, ensuring the actual stored name is unique.

Parameters
dict
name
javaType
Returns

Definition at line 159 of file DynamicClassLoaderFactory.cs.

160 {
161 // the CLR maximum type name length is 1023 characters, but we need to leave some room for the suffix that we may need to append to make the name unique
162 const int MaxLength = 1000;
163
164 if (name.Length > MaxLength)
165 name = name.Substring(0, MaxLength) + "/truncated";
166
167 var mangledTypeName = TypeNameUtil.ReplaceIllegalCharacters(name);
168 var baseTypeName = mangledTypeName;
169 int instanceId = 0;
170
171 // advance through unique type names until we find a free one
172 while (dict.TryAdd(mangledTypeName, javaType) == false)
173 {
174 javaType.ClassLoader.Diagnostics.GenericCompilerWarning($"Class name clash: {mangledTypeName}");
175 mangledTypeName = baseTypeName + "/" + (++instanceId);
176 }
177
178 return mangledTypeName;
179 }

The documentation for this class was generated from the following file: